20#ifndef HYBRID_ARRAY_LIST_H_
21#define HYBRID_ARRAY_LIST_H_
23#define HYBRID_ARRAY_LIST_DEFAULT_STATIC_CAPACITY 8
24#define HYBRID_ARRAY_LIST_DYNAMIC_GROWTH_FACTOR 1.5
32template<
typename T,
int STATIC_CAPACITY = HYBRID_ARRAY_LIST_DEFAULT_STATIC_CAPACITY>
46 delete[] _dynamicArray;
54 void add(
const T& item) {
59 if (_size < STATIC_CAPACITY) {
60 _staticArray[_size] = item;
64 _dynamicArray[_size - STATIC_CAPACITY] = item;
75 void insert(
int index,
const T& item) {
77 if (index < 0 || (
size_t)index > _size) {
85 if (index < STATIC_CAPACITY) {
88 if (_size < STATIC_CAPACITY) {
90 for (
int i = _size; i > index; --i) {
91 _staticArray[i] = _staticArray[i - 1];
94 _staticArray[index] = item;
99 T temp = _staticArray[STATIC_CAPACITY - 1];
100 for (
int i = STATIC_CAPACITY - 1; i > index; --i) {
101 _staticArray[i] = _staticArray[i - 1];
104 _staticArray[index] = item;
106 insert(STATIC_CAPACITY, temp);
112 for (
int i = _size - STATIC_CAPACITY; i > index - STATIC_CAPACITY; --i) {
113 _dynamicArray[i] = _dynamicArray[i - 1];
116 _dynamicArray[index - STATIC_CAPACITY] = item;
130 for (
size_t i = 0; i < STATIC_CAPACITY && i < _size; ++i) {
131 if (_staticArray[i] == item) {
136 for (
size_t i = 0; i < _size - STATIC_CAPACITY; ++i) {
137 if (_dynamicArray[i] == item) {
138 return (
int)(i + STATIC_CAPACITY);
170 if (index < 0 || (
size_t)index >= _size) {
174 if (index < STATIC_CAPACITY) {
176 for (
size_t i = index; i < STATIC_CAPACITY - 1; i++) {
177 _staticArray[i] = _staticArray[i + 1];
180 if (_size > STATIC_CAPACITY) {
181 _staticArray[STATIC_CAPACITY - 1] = _dynamicArray[0];
182 for (
size_t i = 0; i < _size - STATIC_CAPACITY - 1; i++) {
183 _dynamicArray[i] = _dynamicArray[i + 1];
190 for (
size_t i = index - STATIC_CAPACITY; i < _size - STATIC_CAPACITY - 1; i++) {
191 _dynamicArray[i] = _dynamicArray[i + 1];
200 T& operator[](
int index) {
201 index = constrain(index, 0, (
int)_size - 1);
202 return index < STATIC_CAPACITY ? _staticArray[index] : _dynamicArray[index - STATIC_CAPACITY];
206 const T& operator[](
int index)
const {
207 return this->operator[](index);
217 return this->operator[](index);
228 size_t size()
const {
233 size_t capacity()
const {
234 return STATIC_CAPACITY + _dynamicCapacity;
238 T _staticArray[STATIC_CAPACITY];
241 size_t _dynamicCapacity;
246 void _ensureCapacity() {
248 if (_size == STATIC_CAPACITY) {
250 if (!_dynamicArray) {
251 _dynamicCapacity = STATIC_CAPACITY * (HYBRID_ARRAY_LIST_DYNAMIC_GROWTH_FACTOR - 1);
252 _dynamicArray =
new T[_dynamicCapacity];
256 else if (_size >= STATIC_CAPACITY + _dynamicCapacity) {
258 size_t newDynamicCapacity = capacity() * HYBRID_ARRAY_LIST_DYNAMIC_GROWTH_FACTOR - STATIC_CAPACITY;
259 T* newDynamicArray =
new T[newDynamicCapacity];
261 memcpy(newDynamicArray, _dynamicArray, _dynamicCapacity *
sizeof(T));
263 delete[] _dynamicArray;
264 _dynamicArray = newDynamicArray;
265 _dynamicCapacity = newDynamicCapacity;
A hybrid array list that starts with a static array and switches to dynamic allocation when full.
Definition HybridArrayList.h:33
HybridArrayList()
Constructs a new Hybrid Array List object.
Definition HybridArrayList.h:39
int indexOf(const T &item)
Finds the first occurrence of the specified element in this list.
Definition HybridArrayList.h:128
void insert(int index, const T &item)
Inserts an element at the specified position in the list.
Definition HybridArrayList.h:75
T get(int index) const
Retrieves the element at the specified position in the list.
Definition HybridArrayList.h:216
void add(const T &item)
Adds an element to the end of the list.
Definition HybridArrayList.h:54
bool removeItem(const T &item)
Removes the first occurrence of the specified element from this list, if it is present.
Definition HybridArrayList.h:150
void removeAll()
Removes all items from the list without changing its capacity.
Definition HybridArrayList.h:223
~HybridArrayList()
Destroys the Hybrid Array List object, deallocating any dynamic memory used.
Definition HybridArrayList.h:44
void remove(int index)
Removes the element at the specified position in the list.
Definition HybridArrayList.h:168