libUTL++
SortedCollection.h
1 #pragma once
2 
4 
5 #include <libutl/Collection.h>
6 #include <libutl/Ordering.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
31 
34 {
37 
38 public:
45  void clobber(const SortedCollection* rhs);
46 
51  virtual int compare(const Object& rhs) const;
52 
53  bool
54  addOrFind(const Object& object)
55  {
56  return super::addOrFind(object);
57  }
58 
59  virtual Object* addOrFind(const Object* object);
60 
61  bool
62  addOrUpdate(const Object& object)
63  {
64  return super::addOrUpdate(object);
65  }
66 
67  virtual bool addOrUpdate(const Object* object);
68 
74  void
75  insert(const Object& object, const BidIt& it)
76  {
77  if (isOwner())
78  insert(object.clone(), it);
79  else
80  return insert(&object, it);
81  }
82 
88  virtual void insert(const Object* object, const BidIt& it);
89 
94  void reverse();
95 
97 
98 
103  void
104  setOrdering(const Ordering& ordering, uint_t algorithm = sort_quickSort)
105  {
106  setOrdering(ordering.clone(), algorithm);
107  }
108 
114  virtual void setOrdering(Ordering* ordering, uint_t algorithm = sort_quickSort);
116 
118 
119 
125  void
126  findEqualRange(const Object& key, BidIt& begin, BidIt& end) const
127  {
128  const_cast_this->findEqualRange(key, begin, end);
129  IFDEBUG(begin.setConst(true));
130  IFDEBUG(end.setConst(true));
131  }
132 
139  virtual void findEqualRange(const Object& key, BidIt& begin, BidIt& end);
140 
147  void
148  findFirstIt(const Object& key, BidIt& it, bool insert = false) const
149  {
150  const_cast_this->findFirstIt(key, it);
151  IFDEBUG(it.setConst(true));
152  }
153 
160  virtual void findFirstIt(const Object& key, BidIt& it, bool insert = false);
161 
167  void
168  findLastIt(const Object& key, BidIt& it) const
169  {
170  const_cast_this->findLastIt(key, it);
171  IFDEBUG(it.setConst(true));
172  }
173 
179  virtual void findLastIt(const Object& key, BidIt& it);
180 
187  Object* findLinear(const Object& key) const;
188 
195  Object* findLinearSorted(const Object& key) const;
197 
199 
200  bool
201  remove(const Object* key)
202  {
203  ASSERTD(key != nullptr);
204  return remove(*key);
205  }
206 
207  virtual bool
208  remove(const Object& key)
209  {
210  return super::remove(key);
211  }
212 
218  void
219  remove(const SortedCollection* rhs)
220  {
221  difference(rhs, this);
222  }
223 
228  virtual void
230  {
231  super::removeIt(it);
232  }
233 
239  virtual void removeIt(BidIt& begin, const BidIt& end);
240 
247  {
248  remove(rhs);
249  return *this;
250  }
252 
254 
255 
262  Collection* difference(const SortedCollection* rhs, Collection* out = nullptr) const;
263 
269  void
271  {
272  intersection(rhs, this);
273  }
274 
284  Collection* intersection(const SortedCollection* rhs,
285  Collection* out = nullptr,
286  bool multiSet = false) const;
287 
294  size_t intersectCard(const SortedCollection* rhs) const;
295 
301  bool intersects(const SortedCollection* rhs) const;
302 
309  bool isSubSet(const SortedCollection* rhs) const;
310 
317  bool
318  isSuperSet(const SortedCollection* rhs) const
319  {
320  return rhs->isSubSet(this);
321  }
322 
330  Collection* merge(const SortedCollection* rhs, Collection* out = nullptr) const;
331 
339  Collection* symmetricDifference(const SortedCollection* rhs, Collection* out = nullptr) const;
340 
348  void
349  symmetricDifference(const SortedCollection* rhs, Collection* lhsOut, Collection* rhsOut) const;
350 
357  Collection* unique(Collection* out = nullptr);
358 
365  SortedCollection& operator&=(const SortedCollection* rhs)
366  {
367  intersect(rhs);
368  return *this;
369  }
370 
377  bool operator<=(const SortedCollection* rhs)
378  {
379  return isSubSet(rhs);
380  }
381 
388  bool operator>=(const SortedCollection* rhs)
389  {
390  return isSuperSet(rhs);
391  }
393 
395 
396  bool testSorted() const;
397 
399 
407  void multiKeyQuickSort(bool key = true, bool reverse = false);
408 
410  void
412  {
414  }
415 
421  virtual void sort(uint_t algorithm);
423 
424 #ifdef DEBUG
425  virtual void sanityCheck() const;
426 #endif
427 protected:
428  inline int
429  compareObjects(const Object* lhs, const Object* rhs) const
430  {
431  return super::compareObjects(lhs, rhs);
432  }
433 
434 #ifdef DEBUG
435  void checkInsert(const Object* object, const BidIt& it, bool sorted) const;
436 #endif
437 };
438 
440 
441 UTL_NS_END;
bool isSubSet(const SortedCollection *rhs) const
Determine whether self is a subset of rhs.
void intersect(const SortedCollection *rhs)
Set self to the intersection of self and rhs.
#define UTL_CLASS_DEFID
Default init() and deInit() (which are merely place-holders).
Definition: macros.h:532
T * clone(const T *object)
Create a clone of the given object.
Definition: util_inl.h:404
void intersect(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false, bool multiSet=false, bool outIsLHS=false)
Determine the intersection of two sorted sequences.
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
void merge(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false)
Merge two sorted sequences into a single sorted sequence.
Object comparison abstraction.
Definition: Ordering.h:30
bool intersects(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr)
Determine whether the intersection of two sorted sequences is non-empty.
void difference(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false, bool outIsLHS=false)
Determine the difference between two sorted sequences.
void sort(const FwdIt &begin, const FwdIt &end, const Ordering *ordering=nullptr, uint_t algorithm=sort_quickSort, size_t size=size_t_max)
Sort a sequence using a given algorithm.
void multiKeyQuickSort(const FwdIt &begin, const FwdIt &end, bool key=true, bool reverse=false, size_t size=size_t_max)
Multi-key quick-sort a sequence.
void remove(FwdIt &begin, const FwdIt &end, bool cmp=false, const Predicate *pred=nullptr, bool predVal=false)
Remove objects from a sequence.
quick sort
Definition: algorithms.h:25
void findLastIt(const Object &key, BidIt &it) const
Find the last object matching the given key.
void reverse(const BidIt &begin, const BidIt &end)
Reverse a sequence.
SortedCollection & operator-=(const SortedCollection &rhs)
Remove objects that have a match in rhs.
void symmetricDifference(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false)
Determine the symmetric difference of two sorted sequences.
#define IFDEBUG(x)
Do x in DEBUG mode only.
void findFirstIt(const Object &key, BidIt &it, bool insert=false) const
Find the first object matching the given key.
bool operator<=(const SortedCollection *rhs)
Determine whether self is a subset of rhs.
void sort()
Sort the collection using the quicksort algorithm.
Abstraction for a Collection whose objects may be sorted.
size_t intersectCard(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr, bool multiSet=false)
Determine the cardinality of the intersection of two sorted sequences.
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
#define UTL_CLASS_DECL_ABC(DC, BC)
Declaration of standard UTL++ functionality for an abstract base class (ABC).
Definition: macros.h:650
bool isSuperSet(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr)
Determine whether the lhs sorted sequence is a superset of the rhs sorted sequence.
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Bi-directional iterator abstraction.
Definition: BidIt.h:25
bool operator>=(const SortedCollection *rhs)
Determine whether self is a superset of rhs.
void findEqualRange(const Object &key, BidIt &begin, BidIt &end) const
Find the range elements [begin,end) that match a given key.
bool isSubSet(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr)
Determine whether the lhs sorted sequence is a subset of the rhs sorted sequence. ...
virtual void removeIt(BidIt &it)
Remove the object the given iterator points to.
void clobber()
Fake read/write access to all memory.
Definition: util_inl.h:973
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void insert(const Object &object, const BidIt &it)
Insert an object before the pointed-to location.
bool isSuperSet(const SortedCollection *rhs) const
Determine whether self is a superset of rhs.
void unique(const FwdIt &begin, const FwdIt &end, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false, bool outIsSrc=false)
Remove duplicate objects from a sorted sequence.
#define ASSERTD
Do an assertion in DEBUG mode only.
void setOrdering(const Ordering &ordering, uint_t algorithm=sort_quickSort)
Set the ordering, and optionally sort the collection to reflect the new ordering. ...
A collection of objects.
Definition: Collection.h:62