summaryrefslogtreecommitdiffstats
path: root/kresources/exchange/dateset.cpp
blob: d28409a9c80899e9175eebed95676ddb2141e7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
    This file is part of libkpimexchange.
    Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

// $Id$

#include <tqptrlist.h>
#include <tqdatetime.h>
#include <tqpair.h>

#include <kdebug.h>

#include "dateset.h"

DateSet::DateSet()
{
  kdDebug() << "Creating DateSet" << endl;
  // mOldestDate = 
  mDates = new RangeList();
  mDates->setAutoDelete( true );
}

DateSet::~DateSet()
{
  kdDebug() << "Deleting DateSet" << endl;
  delete mDates;
}

void DateSet::add( TQDate const& date )
{
  if (mDates->isEmpty()) {
    mDates->insert( 0, new TQPair<TQDate,TQDate>( date, date ) );
    return;
  } 
  int i = tqfind( date );
  mDates->insert( i, new TQPair<TQDate,TQDate>( date, date ) );
  tryMerge( i );
  tryMerge( i-1 );
}

void DateSet::add( TQDate const& from, TQDate const& to )
{
  if (mDates->isEmpty()) {
    mDates->insert( 0, new TQPair<TQDate,TQDate>( from, to ) );
    return;
  } 
  uint i = tqfind( from );
  kdDebug() << "Adding range at position " << i << endl;
  mDates->insert( i, new TQPair<TQDate,TQDate>( from, to ) );

  do {
  } while ( tryMerge( i ) );
  do {
  } while ( tryMerge( i-1 ) );
/*  
  TQPair<TQDate,TQDate>* item = mDates->at( i );

  if (to >= item->first)
    return;

  if (to.daysTo( item->first ) == 1 )
    item->first = from;
  else
    mDates->insert( i, new TQPair<TQDate,TQDate>( from, to ) );
*/
}

void DateSet::remove( TQDate const& date )
{
  if (mDates->isEmpty()) {
    return;
  } 

  uint i = tqfind( date );
  if ( i == mDates->count() )
    return;

  TQPair<TQDate,TQDate>* item = mDates->at( i );
  if ( date < item->first )
    return;
  if ( date == item->first ) {
    if ( date == item->second ) {
      mDates->remove( i );
    } else {
      item->first = item->first.addDays( 1 );
    }
    return;
  }

  if ( date == item->second ) {
    item->second = item->second.addDays( -1 );
  } else {
    mDates->insert( i, new TQPair<TQDate,TQDate>(item->first, date.addDays( -1 ) ) );
    item->first = date.addDays( 1 );
  }
}

void DateSet::remove( TQDate const& from, TQDate const& to )
{
  if (mDates->isEmpty()) {
    return;
  } 
  
  uint i = tqfind( from );
  if ( i == mDates->count() )
    return;
  
  while( i < mDates->count() ) {
    TQPair<TQDate,TQDate>* item = mDates->at( i );
    // Check if we're done: next item is later dan removal period
    if ( to < item->first )
      break;

    // Check if entire item should be removed
    if ( from <= item->first && to >= item->second ) {
      mDates->remove( i );
      // Don't skip the next range
      continue;
    }

    // Check if we should take a slice out of the middle of the item
    if ( from > item->first && to < item->second ) {
      mDates->insert( i, new TQPair<TQDate,TQDate>( item->first, from.addDays( -1 ) ) );
      item->first = to.addDays( 1 );
      break; // We're done
    }

    // Now check if we should take off the beginning of the item
    if ( from <= item->first ) {
      item->first = to.addDays( 1 );
      // Finished
      break;
    }

    // Only one possibility left: we should take off the end
    // of the current range
    item->second = from.addDays( -1 );
    i++;
  }
}

bool DateSet::tqcontains( TQDate const& date )
{
  if (mDates->isEmpty()) {
    return false;
  } 

  uint i = tqfind( date );
//  kdDebug() << "contains looking for " << date.toString() << " at range " << i << endl;
  if ( i == mDates->count() )
    return false;

  TQPair<TQDate,TQDate>* item = mDates->at( i );
  // kdDebug() << "contains looking at range " << item->first.toString() << " -- " << item->second.toString() << endl;
  return ( item->first <= date );
}

// returns true if and only if the whole range is in the set
bool DateSet::tqcontains( TQDate const& from, TQDate const& to )
{
  if (mDates->isEmpty()) {
    return false;
  } 

  uint i = tqfind( from );
  if ( i == mDates->count() )
    return false;

  TQPair<TQDate,TQDate>* item = mDates->at( i );

  return ( from >= item->first && to <= item->second );
}

// Finds the index in mDates of the range containing date, if it
// exists. Else, return the index of the range following the date.
// If mDates is empty, return 0.
// If date is later than the last item in mDates, return mDates->count()

int DateSet::tqfind( TQDate const& date )
{
  if ( mDates->isEmpty() )
    return 0;

  int start = 0;
  int end = mDates->count();
  while ( start < end ) {
    int i = start + (end-start) / 2;
    // kdDebug() << start << ", " << i << ", " << end << endl;
    TQPair<TQDate,TQDate> *item = mDates->at( i );
    if ( item->first <= date && date <= item->second )
      return i;
    if ( date > item->second ) {
      start = i+1;
    } else { // this means date < item->first
      end = i;
    }
  }

  // kdDebug() << "Found for date " << date.toString() << " range " << end << endl;
  return end;
/*
  // Now either start==end or start+1 == end
  if ( mDates->at( end )->second < date )
    return end+1;
  else if (mDates->at( start )->first > date )
    return start;
  else
    return end;
*/
}

void DateSet::print()
{
  for( uint i=0; i<mDates->count(); i++ )
  {
    TQDate start = mDates->at( i )->first;
    TQDate end = mDates->at( i )->second;
    if (start == end)
      kdDebug() << start.toString() << endl;
    else
      kdDebug() << "(" << start.toString() << " , " << end.toString() << ")" << endl;
  }
}

// Try and merge range i with range i+1
// NOT TRUE preconditions: range i starts before range i+1, but MAY end later!
// preconditions: range i starts before or in range i+1
bool DateSet::tryMerge( int i )
{
  if ( i < 0 || i+1 >= (int)(mDates->count()) )
    return false;

  TQPair<TQDate,TQDate>* item1 = mDates->at( i );
  TQPair<TQDate,TQDate>* item2 = mDates->at( i+1 );

  // First case: item1 starts before or on the same date as item2
  if ( item1->first <= item2->first ) {
    // Check for overlap
    if ( item1->second >= item2->first ||
         item1->second.daysTo( item2->first ) == 1 ) {
      kdDebug() << "Merging items " << i << " and " << (i+1) << endl;
      if (item1->second < item2->second) item1->second = item2->second;
      mDates->remove( i+1 );
      return true;
    }
    return false;
  }

  // Second case: item1 starts later than item2 (but at the latest on
  // the last day of item2, see preconditions!)
  
  // Check for overlap
  if ( item1->second >= item2->first ||
       item1->second.daysTo( item2->first ) == 1 ) {
    kdDebug() << "Merging items " << i << " and " << (i+1) << endl;
    if (item1->second < item2->second) item1->second = item2->second;
    item1->first = item2->first;
    mDates->remove( i+1 );
    return true;
  }
  return false;
}