about summary refs log tree commit diff
path: root/src/game/editor/array.h
blob: fe9f2739aff97e8683a804adfddc5c18bee9c5f8 (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
232
233
234
235
236
237
238

template <class T>
class array
{
	// 
	//
	void init()
	{
		list = 0;
		clear();
	}

public:
	array()
	{
		init();
	}
	
	//
	array(const array &other)
	{
		init();
		setsize(other.len());
		for(int i = 0; i < len(); i++)
			(*this)[i] = other[i];
	}


	// 
	//
	virtual ~array()
	{
		delete [] list;
		list = 0;
	}

	//
	//
	void deleteall()
	{
		for(int i = 0; i < len(); i++)
			delete list[i];

		clear();
	}


	//
	//
	void clear()
	{
		delete [] list;
		
		list_size = 1;
		list = new T[1];
		num_elements = 0;
	}

	int find(T val)
	{
		for(int i = 0; i < len(); i++)
			if((*this)[i] == val)
				return i;
		return -1;
	}

	bool exist(T val)
	{
		return find(val) != -1;
	}

	//
	// returns the number of elements in the list
	//
	int len() const
	{
		return num_elements;
	}

	//
	// This doesn't conserve the order in the list. Be careful
	//
	void removebyindexfast(int index)
	{
		//ASSUME(_Pos >= 0 && _Pos < num_elements);
		list[index] = list[num_elements-1];
		setsize(len()-1);
	}

	void removefast(const T& _Elem)
	{
		for(int i = 0; i < len(); i++)
			if(list[i] == _Elem)
			{
				removebyindexfast(i);
				return;
			}
	}

	//
	//
	void removebyindex(int index)
	{
		//ASSUME(_Pos >= 0 && _Pos < num_elements);

		for(int i = index+1; i < num_elements; i++)
			list[i-1] = list[i];
		
		setsize(len()-1);
	}

	void insert(int index, const T& element)
	{
		int some_len = len();
		if (index < some_len)
			setsize(some_len+1);
		else
			setsize(index + 1);

		for(int i = num_elements-2; i >= index; i--)
			list[i+1] = list[i];

		list[index] = element;
	}

	bool remove(const T& element)
	{
		for(int i = 0; i < len(); i++)
			if(list[i] == element)
			{
				removebyindex(i);
				return true;
			}
		return false;
	}

	// 
	//
	int add(const T& element)
	{
		//if(num_elements == list_size)
		setsize(len()+1);
		list[num_elements-1] = element;
		return num_elements-1;
	}

	// 
	//
	int add(const T& elem, int index)
	{
		setsize(len()+1);
		
		for(int i = num_elements-1; i > index; i--)
			list[i] = list[i-1];

		list[index] = elem;
		
		//num_elements++;
		return num_elements-1;
	}

	// 
	//
	T& operator[] (int index)
	{
		return list[index];
	}

	const T& operator[] (int index) const
	{
		return list[index];
	}

	//
	//
	T *getptr()
	{
		return list;
	}

	const T *getptr() const
	{
		return list;
	}

	//
	//
	//
	void setsize(int new_len)
	{
		if (list_size < new_len)
			allocsize(new_len);
		num_elements = new_len;
	}

	// removes unnessasary data, returns how many bytes was earned
	int optimize()
	{
		int Before = memoryusage();
		setsize(num_elements);
		return Before - memoryusage();
	}

	// returns how much memory this dynamic array is using
	int memoryusage()
	{
		return sizeof(array) + sizeof(T)*list_size;
	}

	//
	array &operator = (const array &other)
	{
		setsize(other.len());
		for(int i = 0; i < len(); i++)
			(*this)[i] = other[i];
		return *this;
	}		
private:
	void allocsize(int new_len)
	{
		list_size = new_len;
		T *new_list = new T[list_size];
		
		long end = num_elements < list_size ? num_elements : list_size;
		for(int i = 0; i < end; i++)
			new_list[i] = list[i];
		
		delete [] list;
		list = 0;
		num_elements = num_elements < list_size ? num_elements : list_size;
		list = new_list;
	}

	T *list;
	long list_size;
	long num_elements;
};