20#ifndef HYBRID_ARRAY_LIST_H_
21#define HYBRID_ARRAY_LIST_H_
25#define HYBRID_ARRAY_LIST_DEFAULT_STATIC_CAPACITY 8
26#define HYBRID_ARRAY_LIST_DYNAMIC_GROWTH_FACTOR 1.5
36template<
typename T,
int STATIC_CAPACITY = HYBRID_ARRAY_LIST_DEFAULT_STATIC_CAPACITY>
50 delete[] _dynamicArray;
58 void add(
const T& item) {
63 if (_size < STATIC_CAPACITY) {
64 _staticArray[_size] = item;
68 _dynamicArray[_size - STATIC_CAPACITY] = item;
79 void insert(
int index,
const T& item) {
81 if (index < 0 || (
size_t)index > _size) {
89 if (index < STATIC_CAPACITY) {
92 if (_size < STATIC_CAPACITY) {
94 for (
int i = _size; i > index; --i) {
95 _staticArray[i] = _staticArray[i - 1];
98 _staticArray[index] = item;
103 T temp = _staticArray[STATIC_CAPACITY - 1];
104 for (
int i = STATIC_CAPACITY - 1; i > index; --i) {
105 _staticArray[i] = _staticArray[i - 1];
108 _staticArray[index] = item;
110 insert(STATIC_CAPACITY, temp);
116 for (
int i = _size - STATIC_CAPACITY; i > index - STATIC_CAPACITY; --i) {
117 _dynamicArray[i] = _dynamicArray[i - 1];
120 _dynamicArray[index - STATIC_CAPACITY] = item;
134 for (
size_t i = 0; i < STATIC_CAPACITY && i < _size; ++i) {
135 if (_staticArray[i] == item) {
140 for (
size_t i = 0; i < _size - STATIC_CAPACITY; ++i) {
141 if (_dynamicArray[i] == item) {
142 return (
int)(i + STATIC_CAPACITY);
174 if (index < 0 || (
size_t)index >= _size) {
178 if (index < STATIC_CAPACITY) {
180 for (
size_t i = index; i < STATIC_CAPACITY - 1; i++) {
181 _staticArray[i] = _staticArray[i + 1];
184 if (_size > STATIC_CAPACITY) {
185 _staticArray[STATIC_CAPACITY - 1] = _dynamicArray[0];
186 for (
size_t i = 0; i < _size - STATIC_CAPACITY - 1; i++) {
187 _dynamicArray[i] = _dynamicArray[i + 1];
194 for (
size_t i = index - STATIC_CAPACITY; i < _size - STATIC_CAPACITY - 1; i++) {
195 _dynamicArray[i] = _dynamicArray[i + 1];
204 T& operator[](
int index) {
205 index = constrain(index, 0, (
int)_size - 1);
206 return index < STATIC_CAPACITY ? _staticArray[index] : _dynamicArray[index - STATIC_CAPACITY];
210 const T& operator[](
int index)
const {
211 return this->operator[](index);
221 return this->operator[](index);
232 size_t size()
const {
237 size_t capacity()
const {
238 return STATIC_CAPACITY + _dynamicCapacity;
242 T _staticArray[STATIC_CAPACITY];
245 size_t _dynamicCapacity;
250 void _ensureCapacity() {
252 if (_size == STATIC_CAPACITY) {
254 if (!_dynamicArray) {
255 _dynamicCapacity = STATIC_CAPACITY * (HYBRID_ARRAY_LIST_DYNAMIC_GROWTH_FACTOR - 1);
256 _dynamicArray =
new T[_dynamicCapacity];
260 else if (_size >= STATIC_CAPACITY + _dynamicCapacity) {
262 size_t newDynamicCapacity = capacity() * HYBRID_ARRAY_LIST_DYNAMIC_GROWTH_FACTOR - STATIC_CAPACITY;
263 T* newDynamicArray =
new T[newDynamicCapacity];
265 memcpy(newDynamicArray, _dynamicArray, _dynamicCapacity *
sizeof(T));
267 delete[] _dynamicArray;
268 _dynamicArray = newDynamicArray;
269 _dynamicCapacity = newDynamicCapacity;
A hybrid array list that starts with a static array and switches to dynamic allocation when full.
Definition HybridArrayList.h:37
void removeAll()
Removes all items from the list without changing its capacity.
Definition HybridArrayList.h:227
~HybridArrayList()
Destroys the Hybrid Array List object, deallocating any dynamic memory used.
Definition HybridArrayList.h:48
HybridArrayList()
Constructs a new Hybrid Array List object.
Definition HybridArrayList.h:43
void remove(int index)
Removes the element at the specified position in the list.
Definition HybridArrayList.h:172
void insert(int index, const T &item)
Inserts an element at the specified position in the list.
Definition HybridArrayList.h:79
void add(const T &item)
Adds an element to the end of the list.
Definition HybridArrayList.h:58
bool removeItem(const T &item)
Removes the first occurrence of the specified element from this list, if it is present.
Definition HybridArrayList.h:154
int indexOf(const T &item)
Finds the first occurrence of the specified element in this list.
Definition HybridArrayList.h:132
T get(int index) const
Retrieves the element at the specified position in the list.
Definition HybridArrayList.h:220