libUTL++
Array.h
1 #pragma once
2 
4 
5 #include <libutl/RandUtils.h>
6 #include <libutl/SortedCollection.h>
7 #include <libutl/Vector.h>
8 
10 
11 UTL_NS_BEGIN;
12 
14 
15 class ArrayIt;
16 
114 
116 class Array : public SortedCollection
117 {
119  friend class ArrayIt;
120 
121 public:
122  typedef Object* const* iterator; // native iterator
123  typedef ArrayIt base_iterator; // iterator for compatibility with base
124 
125 public:
135  Array(size_t size,
136  size_t increment = size_t_max,
137  bool owner = true,
138  bool multiSet = true,
139  bool keepSorted = false,
140  Ordering* ordering = nullptr);
141 
149  Array(bool owner, bool multiSet = true, bool keepSorted = false, Ordering* ordering = nullptr);
150 
151  virtual void copy(const Object& rhs);
152 
153  virtual void steal(Object& rhs);
154 
156 
157  virtual size_t innerAllocatedSize() const;
158 
160  size_t
161  firstNull() const
162  {
163  return _firstNull;
164  }
165 
171  Object*
172  get(size_t idx) const
173  {
174  return _array[idx];
175  }
176 
178  size_t
179  totalSize() const
180  {
181  return _array.size();
182  }
183 
185  bool
186  hasHole() const
187  {
188  return (_firstNull != _array.size());
189  }
190 
192  bool
193  isKeptSorted() const
194  {
195  return getFlag(flg_keepSorted);
196  }
197 
199  bool
200  isSorted() const
201  {
202  return getFlag(flg_sorted);
203  }
205 
207 
208 
213  void removeHoles(size_t low = 0, size_t high = size_t_max);
214 
216  void
218  {
219  _array.economize();
220  }
221 
226  void
228  {
229  clear();
230  _array.clear();
231  }
232 
237  void
238  reserve(size_t minSize)
239  {
240  _array.reserve(minSize);
241  }
242 
249  void
250  set(size_t idx, const Object& object, size_t firstNull = size_t_max)
251  {
252  set(idx, isOwner() ? object.clone() : &object, firstNull);
253  }
254 
261  void set(size_t idx, const Object* object, size_t firstNull = size_t_max);
262 
264  void
265  setIncrement(size_t increment)
266  {
267  _array.setIncrement(increment);
268  }
269 
271  void
272  setKeepSorted(bool keepSorted)
273  {
274  setFlag(flg_keepSorted, keepSorted);
275  if (keepSorted) setFlag(flg_sorted, true);
276  }
277 
279  void
280  setSorted(bool sorted)
281  {
282  setFlag(flg_sorted, sorted);
283  if (!sorted) setFlag(flg_keepSorted, false);
284  }
285 
292  void
293  shuffle(size_t low = 0, size_t high = size_t_max, randutils::mt19937_64_rng* rng = nullptr);
294 
295  void
296  sort()
297  {
299  }
300 
301  virtual void sort(uint_t algorithm);
303 
305 
306  virtual Object* find(const Object& key) const;
307 
317  size_t findIndex(const Object& key,
318  uint_t findType = uint_t_max,
319  size_t low = 0,
320  size_t high = size_t_max) const;
321 
327  iterator findIt(const Object& key) const;
328 
329  void
330  findIt(const Object& key, BidIt& it) const
331  {
332  super::findIt(key, it);
333  }
334 
335  virtual void findIt(const Object& key, BidIt& it);
336 
343  iterator findFirstIt(const Object& key, bool insert = false) const;
344 
345  void
346  findFirstIt(const Object& key, BidIt& it, bool insert) const
347  {
348  const_cast_this->findFirstIt(key, it, insert);
349  IFDEBUG(it.setConst(true));
350  }
351 
352  virtual void findFirstIt(const Object& key, BidIt& it, bool insert = false);
353 
359  iterator findLastIt(const Object& key) const;
360 
361  void
362  findLastIt(const Object& key, BidIt& it) const
363  {
364  const_cast_this->findLastIt(key, it);
365  IFDEBUG(it.setConst(true));
366  }
367 
368  virtual void findLastIt(const Object& key, BidIt& it);
370 
372 
373  bool
374  add(const Object& object)
375  {
376  return super::add(object);
377  }
378 
379  virtual bool
380  add(const Object* object)
381  {
382  return add(object, isKeptSorted());
383  }
384 
391  bool add(const Object* object, bool keepSorted);
392 
393  void
394  add(const Collection& collection)
395  {
396  super::add(collection);
397  }
398 
404  void add(size_t idx, size_t num = 1);
405 
412  void
413  add(size_t idx, const Object& object)
414  {
415  add(idx, isOwner() ? object.clone() : &object);
416  }
417 
424  void add(size_t idx, const Object* object);
425 
426  void
427  insert(const Object& object, iterator it)
428  {
429  insert(isOwner() ? object.clone() : &object, it);
430  }
431 
432  void insert(const Object* object, iterator it);
433 
434  void
435  insert(const Object& object, const BidIt& it)
436  {
437  return super::insert(object, it);
438  }
439 
440  virtual void insert(const Object* object, const BidIt& it);
442 
444 
445 
446  virtual void clear();
447 
448  bool
449  remove(const Object* key)
450  {
451  ASSERTD(key != nullptr);
452  return remove(*key, true, true);
453  }
454 
455  virtual bool
456  remove(const Object& key)
457  {
458  return remove(key, true, true);
459  }
460 
468  bool remove(const Object& key, bool relocate, bool preserveOrder = true);
469 
476  void remove(size_t idx, bool relocate = true, bool preserveOrder = true);
477 
484  void removeIt(iterator it, bool relocate = true, bool preserveOrder = true);
485 
493  void removeIt(iterator begin, iterator end, bool relocate = true, bool preserveOrder = true);
494 
495  virtual void removeIt(BidIt& it);
496 
497  virtual void removeIt(BidIt& begin, BidIt& end);
498 
506  void removeItems(size_t idx, size_t num, bool relocate = true, bool preserveOrder = true);
508 
510 
517  void replace(iterator it, const Object* newObject, bool keepSorted = true);
518 
524  void replace(base_iterator& it, const Object* newObject);
525 
531  void
532  replace(size_t idx, const Object* newObject)
533  {
534  replace(this->begin() + idx, newObject);
535  }
536 
543  bool
544  replace(const Object& oldObject, const Object* newObject)
545  {
546  return replace(&oldObject, newObject);
547  }
548 
555  bool replace(const Object* oldObject, const Object* newObject);
557 
559 
560 
565  void
566  relocate(iterator destIt, iterator srcIt)
567  {
568  _array.relocate(indexOf(destIt), indexOf(srcIt));
569  }
570 
576  void
577  relocate(size_t destIdx, size_t srcIdx)
578  {
579  _array.relocate(destIdx, srcIdx);
580  }
582 
584 
585  inline iterator
586  begin() const
587  {
588  return _array.begin();
589  }
590 
591  inline iterator
592  end() const
593  {
594  return _array.end();
595  }
596 
597  inline virtual RandIt* beginNew() const;
598 
599  inline virtual RandIt* beginNew();
600 
601  inline virtual RandIt* endNew() const;
602 
603  inline virtual RandIt* endNew();
604 
605  size_t
606  indexOf(iterator it) const
607  {
608  ASSERTD(isValid(it));
609  return it - this->begin();
610  }
611 
612 #ifdef DEBUG
613  bool
614  isValid(iterator it) const
615  {
616  return (it >= this->begin()) && (it <= this->end());
617  }
618 #endif
619 
620 
622 
623 
629  Object* operator[](size_t idx) const
630  {
631  return get(idx);
632  }
633 
634  Object* operator[](int idx) const
635  {
636  return operator[]((size_t)idx);
637  }
638 
639  Object* operator[](size_t idx)
640  {
641  return get(idx);
642  }
643 
644  Object* operator[](int idx)
645  {
646  return operator[]((size_t)idx);
647  }
648 
649 #if UTL_HOST_WORDSIZE == 64
650  Object* operator[](uint_t idx) const
651  {
652  return operator[]((size_t)idx);
653  }
654  Object* operator[](uint_t idx)
655  {
656  return operator[]((size_t)idx);
657  }
658 #endif
659 
666  Object& operator()(size_t idx) const
667  {
668  ASSERTD(idx < _array.size());
669  ASSERTD(_array[idx] != nullptr);
670  return *(_array[idx]);
671  }
672 
673  Object& operator()(size_t idx)
674  {
675  return const_self(idx);
676  }
677  Object& operator()(int idx) const
678  {
679  return operator()((size_t)idx);
680  }
681  Object& operator()(int idx)
682  {
683  return operator()((size_t)idx);
684  }
685 
686 #if UTL_HOST_WORDSIZE == 64
687  Object& operator()(uint_t idx) const
688  {
689  return operator()((size_t)idx);
690  }
691  Object& operator()(uint_t idx)
692  {
693  return operator()((size_t)idx);
694  }
695 #endif
696 
698  Object* operator[](const Object* object)
699  {
700  return addOrFind(object);
701  }
702 
704  Object& operator()(const Object* object)
705  {
706  return *addOrFind(object);
707  }
708 
709 #ifdef DEBUG
710  virtual void sanityCheck() const;
711 #endif
712 
713 private:
714  void init(size_t size = 4,
715  size_t increment = size_t_max,
716  bool owner = true,
717  bool multiSet = true,
718  bool keepSorted = false,
719  Ordering* ordering = nullptr);
720  void deInit();
721 
722  iterator findIt(const Object& key, uint_t findType) const;
723 
724  void _remove(size_t idx);
725 
726  void setFirstNull(size_t idx);
727 
728  void
729  fixArraySize()
730  {
731  fixArraySize(_array.size());
732  }
733 
734  void fixArraySize(size_t size);
735 
736 private:
737  enum flg_t
738  {
739  flg_keepSorted = 4,
740  flg_sorted = 5
741  };
742 
743 private:
744  size_t _firstNull;
745  Vector<Object*> _array;
746 };
747 
749 
750 UTL_NS_END;
751 
753 
754 #include <libutl/ArrayIt.h>
755 #include <libutl/TArray.h>
void setKeepSorted(bool keepSorted)
Set the keepSorted flag.
Definition: Array.h:272
T * clone(const T *object)
Create a clone of the given object.
Definition: util_inl.h:404
void setSorted(bool sorted)
Set the sorted flag.
Definition: Array.h:280
size_t totalSize() const
Get the size of the array (which is only as large as needed).
Definition: Array.h:179
bool isSorted() const
Return the sorted flag.
Definition: Array.h:200
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
Object & operator()(size_t idx) const
Array access operator (returning reference instead of pointer).
Definition: Array.h:666
void deInit()
De-initialize UTL++.
Random-access Array iterator.
Definition: ArrayIt.h:22
Object comparison abstraction.
Definition: Ordering.h:30
size_t firstNull() const
Return the index of the first null object in the array.
Definition: Array.h:161
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 relocate(iterator destIt, iterator srcIt)
Relocate an object to a new position.
Definition: Array.h:566
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
quick sort
Definition: algorithms.h:25
void relocate(size_t destIdx, size_t srcIdx)
Relocate an object to a new position.
Definition: Array.h:577
void add(size_t idx, const Object &object)
Add an object at a given index.
Definition: Array.h:413
void economize()
Make the allocation exactly large enough to contain the last non-null Object*.
Definition: Array.h:217
Random-access iterator abstraction.
Definition: RandIt.h:25
const size_t size_t_max
Maximum size_t value.
Object * operator[](size_t idx) const
Array access operator.
Definition: Array.h:629
#define IFDEBUG(x)
Do x in DEBUG mode only.
SortedCollection that stores objects in an array.
Definition: Array.h:116
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
Abstraction for a Collection whose objects may be sorted.
Object & operator()(const Object *object)
Add-or-find access operator (returns Object&).
Definition: Array.h:704
bool isKeptSorted() const
Return the keepSorted flag.
Definition: Array.h:193
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
const uint_t uint_t_max
Maximum uint_t value.
Bi-directional iterator abstraction.
Definition: BidIt.h:25
void excise()
Same as clear(), but in addition the Object*[] array is deleted.
Definition: Array.h:227
virtual bool add(const Object *object)
Add an object to the collection.
Definition: Array.h:380
A sequence of same-type objects.
Definition: Ordering.h:14
void reserve(size_t minSize)
Reserve allocation space.
Definition: Array.h:238
bool hasHole() const
Determine whether there are one or more "holes" in the array.
Definition: Array.h:186
Object * operator[](const Object *object)
Add-or-find access operator (returns Object*).
Definition: Array.h:698
bool replace(const Object &oldObject, const Object *newObject)
Replace oldObject with newObject.
Definition: Array.h:544
Root of UTL++ class hierarchy.
Definition: Object.h:52
void setIncrement(size_t increment)
Set the growth increment.
Definition: Array.h:265
#define const_self
Reference to the object the method was invoked on (adding const qualifier).
Definition: macros.h:17
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
void replace(size_t idx, const Object *newObject)
Replace object at idx with newObject.
Definition: Array.h:532
A collection of objects.
Definition: Collection.h:62