4#define TINY_ARRAY_BUILTIN_SIZE 4
7template <
typename TYPE >
16 TYPE m_typea[TINY_ARRAY_BUILTIN_SIZE];
20 tiny_array() : m_iCount(0), m_iAllocation(0), m_p(
nullptr) { }
21 ~tiny_array() { free(); }
24 void allocate(::i32 iCount);
27 void set_at_grow(::i32 iIndex,
const TYPE& t);
28 void erase_at(::i32 iIndex);
29 void push(
const TYPE & t) { set_at_grow(count(), t);}
30 TYPE pop() { TYPE t = element_at(count() - 1); erase_at(count() -1);
return t; }
31 ::i32 count()
const {
return m_iCount;}
33 TYPE& element_at_grow(::i32 iIndex);
34 TYPE& element_at(::i32 iIndex) {
return m_p[iIndex];}
46template <
typename TYPE >
47void tiny_array < TYPE > ::allocate(::i32 iCount)
53 throw_exception(error_bad_argument);
59 if (iCount > TINY_ARRAY_BUILTIN_SIZE || m_iAllocation > 0)
62 if (m_iAllocation <= 0)
65 memsize sizeAllocated = 0;
67 auto p = (TYPE *) ::acme::get()->m_pheapmanagement->memory(::heap::e_memory_main)->allocate(
sizeof(TYPE) * iCount, &sizeAllocated);
69 auto iAllocation = (::i32) (sizeAllocated /
sizeof(TYPE));
71 ::zero(p,
sizeof(TYPE) * iAllocation);
73 m_iAllocation = iAllocation;
79 else if (iCount > m_iAllocation)
82 auto p = (TYPE *) ::acme::get()->m_pheapmanagement->memory(::heap::e_memory_main)->reallocate(m_p,
sizeof(TYPE) * iCount);
84 ::zero(p + m_iCount,
sizeof(TYPE) * (iCount - m_iAllocation));
86 m_iAllocation = iCount;
105template <
typename TYPE >
106void tiny_array < TYPE > ::free()
109 if (m_iAllocation > 0)
112 ::acme::get()->m_pheapmanagement->memory(::heap::e_memory_main)->free(m_p);
125template <
typename TYPE >
126void tiny_array < TYPE > ::zero()
129 ::zero(m_p,
sizeof(TYPE) * m_iCount);
134template <
typename TYPE >
135void tiny_array < TYPE > ::set_at_grow(::i32 iIndex,
const TYPE& t)
138 element_at_grow(iIndex) = t;
143template <
typename TYPE >
144void tiny_array < TYPE > ::erase_at(::i32 iIndex)
147 if(iIndex < 0 || iIndex >= m_iCount)
150 throw_exception(error_index_out_of_bounds);
154 auto iNext = iIndex + 1;
156 auto countMove = m_iCount - iNext;
161 ::memory_transfer(m_p + iIndex, m_p + iNext, countMove *
sizeof(TYPE));
170template <
typename TYPE >
171TYPE & tiny_array < TYPE > ::element_at_grow(::i32 iIndex)
177 throw_exception(error_bad_argument);
181 if (iIndex >= m_iCount)
184 allocate(iIndex + 1);