added "fixed some header guards and added a script to fix them all!" by Choupom
[twcon.git] / src / base / tl / array.h
blob0cee2afc7d38b053e0424f3250f16c7cc652e4e5
1 /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2 /* If you are missing that file, acquire a complete release at teeworlds.com. */
3 #ifndef BASE_TL_ARRAY_H
4 #define BASE_TL_ARRAY_H
6 #include "range.h"
7 #include "allocator.h"
11 Class: array
12 Normal dynamic array class
14 Remarks:
15 - Grows 50% each time it needs to fit new items
16 - Use set_size() if you know how many elements
17 - Use optimize() to reduce the needed space.
19 template <class T, class ALLOCATOR = allocator_default<T> >
20 class array : private ALLOCATOR
22 void init()
24 list = 0x0;
25 clear();
28 public:
29 typedef plain_range<T> range;
32 Function: array constructor
34 array()
36 init();
40 Function: array copy constructor
42 array(const array &other)
44 init();
45 set_size(other.size());
46 for(int i = 0; i < size(); i++)
47 (*this)[i] = other[i];
52 Function: array destructor
54 ~array()
56 ALLOCATOR::free_array(list);
57 list = 0x0;
62 Function: delete_all
64 Remarks:
65 - Invalidates ranges
67 void delete_all()
69 for(int i = 0; i < size(); i++)
70 delete list[i];
71 clear();
76 Function: clear
78 Remarks:
79 - Invalidates ranges
81 void clear()
83 ALLOCATOR::free_array(list);
84 list_size = 1;
85 list = ALLOCATOR::alloc_array(list_size);
86 num_elements = 0;
90 Function: size
92 int size() const
94 return num_elements;
98 Function: remove_index_fast
100 Remarks:
101 - Invalidates ranges
103 void remove_index_fast(int index)
105 list[index] = list[num_elements-1];
106 set_size(size()-1);
110 Function: remove_fast
112 Remarks:
113 - Invalidates ranges
115 void remove_fast(const T& item)
117 for(int i = 0; i < size(); i++)
118 if(list[i] == item)
120 remove_index_fast(i);
121 return;
126 Function: remove_index
128 Remarks:
129 - Invalidates ranges
131 void remove_index(int index)
133 for(int i = index+1; i < num_elements; i++)
134 list[i-1] = list[i];
136 set_size(size()-1);
140 Function: remove
142 Remarks:
143 - Invalidates ranges
145 bool remove(const T& item)
147 for(int i = 0; i < size(); i++)
148 if(list[i] == item)
150 remove_index(i);
151 return true;
153 return false;
157 Function: add
158 Adds an item to the array.
160 Arguments:
161 item - Item to add.
163 Remarks:
164 - Invalidates ranges
165 - See remarks about <array> how the array grows.
167 int add(const T& item)
169 incsize();
170 set_size(size()+1);
171 list[num_elements-1] = item;
172 return num_elements-1;
176 Function: insert
177 Inserts an item into the array at a specified location.
179 Arguments:
180 item - Item to insert.
181 r - Range where to insert the item
183 Remarks:
184 - Invalidates ranges
185 - See remarks about <array> how the array grows.
187 int insert(const T& item, range r)
189 if(r.empty())
190 return add(item);
192 int index = (int)(&r.front()-list);
193 incsize();
194 set_size(size()+1);
196 for(int i = num_elements-1; i > index; i--)
197 list[i] = list[i-1];
199 list[index] = item;
201 return num_elements-1;
205 Function: operator[]
207 T& operator[] (int index)
209 return list[index];
213 Function: const operator[]
215 const T& operator[] (int index) const
217 return list[index];
221 Function: base_ptr
223 T *base_ptr()
225 return list;
229 Function: base_ptr
231 const T *base_ptr() const
233 return list;
237 Function: set_size
238 Resizes the array to the specified size.
240 Arguments:
241 new_size - The new size for the array.
243 void set_size(int new_size)
245 if(list_size < new_size)
246 alloc(new_size);
247 num_elements = new_size;
251 Function: hint_size
252 Allocates the number of elements wanted but
253 does not increase the list size.
255 Arguments:
256 hint - Size to allocate.
258 Remarks:
259 - If the hint is smaller then the number of elements, nothing will be done.
260 - Invalidates ranges
262 void hint_size(int hint)
264 if(num_elements < hint)
265 alloc(hint);
270 Function: optimize
271 Removes unnessasary data, returns how many bytes was earned.
273 Remarks:
274 - Invalidates ranges
276 int optimize()
278 int before = memusage();
279 alloc(num_elements);
280 return before - memusage();
284 Function: memusage
285 Returns how much memory this dynamic array is using
287 int memusage()
289 return sizeof(array) + sizeof(T)*list_size;
293 Function: operator=(array)
295 Remarks:
296 - Invalidates ranges
298 array &operator = (const array &other)
300 set_size(other.size());
301 for(int i = 0; i < size(); i++)
302 (*this)[i] = other[i];
303 return *this;
307 Function: all
308 Returns a range that contains the whole array.
310 range all() { return range(list, list+num_elements); }
311 protected:
313 void incsize()
315 if(num_elements == list_size)
317 if(list_size < 2)
318 alloc(list_size+1);
319 else
320 alloc(list_size+list_size/2);
324 void alloc(int new_len)
326 list_size = new_len;
327 T *new_list = ALLOCATOR::alloc_array(list_size);
329 int end = num_elements < list_size ? num_elements : list_size;
330 for(int i = 0; i < end; i++)
331 new_list[i] = list[i];
333 ALLOCATOR::free_array(list);
335 num_elements = num_elements < list_size ? num_elements : list_size;
336 list = new_list;
339 T *list;
340 int list_size;
341 int num_elements;
344 #endif // TL_FILE_ARRAY_HPP