summaryrefslogtreecommitdiff
path: root/tools/transpiler/List.h
blob: 00a466aa09b18958ac615095d6092085552cb909 (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
#pragma once
#include <cstdlib>
#include <cstring>
#include "Logger.h"

#define FOREACH(list)												\
	for (size_t idx = 0; idx < list.numElements; idx++) \
		if (auto value = list.getValue(idx)) \
		

template <typename T>
struct List {
	T* data = nullptr;
	size_t capacity = 0;
	size_t numElements = 0;
	bool growDynamically = true;

	void allocate(size_t size);
	void add(T* element);
	void add(T& element);
	void add(T&& element);
	bool grow(size_t newSize);
	void set(T* value, size_t index);
	void remove(size_t index);
	void clear();
	void deallocate();
	bool isEmpty() {
		return data == nullptr || numElements == 0;
	}
	T* getValue(int index) const;
	T& operator[](int idx) const;
	void binarySort(int (*f)(T *first, T* second));
	void setFromArray(T* arry, int size) {
	    allocate(size);
		memcpy(data, arry, size * sizeof(T));
		numElements = size;
	}
	void remove(int index) {
		if (index >= numElements) {
			logger_error("Cannot remove element at index: %d", index);
			return;
		}

		
		if (index == numElements - 1) {
			numElements--;
			return;
		}
		
		memmove(data[index], data[index + 1], sizeof(T) * (numElements - index));
		numElements--;
	}
};

template <typename T>
void List<T>::allocate(size_t size) {
	if (size == 0 || size == capacity) {
		numElements = 0;
		return;
	}

	if (data != nullptr) {
		deallocate();
	}

	data = static_cast<T*>(malloc(sizeof(T) * size));
	capacity = size;
	numElements = 0;
}

template <typename T>
bool List<T>::grow(size_t newSize) {
	if (!growDynamically) {
		return false;
	}

	if (newSize == 0) {
		return false;
	}

	T* newData = static_cast<T*>(malloc(sizeof(T) * newSize));

	if (data != nullptr) {
		memcpy(newData, data, numElements * sizeof(T));
		delete data;
	}

	data = newData;
	capacity = newSize;
	return true;
}

template <typename T>
void List<T>::set(T* value, size_t index) {
	if (index >= capacity && !grow(index * 2)) {
		return;
	}

	memcpy(&data[index], value, sizeof(T));
}

template <typename T>
void List<T>::add(T* element) {
	if (data == nullptr) {
		allocate(2);
	}

	if (element == nullptr) {
		logger_error("Element not defined");
		return;
	}

	size_t newNumElements = numElements + 1;
	if (newNumElements > capacity) {
		if (!grow(2 * capacity)) {
			logger_error("Trying to add to list but unable to grow the array");
			return;
		}
	}

	memcpy(&data[numElements], element, sizeof(T));
	numElements = newNumElements;
}

template <typename T>
void List<T>::add(T& element) {
	if (data == nullptr) {
		allocate(2);
	}

	size_t newNumElements = numElements + 1;
	if (newNumElements > capacity) {
		if (!grow(2 * capacity)) {
			logger_error("Trying to add to list but unable to grow the array");
			return;
		}
	}

	memcpy(&data[numElements], &element, sizeof(T));
	numElements = newNumElements;
}

template <typename T>
void List<T>::add(T&& element) {
	if (data == nullptr) {
		allocate(2);
	}

	size_t newNumElements = numElements + 1;
	if (newNumElements > capacity) {
		if (!grow(2 * capacity)) {
			logger_error("Trying to add to list but unable to grow the array");
			return;
		}
	}

	memcpy(&data[numElements], &element, sizeof(T));
	numElements = newNumElements;
}

template <typename T>
void List<T>::remove(size_t idx) {
	if (idx >= numElements) {
		logger_error("Index is outside of the list: %d >= %d", idx, numElements);
		return;
	}

	for (; idx < numElements - 1; idx++) {
		data[idx] = data[idx + 1];
	}

	numElements--;
}

template <typename T>
void List<T>::deallocate() {
	if (data != nullptr) {
		free(data);
		data = nullptr;
	}

	capacity = 0;
	numElements = 0;
}

template <typename T>
void List<T>::clear() {
	numElements = 0;
}

template <typename T>
T* List<T>::getValue(int idx) const {
	return &data[idx];
}

template <typename T>
T& List<T>::operator[](int idx) const {
	return data[idx];
}

template <typename T>
void List<T>::binarySort(int (*f)(T *first, T* second)) {
	if (data == nullptr) {
		return;
	}

	for (size_t idx = 0; idx < numElements - 1; idx++) {
		int minIdx = idx;
		T firstValue = data[idx];

		for (int innerIdx = idx + 1; innerIdx < numElements; innerIdx++) {\
			T secondValue = data[innerIdx];
			if (f(&firstValue, &secondValue) > 0) {
				minIdx= innerIdx;
			}
		}

		T temp = data[minIdx];
		memmove(&data[minIdx], &data[idx], sizeof(T));
		memmove(&data[idx], &temp, sizeof(T));
	}
}