|
1 /* $Id$ */ |
|
2 |
|
3 #ifndef ARRAY_HPP |
|
4 #define ARRAY_HPP |
|
5 |
|
6 #include "fixedsizearray.hpp" |
|
7 |
|
8 /** Flexible array with size limit. Implemented as fixed size |
|
9 array of fixed size arrays */ |
|
10 template <class Titem_, int Tblock_size_ = 1024, int Tnum_blocks_ = Tblock_size_> |
|
11 class CArrayT { |
|
12 public: |
|
13 typedef Titem_ Titem; ///< Titem is now visible from outside |
|
14 typedef CFixedSizeArrayT<Titem_, Tblock_size_> CSubArray; ///< inner array |
|
15 typedef CFixedSizeArrayT<CSubArray, Tnum_blocks_> CSuperArray; ///< outer array |
|
16 |
|
17 protected: |
|
18 CSuperArray m_a; ///< array of arrays of items |
|
19 |
|
20 public: |
|
21 ST_CONST(int, Tblock_size = Tblock_size_); ///< block size is now visible from outside |
|
22 ST_CONST(int, Tnum_blocks = Tnum_blocks_); ///< number of blocks is now visible from outside |
|
23 ST_CONST(int, Tcapacity = Tblock_size * Tnum_blocks); ///< total max number of items |
|
24 |
|
25 /** implicit constructor */ |
|
26 FORCEINLINE CArrayT() { } |
|
27 /** Return actual number of items */ |
|
28 FORCEINLINE int Size() const |
|
29 { |
|
30 int super_size = m_a.Size(); |
|
31 if (super_size == 0) return 0; |
|
32 int sub_size = m_a[super_size - 1].Size(); |
|
33 return (super_size - 1) * Tblock_size + sub_size; |
|
34 } |
|
35 /** return true if array is empty */ |
|
36 FORCEINLINE bool IsEmpty() { return m_a.IsEmpty(); } |
|
37 /** return true if array is full */ |
|
38 FORCEINLINE bool IsFull() { return m_a.IsFull() && m_a[Tnum_blocks - 1].IsFull(); } |
|
39 /** return first sub-array with free space for new item */ |
|
40 FORCEINLINE CSubArray& FirstFreeSubArray() |
|
41 { |
|
42 int super_size = m_a.Size(); |
|
43 if (super_size > 0) { |
|
44 CSubArray& sa = m_a[super_size - 1]; |
|
45 if (!sa.IsFull()) return sa; |
|
46 } |
|
47 return m_a.Add(); |
|
48 } |
|
49 /** allocate but not construct new item */ |
|
50 FORCEINLINE Titem_& AddNC() { return FirstFreeSubArray().AddNC(); } |
|
51 /** allocate and construct new item */ |
|
52 FORCEINLINE Titem_& Add() { return FirstFreeSubArray().Add(); } |
|
53 /** indexed access (non-const) */ |
|
54 FORCEINLINE Titem& operator [] (int idx) |
|
55 { |
|
56 CSubArray& sa = m_a[idx / Tblock_size]; |
|
57 Titem& item = sa [idx % Tblock_size]; |
|
58 return item; |
|
59 } |
|
60 /** indexed access (const) */ |
|
61 FORCEINLINE const Titem& operator [] (int idx) const |
|
62 { |
|
63 CSubArray& sa = m_a[idx / Tblock_size]; |
|
64 Titem& item = sa [idx % Tblock_size]; |
|
65 return item; |
|
66 } |
|
67 }; |
|
68 |
|
69 #endif /* ARRAY_HPP */ |