libUTL++
Vector.h
1 #pragma once
2 
4 
5 #include <libutl/FlagsMI.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
40 
42 template <typename T>
43 class Vector : public Object, protected FlagsMI
44 {
45  UTL_CLASS_DECL_TPL(Vector, T, Object);
46 
47 public:
48  typedef T type;
49  typedef T value_type;
50  typedef T* iterator;
51  typedef const T* const_iterator;
52 
53 public:
58  Vector(std::initializer_list<T> list);
59 
65  Vector(size_t size, size_t increment = 1);
66 
74  Vector(T* array, size_t size, bool owner = true, size_t increment = 1);
75 
77 
78  virtual int compare(const Object& rhs) const;
79 
80  virtual size_t
82  {
83  if (!isOwner()) return 0;
84  return (_allocSize * sizeof(T));
85  }
86 
88  bool
89  isOwner() const
90  {
91  return getFlag(flg_owner);
92  }
93 
95  bool
96  autoInit() const
97  {
98  return getFlag(flg_autoInit);
99  }
100 
102  const T*
103  get() const
104  {
105  return _array;
106  }
107 
109  T*
110  get()
111  {
112  return _array;
113  }
114 
116  const T&
117  get(size_t idx) const
118  {
119  ASSERTD(idx < _size);
120  return _array[idx];
121  }
122 
124  const T&
125  get(int idx) const
126  {
127  return get((size_t)idx);
128  }
129 
130 #if UTL_HOST_WORDSIZE == 64
131 
132  const T&
133  get(uint_t idx) const
134  {
135  return get((size_t)idx);
136  }
137 #endif
138 
140  bool
141  empty() const
142  {
143  return (_size == 0);
144  }
145 
147  size_t
148  length() const
149  {
150  return _size;
151  }
152 
154  size_t
155  size() const
156  {
157  return _size;
158  }
159 
161  size_t
162  increment() const
163  {
164  return _increment;
165  }
167 
169 
170 
171  const_iterator
172  begin() const
173  {
174  return _array;
175  }
176 
178  const_iterator
179  end() const
180  {
181  return _array + _size;
182  }
183 
185  const_iterator
187  {
188  return _array;
189  }
190 
192  const_iterator
194  {
195  return _array + _size;
196  }
197 
199  iterator
201  {
202  return _array;
203  }
204 
206  iterator
207  end()
208  {
209  return _array + _size;
210  }
212 
214 
215 
216  size_t
217  indexOf(const_iterator it) const
218  {
219  ASSERTD((it >= this->begin()) && (it <= this->end()));
220  return (it - this->begin());
221  }
222 
224  size_t
225  indexOf(iterator it)
226  {
227  ASSERTD((it >= this->begin()) && (it <= this->end()));
228  return (it - this->begin());
229  }
231 
233 
234 
235  void
237  {
238  _size = 0;
239  }
240 
242  void excise();
243 
245  void economize();
246 
247  virtual void steal(Object& rhs);
248 
249  virtual void copy(const Object&);
250 
259  void
260  copy(size_t idx, const Vector<T>& src, size_t srcBegin = 0, size_t srcEnd = size_t_max)
261  {
262  copy(idx, src._array, srcBegin, min(srcEnd, src._size));
263  }
264 
273  void copy(size_t idx, const T* src, size_t srcBegin, size_t srcEnd);
274 
280  void copy(size_t idx, std::initializer_list<T> list);
281 
290  void
291  move(size_t idx, const Vector<T>& src, size_t srcBegin = 0, size_t srcEnd = size_t_max)
292  {
293  move(idx, src._array, srcBegin, min(srcEnd, src._size));
294  }
295 
304  void move(size_t idx, T* src, size_t srcBegin, size_t srcEnd);
305 
306  virtual void serialize(Stream& stream, uint_t io, uint_t mode = ser_default);
307 
309  void
310  setOwner(bool owner)
311  {
312  setFlag(flg_owner, owner);
313  }
314 
316  void assertOwner();
317 
322  void assertOwner(size_t allocSize);
323 
329  void
330  assertOwner(size_t allocSize, size_t increment)
331  {
332  setIncrement(increment);
333  assertOwner(allocSize);
334  }
335 
337  T&
338  get(size_t idx)
339  {
340  ASSERTD(idx < _size);
341  return _array[idx];
342  }
343 
345  T&
346  get(int idx)
347  {
348  return get((size_t)idx);
349  }
350 
351 #if UTL_HOST_WORDSIZE == 64
352 
353  T&
354  get(uint_t idx)
355  {
356  return get((size_t)idx);
357  }
358 #endif
359 
361  void setSize(size_t size);
362 
367  void grow(size_t size);
368 
374  void
375  grow(size_t size, size_t increment)
376  {
377  setIncrement(increment);
378  grow(size);
379  }
380 
385  void
386  reserve(size_t allocSize)
387  {
388  if (allocSize > _allocSize) assertOwner(allocSize);
389  }
390 
396  void
397  reserve(size_t allocSize, size_t increment)
398  {
399  if (allocSize > _allocSize) assertOwner(allocSize, increment);
400  }
401 
403  void
404  setAutoInit(bool autoInit)
405  {
406  setFlag(flg_autoInit, autoInit);
407  }
408 
410  void
411  append(const T& val)
412  {
413  append(&val, 1);
414  }
415 
417  void
418  append(const Vector<T>& v)
419  {
420  append(v._array, v._size);
421  }
422 
424  void append(const T* array, size_t arraySize);
425 
427  void append(std::initializer_list<T> list);
428 
430  void insert(size_t idx, size_t num);
431 
438  void
439  insert(size_t idx, size_t num, const T& val)
440  {
441  insert(idx, num);
442  set(idx, num, val);
443  }
444 
451  void
452  insert(size_t idx, size_t num, const T* val)
453  {
454  insert(idx, num);
455  set(idx, num, *val);
456  }
457 
464  void
465  insert(size_t idx, const T* array, size_t arraySize)
466  {
467  insert(idx, arraySize);
468  copy(idx, array, 0, arraySize);
469  }
470 
476  void
477  insert(size_t idx, std::initializer_list<T> list)
478  {
479  insert(idx, list.size());
480  copy(idx, list);
481  }
482 
488  void remove(size_t idx, size_t num);
489 
495  void relocate(size_t destIdx, size_t srcIdx);
496 
502  void reverse(size_t idx, size_t num);
503 
509  void
510  set(T* array, size_t size)
511  {
512  set(array, size, isOwner(), _increment);
513  }
514 
522  void set(T* array, size_t size, bool owner, size_t increment);
523 
525  void
526  set(const T& val)
527  {
528  set(0, _size, val);
529  }
530 
537  void set(size_t idx, size_t num, const T& val);
538 
544  void
545  set(size_t idx, const T& val)
546  {
547  ASSERTD(idx < _size);
548  _array[idx] = val;
549  }
550 
552  void
553  setIncrement(size_t increment)
554  {
555  ASSERTD((increment == size_t_max) || (increment == nextPow2(increment)));
556  _increment = increment;
557  }
558 
560  inline void swap(size_t i, size_t j);
561 
566  inline void swap(size_t i, size_t j, size_t n);
568 
570 
571 
576  T& operator[](size_t idx)
577  {
578  ASSERTD(idx < _size);
579  return _array[idx];
580  }
581 
587  const T& operator[](size_t idx) const
588  {
589  ASSERTD(idx < _size);
590  return _array[idx];
591  }
592 
594  T& operator[](int idx)
595  {
596  return operator[]((size_t)idx);
597  }
598 
600  const T& operator[](int idx) const
601  {
602  return operator[]((size_t)idx);
603  }
604 
605 #if UTL_HOST_WORDSIZE == 64
606 
607  T& operator[](uint_t idx)
608  {
609  return operator[]((size_t)idx);
610  }
611 
613  const T& operator[](uint_t idx) const
614  {
615  return operator[]((size_t)idx);
616  }
617 #endif
618 
620  operator const T*() const
621  {
622  return _array;
623  }
624 
626  operator const T*()
627  {
628  return _array;
629  }
630 
632  operator T*()
633  {
634  return _array;
635  }
636 
638  operator const void*() const
639  {
640  return _array;
641  }
642 
644  operator const void*()
645  {
646  return _array;
647  }
648 
650  operator void*()
651  {
652  return _array;
653  }
654 
656  T* operator+(ssize_t n)
657  {
658  ASSERTD(n >= 0);
659  ASSERTD((size_t)n <= _size);
660  return _array + n;
661  }
662 
664  const T* operator+(ssize_t n) const
665  {
666  ASSERTD(n >= 0);
667  ASSERTD((size_t)n <= _size);
668  return _array + n;
669  }
670 
672  T* operator+(size_t n)
673  {
674  ASSERTD(n <= _size);
675  return _array + n;
676  }
677 
679  const T* operator+(size_t n) const
680  {
681  ASSERTD(n <= _size);
682  return _array + n;
683  }
685 protected:
686  T* _array;
687  size_t _size;
688  size_t _allocSize;
689  size_t _increment;
690 
691 private:
692  inline void
693  init()
694  {
695  _array = nullptr;
696  _size = _allocSize = 0;
697  _increment = 1;
698  setOwner(true);
699  }
700 
701  inline void init(T* array, size_t size = 0, bool owner = true, size_t increment = 1);
702 
703  void
704  deInit()
705  {
706  excise();
707  }
708 
709  void _setAllocSize(size_t allocSize);
710 
711  void _autoInit(size_t begin, size_t end);
712 
713 private:
714  enum flg_t
715  {
716  flg_autoInit,
717  flg_owner
718  };
719 };
720 
722 
723 UTL_NS_END;
724 
726 
728 
730 
731 #include <libutl/Stream.h>
732 
734 
735 UTL_NS_BEGIN;
736 
738 
739 template <typename T>
740 Vector<T>::Vector(std::initializer_list<T> list)
741 {
742  init();
743  _setAllocSize(list.size());
744  for (auto obj : list) append(obj);
745 }
746 
748 
749 template <typename T>
750 Vector<T>::Vector(size_t size, size_t increment)
751 {
752  init(nullptr, size, true, increment);
753 }
754 
756 
757 template <typename T>
758 Vector<T>::Vector(T* array, size_t size, bool owner, size_t increment)
759 {
760  init(array, size, owner, increment);
761 }
762 
764 
765 template <typename T>
766 void
768 {
769  ASSERTD(isOwner());
770  if (_allocSize > _size)
771  {
772  auto saveIncrement = _increment;
773  _increment = 1;
774  _setAllocSize(_size);
775  _increment = saveIncrement;
776  }
777 }
778 
780 
781 template <typename T>
782 void
784 {
785  if (isOwner()) delete[] _array;
786  _array = nullptr;
787  _size = 0;
788  _allocSize = 0;
789 }
790 
792 
793 template <typename T>
794 int
795 Vector<T>::compare(const Object& rhs) const
796 {
797  auto& v = utl::cast<const Vector<T>>(rhs);
798  if (&v == this) return 0;
799  return utl::compare(_array, _size, v._array, v._size);
800 }
801 
803 
804 template <typename T>
805 void
807 {
808  auto& v = utl::cast<Vector<T>>(rhs);
809  if (_array != nullptr) excise();
810  FlagsMI::copyFlags(v);
811  _array = v._array;
812  _size = v._size;
813  _allocSize = v._allocSize;
814  _increment = v._increment;
815  v._array = nullptr;
816  v._size = 0;
817  v._allocSize = 0;
818 }
819 
821 
822 template <typename T>
823 void
825 {
826  auto& v = utl::cast<const Vector<T>>(rhs);
827  if (&v == this) return;
828  bool lhsOwner = this->isOwner();
829  bool rhsOwner = v.isOwner();
830  if (lhsOwner && rhsOwner)
831  {
832  clear();
833  }
834  else
835  {
836  excise();
837  }
838  FlagsMI::copyFlags(v);
839  _increment = v._increment;
840  if (rhsOwner)
841  {
842  copy(0, v);
843  }
844  else
845  {
846  _array = v._array;
847  _size = v._size;
848  _allocSize = v._size;
849  }
850 }
851 
853 
854 template <typename T>
855 void
856 Vector<T>::copy(size_t idx, const T* src, size_t srcBegin, size_t srcEnd)
857 {
858  // grow to required size
859  grow(idx + (srcEnd - srcBegin));
860 
861  // perform the copy
862  auto dstPtr = _array + idx;
863  auto srcPtr = src + srcBegin;
864  auto srcLim = src + srcEnd;
865  if (std::is_trivially_copyable<T>())
866  {
867  memcpy(dstPtr, srcPtr, (srcLim - srcPtr) * sizeof(T));
868  }
869  else
870  {
871  while (srcPtr != srcLim) *dstPtr++ = *srcPtr++;
872  }
873 }
874 
876 
877 template <typename T>
878 void
879 Vector<T>::copy(size_t idx, std::initializer_list<T> list)
880 {
881  grow(idx + list.size());
882  T* ptr = _array + idx;
883  for (auto obj : list) *ptr++ = obj;
884 }
885 
887 
888 template <typename T>
889 void
890 Vector<T>::move(size_t idx, T* src, size_t srcBegin, size_t srcEnd)
891 {
892  // grow to required size
893  grow(idx + (srcEnd - srcBegin));
894 
895  // move objects
896  auto dstPtr = _array + idx;
897  auto srcPtr = src + srcBegin;
898  auto srcLim = src + srcEnd;
899  if (std::is_trivially_copyable<T>())
900  {
901  memcpy(dstPtr, srcPtr, (srcLim - srcPtr) * sizeof(T));
902  }
903  else
904  {
905  while (srcPtr != srcLim) *dstPtr++ = std::move(*srcPtr++);
906  }
907 }
908 
910 
911 template <typename T>
912 void
914 {
915  if (io == io_rd)
916  {
917  clear();
918  setOwner(true);
919  // increment
920  utl::serialize(_increment, stream, io, mode);
921  if (_increment == 0) _increment = 1;
922  // size
923  size_t size;
924  utl::serialize(size, stream, io, mode);
925  setSize(size);
926  }
927  else
928  {
929  // increment
930  utl::serialize(_increment, stream, io, mode);
931  // size
932  utl::serialize(_size, stream, io, mode);
933  }
934 
935  for (size_t i = 0; i < _size; i++)
936  {
937  utl::serialize(_array[i], stream, io, mode);
938  }
939 }
940 
942 
943 template <typename T>
944 void
946 {
947  if (isOwner()) return;
948 
949  // set the flag
950  setOwner(true);
951 
952  // remember _array, _size
953  T* oldArray = _array;
954  size_t oldSize = _size;
955 
956  // _array = null
957  _array = nullptr;
958  _size = 0;
959  _allocSize = 0;
960 
961  // grow allocation to the old size, re-set _size
962  _setAllocSize(oldSize);
963  _size = oldSize;
964 
965  // copy from the old array to the new one
966  copy(0, oldArray, 0, oldSize);
967 }
968 
970 
971 template <typename T>
972 void
973 Vector<T>::assertOwner(size_t allocSize)
974 {
975  // we already own the array?
976  if (isOwner())
977  {
978  if (allocSize > _allocSize)
979  {
980  _setAllocSize(allocSize);
981  }
982  }
983  else
984  {
985  // set the flag
986  setOwner(true);
987 
988  // empty sequence?
989  if (allocSize == 0)
990  {
991  // just nuke the array
992  _size = 0;
993  _allocSize = 0;
994  _array = nullptr;
995  }
996  else
997  {
998  // remember _array, _size
999  T* oldArray = _array;
1000  size_t oldSize = _size;
1001 
1002  // _array = null
1003  _array = nullptr;
1004  _size = 0;
1005  _allocSize = 0;
1006 
1007  // grow allocation to the new size
1008  _setAllocSize(allocSize);
1009 
1010  // move as much as we can from the old allocation to the new one
1011  _size = min(oldSize, allocSize);
1012  move(0, oldArray, 0, _size);
1013  }
1014  }
1015 }
1016 
1018 
1019 template <typename T>
1020 void
1022 {
1023  if (isOwner() && (size > _allocSize)) _setAllocSize(size);
1024  if ((size > _size) && autoInit()) _autoInit(_size, size);
1025  _size = size;
1026 }
1027 
1029 
1030 template <typename T>
1031 void
1032 Vector<T>::grow(size_t size)
1033 {
1034  if (size <= _size) return;
1035  if (size > _allocSize) assertOwner(size);
1036  if (autoInit()) _autoInit(_size, size);
1037  _size = size;
1038 }
1039 
1041 
1042 template <typename T>
1043 void
1044 Vector<T>::append(const T* array, size_t arraySize)
1045 {
1046  size_t oldSize = _size;
1047  grow(_size + arraySize);
1048  if (std::is_trivially_copyable<T>())
1049  {
1050  memcpy(_array + oldSize, array, arraySize * sizeof(T));
1051  }
1052  else
1053  {
1054  T* op = _array + oldSize;
1055  const T* ip = array;
1056  T* lim = _array + _size;
1057  for (; op < lim; ++ip, ++op) *op = *ip;
1058  }
1059 }
1060 
1062 
1063 template <typename T>
1064 void
1065 Vector<T>::append(std::initializer_list<T> list)
1066 {
1067  reserve(_size + list.size());
1068  for (auto obj : list) append(obj);
1069 }
1070 
1072 
1073 template <typename T>
1074 void
1075 Vector<T>::insert(size_t idx, size_t num)
1076 {
1077  // the sequence grows in size by num
1078  grow(_size + num);
1079 
1080  // shift elements over
1081  if (std::is_trivially_copyable<T>())
1082  {
1083  memmove(_array + idx + num, _array + idx, (_size - idx - num) * sizeof(T));
1084  }
1085  else
1086  {
1087  T* op = _array + _size - 1;
1088  T* opLim = _array + idx + num;
1089  T* ip = op - num;
1090  for (; op >= opLim; --ip, --op) *op = std::move(*ip);
1091  }
1092  if (autoInit()) _autoInit(idx, idx + num);
1093 }
1094 
1096 
1097 template <typename T>
1098 void
1099 Vector<T>::remove(size_t idx, size_t num)
1100 {
1101  ASSERTD(_size >= (idx + num));
1102 
1103  // shift elements over
1104  if (std::is_trivially_copyable<T>())
1105  {
1106  memmove(_array + idx, _array + idx + num, (_size - idx - num) * sizeof(T));
1107  }
1108  else
1109  {
1110  T* op = _array + idx;
1111  T* opLim = _array + _size - num;
1112  T* ip = op + num;
1113  for (; op != opLim; ++ip, ++op) *op = std::move(*ip);
1114  }
1115 
1116  _size -= num;
1117 }
1118 
1120 
1121 template <typename T>
1122 void
1123 Vector<T>::relocate(size_t destIdx, size_t srcIdx)
1124 {
1125  ASSERTD(destIdx <= _size);
1126  ASSERTD(srcIdx < _size);
1127  if ((destIdx == srcIdx) || (destIdx == (srcIdx + 1))) return;
1128  T tmp = _array[srcIdx];
1129  if (std::is_trivially_copyable<T>())
1130  {
1131  if (srcIdx < destIdx)
1132  {
1133  // objects in (srcIdx,destIdx) move to the left
1134  memmove(_array + srcIdx, _array + srcIdx + 1, (destIdx - srcIdx - 1) * sizeof(T));
1135  _array[destIdx - 1] = tmp;
1136  }
1137  else
1138  {
1139  // objects in [destIdx,srcIdx) move to the right
1140  memmove(_array + destIdx + 1, _array + destIdx, (srcIdx - destIdx) * sizeof(T));
1141  _array[destIdx] = tmp;
1142  }
1143  }
1144  else
1145  {
1146  T* destPtr;
1147  T* srcPtr;
1148  T* srcLim;
1149  if (srcIdx < destIdx)
1150  {
1151  // objects in (srcIdx,destIdx) move to the left
1152  destPtr = _array + srcIdx;
1153  srcPtr = _array + srcIdx + 1;
1154  srcLim = srcPtr + (destIdx - srcIdx - 1);
1155  for (; srcPtr != srcLim; ++srcPtr, ++destPtr) *destPtr = std::move(*srcPtr);
1156  _array[destIdx - 1] = tmp;
1157  }
1158  else
1159  {
1160  // objects in [destIdx,srcIdx) move to the right
1161  destPtr = _array + destIdx + 1;
1162  srcPtr = _array + destIdx;
1163  srcLim = srcPtr + (srcIdx - destIdx);
1164  for (; srcPtr != srcLim; ++srcPtr, ++destPtr) *destPtr = std::move(*srcPtr);
1165  _array[destIdx] = tmp;
1166  }
1167  }
1168 }
1169 
1171 
1172 template <typename T>
1173 void
1174 Vector<T>::reverse(size_t idx, size_t num)
1175 {
1176  ASSERTD((idx + num) <= _size);
1177  T* lhs = _array + idx;
1178  T* rhs = _array + idx + num - 1;
1179  for (; lhs < rhs; ++lhs, --rhs)
1180  {
1181  std::swap(*lhs, *rhs);
1182  }
1183 }
1184 
1186 
1187 template <typename T>
1188 void
1189 Vector<T>::set(T* array, size_t size, bool owner, size_t increment)
1190 {
1191  if (_array != nullptr) clear();
1192  setOwner(owner);
1193  _array = array;
1194  _size = size;
1195  _allocSize = size;
1196  _increment = increment;
1197 }
1198 
1200 
1201 template <typename T>
1202 void
1203 Vector<T>::set(size_t idx, size_t num, const T& val)
1204 {
1205  T* lim = _array + idx + num;
1206  for (T* ptr = _array + idx; ptr != lim; ++ptr)
1207  {
1208  *ptr = val;
1209  }
1210 }
1211 
1213 
1214 template <typename T>
1215 void
1216 Vector<T>::swap(size_t i, size_t j)
1217 {
1218  ASSERTD(i < _size);
1219  ASSERTD(j < _size);
1220  std::swap(_array[i], _array[j]);
1221 }
1222 
1224 
1225 template <typename T>
1226 void
1227 Vector<T>::swap(size_t i, size_t j, size_t n)
1228 {
1229  ASSERTD((i + n) <= _size);
1230  ASSERTD((j + n) <= _size);
1231 
1232  T* lhs = _array + i;
1233  T* rhs = _array + j;
1234  T* lhsLim = lhs + n;
1235  for (; lhs != lhsLim; ++lhs, ++rhs)
1236  {
1237  std::swap(*lhs, *rhs);
1238  }
1239 }
1240 
1242 
1243 template <typename T>
1244 void
1245 Vector<T>::init(T* array, size_t size, bool owner, size_t increment)
1246 {
1247  setOwner(owner);
1248  if ((array == nullptr) && (size > 0))
1249  {
1250  ASSERTD(owner);
1251  _array = nullptr;
1252  _size = 0;
1253  _allocSize = 0;
1254  grow(size, increment);
1255  }
1256  else
1257  {
1258  _array = array;
1259  _size = size;
1260  _allocSize = _size;
1261  setIncrement(increment);
1262  }
1263 }
1264 
1266 
1267 template <typename T>
1268 void
1269 Vector<T>::_setAllocSize(size_t reqAllocSize)
1270 {
1271  ASSERTD(isOwner());
1272 
1273  // figure out the new allocation size
1274  size_t allocSize;
1275  if (_increment == size_t_max)
1276  {
1277  // find nearest power of 2
1278  allocSize = nextPow2(reqAllocSize);
1279  }
1280  else
1281  {
1282  // find nearest multiple of _increment
1283  allocSize = nextMultipleOfPow2(_increment, reqAllocSize);
1284  }
1285  ASSERTD(allocSize >= reqAllocSize);
1286 
1287  // ensure _size <= requested allocSize
1288  _size = min(_size, reqAllocSize);
1289 
1290  // create new array
1291  if (std::is_trivially_copyable<T>())
1292  {
1293  _array
1294  = static_cast<T*>(utl::realloc(_array, _allocSize * sizeof(T), allocSize * sizeof(T)));
1295  }
1296  else
1297  {
1298  // remember the old allocation, make a new one
1299  T* oldArray = _array;
1300  _array = new T[allocSize];
1301 
1302  // move objects to the new array
1303  T* lhs = _array;
1304  T* rhs = oldArray;
1305  T* lhsLim = lhs + _size;
1306  for (; lhs != lhsLim; ++lhs, ++rhs) *lhs = std::move(*rhs);
1307 
1308  // nuke the old allocation
1309  delete[] oldArray;
1310  }
1311 
1312  // re-set allocSize
1313  _allocSize = allocSize;
1314 }
1315 
1317 
1318 template <typename T>
1319 void
1320 Vector<T>::_autoInit(size_t begin, size_t end)
1321 {
1322  auto ptr = _array + begin;
1323  auto lim = _array + end;
1324  for (; ptr != lim; ++ptr) *ptr = T();
1325 }
1326 
1328 
1329 UTL_NS_END;
#define UTL_CLASS_IMPL_TPL(className, T)
Implementation of standard UTL++ functionality for a template class.
Definition: macros.h:906
void insert(size_t idx, std::initializer_list< T > list)
Insert another sequence into this one.
Definition: Vector.h:477
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
void move(size_t idx, const Vector< T > &src, size_t srcBegin=0, size_t srcEnd=size_t_max)
Move (part of) another vector.
Definition: Vector.h:291
size_t size() const
Get the size of the sequence.
Definition: Vector.h:155
const_iterator end() const
Get end iterator (const version).
Definition: Vector.h:179
void setAutoInit(bool autoInit)
Set the autoInit flag.
Definition: Vector.h:404
void deInit()
De-initialize UTL++.
void insert(size_t idx, size_t num, const T *val)
Insert objects into the sequence.
Definition: Vector.h:452
void reserve(size_t allocSize)
Reserve allocation space.
Definition: Vector.h:386
const T & operator[](int idx) const
Same as above, but takes an int parameter.
Definition: Vector.h:600
default representation (via getSerializeMode())
Definition: util.h:75
size_t length() const
Get the size of the sequence.
Definition: Vector.h:148
void remove(FwdIt &begin, const FwdIt &end, bool cmp=false, const Predicate *pred=nullptr, bool predVal=false)
Remove objects from a sequence.
const_iterator cend()
Get end iterator (const version).
Definition: Vector.h:193
void append(const T &val)
Append an object to the sequence.
Definition: Vector.h:411
void reverse(const BidIt &begin, const BidIt &end)
Reverse a sequence.
const T * operator+(size_t n) const
Pointer addition (size_t argument).
Definition: Vector.h:679
void assertOwner(size_t allocSize, size_t increment)
Ensure that self owns the allocation.
Definition: Vector.h:330
const size_t size_t_max
Maximum size_t value.
void swap(T *array, size_t lhsIdx, size_t rhsIdx)
Swap two elements of the given array.
Definition: util_inl.h:930
virtual size_t innerAllocatedSize() const
Get the "inner" allocated size.
Definition: Vector.h:81
bool isOwner() const
Get the owner flag.
Definition: Vector.h:89
void append(const Vector< T > &v)
Append another sequence to this one.
Definition: Vector.h:418
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
T & operator[](int idx)
Same as above, but takes an int parameter.
Definition: Vector.h:594
bool empty() const
Is the sequence empty?
Definition: Vector.h:141
read
Definition: util.h:58
uint32_t nextMultipleOfPow2(uint32_t x, uint32_t target)
Return the smallest number n s.t.
Definition: util_inl.h:158
void insert(size_t idx, size_t num, const T &val)
Insert objects into the sequence.
Definition: Vector.h:439
void copy(size_t idx, const Vector< T > &src, size_t srcBegin=0, size_t srcEnd=size_t_max)
Copy (part of) another vector.
Definition: Vector.h:260
void setIncrement(size_t increment)
Set the growth increment.
Definition: Vector.h:553
const_iterator cbegin()
Get begin iterator (const version).
Definition: Vector.h:186
void reserve(size_t allocSize, size_t increment)
Reserve allocation space.
Definition: Vector.h:397
bool autoInit() const
Get the autoInit flag.
Definition: Vector.h:96
iterator end()
Get end iterator.
Definition: Vector.h:207
void insert(size_t idx, const T *array, size_t arraySize)
Insert another sequence into this one.
Definition: Vector.h:465
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
void grow(size_t size, size_t increment)
Ensure the sequence is no smaller than the given size.
Definition: Vector.h:375
T & operator[](size_t idx)
Array access operator.
Definition: Vector.h:576
Stream I/O abstraction.
Definition: Stream.h:68
T * operator+(size_t n)
Pointer addition (size_t argument).
Definition: Vector.h:672
const T & min(const T &a, const T &b, const R &... args)
Return the smallest value among two or more provided values of the same type.
Definition: util_inl.h:61
#define UTL_CLASS_DECL_TPL(DC, T, BC)
Declaration of standard UTL++ functionality for a template class with one parameter.
Definition: macros.h:713
A sequence of same-type objects.
Definition: Ordering.h:14
void * realloc(void *ptr, size_t size, size_t newSize)
Re-allocate the given block.
size_t increment() const
Get the growth increment.
Definition: Vector.h:162
void setOwner(bool owner)
Set the ownership flag.
Definition: Vector.h:310
const T * operator+(ssize_t n) const
Pointer addition (ssize_t argument).
Definition: Vector.h:664
size_t indexOf(const_iterator it) const
Convert iterator to index.
Definition: Vector.h:217
const T & operator[](size_t idx) const
Array access operator.
Definition: Vector.h:587
const_iterator begin() const
Get begin iterator (const version).
Definition: Vector.h:172
size_t indexOf(iterator it)
Convert iterator to index.
Definition: Vector.h:225
Root of UTL++ class hierarchy.
Definition: Object.h:52
T * operator+(ssize_t n)
Pointer addition (ssize_t argument).
Definition: Vector.h:656
int compare(bool lhs, bool rhs)
Compare two boolean values.
void clear()
Empty the sequence.
Definition: Vector.h:236
iterator begin()
Get begin iterator.
Definition: Vector.h:200
uint32_t nextPow2(uint32_t n)
Return the smallest power of 2 which is >= n.
Definition: util_inl.h:102
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.