summaryrefslogtreecommitdiff
path: root/src/dynamic-array.hpp
blob: c08f0385db58f10746b61b92535de1fd37d10f77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef PKE_DYNAMIC_ARRAY_HPP
#define PKE_DYNAMIC_ARRAY_HPP

#include "pk.h"

#include <cstdint>
#include <cstring>
#include <cassert>
#include <type_traits>
#include <new>

#define BAKE_DYN_ARRAY(T) template struct DynArray<T>;

struct DynArrayBase {
	mutable struct pk_membucket *bkt = nullptr;
	mutable char *ptr = nullptr;
	int64_t elementSize = 0;
	int64_t elementCount = 0;
	int64_t reservedCount = 0;
};

template <typename T>
struct DynArray: DynArrayBase {
	explicit DynArray(struct pk_membucket *bucket = nullptr);
	explicit DynArray(int64_t reserveCount, struct pk_membucket *bucket = nullptr);
	DynArray(const DynArray<T> &other);
	DynArray(DynArray<T> &&other);
	DynArray &operator=(const DynArray<T> &other);
	DynArray &operator=(DynArray<T> &&other);
	~DynArray();
	T &operator[](int64_t index);
	T *GetPtr();
	int64_t Count() const;
	bool Has(const T &val);
	template <typename T2>
	int64_t FindFirstIndex(bool fn(const T&, const T2&), const T2 &val);
	T& Push();
	void Push(const T &val);
	T Pop();
	void Remove(int64_t index, int64_t count = 1);
	void Reserve(int64_t count);
	void Resize(int64_t count);
	protected:
		using DynArrayBase::bkt;
		using DynArrayBase::ptr;
		using DynArrayBase::elementSize;
		using DynArrayBase::elementCount;
		using DynArrayBase::reservedCount;
};

void DynArrayReserve(DynArrayBase *arr, int64_t count);
void DynArrayDestroy(DynArrayBase *arr);

template <typename T> inline DynArray<T>::DynArray(int64_t count, struct pk_membucket *bucket) {
	this->bkt = bucket;
	this->elementSize = sizeof(T);
	if (count > 0) DynArrayReserve(this, count);
	if IS_CONSTRUCTIBLE(T) {
		for (long i = 0; i < count; ++i) {
			new (this->ptr + (i * sizeof(T))) T{};
		}
	}
}

template <typename T> inline DynArray<T>::DynArray(struct pk_membucket *bucket) {
	this->bkt = bucket;
	this->elementSize = sizeof(T);
}

template <typename T> inline DynArray<T>::DynArray(const DynArray<T> &rh) {
	this->bkt = rh.bkt;
	this->ptr = rh.ptr;
	this->elementSize = rh.elementSize;
	this->elementCount = rh.elementCount;
	this->reservedCount = rh.reservedCount;
	rh.ptr = nullptr;
}

template <typename T> inline DynArray<T>::DynArray(DynArray<T> &&rh) {
	this->bkt = rh.bkt;
	this->ptr = rh.ptr;
	this->elementSize = rh.elementSize;
	this->elementCount = rh.elementCount;
	this->reservedCount = rh.reservedCount;
	rh.ptr = nullptr;
}

template <typename T> inline DynArray<T> &DynArray<T>::operator=(const DynArray<T> &rh) {
	this->bkt = rh.bkt;
	this->ptr = rh.ptr;
	this->elementSize = rh.elementSize;
	this->elementCount = rh.elementCount;
	this->reservedCount = rh.reservedCount;
	rh.ptr = nullptr;
	return *this;
}

template <typename T> inline DynArray<T> &DynArray<T>::operator=(DynArray<T> &&rh) {
	this->bkt = rh.bkt;
	this->ptr = rh.ptr;
	this->elementSize = rh.elementSize;
	this->elementCount = rh.elementCount;
	this->reservedCount = rh.reservedCount;
	rh.ptr = nullptr;
	return *this;
}

template <typename T> inline DynArray<T>::~DynArray() {
	if (this->ptr == nullptr || this->ptr == CAFE_BABE(char)) return;
	if IS_DESTRUCTIBLE(T) {
		for (long i = 0; i < this->elementCount; ++i) {
			reinterpret_cast<T *>(this->ptr + (i * sizeof(T)))->~T();
		}
	}
	DynArrayDestroy(this);
}

template <typename T> inline T &DynArray<T>::operator[](int64_t index) {
	assert(index < this->elementCount && "Invalid DynArray<T>[] index - out of bounds");
	assert(index < this->reservedCount && "Invalid DynArray<T>[] index - out of reserved bounds");
	assert(index >= 0 && "Invalid DynArray<T>[] index - unlikely value");
	return *reinterpret_cast<T *>((this->ptr + (sizeof(T) * index)));
}

template <typename T> inline T *DynArray<T>::GetPtr() {
	return reinterpret_cast<T *>(reinterpret_cast<void *>(this->ptr));
}

template <typename T> inline int64_t DynArray<T>::Count() const {
	return this->elementCount;
}

template <typename T> inline bool DynArray<T>::Has(const T &val) {
	for (long i = 0; i < this->elementCount; ++i) {
		if ((*this)[i] == val) return true;
	}
	return false;
}

template <typename T>
template <typename T2>
inline int64_t DynArray<T>::FindFirstIndex(bool Fn(const T&, const T2&), const T2 &val) {
	for (long i = 0; i < this->elementCount; ++i) {
		if (Fn((*this)[i], val)) return i;
	}
	return -1;
}

template <typename T> inline T &DynArray<T>::Push() {
	if (this->elementCount + 1 > this->reservedCount) {
		auto safeReserveCount = this->reservedCount < 2 ? 2 : this->reservedCount;
		DynArrayReserve(this, int64_t(safeReserveCount * 1.5));
	}
	auto itemPtr = this->ptr + (sizeof(T) * this->elementCount);
	if IS_CONSTRUCTIBLE(T) {
		new (itemPtr) T{};
	}
	this->elementCount += 1;
	return *reinterpret_cast<T *>(itemPtr);
}

template <typename T> inline void DynArray<T>::Push(const T &val) {
	if (this->elementCount + 1 > this->reservedCount) {
		auto safeReserveCount = this->reservedCount < 2 ? 2 : this->reservedCount;
		DynArrayReserve(this, int64_t(safeReserveCount * 1.5));
	}
	auto itemPtr = this->ptr + (sizeof(T) * this->elementCount);
	auto &targetItem = *reinterpret_cast<T *>(itemPtr);
	if constexpr (std::is_assignable<T&, const T&>::value) {
		targetItem = val;
	} else {
		memcpy(targetItem, val, sizeof(T));
	}
	this->elementCount += 1;
}

template <typename T> inline T DynArray<T>::Pop() {
	assert(this->elementCount > 0 && "Invalid DynArray<T>::Pop() - Contains no elements");
	this->elementCount -= 1;
	return *reinterpret_cast<T *>((this->ptr + (sizeof(T) * this->elementCount)));
}

template <typename T> inline void DynArray<T>::Remove(int64_t index, int64_t count) {
	assert(index <= this->elementCount && "Invalid DynArray<T>::Remove() - Out of bounds");
	int64_t moveCount = (this->elementCount - index - count);
	assert(moveCount >= 0 && "Invalid DynArray<T>::Remove() - Removing too many elements");
	assert(moveCount <= this->elementCount - index && "Invalid DynArray<T>::Remove() - Removing too many elements");
	if IS_DESTRUCTIBLE(T) {
		for (long i = 0; i < count; ++i) {
			reinterpret_cast<T *>(this->ptr + ((index + i) * sizeof(T)))->~T();
		}
	}
	if (moveCount == 0) {
		this->elementCount = index;
		return;
	}
	T *tmp = pk_new_arr<T>(moveCount, this->bkt);
	memcpy(tmp, this->ptr + (sizeof(T) * (index + count)), sizeof(T) * moveCount);
	memcpy(this->ptr + (sizeof(T) * index), tmp, sizeof(T) * moveCount);
	pk_delete_arr<T>(tmp, moveCount, this->bkt);
	this->elementCount -= count;
}

template <typename T> inline void DynArray<T>::Reserve(int64_t count) {
	if (count > 0) {
		return DynArrayReserve(this, count);
	}
}

template <typename T> inline void DynArray<T>::Resize(int64_t count) {
	int64_t beforeCount = this->elementCount;
	if IS_DESTRUCTIBLE(T) {
		if (beforeCount > count) {
			for (int64_t i = beforeCount - 1; i >= count; --i) {
				T *ptr = reinterpret_cast<T *>(this->ptr + (i * sizeof(T)));
				ptr->~T();
			}
		}
	}
	if (count > 0) {
		DynArrayReserve(this, count);
		if IS_CONSTRUCTIBLE(T) {
			for (long i = beforeCount; i < count; ++i) {
				new (this->ptr + (i * sizeof(T))) T{};
			}
		}
	}
	this->elementCount = count;
}

#endif /* PKE_DYNAMIC_ARRAY_HPP */