Skylicht Engine
Loading...
Searching...
No Matches
irrArray.h
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine" and the "irrXML" project.
3// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
4
5#ifndef __IRR_ARRAY_H_INCLUDED__
6#define __IRR_ARRAY_H_INCLUDED__
7
8#include "irrTypes.h"
9#include "heapsort.h"
10#include "irrAllocator.h"
11#include "irrMath.h"
12
13namespace irr
14{
15namespace core
16{
17
19
21template <class T, typename TAlloc = irrAllocator<T> >
22class array
23{
24
25public:
26
28 array() : data(0), allocated(0), used(0),
29 strategy(ALLOC_STRATEGY_DOUBLE), free_when_destroyed(true), is_sorted(true)
30 {
31 }
32
33
35
36 array(u32 start_count) : data(0), allocated(0), used(0),
37 strategy(ALLOC_STRATEGY_DOUBLE),
38 free_when_destroyed(true), is_sorted(true)
39 {
40 reallocate(start_count);
41 }
42
43
45 array(const array<T, TAlloc>& other) : data(0)
46 {
47 *this = other;
48 }
49
50
52
55 {
56 clear();
57 }
58
59
61
66 void reallocate(u32 new_size, bool canShrink=true)
67 {
68 if (allocated==new_size)
69 return;
70 if (!canShrink && (new_size < allocated))
71 return;
72
73 T* old_data = data;
74
75 data = allocator.allocate(new_size); //new T[new_size];
76 allocated = new_size;
77
78 // copy old data
79 s32 end = used < new_size ? used : new_size;
80
81 for (s32 i=0; i<end; ++i)
82 {
83 // data[i] = old_data[i];
84 allocator.construct(&data[i], old_data[i]);
85 }
86
87 // destruct old data
88 for (u32 j=0; j<used; ++j)
89 allocator.destruct(&old_data[j]);
90
91 if (allocated < used)
92 used = allocated;
93
94 allocator.deallocate(old_data); //delete [] old_data;
95 }
96
97
99
102 void setAllocStrategy ( eAllocStrategy newStrategy = ALLOC_STRATEGY_DOUBLE )
103 {
104 strategy = newStrategy;
105 }
106
107
109
111 void push_back(const T& element)
112 {
113 insert(element, used);
114 }
115
116
118
122 void push_front(const T& element)
123 {
124 insert(element);
125 }
126
127
129
134 void insert(const T& element, u32 index=0)
135 {
136 _IRR_DEBUG_BREAK_IF(index>used) // access violation
137
138 if (used + 1 > allocated)
139 {
140 // this doesn't work if the element is in the same
141 // array. So we'll copy the element first to be sure
142 // we'll get no data corruption
143 const T e(element);
144
145 // increase data block
146 u32 newAlloc;
147 switch ( strategy )
148 {
149 case ALLOC_STRATEGY_DOUBLE:
150 newAlloc = used + 1 + (allocated < 500 ?
151 (allocated < 5 ? 5 : used) : used >> 2);
152 break;
153 default:
154 case ALLOC_STRATEGY_SAFE:
155 newAlloc = used + 1;
156 break;
157 }
158 reallocate( newAlloc);
159
160 // move array content and construct new element
161 // first move end one up
162 for (u32 i=used; i>index; --i)
163 {
164 if (i<used)
165 allocator.destruct(&data[i]);
166 allocator.construct(&data[i], data[i-1]); // data[i] = data[i-1];
167 }
168 // then add new element
169 if (used > index)
170 allocator.destruct(&data[index]);
171 allocator.construct(&data[index], e); // data[index] = e;
172 }
173 else
174 {
175 // element inserted not at end
176 if ( used > index )
177 {
178 // create one new element at the end
179 allocator.construct(&data[used], data[used-1]);
180
181 // move the rest of the array content
182 for (u32 i=used-1; i>index; --i)
183 {
184 data[i] = data[i-1];
185 }
186 // insert the new element
187 data[index] = element;
188 }
189 else
190 {
191 // insert the new element to the end
192 allocator.construct(&data[index], element);
193 }
194 }
195 // set to false as we don't know if we have the comparison operators
196 is_sorted = false;
197 ++used;
198 }
199
200
202 void clear()
203 {
204 if (free_when_destroyed)
205 {
206 for (u32 i=0; i<used; ++i)
207 allocator.destruct(&data[i]);
208
209 allocator.deallocate(data); // delete [] data;
210 }
211 data = 0;
212 used = 0;
213 allocated = 0;
214 is_sorted = true;
215 }
216
217
219
227 void set_pointer(T* newPointer, u32 size, bool _is_sorted=false, bool _free_when_destroyed=true)
228 {
229 clear();
230 data = newPointer;
231 allocated = size;
232 used = size;
233 is_sorted = _is_sorted;
234 free_when_destroyed=_free_when_destroyed;
235 }
236
237
239
247 {
248 free_when_destroyed = f;
249 }
250
251
253
256 void set_used(u32 usedNow)
257 {
258 if (allocated < usedNow)
259 reallocate(usedNow);
260
261 used = usedNow;
262 }
263
264
267 {
268 if (this == &other)
269 return *this;
270 strategy = other.strategy;
271
272 if (data)
273 clear();
274
275 //if (allocated < other.allocated)
276 if (other.allocated == 0)
277 data = 0;
278 else
279 data = allocator.allocate(other.allocated); // new T[other.allocated];
280
281 used = other.used;
282 free_when_destroyed = true;
283 is_sorted = other.is_sorted;
284 allocated = other.allocated;
285
286 for (u32 i=0; i<other.used; ++i)
287 allocator.construct(&data[i], other.data[i]); // data[i] = other.data[i];
288
289 return *this;
290 }
291
292
294 bool operator == (const array<T, TAlloc>& other) const
295 {
296 if (used != other.used)
297 return false;
298
299 for (u32 i=0; i<other.used; ++i)
300 if (data[i] != other[i])
301 return false;
302 return true;
303 }
304
305
307 bool operator != (const array<T, TAlloc>& other) const
308 {
309 return !(*this==other);
310 }
311
312
314 T& operator [](u32 index)
315 {
316 _IRR_DEBUG_BREAK_IF(index>=used) // access violation
317
318 return data[index];
319 }
320
321
323 const T& operator [](u32 index) const
324 {
325 _IRR_DEBUG_BREAK_IF(index>=used) // access violation
326
327 return data[index];
328 }
329
330
333 {
334 _IRR_DEBUG_BREAK_IF(!used) // access violation
335
336 return data[used-1];
337 }
338
339
341 const T& getLast() const
342 {
343 _IRR_DEBUG_BREAK_IF(!used) // access violation
344
345 return data[used-1];
346 }
347
348
350
352 {
353 return data;
354 }
355
356
358
359 const T* const_pointer() const
360 {
361 return data;
362 }
363
364
366
367 u32 size() const
368 {
369 return used;
370 }
371
372
374
377 {
378 return allocated;
379 }
380
381
383
384 bool empty() const
385 {
386 return used == 0;
387 }
388
389
391
393 void sort()
394 {
395 if (!is_sorted && used>1)
396 heapsort(data, used);
397 is_sorted = true;
398 }
399
400
402
408 s32 binary_search(const T& element)
409 {
410 sort();
411 return binary_search(element, 0, used-1);
412 }
413
414
416
421 s32 binary_search(const T& element) const
422 {
423 if (is_sorted)
424 return binary_search(element, 0, used-1);
425 else
426 return linear_search(element);
427 }
428
429
431
436 s32 binary_search(const T& element, s32 left, s32 right) const
437 {
438 if (!used)
439 return -1;
440
441 s32 m;
442
443 do
444 {
445 m = (left+right)>>1;
446
447 if (element < data[m])
448 right = m - 1;
449 else
450 left = m + 1;
451
452 } while((element < data[m] || data[m] < element) && left<=right);
453 // this last line equals to:
454 // " while((element != array[m]) && left<=right);"
455 // but we only want to use the '<' operator.
456 // the same in next line, it is "(element == array[m])"
457
458
459 if (!(element < data[m]) && !(data[m] < element))
460 return m;
461
462 return -1;
463 }
464
465
468
474 s32 binary_search_multi(const T& element, s32 &last)
475 {
476 sort();
477 s32 index = binary_search(element, 0, used-1);
478 if ( index < 0 )
479 return index;
480
481 // The search can be somewhere in the middle of the set
482 // look linear previous and past the index
483 last = index;
484
485 while ( index > 0 && !(element < data[index - 1]) && !(data[index - 1] < element) )
486 {
487 index -= 1;
488 }
489 // look linear up
490 while ( last < (s32) used - 1 && !(element < data[last + 1]) && !(data[last + 1] < element) )
491 {
492 last += 1;
493 }
494
495 return index;
496 }
497
498
500
505 s32 linear_search(const T& element) const
506 {
507 for (u32 i=0; i<used; ++i)
508 if (element == data[i])
509 return (s32)i;
510
511 return -1;
512 }
513
514
516
521 s32 linear_reverse_search(const T& element) const
522 {
523 for (s32 i=used-1; i>=0; --i)
524 if (data[i] == element)
525 return i;
526
527 return -1;
528 }
529
530
532
535 void erase(u32 index)
536 {
537 _IRR_DEBUG_BREAK_IF(index>=used) // access violation
538
539 for (u32 i=index+1; i<used; ++i)
540 {
541 allocator.destruct(&data[i-1]);
542 allocator.construct(&data[i-1], data[i]); // data[i-1] = data[i];
543 }
544
545 allocator.destruct(&data[used-1]);
546
547 --used;
548 }
549
550
552
556 void erase(u32 index, s32 count)
557 {
558 if (index>=used || count<1)
559 return;
560 if (index+count>used)
561 count = used-index;
562
563 u32 i;
564 for (i=index; i<index+count; ++i)
565 allocator.destruct(&data[i]);
566
567 for (i=index+count; i<used; ++i)
568 {
569 if (i-count >= index+count) // not already destructed before loop
570 allocator.destruct(&data[i-count]);
571
572 allocator.construct(&data[i-count], data[i]); // data[i-count] = data[i];
573
574 if (i >= used-count) // those which are not overwritten
575 allocator.destruct(&data[i]);
576 }
577
578 used-= count;
579 }
580
581
583 void set_sorted(bool _is_sorted)
584 {
585 is_sorted = _is_sorted;
586 }
587
588
590
594 {
595 core::swap(data, other.data);
596 core::swap(allocated, other.allocated);
597 core::swap(used, other.used);
598 core::swap(allocator, other.allocator); // memory is still released by the same allocator used for allocation
599 eAllocStrategy helper_strategy(strategy); // can't use core::swap with bitfields
600 strategy = other.strategy;
601 other.strategy = helper_strategy;
602 bool helper_free_when_destroyed(free_when_destroyed);
603 free_when_destroyed = other.free_when_destroyed;
604 other.free_when_destroyed = helper_free_when_destroyed;
605 bool helper_is_sorted(is_sorted);
606 is_sorted = other.is_sorted;
607 other.is_sorted = helper_is_sorted;
608 }
609
610
611private:
612 T* data;
613 u32 allocated;
614 u32 used;
615 TAlloc allocator;
616 eAllocStrategy strategy:4;
617 bool free_when_destroyed:1;
618 bool is_sorted:1;
619};
620
621
622} // end namespace core
623} // end namespace irr
624
625#endif
626
const array< T, TAlloc > & operator=(const array< T, TAlloc > &other)
Assignment operator.
Definition irrArray.h:266
u32 allocated_size() const
Get amount of memory allocated.
Definition irrArray.h:376
void clear()
Clears the array and deletes all allocated memory.
Definition irrArray.h:202
void push_front(const T &element)
Adds an element at the front of the array.
Definition irrArray.h:122
s32 binary_search(const T &element)
Performs a binary search for an element, returns -1 if not found.
Definition irrArray.h:408
void insert(const T &element, u32 index=0)
Insert item into array at specified position.
Definition irrArray.h:134
s32 linear_search(const T &element) const
Finds an element in linear time, which is very slow.
Definition irrArray.h:505
array(const array< T, TAlloc > &other)
Copy constructor.
Definition irrArray.h:45
void erase(u32 index)
Erases an element from the array.
Definition irrArray.h:535
array()
Default constructor for empty array.
Definition irrArray.h:28
s32 binary_search_multi(const T &element, s32 &last)
Definition irrArray.h:474
void set_used(u32 usedNow)
Sets the size of the array and allocates new elements if necessary.
Definition irrArray.h:256
T & getLast()
Gets last element.
Definition irrArray.h:332
bool operator!=(const array< T, TAlloc > &other) const
Inequality operator.
Definition irrArray.h:307
void set_pointer(T *newPointer, u32 size, bool _is_sorted=false, bool _free_when_destroyed=true)
Sets pointer to new array, using this as new workspace.
Definition irrArray.h:227
bool operator==(const array< T, TAlloc > &other) const
Equality operator.
Definition irrArray.h:294
void setAllocStrategy(eAllocStrategy newStrategy=ALLOC_STRATEGY_DOUBLE)
set a new allocation strategy
Definition irrArray.h:102
void sort()
Sorts the array using heapsort.
Definition irrArray.h:393
void swap(array< T, TAlloc > &other)
Swap the content of this array container with the content of another array.
Definition irrArray.h:593
T & operator[](u32 index)
Direct access operator.
Definition irrArray.h:314
bool empty() const
Check if array is empty.
Definition irrArray.h:384
T * pointer()
Gets a pointer to the array.
Definition irrArray.h:351
s32 binary_search(const T &element, s32 left, s32 right) const
Performs a binary search for an element, returns -1 if not found.
Definition irrArray.h:436
const T & getLast() const
Gets last element.
Definition irrArray.h:341
~array()
Destructor.
Definition irrArray.h:54
u32 size() const
Get number of occupied elements of the array.
Definition irrArray.h:367
array(u32 start_count)
Constructs an array and allocates an initial chunk of memory.
Definition irrArray.h:36
void set_sorted(bool _is_sorted)
Sets if the array is sorted.
Definition irrArray.h:583
void erase(u32 index, s32 count)
Erases some elements from the array.
Definition irrArray.h:556
const T * const_pointer() const
Gets a const pointer to the array.
Definition irrArray.h:359
void push_back(const T &element)
Adds an element at back of array.
Definition irrArray.h:111
void reallocate(u32 new_size, bool canShrink=true)
Reallocates the array, make it bigger or smaller.
Definition irrArray.h:66
s32 binary_search(const T &element) const
Performs a binary search for an element if possible, returns -1 if not found.
Definition irrArray.h:421
s32 linear_reverse_search(const T &element) const
Finds an element in linear time, which is very slow.
Definition irrArray.h:521
void set_free_when_destroyed(bool f)
Sets if the array should delete the memory it uses upon destruction.
Definition irrArray.h:246
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
eAllocStrategy
defines an allocation strategy
Definition irrAllocator.h:113
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition irrMath.h:177
void heapsort(T *array_, s32 size)
Sorts an array with size 'size' using heapsort.
Definition heapsort.h:41
Everything in the Irrlicht Engine can be found in this namespace.
Definition Skylicht.h:33
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
signed int s32
32 bit signed variable.
Definition irrTypes.h:66