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
12 Normal dynamic array class
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
29 typedef plain_range
<T
> range
;
32 Function: array constructor
40 Function: array copy constructor
42 array(const array
&other
)
45 set_size(other
.size());
46 for(int i
= 0; i
< size(); i
++)
47 (*this)[i
] = other
[i
];
52 Function: array destructor
56 ALLOCATOR::free_array(list
);
69 for(int i
= 0; i
< size(); i
++)
83 ALLOCATOR::free_array(list
);
85 list
= ALLOCATOR::alloc_array(list_size
);
98 Function: remove_index_fast
103 void remove_index_fast(int index
)
105 list
[index
] = list
[num_elements
-1];
110 Function: remove_fast
115 void remove_fast(const T
& item
)
117 for(int i
= 0; i
< size(); i
++)
120 remove_index_fast(i
);
126 Function: remove_index
131 void remove_index(int index
)
133 for(int i
= index
+1; i
< num_elements
; i
++)
145 bool remove(const T
& item
)
147 for(int i
= 0; i
< size(); i
++)
158 Adds an item to the array.
165 - See remarks about <array> how the array grows.
167 int add(const T
& item
)
171 list
[num_elements
-1] = item
;
172 return num_elements
-1;
177 Inserts an item into the array at a specified location.
180 item - Item to insert.
181 r - Range where to insert the item
185 - See remarks about <array> how the array grows.
187 int insert(const T
& item
, range r
)
192 int index
= (int)(&r
.front()-list
);
196 for(int i
= num_elements
-1; i
> index
; i
--)
201 return num_elements
-1;
207 T
& operator[] (int index
)
213 Function: const operator[]
215 const T
& operator[] (int index
) const
231 const T
*base_ptr() const
238 Resizes the array to the specified size.
241 new_size - The new size for the array.
243 void set_size(int new_size
)
245 if(list_size
< new_size
)
247 num_elements
= new_size
;
252 Allocates the number of elements wanted but
253 does not increase the list size.
256 hint - Size to allocate.
259 - If the hint is smaller then the number of elements, nothing will be done.
262 void hint_size(int hint
)
264 if(num_elements
< hint
)
271 Removes unnessasary data, returns how many bytes was earned.
278 int before
= memusage();
280 return before
- memusage();
285 Returns how much memory this dynamic array is using
289 return sizeof(array
) + sizeof(T
)*list_size
;
293 Function: operator=(array)
298 array
&operator = (const array
&other
)
300 set_size(other
.size());
301 for(int i
= 0; i
< size(); i
++)
302 (*this)[i
] = other
[i
];
308 Returns a range that contains the whole array.
310 range
all() { return range(list
, list
+num_elements
); }
315 if(num_elements
== list_size
)
320 alloc(list_size
+list_size
/2);
324 void alloc(int 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
;
344 #endif // TL_FILE_ARRAY_HPP