|
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 static const int Tblock_size = Tblock_size_; ///< block size is now visible from outside |
|
22 static const int Tnum_blocks = Tnum_blocks_; ///< number of blocks is now visible from outside |
|
23 static const int Tcapacity = Tblock_size * Tnum_blocks; ///< total max number of items |
|
24 |
|
25 /** implicit constructor */ |
|
26 FORCEINLINE CArrayT() { } |
|
27 /** Clear (destroy) all items */ |
|
28 FORCEINLINE void Clear() {m_a.Clear();} |
|
29 /** Return actual number of items */ |
|
30 FORCEINLINE int Size() const |
|
31 { |
|
32 int super_size = m_a.Size(); |
|
33 if (super_size == 0) return 0; |
|
34 int sub_size = m_a[super_size - 1].Size(); |
|
35 return (super_size - 1) * Tblock_size + sub_size; |
|
36 } |
|
37 /** return true if array is empty */ |
|
38 FORCEINLINE bool IsEmpty() { return m_a.IsEmpty(); } |
|
39 /** return true if array is full */ |
|
40 FORCEINLINE bool IsFull() { return m_a.IsFull() && m_a[Tnum_blocks - 1].IsFull(); } |
|
41 /** return first sub-array with free space for new item */ |
|
42 FORCEINLINE CSubArray& FirstFreeSubArray() |
|
43 { |
|
44 int super_size = m_a.Size(); |
|
45 if (super_size > 0) { |
|
46 CSubArray& sa = m_a[super_size - 1]; |
|
47 if (!sa.IsFull()) return sa; |
|
48 } |
|
49 return m_a.Add(); |
|
50 } |
|
51 /** allocate but not construct new item */ |
|
52 FORCEINLINE Titem_& AddNC() { return FirstFreeSubArray().AddNC(); } |
|
53 /** allocate and construct new item */ |
|
54 FORCEINLINE Titem_& Add() { return FirstFreeSubArray().Add(); } |
|
55 /** indexed access (non-const) */ |
|
56 FORCEINLINE Titem& operator [] (int idx) |
|
57 { |
|
58 CSubArray& sa = m_a[idx / Tblock_size]; |
|
59 Titem& item = sa [idx % Tblock_size]; |
|
60 return item; |
|
61 } |
|
62 /** indexed access (const) */ |
|
63 FORCEINLINE const Titem& operator [] (int idx) const |
|
64 { |
|
65 CSubArray& sa = m_a[idx / Tblock_size]; |
|
66 Titem& item = sa [idx % Tblock_size]; |
|
67 return item; |
|
68 } |
|
69 }; |
|
70 |
|
71 #endif /* ARRAY_HPP */ |