/kdepim-3.5.10/kresources/exchange/dateset.cpp

# · C++ · 280 lines · 175 code · 39 blank · 66 comment · 51 complexity · 58f09262945de09e3944f146399dfd94 MD5 · raw file

  1. /*
  2. This file is part of libkpimexchange.
  3. Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
  4. This library is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Library General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at your
  7. option) any later version.
  8. This library is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  11. License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. // $Id: dateset.cpp 437980 2005-07-23 19:53:57Z kainhofe $
  18. #include <qptrlist.h>
  19. #include <qdatetime.h>
  20. #include <qpair.h>
  21. #include <kdebug.h>
  22. #include "dateset.h"
  23. DateSet::DateSet()
  24. {
  25. kdDebug() << "Creating DateSet" << endl;
  26. // mOldestDate =
  27. mDates = new RangeList();
  28. mDates->setAutoDelete( true );
  29. }
  30. DateSet::~DateSet()
  31. {
  32. kdDebug() << "Deleting DateSet" << endl;
  33. delete mDates;
  34. }
  35. void DateSet::add( QDate const& date )
  36. {
  37. if (mDates->isEmpty()) {
  38. mDates->insert( 0, new QPair<QDate,QDate>( date, date ) );
  39. return;
  40. }
  41. int i = find( date );
  42. mDates->insert( i, new QPair<QDate,QDate>( date, date ) );
  43. tryMerge( i );
  44. tryMerge( i-1 );
  45. }
  46. void DateSet::add( QDate const& from, QDate const& to )
  47. {
  48. if (mDates->isEmpty()) {
  49. mDates->insert( 0, new QPair<QDate,QDate>( from, to ) );
  50. return;
  51. }
  52. uint i = find( from );
  53. kdDebug() << "Adding range at position " << i << endl;
  54. mDates->insert( i, new QPair<QDate,QDate>( from, to ) );
  55. do {
  56. } while ( tryMerge( i ) );
  57. do {
  58. } while ( tryMerge( i-1 ) );
  59. /*
  60. QPair<QDate,QDate>* item = mDates->at( i );
  61. if (to >= item->first)
  62. return;
  63. if (to.daysTo( item->first ) == 1 )
  64. item->first = from;
  65. else
  66. mDates->insert( i, new QPair<QDate,QDate>( from, to ) );
  67. */
  68. }
  69. void DateSet::remove( QDate const& date )
  70. {
  71. if (mDates->isEmpty()) {
  72. return;
  73. }
  74. uint i = find( date );
  75. if ( i == mDates->count() )
  76. return;
  77. QPair<QDate,QDate>* item = mDates->at( i );
  78. if ( date < item->first )
  79. return;
  80. if ( date == item->first ) {
  81. if ( date == item->second ) {
  82. mDates->remove( i );
  83. } else {
  84. item->first = item->first.addDays( 1 );
  85. }
  86. return;
  87. }
  88. if ( date == item->second ) {
  89. item->second = item->second.addDays( -1 );
  90. } else {
  91. mDates->insert( i, new QPair<QDate,QDate>(item->first, date.addDays( -1 ) ) );
  92. item->first = date.addDays( 1 );
  93. }
  94. }
  95. void DateSet::remove( QDate const& from, QDate const& to )
  96. {
  97. if (mDates->isEmpty()) {
  98. return;
  99. }
  100. uint i = find( from );
  101. if ( i == mDates->count() )
  102. return;
  103. while( i < mDates->count() ) {
  104. QPair<QDate,QDate>* item = mDates->at( i );
  105. // Check if we're done: next item is later dan removal period
  106. if ( to < item->first )
  107. break;
  108. // Check if entire item should be removed
  109. if ( from <= item->first && to >= item->second ) {
  110. mDates->remove( i );
  111. // Don't skip the next range
  112. continue;
  113. }
  114. // Check if we should take a slice out of the middle of the item
  115. if ( from > item->first && to < item->second ) {
  116. mDates->insert( i, new QPair<QDate,QDate>( item->first, from.addDays( -1 ) ) );
  117. item->first = to.addDays( 1 );
  118. break; // We're done
  119. }
  120. // Now check if we should take off the beginning of the item
  121. if ( from <= item->first ) {
  122. item->first = to.addDays( 1 );
  123. // Finished
  124. break;
  125. }
  126. // Only one possibility left: we should take off the end
  127. // of the current range
  128. item->second = from.addDays( -1 );
  129. i++;
  130. }
  131. }
  132. bool DateSet::contains( QDate const& date )
  133. {
  134. if (mDates->isEmpty()) {
  135. return false;
  136. }
  137. uint i = find( date );
  138. // kdDebug() << "contains looking for " << date.toString() << " at range " << i << endl;
  139. if ( i == mDates->count() )
  140. return false;
  141. QPair<QDate,QDate>* item = mDates->at( i );
  142. // kdDebug() << "contains looking at range " << item->first.toString() << " -- " << item->second.toString() << endl;
  143. return ( item->first <= date );
  144. }
  145. // returns true if and only if the whole range is in the set
  146. bool DateSet::contains( QDate const& from, QDate const& to )
  147. {
  148. if (mDates->isEmpty()) {
  149. return false;
  150. }
  151. uint i = find( from );
  152. if ( i == mDates->count() )
  153. return false;
  154. QPair<QDate,QDate>* item = mDates->at( i );
  155. return ( from >= item->first && to <= item->second );
  156. }
  157. // Finds the index in mDates of the range containing date, if it
  158. // exists. Else, return the index of the range following the date.
  159. // If mDates is empty, return 0.
  160. // If date is later than the last item in mDates, return mDates->count()
  161. int DateSet::find( QDate const& date )
  162. {
  163. if ( mDates->isEmpty() )
  164. return 0;
  165. int start = 0;
  166. int end = mDates->count();
  167. while ( start < end ) {
  168. int i = start + (end-start) / 2;
  169. // kdDebug() << start << ", " << i << ", " << end << endl;
  170. QPair<QDate,QDate> *item = mDates->at( i );
  171. if ( item->first <= date && date <= item->second )
  172. return i;
  173. if ( date > item->second ) {
  174. start = i+1;
  175. } else { // this means date < item->first
  176. end = i;
  177. }
  178. }
  179. // kdDebug() << "Found for date " << date.toString() << " range " << end << endl;
  180. return end;
  181. /*
  182. // Now either start==end or start+1 == end
  183. if ( mDates->at( end )->second < date )
  184. return end+1;
  185. else if (mDates->at( start )->first > date )
  186. return start;
  187. else
  188. return end;
  189. */
  190. }
  191. void DateSet::print()
  192. {
  193. for( uint i=0; i<mDates->count(); i++ )
  194. {
  195. QDate start = mDates->at( i )->first;
  196. QDate end = mDates->at( i )->second;
  197. if (start == end)
  198. kdDebug() << start.toString() << endl;
  199. else
  200. kdDebug() << "(" << start.toString() << " , " << end.toString() << ")" << endl;
  201. }
  202. }
  203. // Try and merge range i with range i+1
  204. // NOT TRUE preconditions: range i starts before range i+1, but MAY end later!
  205. // preconditions: range i starts before or in range i+1
  206. bool DateSet::tryMerge( int i )
  207. {
  208. if ( i < 0 || i+1 >= (int)(mDates->count()) )
  209. return false;
  210. QPair<QDate,QDate>* item1 = mDates->at( i );
  211. QPair<QDate,QDate>* item2 = mDates->at( i+1 );
  212. // First case: item1 starts before or on the same date as item2
  213. if ( item1->first <= item2->first ) {
  214. // Check for overlap
  215. if ( item1->second >= item2->first ||
  216. item1->second.daysTo( item2->first ) == 1 ) {
  217. kdDebug() << "Merging items " << i << " and " << (i+1) << endl;
  218. if (item1->second < item2->second) item1->second = item2->second;
  219. mDates->remove( i+1 );
  220. return true;
  221. }
  222. return false;
  223. }
  224. // Second case: item1 starts later than item2 (but at the latest on
  225. // the last day of item2, see preconditions!)
  226. // Check for overlap
  227. if ( item1->second >= item2->first ||
  228. item1->second.daysTo( item2->first ) == 1 ) {
  229. kdDebug() << "Merging items " << i << " and " << (i+1) << endl;
  230. if (item1->second < item2->second) item1->second = item2->second;
  231. item1->first = item2->first;
  232. mDates->remove( i+1 );
  233. return true;
  234. }
  235. return false;
  236. }