34 template <
class Derived,
class ArrayType,
typename T,
bool RET_CPY>
51 : array_ptr(nullptr), curr(0)
57 : array_ptr(&const_cast<ArrayType &>(a)), curr(0)
63 : array_ptr(&const_cast<ArrayType &>(a)), curr(c)
69 : array_ptr(it.array_ptr), curr(it.curr)
99 std::swap(curr, it.
curr);
104 return curr < array_ptr->size();
117 curr = std::min(curr + p, array_ptr->size());
141 curr = std::min(p, array_ptr->size());
155 template <
class ArrayType,
typename T>
160 ArrayType, T,
false>;
166 if (not Base::has_current())
167 throw std::overflow_error(
"There is not current element");
169 return (*Base::array_ptr)[Base::curr];
174 if (not Base::has_current())
175 throw std::overflow_error(
"There is not current element");
177 return (*Base::array_ptr)[Base::curr];
181 template <
typename T>
187 void init(
const T &);
204 : cap(0), array_ptr(nullptr)
210 : cap(c), array_ptr(new T[cap])
247 array_ptr =
new T[cap];
260 std::swap(cap, a.cap);
261 std::swap(array_ptr, a.array_ptr);
273 return get_capacity();
279 throw std::out_of_range(
"Index out of range");
287 throw std::out_of_range(
"Index out of range");
310 return Iterator(*
this);
315 return Iterator(*
this);
320 return Iterator(*
this, cap);
325 return Iterator(*
this, cap);
329 template <
typename T>
332 for (
nat_t i = 0; i < cap; ++i)
336 template <
typename T>
340 memcpy(
array_ptr, a.array_ptr,
sizeof(T) * cap);
342 for (
nat_t i = 0; i < cap; ++i)
346 template <
typename T>
352 T * new_array_ptr =
new T[c];
354 nat_t sz = std::min(c, cap);
356 for (
nat_t i = 0; i < sz; ++i)
364 template <
typename T>
370 for (
const T & item : l)
371 array_ptr[i++] = item;
374 template <
typename T>
388 static constexpr
nat_t MIN_SIZE = 32;
389 static constexpr
real_t RESIZE_FACTOR = 0.4;
397 if (num_items < BaseArray::get_capacity())
400 assert(BaseArray::get_capacity() * (1 + RESIZE_FACTOR) > num_items);
402 BaseArray::resize(BaseArray::get_capacity() * (1 + RESIZE_FACTOR));
407 if (num_items > BaseArray::get_capacity() * RESIZE_FACTOR or
408 BaseArray::get_capacity() == MIN_SIZE)
411 assert(BaseArray::get_capacity() * (1 - RESIZE_FACTOR) > num_items);
413 nat_t new_cap = std::max<real_t>(BaseArray::get_capacity() *
414 (1 - RESIZE_FACTOR), MIN_SIZE);
416 BaseArray::resize(new_cap);
419 void open_breach(
nat_t);
421 void close_breach(
nat_t);
431 :
BaseArray(cap, init_val), num_items(cap)
454 DynArray(
const std::initializer_list<T> &);
459 std::swap(num_items, a.num_items);
464 return BaseArray::get_capacity();
474 return num_items == 0;
481 if (BaseArray::get_capacity() != MIN_SIZE)
484 BaseArray::swap(new_array);
491 throw std::underflow_error(
"Array is empty");
493 return BaseArray::at(0);
499 throw std::underflow_error(
"Array is empty");
501 return BaseArray::at(0);
507 throw std::overflow_error(
"Array is empty");
509 return BaseArray::at(num_items - 1);
515 throw std::overflow_error(
"Array is empty");
517 return BaseArray::at(num_items - 1);
523 throw std::out_of_range(
"Index is out of range");
527 BaseArray::at(pos) = item;
533 return BaseArray::at(pos);
539 throw std::out_of_range(
"Index is out of range");
543 BaseArray::at(pos) = std::move(item);
549 return BaseArray::at(pos);
554 return insert(0, item);
559 return insert(0, std::forward<T>(item));
564 BaseArray::at(num_items++) = item;
566 return BaseArray::at(num_items - 1);
571 BaseArray::at(num_items++) = std::move(item);
573 return BaseArray::at(num_items - 1);
578 if (pos >= num_items)
579 throw std::out_of_range(
"Index is out of range");
581 T ret_val = std::move(BaseArray::at(pos));
582 BaseArray::at(pos) = std::move(BaseArray::at(--num_items));
588 nat_t i = BaseArray::item_to_pos(item);
591 throw std::logic_error(
"Item does not belong to array");
593 return remove_pos(i);
598 if (pos >= num_items)
599 throw std::out_of_range(
"Index is out of range");
601 T ret_val = std::move(BaseArray::at(pos));
614 nat_t i = BaseArray::item_to_pos(item);
617 throw std::logic_error(
"Item does not belong to array");
619 return remove_pos_closing_breach(i);
625 throw std::underflow_error(
"Array is empty");
627 return remove_pos(0);
632 T ret_val = std::move(BaseArray::at(--num_items));
643 num_items = a.num_items;
656 throw std::out_of_range(
"Index is out of range");
658 return BaseArray::at(i);
664 throw std::out_of_range(
"Index is out of range");
666 return BaseArray::at(i);
687 if (not Base::has_current())
688 throw std::logic_error(
"There is not current element");
690 return Base::array_ptr->remove_pos_closing_breach(Base::curr);
715 template <
typename T>
719 for (
const T & item : l)
723 template <
typename T>
726 for (
nat_t i = 0; i < num_items; ++i)
730 template <
typename T>
733 for (
nat_t i = num_items; i > p; --i)
737 template <
typename T>
740 for (
nat_t i = p; i < num_items; ++i)
744 template <
typename T, nat_t N = 2>
747 static_assert(N >= 2,
"N must be greater than 1");
757 void swap(Slice & another)
759 std::swap(sz, another.sz);
760 std::swap(exts, another.exts);
761 std::swap(strs, another.strs);
766 : sz(1), exts(N), strs(N)
771 Slice(
const Slice & slice)
772 : sz(slice.sz), exts(slice.exts), strs(slice.strs)
777 Slice(Slice && slice)
783 Slice(
const std::initializer_list<nat_t> & l)
784 : sz(1), exts(l), strs(l.size())
789 template <
typename... Dims>
791 : sz(1), exts({
nat_t(dims)...}), strs(
sizeof...(Dims))
793 static_assert(
sizeof...(Dims) == N,
"");
813 template <
typename... Dims>
814 nat_t operator () (Dims...);
848 std::swap(slice, another.slice);
849 std::swap(array, another.array);
855 template <
typename... Dims>
857 : slice(dims...), array(slice.
size())
863 : slice(mda.slice), array(mda.array)
876 return slice.extents()[i];
879 template <
typename... Dims>
880 T &
get(Dims... dims)
882 nat_t idx = slice(dims...);
883 if (idx >= slice.size())
884 throw std::overflow_error(
"Invalid position");
888 template <
typename... Dims>
889 const T &
get(Dims... dims)
const 891 nat_t idx = slice(dims...);
892 if (idx >= slice.size())
893 throw std::overflow_error(
"Invalid position");
897 template <
typename... Dims>
903 template <
typename... Dims>
904 const T &
at(Dims... dims)
const 909 template <
typename... Dims>
910 T & operator () (Dims... dims)
915 template <
typename... Dims>
916 const T & operator () (Dims... dims)
const 986 return Iterator(*
this, slice.size());
991 return Iterator(*
this, slice.size());
995 template <
typename T, nat_t N>
998 for (
nat_t i = 0; i < N; ++i)
1003 for (
nat_t i = N - 1; i > 0; --i)
1004 strs[i - 1] = strs[i] * exts[i];
1007 template <
typename T, nat_t N>
1008 template <
typename... Dims>
1011 static_assert(
sizeof...(Dims) == N,
"");
1017 for (
size_t i = 0; i < N; ++i)
1018 ret += arr[i] * strs[i];
1024 # endif // DSGARRAY_H
T & at(Dims...dims)
Definition: array.H:898
const T & get_first() const
Definition: array.H:496
T ItemType
Definition: array.H:836
Iterator end()
Definition: array.H:704
void resize(nat_t)
Definition: array.H:347
T & get_current()
Definition: array.H:164
ArrayIterator()
Definition: array.H:50
T remove_pos_closing_breach(nat_t pos)
Definition: array.H:596
T DataType
Definition: array.H:194
nat_t size(nat_t i) const
Definition: array.H:874
T & append(T &&item)
Definition: array.H:569
T remove_last()
Definition: array.H:630
void reset()
Definition: array.H:144
FixedArray(nat_t c)
Definition: array.H:209
Iterator begin() const
Definition: array.H:699
const T & get_current() const
Definition: array.H:172
MultiDimArray(Dims...dims)
Definition: array.H:856
const T & get_last() const
Definition: array.H:512
T KeyType
Definition: array.H:837
Definition: iterator.H:132
DynArray(const DynArray &a)
Definition: array.H:442
Iterator end() const
Definition: array.H:709
double real_t
Definition: types.H:51
Iterator end()
Definition: array.H:318
ArrayIterator(const ArrayType &a, nat_t c)
Definition: array.H:62
T & insert(const T &item)
Definition: array.H:552
nat_t size() const
Definition: array.H:271
MultiDimArray(const MultiDimArray &mda)
Definition: array.H:862
T & insert(T &&item)
Definition: array.H:557
nat_t get_location() const
Definition: array.H:44
Definition: iterator.H:36
MultiDimArray(MultiDimArray &&mda)
Definition: array.H:868
T & insert(nat_t pos, const T &item)
Definition: array.H:520
T & get_first()
Definition: array.H:488
ArrayType * array_ptr
Definition: array.H:41
const T & at(nat_t i) const
Definition: array.H:661
nat_t size() const
Definition: array.H:467
nat_t get_capacity() const
Definition: array.H:462
Iterator end() const
Definition: array.H:989
T remove_closing_breach(T &item)
Definition: array.H:612
T & get_last()
Definition: array.H:504
T & at(nat_t i)
Definition: array.H:653
ArrayIterator(const ArrayIterator &it)
Definition: array.H:68
ArrayIterator(ArrayIterator &&it)
Definition: array.H:74
void swap(ArrayIterator &it)
Definition: array.H:96
T & append(const T &item)
Definition: array.H:562
ArrayIterator(const ArrayType &a)
Definition: array.H:56
T DataType
Definition: array.H:838
T remove_pos(nat_t pos)
Definition: array.H:576
void clear()
Definition: array.H:477
~FixedArray()
Definition: array.H:235
DynArray(DynArray &&a)
Definition: array.H:448
Iterator(const Iterator &itor)
Definition: array.H:961
void swap(FixedArray &a)
Definition: array.H:258
Iterator(const MultiDimArray &a)
Definition: array.H:949
DynArray & operator=(const DynArray &a)
Definition: array.H:637
void swap(DynArray &a)
Definition: array.H:456
bool has_current() const
Definition: array.H:102
T ItemType
Definition: array.H:192
Iterator(const MultiDimArray &a, nat_t c)
Definition: array.H:955
Iterator begin()
Definition: array.H:694
Iterator begin() const
Definition: array.H:979
FixedArray(nat_t c, const T &init_value)
Definition: array.H:215
T & insert(nat_t pos, T &&item)
Definition: array.H:536
nat_t item_to_pos(T &item)
Definition: array.H:198
Iterator end()
Definition: array.H:984
nat_t curr
Definition: array.H:42
T ValueType
Definition: array.H:839
Definition: containeralgorithms.H:33
bool is_empty() const
Definition: array.H:472
Iterator()
Definition: array.H:943
Iterator begin() const
Definition: array.H:313
DynArray(nat_t cap, const T &init_val)
Definition: array.H:430
RetType< RET_CPY, T, T & > operator[](nat_t i)
Definition: iterator.H:137
nat_t SizeType
Definition: array.H:196
FixedArray(const FixedArray &a)
Definition: array.H:221
Iterator begin()
Definition: array.H:308
ArrayIterator & operator=(const ArrayIterator &it)
Definition: array.H:80
FixedArray()
Definition: array.H:203
T del()
Definition: array.H:685
T & operator[](nat_t i)
Definition: array.H:292
nat_t SizeType
Definition: array.H:840
nat_t get_capacity() const
Definition: array.H:266
FixedArray & operator=(const FixedArray &a)
Definition: array.H:240
T ValueType
Definition: array.H:195
unsigned long int nat_t
Definition: types.H:50
DynArray()
Definition: array.H:436
void prev()
Definition: array.H:120
Iterator begin()
Definition: array.H:974
void next()
Definition: array.H:107
DynArray(nat_t cap)
Definition: array.H:424
Value & value(MapKey< Key, Value > &item)
Definition: map.H:77
nat_t get_position() const
Definition: array.H:149
T remove_first()
Definition: array.H:622
Definition: typetraits.H:34
Iterator(Iterator &&itor)
Definition: array.H:967
void prev_n(nat_t p)
Definition: array.H:128
Iterator end() const
Definition: array.H:323
FixedArray(FixedArray &&a)
Definition: array.H:227
void next_n(nat_t p)
Definition: array.H:115
void move_to(nat_t p)
Definition: array.H:139
T KeyType
Definition: array.H:193
T & at(nat_t i)
Definition: array.H:276
const T & at(nat_t i) const
Definition: array.H:284
const T & at(Dims...dims) const
Definition: array.H:904