5 #include <libutl/FlagsMI.h> 43 class Vector :
public Object,
protected FlagsMI
51 typedef const T* const_iterator;
58 Vector(std::initializer_list<T> list);
65 Vector(
size_t size,
size_t increment = 1);
74 Vector(T* array,
size_t size,
bool owner =
true,
size_t increment = 1);
78 virtual int compare(
const Object& rhs)
const;
83 if (!isOwner())
return 0;
84 return (_allocSize *
sizeof(T));
91 return getFlag(flg_owner);
98 return getFlag(flg_autoInit);
117 get(
size_t idx)
const 127 return get((size_t)idx);
130 #if UTL_HOST_WORDSIZE == 64 135 return get((size_t)idx);
181 return _array + _size;
195 return _array + _size;
209 return _array + _size;
219 ASSERTD((it >= this->begin()) && (it <= this->end()));
220 return (it - this->begin());
227 ASSERTD((it >= this->begin()) && (it <= this->end()));
228 return (it - this->begin());
247 virtual void steal(
Object& rhs);
262 copy(idx, src._array, srcBegin,
min(srcEnd, src._size));
273 void copy(
size_t idx,
const T* src,
size_t srcBegin,
size_t srcEnd);
280 void copy(
size_t idx, std::initializer_list<T> list);
293 move(idx, src._array, srcBegin,
min(srcEnd, src._size));
304 void move(
size_t idx, T* src,
size_t srcBegin,
size_t srcEnd);
312 setFlag(flg_owner, owner);
322 void assertOwner(
size_t allocSize);
332 setIncrement(increment);
333 assertOwner(allocSize);
348 return get((size_t)idx);
351 #if UTL_HOST_WORDSIZE == 64 356 return get((size_t)idx);
361 void setSize(
size_t size);
367 void grow(
size_t size);
375 grow(
size_t size,
size_t increment)
377 setIncrement(increment);
388 if (allocSize > _allocSize) assertOwner(allocSize);
399 if (allocSize > _allocSize) assertOwner(allocSize, increment);
406 setFlag(flg_autoInit, autoInit);
420 append(v._array, v._size);
424 void append(
const T* array,
size_t arraySize);
427 void append(std::initializer_list<T> list);
430 void insert(
size_t idx,
size_t num);
439 insert(
size_t idx,
size_t num,
const T& val)
452 insert(
size_t idx,
size_t num,
const T* val)
465 insert(
size_t idx,
const T* array,
size_t arraySize)
467 insert(idx, arraySize);
468 copy(idx, array, 0, arraySize);
477 insert(
size_t idx, std::initializer_list<T> list)
479 insert(idx, list.size());
488 void remove(
size_t idx,
size_t num);
495 void relocate(
size_t destIdx,
size_t srcIdx);
502 void reverse(
size_t idx,
size_t num);
510 set(T* array,
size_t size)
512 set(array, size, isOwner(), _increment);
522 void set(T* array,
size_t size,
bool owner,
size_t increment);
537 void set(
size_t idx,
size_t num,
const T& val);
545 set(
size_t idx,
const T& val)
556 _increment = increment;
560 inline void swap(
size_t i,
size_t j);
566 inline void swap(
size_t i,
size_t j,
size_t n);
596 return operator[]((
size_t)idx);
602 return operator[]((
size_t)idx);
605 #if UTL_HOST_WORDSIZE == 64 609 return operator[]((
size_t)idx);
613 const T& operator[](
uint_t idx)
const 615 return operator[]((
size_t)idx);
620 operator const T*()
const 638 operator const void*()
const 644 operator const void*()
696 _size = _allocSize = 0;
701 inline void init(T* array,
size_t size = 0,
bool owner =
true,
size_t increment = 1);
709 void _setAllocSize(
size_t allocSize);
711 void _autoInit(
size_t begin,
size_t end);
731 #include <libutl/Stream.h> 739 template <
typename T>
743 _setAllocSize(list.size());
744 for (
auto obj : list) append(obj);
749 template <
typename T>
752 init(
nullptr, size,
true, increment);
757 template <
typename T>
760 init(array, size, owner, increment);
765 template <
typename T>
770 if (_allocSize > _size)
772 auto saveIncrement = _increment;
774 _setAllocSize(_size);
775 _increment = saveIncrement;
781 template <
typename T>
785 if (isOwner())
delete[] _array;
793 template <
typename T>
797 auto& v = utl::cast<const Vector<T>>(rhs);
798 if (&v ==
this)
return 0;
804 template <
typename T>
808 auto& v = utl::cast<Vector<T>>(rhs);
809 if (_array !=
nullptr) excise();
810 FlagsMI::copyFlags(v);
813 _allocSize = v._allocSize;
814 _increment = v._increment;
822 template <
typename T>
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)
838 FlagsMI::copyFlags(v);
839 _increment = v._increment;
848 _allocSize = v._size;
854 template <
typename T>
859 grow(idx + (srcEnd - srcBegin));
862 auto dstPtr = _array + idx;
863 auto srcPtr = src + srcBegin;
864 auto srcLim = src + srcEnd;
865 if (std::is_trivially_copyable<T>())
867 memcpy(dstPtr, srcPtr, (srcLim - srcPtr) *
sizeof(T));
871 while (srcPtr != srcLim) *dstPtr++ = *srcPtr++;
877 template <
typename T>
881 grow(idx + list.size());
882 T* ptr = _array + idx;
883 for (
auto obj : list) *ptr++ = obj;
888 template <
typename T>
893 grow(idx + (srcEnd - srcBegin));
896 auto dstPtr = _array + idx;
897 auto srcPtr = src + srcBegin;
898 auto srcLim = src + srcEnd;
899 if (std::is_trivially_copyable<T>())
901 memcpy(dstPtr, srcPtr, (srcLim - srcPtr) *
sizeof(T));
905 while (srcPtr != srcLim) *dstPtr++ = std::move(*srcPtr++);
911 template <
typename T>
921 if (_increment == 0) _increment = 1;
935 for (
size_t i = 0; i < _size; i++)
943 template <
typename T>
947 if (isOwner())
return;
953 T* oldArray = _array;
954 size_t oldSize = _size;
962 _setAllocSize(oldSize);
966 copy(0, oldArray, 0, oldSize);
971 template <
typename T>
978 if (allocSize > _allocSize)
980 _setAllocSize(allocSize);
999 T* oldArray = _array;
1000 size_t oldSize = _size;
1008 _setAllocSize(allocSize);
1011 _size =
min(oldSize, allocSize);
1012 move(0, oldArray, 0, _size);
1019 template <
typename T>
1023 if (isOwner() && (size > _allocSize)) _setAllocSize(size);
1024 if ((size > _size) && autoInit()) _autoInit(_size, size);
1030 template <
typename T>
1034 if (size <= _size)
return;
1035 if (size > _allocSize) assertOwner(size);
1036 if (autoInit()) _autoInit(_size, size);
1042 template <
typename T>
1046 size_t oldSize = _size;
1047 grow(_size + arraySize);
1048 if (std::is_trivially_copyable<T>())
1050 memcpy(_array + oldSize, array, arraySize *
sizeof(T));
1054 T* op = _array + oldSize;
1055 const T* ip = array;
1056 T* lim = _array + _size;
1057 for (; op < lim; ++ip, ++op) *op = *ip;
1063 template <
typename T>
1067 reserve(_size + list.size());
1068 for (
auto obj : list) append(obj);
1073 template <
typename T>
1081 if (std::is_trivially_copyable<T>())
1083 memmove(_array + idx + num, _array + idx, (_size - idx - num) *
sizeof(T));
1087 T* op = _array + _size - 1;
1088 T* opLim = _array + idx + num;
1090 for (; op >= opLim; --ip, --op) *op = std::move(*ip);
1092 if (autoInit()) _autoInit(idx, idx + num);
1097 template <
typename T>
1101 ASSERTD(_size >= (idx + num));
1104 if (std::is_trivially_copyable<T>())
1106 memmove(_array + idx, _array + idx + num, (_size - idx - num) *
sizeof(T));
1110 T* op = _array + idx;
1111 T* opLim = _array + _size - num;
1113 for (; op != opLim; ++ip, ++op) *op = std::move(*ip);
1121 template <
typename T>
1127 if ((destIdx == srcIdx) || (destIdx == (srcIdx + 1)))
return;
1128 T tmp = _array[srcIdx];
1129 if (std::is_trivially_copyable<T>())
1131 if (srcIdx < destIdx)
1134 memmove(_array + srcIdx, _array + srcIdx + 1, (destIdx - srcIdx - 1) *
sizeof(T));
1135 _array[destIdx - 1] = tmp;
1140 memmove(_array + destIdx + 1, _array + destIdx, (srcIdx - destIdx) *
sizeof(T));
1141 _array[destIdx] = tmp;
1149 if (srcIdx < destIdx)
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;
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;
1172 template <
typename T>
1176 ASSERTD((idx + num) <= _size);
1177 T* lhs = _array + idx;
1178 T* rhs = _array + idx + num - 1;
1179 for (; lhs < rhs; ++lhs, --rhs)
1187 template <
typename T>
1191 if (_array !=
nullptr) clear();
1196 _increment = increment;
1201 template <
typename T>
1205 T* lim = _array + idx + num;
1206 for (T* ptr = _array + idx; ptr != lim; ++ptr)
1214 template <
typename T>
1225 template <
typename T>
1232 T* lhs = _array + i;
1233 T* rhs = _array + j;
1234 T* lhsLim = lhs + n;
1235 for (; lhs != lhsLim; ++lhs, ++rhs)
1243 template <
typename T>
1248 if ((array ==
nullptr) && (size > 0))
1254 grow(size, increment);
1261 setIncrement(increment);
1267 template <
typename T>
1278 allocSize =
nextPow2(reqAllocSize);
1285 ASSERTD(allocSize >= reqAllocSize);
1288 _size =
min(_size, reqAllocSize);
1291 if (std::is_trivially_copyable<T>())
1294 =
static_cast<T*
>(
utl::realloc(_array, _allocSize *
sizeof(T), allocSize *
sizeof(T)));
1299 T* oldArray = _array;
1300 _array =
new T[allocSize];
1305 T* lhsLim = lhs + _size;
1306 for (; lhs != lhsLim; ++lhs, ++rhs) *lhs = std::move(*rhs);
1313 _allocSize = allocSize;
1318 template <
typename T>
1322 auto ptr = _array + begin;
1323 auto lim = _array + end;
1324 for (; ptr != lim; ++ptr) *ptr = T();
#define UTL_CLASS_IMPL_TPL(className, T)
Implementation of standard UTL++ functionality for a template class.
void insert(size_t idx, std::initializer_list< T > list)
Insert another sequence into this one.
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.
size_t size() const
Get the size of the sequence.
const_iterator end() const
Get end iterator (const version).
void setAutoInit(bool autoInit)
Set the autoInit flag.
void deInit()
De-initialize UTL++.
void insert(size_t idx, size_t num, const T *val)
Insert objects into the sequence.
void reserve(size_t allocSize)
Reserve allocation space.
const T & operator[](int idx) const
Same as above, but takes an int parameter.
default representation (via getSerializeMode())
size_t length() const
Get the size of the sequence.
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).
void append(const T &val)
Append an object to the sequence.
void reverse(const BidIt &begin, const BidIt &end)
Reverse a sequence.
const T * operator+(size_t n) const
Pointer addition (size_t argument).
void assertOwner(size_t allocSize, size_t increment)
Ensure that self owns the allocation.
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.
virtual size_t innerAllocatedSize() const
Get the "inner" allocated size.
bool isOwner() const
Get the owner flag.
void append(const Vector< T > &v)
Append another sequence to this one.
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
T & operator[](int idx)
Same as above, but takes an int parameter.
bool empty() const
Is the sequence empty?
uint32_t nextMultipleOfPow2(uint32_t x, uint32_t target)
Return the smallest number n s.t.
void insert(size_t idx, size_t num, const T &val)
Insert objects into the sequence.
void copy(size_t idx, const Vector< T > &src, size_t srcBegin=0, size_t srcEnd=size_t_max)
Copy (part of) another vector.
void setIncrement(size_t increment)
Set the growth increment.
const_iterator cbegin()
Get begin iterator (const version).
void reserve(size_t allocSize, size_t increment)
Reserve allocation space.
bool autoInit() const
Get the autoInit flag.
iterator end()
Get end iterator.
void insert(size_t idx, const T *array, size_t arraySize)
Insert another sequence into this one.
unsigned int uint_t
Unsigned integer.
void grow(size_t size, size_t increment)
Ensure the sequence is no smaller than the given size.
T & operator[](size_t idx)
Array access operator.
T * operator+(size_t n)
Pointer addition (size_t argument).
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.
#define UTL_CLASS_DECL_TPL(DC, T, BC)
Declaration of standard UTL++ functionality for a template class with one parameter.
A sequence of same-type objects.
void * realloc(void *ptr, size_t size, size_t newSize)
Re-allocate the given block.
size_t increment() const
Get the growth increment.
void setOwner(bool owner)
Set the ownership flag.
const T * operator+(ssize_t n) const
Pointer addition (ssize_t argument).
size_t indexOf(const_iterator it) const
Convert iterator to index.
const T & operator[](size_t idx) const
Array access operator.
const_iterator begin() const
Get begin iterator (const version).
size_t indexOf(iterator it)
Convert iterator to index.
Root of UTL++ class hierarchy.
T * operator+(ssize_t n)
Pointer addition (ssize_t argument).
int compare(bool lhs, bool rhs)
Compare two boolean values.
void clear()
Empty the sequence.
iterator begin()
Get begin iterator.
uint32_t nextPow2(uint32_t n)
Return the smallest power of 2 which is >= n.
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.