libUTL++
Collection.h
1 #pragma once
2 
4 
5 #include <libutl/BidIt.h>
6 #include <libutl/Predicate.h>
7 #include <libutl/algorithms.h>
8 #ifdef DEBUG
9 #include <libutl/Mutex.h>
10 #endif
11 
13 
14 UTL_NS_BEGIN;
15 
17 
18 class ListNode;
19 class SlistNode;
20 
22 
60 
62 class Collection : public Object, protected FlagsMI
63 {
65 
66 public:
67  typedef Object type;
68  typedef Object* value_type;
69  typedef TBidIt<> iterator;
70 
71 public:
76  void assertOwner();
77 
78  virtual void copy(const Object& rhs);
79 
80  virtual void steal(Object& rhs);
81 
82  virtual void vclone(const Object& rhs);
83 
84  virtual void dump(Stream& os, uint_t level = uint_t_max) const;
85 
86  virtual void
87  serialize(Stream& stream, uint_t io, uint_t mode = ser_default)
88  {
89  serialize(nullptr, stream, io, mode);
90  }
91 
98  void serialize(const RunTimeClass* rtc, Stream& stream, uint_t io, uint_t mode = ser_default);
99 
100  virtual size_t innerAllocatedSize() const;
101 
107  virtual bool update(const Object* object);
108 
110 
111 
112  bool
113  isOwner() const
114  {
115  return getFlag(flg_owner);
116  }
117 
119  void
120  setOwner(bool owner)
121  {
122  setFlag(flg_owner, owner);
123  }
124 
126  const Ordering*
127  ordering() const
128  {
129  return _ordering;
130  }
131 
133  Ordering*
135  {
136  return _ordering;
137  }
138 
144  void
145  setOrdering(const Ordering& ordering, uint_t algorithm = sort_quickSort)
146  {
147  setOrdering(ordering.clone(), algorithm);
148  }
149 
155  virtual void setOrdering(Ordering* ordering, uint_t algorithm = sort_quickSort);
156 
158  bool
159  empty() const
160  {
161  return (items() == 0);
162  }
163 
165  bool
166  isMultiSet() const
167  {
168  return getFlag(flg_multiSet);
169  }
170 
172  void
173  setMultiSet(bool multiSet)
174  {
175  setFlag(flg_multiSet, multiSet);
176  }
177 
179  bool
180  isMarked() const
181  {
182  return getFlag(flg_marked);
183  }
184 
186  void
187  setMarked(bool marked = true)
188  {
189  setFlag(flg_marked, marked);
190  }
191 
193  size_t
194  items() const
195  {
196  return _items;
197  }
198 
200  size_t
201  size() const
202  {
203  return _items;
204  }
206 
208 
209 
214  bool
215  add(const Object& object)
216  {
217  if (isOwner())
218  return add(object.clone());
219  else
220  return add(&object);
221  }
222 
228  virtual bool add(const Object* object) = 0;
229 
234  void
235  add(const Collection& collection)
236  {
237  copyItems(collection);
238  }
239 
245  virtual BidIt* addIt(const Object* object);
246 
251  Object*
252  addOrFind(const Object& object)
253  {
254  if (isOwner()) return addOrFind(object.clone());
255  return addOrFind(&object);
256  }
257 
262  virtual Object* addOrFind(const Object* object);
263 
270  bool
271  addOrUpdate(const Object& object)
272  {
273  if (isOwner()) return addOrUpdate(object.clone());
274  return addOrUpdate(&object);
275  }
276 
283  virtual bool addOrUpdate(const Object* object);
284 
293  Collection&
294  copyItems(const Collection* src, const Predicate* pred = nullptr, bool predVal = true);
295 
303  Collection&
304  copyItems(const ListNode* src, const Predicate* pred = nullptr, bool predVal = true);
305 
313  Collection&
314  copyItems(const SlistNode* src, const Predicate* pred = nullptr, bool predVal = true);
315 
317  Collection& stealItems(Collection* src);
318 
326  {
327  add(rhs);
328  return *this;
329  }
330 
337  {
338  add(rhs);
339  return *this;
340  }
341 
348  {
349  add(rhs);
350  return *this;
351  }
352 
359  {
360  remove(rhs);
361  return *this;
362  }
364 
366 
367 
376  void dump(Stream& os,
377  uint_t level,
378  bool key,
379  bool printClassName,
380  uint_t indent,
381  const char* separator) const;
382 
386  virtual String toString() const;
387 
396  String toString(bool key) const;
397 
407  String
408  toString(const char* sep, bool key = false) const
409  {
410  return toString(String(sep, false), key);
411  }
412 
422  String toString(const String& sep, bool key = false) const;
424 
426 
427 
428  iterator begin() const;
429 
431  iterator begin();
432 
434  virtual BidIt* beginNew() const = 0;
435 
437  virtual BidIt* beginNew() = 0;
438 
440  virtual BidIt* createIt() const;
441 
443  virtual BidIt* createIt();
444 
446  iterator end() const;
447 
449  iterator end();
450 
452  virtual BidIt* endNew() const = 0;
453 
455  virtual BidIt* endNew() = 0;
457 
459 
460 
465  bool
466  contains(const Object* key) const
467  {
468  ASSERTD(key != nullptr);
469  return contains(*key);
470  }
471 
477  bool contains(const Object& key) const;
478 
484  size_t count(const Predicate* pred = nullptr, bool predVal = true) const;
485 
491  Object*
492  find(const Object* key) const
493  {
494  ASSERTD(key != nullptr);
495  return find(*key);
496  }
497 
503  virtual Object* find(const Object& key) const;
504 
510  inline void
511  findIt(const Object& key, BidIt& it) const
512  {
513  const_cast_this->findIt(key, it);
514  IFDEBUG(it.setConst(true));
515  }
516 
522  virtual void findIt(const Object& key, BidIt& it);
523 
525  Object* first() const;
526 
528  bool
529  has(const Object* key) const
530  {
531  ASSERTD(key != nullptr);
532  return contains(*key);
533  }
534 
536  bool
537  has(const Object& key) const
538  {
539  return contains(key);
540  }
541 
543  Object* last() const;
545 
547 
548 
549  virtual void clear() = 0;
550 
556  bool
557  remove(const Object* key)
558  {
559  ASSERTD(key != nullptr);
560  return remove(*key);
561  }
562 
568  virtual bool remove(const Object& key);
569 
575  virtual void removeIt(BidIt& it);
576 
583  Object* take(BidIt& it);
585 
587 
588 
589  Object* operator[](const Object* object)
590  {
591  return addOrFind(object);
592  }
593 
595  Object& operator()(const Object* object)
596  {
597  return *addOrFind(object);
598  }
600 
601 #ifdef DEBUG
602  virtual void sanityCheck() const;
603 
604  virtual void addOwnedIt(const FwdIt* it) const;
605 
606  virtual bool hasOwnedIt(const FwdIt* it) const;
607 
608  virtual void removeOwnedIt(const FwdIt* it) const;
609 
610  void exciseOwnedIts() const;
611 #endif
612 
613 protected:
614  enum flg_t
615  {
616  flg_owner,
617  flg_multiSet,
618  flg_marked
619  };
620 
621 protected:
622  inline int
623  compareObjects(const Object* lhs, const Object* rhs) const
624  {
625  ASSERTD(lhs != nullptr);
626  ASSERTD(rhs != nullptr);
627  return (_ordering == nullptr) ? lhs->compare(*rhs) : _ordering->cmp(lhs, rhs);
628  }
629 
630 protected:
631  size_t _items;
632 
633 private:
634  void init();
635  void deInit();
636 
637 private:
638  Ordering* _ordering;
639 #ifdef DEBUG
640  mutable class RBtree* _ownedIts;
641  mutable class Mutex _ownedItsLock;
642 #endif
643 };
644 
646 
647 UTL_NS_END;
List node.
Definition: ListNode.h:28
T * clone(const T *object)
Create a clone of the given object.
Definition: util_inl.h:404
String toString(const FwdIt &begin, const FwdIt &end, const String &sep, bool key=false)
Obtain a string representation of a sequence (via Object::toString()).
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
const Ordering * ordering() const
Get the ordering.
Definition: Collection.h:127
void deInit()
De-initialize UTL++.
Object comparison abstraction.
Definition: Ordering.h:30
bool empty() const
Determine whether the collection is empty.
Definition: Collection.h:159
default representation (via getSerializeMode())
Definition: util.h:75
Store information about a class.
Definition: RunTimeClass.h:23
quick sort
Definition: algorithms.h:25
Character string.
Definition: String.h:31
void add(const Collection &collection)
Add another collection&#39;s objects.
Definition: Collection.h:235
bool contains(const Object *key) const
Determine whether the collection contains an object matching the given key.
Definition: Collection.h:466
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
Templated proxy for BidIt.
Definition: TBidIt.h:19
size_t size() const
Get the number of contained objects.
Definition: Collection.h:201
#define IFDEBUG(x)
Do x in DEBUG mode only.
Red/black tree.
Definition: RBtree.h:37
MUTual EXclusion device.
Definition: Mutex.h:27
Object * operator[](const Object *object)
add-or-find access operator (returns Object*).
Definition: Collection.h:589
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
void setOrdering(const Ordering &ordering, uint_t algorithm=sort_quickSort)
Set the ordering, and optionally sort the collection to reflect the new ordering. ...
Definition: Collection.h:145
void setMarked(bool marked=true)
Set marked flag.
Definition: Collection.h:187
bool isOwner() const
Get the owner flag.
Definition: Collection.h:113
String toString(const char *sep, bool key=false) const
Obtain a string represenation by invoking Object::toString() on all contained objects (or their keys)...
Definition: Collection.h:408
Logical predicate abstraction.
Definition: Predicate.h:26
Forward iterator abstraction.
Definition: FwdIt.h:26
void setOwner(bool owner)
Set the owner flag.
Definition: Collection.h:120
void findIt(const Object &key, BidIt &it) const
Find an object matching a given key.
Definition: Collection.h:511
bool addOrUpdate(const Object &object)
Add or update the given object.
Definition: Collection.h:271
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
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
const uint_t uint_t_max
Maximum uint_t value.
bool has(const Object *key) const
See contains().
Definition: Collection.h:529
Stream I/O abstraction.
Definition: Stream.h:68
Bi-directional iterator abstraction.
Definition: BidIt.h:25
Collection & operator+=(const Collection &rhs)
Add another collection&#39;s objects.
Definition: Collection.h:347
size_t items() const
Get the number of contained objects.
Definition: Collection.h:194
bool has(const Object &key) const
See contains().
Definition: Collection.h:537
virtual int compare(const Object &rhs) const
Compare with another object.
void setMultiSet(bool multiSet)
Set the multiSet flag.
Definition: Collection.h:173
bool isMarked() const
Get marked flag.
Definition: Collection.h:180
Object * addOrFind(const Object &object)
Add the given object, or find a matching object already contained.
Definition: Collection.h:252
bool isMultiSet() const
Get the multiSet flag.
Definition: Collection.h:166
Collection & operator-=(const Object &rhs)
Remove an object from the collection.
Definition: Collection.h:358
Object * find(const Object *key) const
Find an object matching a given key.
Definition: Collection.h:492
Object & operator()(const Object *object)
add-or-find access operator (returns Object&).
Definition: Collection.h:595
virtual void serialize(Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize to or from a stream.
Definition: Collection.h:87
Ordering * ordering()
Get the ordering.
Definition: Collection.h:134
Singly-linked list node.
Definition: SlistNode.h:26
Root of UTL++ class hierarchy.
Definition: Object.h:52
bool add(const Object &object)
Add an object to the collection.
Definition: Collection.h:215
Collection & operator+=(const Object &rhs)
Add an object to the collection.
Definition: Collection.h:325
void dump(const FwdIt &begin, const FwdIt &end, Stream &os, uint_t level=uint_t_max, bool key=false, bool printClassName=false, uint_t indent=0, const char *separator=nullptr)
Dump objects to the given stream (with Object::dump()).
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
size_t count(const FwdIt &begin, const FwdIt &end, const Predicate *pred=nullptr, bool predVal=true)
Count objects that satisfy a Predicate.
A collection of objects.
Definition: Collection.h:62
Collection & operator+=(const Object *rhs)
Add an object to the collection.
Definition: Collection.h:336