libUTL++
List.h
1 #pragma once
2 
4 
5 #include <libutl/SortedCollection.h>
6 #include <libutl/ListNode.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
14 class ListIt;
15 
17 
78 
80 class List : public SortedCollection
81 {
83 
84 public:
85  typedef ListIt iterator;
86 
87 public:
95  List(bool owner, bool multiSet = true, bool keepSorted = false, Ordering* ordering = nullptr);
96 
97  virtual void steal(Object& rhs);
98 
99  virtual size_t innerAllocatedSize() const;
100 
102 
103 
104  Object*
105  front() const
106  {
107  return first();
108  }
109 
111  Object*
112  back() const
113  {
114  return last();
115  }
116 
118  const ListNode*
119  frontNode() const
120  {
121  return _front;
122  }
123 
125  bool
126  isKeptSorted() const
127  {
128  return getFlag(flg_keepSorted);
129  }
130 
132  bool
133  isSorted() const
134  {
135  return getFlag(flg_sorted);
136  }
138 
140 
141 
142  void
143  setKeepSorted(bool keepSorted)
144  {
145  setFlag(flg_keepSorted, keepSorted);
146  if (keepSorted) setFlag(flg_sorted, true);
147  }
148 
149  virtual void setOrdering(Ordering* ordering, uint_t algorithm = sort_mergeSort);
150 
151  void
152  setOrdering(const Ordering& ordering, uint_t algorithm = sort_mergeSort)
153  {
154  setOrdering(ordering.clone(), algorithm);
155  }
156 
158  void
159  setSorted(bool sorted)
160  {
161  setFlag(flg_sorted, sorted);
162  if (!sorted) setFlag(flg_keepSorted, false);
163  }
164 
165  void
166  sort()
167  {
169  }
170 
171  virtual void sort(uint_t algorithm);
172 
178  void moveBefore(const iterator& lhs, const iterator& rhs);
180 
182 
183  virtual Object* find(const Object& key) const;
184 
185  iterator findIt(const Object& key) const;
186 
187  iterator findIt(const Object& key);
188 
189  void
190  findIt(const Object& key, BidIt& it) const
191  {
192  const_cast_this->findIt(key, it);
193  IFDEBUG(it.setConst(true));
194  }
195 
196  virtual void findIt(const Object& key, BidIt& it);
197 
198  void
199  findEqualRange(const Object& key, BidIt& begin, BidIt& end) const
200  {
201  const_cast_this->findEqualRange(key, begin, end);
202  IFDEBUG(begin.setConst(true));
203  IFDEBUG(end.setConst(true));
204  }
205 
206  virtual void findEqualRange(const Object& key, BidIt& begin, BidIt& end);
207 
208  void
209  findFirstIt(const Object& key, BidIt& it, bool insert = false) const
210  {
211  const_cast_this->findFirstIt(key, it, insert);
212  IFDEBUG(it.setConst(true));
213  }
214 
215  virtual void findFirstIt(const Object& key, BidIt& it, bool insert = false);
216 
223  iterator findFirstIt(const Object& key, bool insert = false) const;
224 
231  iterator findFirstIt(const Object& key, bool insert = false);
232 
233  void
234  findLastIt(const Object& key, BidIt& it) const
235  {
236  const_cast_this->findLastIt(key, it);
237  IFDEBUG(it.setConst(true));
238  }
239 
240  virtual void findLastIt(const Object& key, BidIt& it);
241 
247  iterator findLastIt(const Object& key) const;
248 
254  iterator findLastIt(const Object& key);
256 
258 
259  bool
260  add(const Object& object)
261  {
262  return super::add(object);
263  }
264 
265  virtual bool add(const Object* object);
266 
267  void
268  add(const Collection& collection)
269  {
270  super::add(collection);
271  }
272 
278  void addBefore(const Object* object, const iterator& it);
279 
281  void
282  pushFront(const Object& object)
283  {
284  isOwner() ? pushFront(object.clone()) : pushFront(&object);
285  }
286 
288  void
289  pushBack(const Object& object)
290  {
291  isOwner() ? pushBack(object.clone()) : pushBack(&object);
292  }
293 
295  void pushFront(const Object* object);
296 
298  void pushBack(const Object* object);
299 
300  void
301  insert(const Object& object, const BidIt& it)
302  {
303  return super::insert(object, it);
304  }
305 
306  virtual void insert(const Object* object, const BidIt& it);
308 
310 
311 
312  virtual void clear();
313 
314  bool
315  remove(const Object* key)
316  {
317  ASSERTD(key != nullptr);
318  return remove(*key);
319  }
320 
321  virtual bool remove(const Object& key);
322 
323  virtual void removeIt(BidIt& it);
324 
326  bool removeFront();
327 
329  bool removeBack();
330 
332  Object* popFront();
333 
335  Object* popBack();
337 
339 
340  inline iterator begin() const;
341 
342  inline iterator begin();
343 
344  inline virtual BidIt* beginNew() const;
345 
346  inline virtual BidIt* beginNew();
347 
348  inline iterator end() const;
349 
350  inline iterator end();
351 
352  inline virtual BidIt* endNew() const;
353 
354  inline virtual BidIt* endNew();
356 
357 private:
358  void init(bool owner = true,
359  bool multiSet = true,
360  bool keepSorted = false,
361  Ordering* ordering = nullptr);
362  void deInit();
363  ListNode* findInsertionPoint(const Object& key) const;
364  iterator findIt(const Object& key, uint_t findType) const;
365  iterator findIt(const Object& key, uint_t findType);
366  ListNode* findNode(const Object& key) const;
368  ListNode* merge(ListNode* a, ListNode* b);
370  void removeNode(ListNode* node);
371 
372 private:
373  ListNode *_front, *_back;
374  enum flg_t
375  {
376  flg_keepSorted = 4,
377  flg_sorted = 5
378  };
379 };
380 
382 
383 UTL_NS_END;
384 
386 
387 #include <libutl/TList.h>
List node.
Definition: ListNode.h:28
void mergeSort(const FwdIt &begin, const FwdIt &end, const Ordering *ordering=nullptr, size_t size=size_t_max)
Merge-sort a sequence.
Doubly-linked list.
Definition: List.h:80
T * clone(const T *object)
Create a clone of the given object.
Definition: util_inl.h:404
bool isSorted() const
Get the sorted flag.
Definition: List.h:133
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
Bi-directional List iterator.
Definition: ListIt.h:22
void deInit()
De-initialize UTL++.
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
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.
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Templated proxy for BidIt.
Definition: TBidIt.h:19
#define IFDEBUG(x)
Do x in DEBUG mode only.
Object * back() const
Return the object at the back of the list (nullptr if none).
Definition: List.h:112
Abstraction for a Collection whose objects may be sorted.
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
void setKeepSorted(bool keepSorted)
Set the keepSorted flag.
Definition: List.h:143
void setSorted(bool sorted)
Set the sorted flag.
Definition: List.h:159
Bi-directional iterator abstraction.
Definition: BidIt.h:25
void insertionSort(const FwdIt &begin, const FwdIt &end, const Ordering *ordering=nullptr, size_t size=size_t_max)
Insertion-sort a sequence.
const ListNode * frontNode() const
Return the front node.
Definition: List.h:119
void pushFront(const Object &object)
Add the given object at the front end.
Definition: List.h:282
Object * front() const
Return the object at the front of the list (nullptr if none).
Definition: List.h:105
Root of UTL++ class hierarchy.
Definition: Object.h:52
bool isKeptSorted() const
Get the keepSorted flag.
Definition: List.h:126
merge sort
Definition: algorithms.h:24
void pushBack(const Object &object)
Add the given object at the back end.
Definition: List.h:289
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
A collection of objects.
Definition: Collection.h:62