8 #ifndef INCL_evo_array_h 9 #define INCL_evo_array_h 23 #define EVO_IMPL_ARRAY_GROW_ADD(SIZE) { \ 25 Size newsize = size_ + SIZE; \ 26 T* const old_data = data_; \ 27 data_ = (T*)::malloc((size_t)newsize*sizeof(T)); \ 28 memcpy(data_, old_data, (size_t)size_*sizeof(T)); \ 31 data_ = (T*)::malloc((size_t)SIZE*sizeof(T)); \ 34 #define EVO_IMPL_ARRAY_GROW_INSERT(INDEX, SIZE) { \ 35 assert( size_ > 0 ); \ 36 Size newsize = size_ + SIZE; \ 37 T* const old_data = data_; \ 38 data_ = (T*)::malloc((size_t)newsize*sizeof(T)); \ 40 memcpy(data_, old_data, (size_t)INDEX*sizeof(T)); \ 41 Size tail = size_ - INDEX; \ 43 memcpy(data_+INDEX+SIZE, old_data+INDEX, (size_t)tail*sizeof(T)); \ 45 memcpy(data_+SIZE, old_data, (size_t)size_*sizeof(T)); \ 184 template<
class T,
class TSize=SizeT>
223 data_ = (T*)::malloc((
size_t)src.
size_*
sizeof(T));
236 data_ = (T*)::malloc((
size_t)size*
sizeof(T));
238 }
else if (data == NULL)
253 #if defined(EVO_CPP11) 259 resize((Size)init.size());
261 for (
const auto&
item : init)
269 ::memcpy(
this, &src,
sizeof(ThisType));
270 ::memset(&src, 0,
sizeof(ThisType));
279 ::memcpy(
this, &src,
sizeof(ThisType));
280 ::memset(&src, 0,
sizeof(ThisType));
357 ThisType&
set(
const ThisType& src) {
360 if (
size_ == src.size_) {
371 data_ = (T*)::malloc((
size_t)src.size_*
sizeof(T));
398 else if (data == NULL)
408 data_ = (T*)::malloc((
size_t)
size*
sizeof(T));
410 }
else if (data == NULL)
440 {
return (
data_ == NULL); }
448 {
return (
size_ == 0); }
479 { assert( index <
size_ );
return data_[index]; }
488 { assert( index <
size_ );
return data_[index]; }
538 else if (
data_ == NULL)
539 result = (data.
data_ == NULL ? 0 : -1);
540 else if (data.
data_ == NULL)
555 else if (
data_ == NULL)
556 result = (data.
data_ == NULL);
572 else if (
data_ == NULL)
573 result = (data.
data_ != NULL);
628 {
return Iter(*
this); }
648 {
return IterM(*
this); }
657 {
return Iter(*
this); }
696 { assert( index <
size_ );
return data_[index]; }
705 { assert( index <
size_ );
return data_[index]; }
752 }
else if (
size_ != size) {
757 T*
const old_data =
data_;
759 data_ = (T*)::malloc((
size_t)size*
sizeof(T));
760 memcpy(
data_, old_data, (
size_t)save_size*
sizeof(T));
769 data_ = (T*)::malloc((
size_t)size*
sizeof(T));
787 Size size2 = (size >= 256 ? 256 : 1);
804 EVO_IMPL_ARRAY_GROW_ADD(
size);
816 EVO_IMPL_ARRAY_GROW_ADD(1);
832 EVO_IMPL_ARRAY_GROW_INSERT(index,
size);
835 EVO_IMPL_ARRAY_GROW_ADD(
size);
850 EVO_IMPL_ARRAY_GROW_INSERT(index, 1);
853 EVO_IMPL_ARRAY_GROW_ADD(1);
867 Size
remove(Key index, Size
size=1) {
868 if (index < size_ && size > 0) {
869 Size tempsize =
size_ - index;
875 T*
const old_data =
data_;
876 data_ = (T*)::malloc((
size_t)tempsize*
sizeof(T));
878 memcpy(
data_, old_data, (
size_t)index*
sizeof(T));
881 memcpy(
data_+index, old_data+index+size, (
size_t)tail*
sizeof(T));
914 const Size newsize = index +
size;
930 { EVO_IMPL_CONTAINER_SWAP(
this, &array, ThisType); }
980 }
else if (
size_ != size) {
985 T*
const old_data =
data_;
987 data_ = (T*)::malloc((
size_t)size*
sizeof(T));
988 memcpy(
data_, old_data, (
size_t)save_size*
sizeof(T));
997 data_ = (T*)::malloc((
size_t)size*
sizeof(T));
1009 void iterInitMutable()
1011 const IterItem* iterFirst(IterKey& key)
const 1012 { key = 0;
return data_; }
1013 const IterItem* iterNext(IterKey& key)
const {
1014 const IterItem* result = NULL;
1017 result = &(
data_[key]);
1023 const IterItem* iterNext(Size count, IterKey& key)
const {
1024 const T* result = NULL;
1026 if ( (key+=count) <
size_ )
1027 result = &(
data_[key]);
1033 const IterItem* iterLast(IterKey& key)
const {
1034 const IterItem* result = NULL;
1037 result = &(
data_[key]);
1042 const IterItem* iterPrev(IterKey& key)
const {
1043 const IterItem* result = NULL;
1046 result = &(
data_[--key]);
1052 const IterItem* iterPrev(Size count, IterKey& key)
const {
1053 const IterItem* result = NULL;
1055 if (key > 0 && count <= key)
1056 result = &(
data_[(key-=count)]);
1062 Size iterCount()
const 1064 const IterItem* iterSet(IterKey key)
const {
1065 const IterItem* result = NULL;
1067 result = &(
data_[key]);
1080 #undef EVO_IMPL_ARRAY_GROW_ADD 1081 #undef EVO_IMPL_ARRAY_GROW_INSERT Iter cend() const
Get iterator at end (const).
Definition: array.h:637
T & operator[](Key index)
Get item at position (mutable).
Definition: array.h:695
Size size_
Definition: array.h:1074
IteratorRa< ThisType > IterM
Iterator (mutable) - IteratorRa.
Definition: array.h:201
static void init_tail_safe(Item *data, ulong oldSize, ulong newSize)
Initialize new tail data (default constructor).
Definition: container.h:252
Array(std::initializer_list< T > init)
Sequence constructor (C++11).
Definition: array.h:257
bool empty() const
Get whether empty.
Definition: array.h:447
bool ends(const T &item) const
Check if ends with given item.
Definition: array.h:606
IterM begin()
Get iterator at first item (mutable).
Definition: array.h:647
Iter begin() const
Get iterator at first item (const).
Definition: array.h:656
ThisType & operator=(ThisType &&src)
Move assignment operator (C++11).
Definition: array.h:277
Size size() const
Get size.
Definition: array.h:453
IterM end()
Get iterator at end.
Definition: array.h:666
ValEmpty
Special empty value type, pass as vEMPTY.
Definition: sys.h:1101
IteratorRa< ThisType >::Const Iter
Iterator (const) - IteratorRa.
Definition: array.h:200
void swap(ThisType &array)
Swap with another array.
Definition: array.h:929
ulong hash(ulong seed=0) const
Get data hash value for whole array.
Definition: array.h:525
ThisType & resize2(Size size)
Resize as power of 2 while preserving existing data (modifier).
Definition: array.h:785
const T * last() const
Get last item (const).
Definition: array.h:508
T Item
Item type (same as Value)
Definition: array.h:192
bool ends(const T *items, Size size) const
Check if ends with given items.
Definition: array.h:616
T * first()
Get first item (mutable).
Definition: array.h:719
Random access iterator.
Definition: iter.h:904
ValNull
Unique null value type and value (vNULL).
Definition: sys.h:1096
T & advRing(Key index)
Advanced: Get ring-buffer item at position (mutable).
Definition: array.h:954
ThisType & operator=(const ValEmpty &)
Assignment operator to set as empty but not null.
Definition: array.h:322
int compare(const ThisType &data) const
Comparison.
Definition: array.h:534
bool operator!=(const ThisType &data) const
Inequality operator.
Definition: array.h:568
ThisType & unshare()
Make data unique – no-op.
Definition: array.h:733
const T & operator[](Key index) const
Get item at position (const).
Definition: array.h:478
bool operator==(const ThisType &data) const
Equality operator.
Definition: array.h:551
static void init_tail_fast(Item *data, ulong oldSize, ulong newSize)
Initialize new tail data (default constructor).
Definition: container.h:270
Basic integer type.
Definition: type.h:980
Array(const ValEmpty &val)
Constructor sets as empty but not null.
Definition: array.h:212
#define EVO_PEMPTY
Special pointer value for empty but not NULL (used in containers).
Definition: type.h:1858
Size Key
Key type (item index)
Definition: array.h:190
const T * data() const
Get data pointer (const).
Definition: array.h:469
Data equality helper.
Definition: container.h:712
bool shared() const
Get whether shared (false).
Definition: array.h:461
Evo implementation detail: Container iterators.
ThisType & resize(Size size)
Resize while preserving existing data (modifier).
Definition: array.h:743
const T & ring(Key index) const
Get ring-buffer item at position (const).
Definition: array.h:496
ThisType & operator=(const ThisType &src)
Assignment operator.
Definition: array.h:300
Key iend(Size offset=0) const
Get index from last item using offset.
Definition: array.h:518
static const EndT END
Special integer value for indicating end of items or no item.
Definition: type.h:1846
ThisType & clear()
Clear by removing all items.
Definition: array.h:330
const T & item(Key index) const
Get item at position (const).
Definition: array.h:487
static const EndT ALL
Special integer value for indicating all items or all remaining items.
Definition: type.h:1839
T & item(Key index)
Get item at position (mutable).
Definition: array.h:704
bool is_pow2(T num)
Get whether a number is a power of 2.
Definition: type.h:1967
Array< T, TSize > ThisType
This array type.
Definition: array.h:188
T * data_
Definition: array.h:1073
Iter end() const
Get iterator at end.
Definition: array.h:676
static ulong hash(const T *data, ulong size, ulong seed=0)
Compute hash value from data.
Definition: container.h:899
Evo C++ Library namespace.
Definition: alg.h:11
static bool equal(const T *data1, const T *data2, ulong size)
Compare array data for equality.
Definition: container.h:721
ThisType & setempty()
Set as empty but not null.
Definition: array.h:422
T Value
Value type (same as Item)
Definition: array.h:191
ThisType & advResize(Size size)
Advanced: Resize while preserving existing data, POD items not initialized (modifier).
Definition: array.h:971
Array()
Default constructor sets as null.
Definition: array.h:204
Iter cbegin() const
Get iterator at first item (const).
Definition: array.h:627
const T * first() const
Get first item (const).
Definition: array.h:502
T * last()
Get last item (mutable).
Definition: array.h:725
static void uninit(Item *data, ulong size)
Uninitialize data (destructor).
Definition: container.h:311
static int compare(const T *data1, ulong size1, const T *data2, ulong size2)
Compare data.
Definition: container.h:769
const ThisType & asconst() const
Explicitly use a const reference to this.
Definition: array.h:290
static void init(Item *data, ulong size=1)
Initialize data using default constructor.
Definition: container.h:198
ThisType & add(const Item &item)
Append new item.
Definition: array.h:815
TSize Size
List size integer type.
Definition: array.h:189
static void fill(T *dest, ulong size, const T &value)
Fill with copies of given item.
Definition: container.h:621
bool starts(const T &item) const
Check if starts with given item.
Definition: array.h:587
Array(const T *data, Size size)
Copy constructor.
Definition: array.h:234
ThisType & operator=(const ValNull &)
Assignment operator to set as null and empty.
Definition: array.h:312
static void init_safe(Item *data, ulong size=1)
Initialize data using default constructor.
Definition: container.h:159
Array(const ThisType &src)
Copy constructor.
Definition: array.h:221
Dynamic array container with similar interface to List.
Definition: array.h:185
Size insertnew(Key index, Size size=1)
Insert new items.
Definition: array.h:829
bool null() const
Get whether null.
Definition: array.h:439
Array(ThisType &&src)
Move constructor (C++11).
Definition: array.h:268
T * data()
Get data pointer (mutable).
Definition: array.h:686
bool starts(const T *items, Size size) const
Check if starts with given items.
Definition: array.h:597
ThisType & addnew(Size size=1)
Append new items.
Definition: array.h:802
ThisType & fill(const T &item, Key index=0, Size size=ALL)
Fill using item (modifier).
Definition: array.h:908
const T & advRing(Key index) const
Advanced: Get ring-buffer item at position (const).
Definition: array.h:942
Size insert(Key index, const Item &item)
Insert new item.
Definition: array.h:848
#define EVO_PARAM_UNUSED(NAME)
Mark function parameter as unused to suppress "unreferenced parameter" compiler warnings on it...
Definition: sys.h:427
~Array()
Destructor to free used memory.
Definition: array.h:246
Evo basic types and traits.
T & ring(Key index)
Get ring-buffer item at position (mutable).
Definition: array.h:713