|
18 | 18 | // Designed to hold _unsorted_ data, but only RemoveItem() |
19 | 19 | // actually disturbs the order, so you can use it for general arrays |
20 | 20 | // if you don't use that function. |
21 | | -template <class T> |
| 21 | +template<class T> |
22 | 22 | class ArbitraryList |
23 | 23 | { |
24 | | - T *pT; // The list. |
25 | | - u32 iSize; // The current size of the list. |
26 | | - u32 iReservedSize; // The current reserved size of the list. |
| 24 | + T *pT; // The list. |
| 25 | + u32 iSize; // The current size of the list. |
| 26 | + u32 iReservedSize; // The current reserved size of the list. |
27 | 27 | public: |
28 | 28 |
|
29 | | - // Constructor, with optional initial size setting. |
30 | | - ArbitraryList ( u32 iInitialSize = 0 ) |
31 | | - { |
32 | | - pT = NULL; |
33 | | - iSize = 0; |
34 | | - iReservedSize = 0; |
35 | | - if ( iInitialSize > 0 ) |
36 | | - resize( iInitialSize ); |
37 | | - } |
38 | | - // Destructor. |
39 | | - ~ArbitraryList ( void ) |
40 | | - { |
41 | | - if ( pT == NULL ){ |
42 | | - VERIFY ( iReservedSize == 0 ); |
43 | | - VERIFY ( iSize == 0 ); |
44 | | - }else{ |
45 | | - VERIFY ( iReservedSize > 0 ); |
46 | | - VERIFY ( iSize > 0 ); |
47 | | - delete[] pT; |
48 | | - pT = NULL; |
49 | | - } |
50 | | - } |
51 | | - // Returns the pointer to the given item. |
52 | | - IC T* item (u32 iItem) |
53 | | - { |
54 | | - VERIFY ( iItem < iSize ); |
55 | | - return ( &pT[iItem] ); |
56 | | - } |
57 | | - // Returns the pointer to the first item. |
58 | | - IC T* ptr ( ) { return (pT);} |
59 | | - // Returns the size of the list |
60 | | - IC u32 size ( ) const { return iSize; } |
61 | | - // Grows or shrinks the list to this number of items. |
62 | | - // Preserves existing items. |
63 | | - // Items that fall off the end of a shrink may vanish. |
64 | | - // Returns the pointer to the first item. |
65 | | - IC T* resize (u32 iNum) |
66 | | - { |
67 | | - VERIFY ( iNum >= 0 ); |
68 | | - iSize = iNum; |
69 | | - if ( iNum <= iReservedSize ){ |
70 | | - if ( iNum == 0 ){ |
71 | | - // Shrunk to 0 - bin the memory. |
72 | | - delete[] pT; |
73 | | - pT = NULL; |
74 | | - iReservedSize = 0; |
75 | | - }else{ |
76 | | - VERIFY ( pT != NULL ); |
77 | | - } |
78 | | - return ( pT ); |
79 | | - }else{ |
80 | | - // Need to grow. Grow by 50% more than |
81 | | - // needed to avoid constant regrows. |
82 | | - u32 iNewSize = ( iNum * 3 ) >> 1; |
83 | | - if ( pT == NULL ){ |
84 | | - VERIFY ( iReservedSize == 0 ); |
85 | | - pT = new T [iNewSize]; |
86 | | - }else{ |
87 | | - VERIFY ( iReservedSize != 0 ); |
88 | | - |
89 | | - T *pOldT = pT; |
90 | | - pT = new T[iNewSize]; |
91 | | - for ( u32 i = 0; i < iReservedSize; i++ ){ |
92 | | - pT[i] = pOldT[i]; |
93 | | - } |
94 | | - delete[] pOldT; |
95 | | - } |
96 | | - VERIFY ( pT != NULL ); |
97 | | - iReservedSize = iNewSize; |
98 | | - return ( pT ); |
99 | | - } |
100 | | - } |
101 | | - // Adds one item to the list and returns a pointer to that new item. |
102 | | - IC T* append ( ) |
103 | | - { |
104 | | - resize ( iSize + 1 ); |
105 | | - return ( &pT[iSize-1] ); |
106 | | - } |
107 | | - // Adds one item to the list and returns a pointer to that new item. |
108 | | - IC void push_back (T& val) |
109 | | - { |
110 | | - resize ( iSize + 1 ); |
111 | | - pT[iSize-1] = val; |
112 | | - } |
113 | | - // Removes the given item number by copying the last item |
114 | | - // to that position and shrinking the list. |
115 | | - IC void erase_fast (u32 iItemNumber) |
116 | | - { |
117 | | - VERIFY ( iItemNumber < iSize ); |
118 | | - pT[iItemNumber] = pT[iSize-1]; |
119 | | - resize ( iSize - 1 ); |
120 | | - } |
121 | | - // Copy the specified data into the list. |
122 | | - IC void insert (u32 iFirstItem, T *p, u32 iNumItems) |
123 | | - { |
124 | | - for ( u32 i = 0; i < iNumItems; i++ ) |
125 | | - *(Item ( i + iFirstItem ) ) = p[i]; |
126 | | - } |
127 | | - // A copy from another arbitrary list of the same type. |
128 | | - IC void insert ( u32 iFirstItem, ArbitraryList<T> &other, u32 iFirstOtherItem, u32 iNumItems ) |
129 | | - { |
130 | | - for ( u32 i = 0; i < iNumItems; i++ ) |
131 | | - *(item ( i + iFirstItem ) ) = *(other.item ( i + iFirstOtherItem ) ); |
132 | | - } |
133 | | - IC T& operator[] (u32 id) { VERIFY(id<iSize); return pT[id]; } |
134 | | - IC const T& operator[] (u32 id) const { VERIFY(id<iSize); return pT[id]; } |
135 | | - |
136 | | - // Copy constructor. |
137 | | - ArbitraryList ( const ArbitraryList<T> &other ) |
138 | | - { |
139 | | - u32 iNumItems = other.size(); |
140 | | - |
141 | | - pT = NULL; |
142 | | - iSize = 0; |
143 | | - iReservedSize = 0; |
144 | | - if ( iNumItems > 0 ) |
145 | | - resize ( iNumItems ); |
146 | | - for ( u32 i = 0; i < iNumItems; i++ ) |
147 | | - *(item(i) ) = other[i]; |
148 | | - } |
| 29 | + // Constructor, with optional initial size setting. |
| 30 | + ArbitraryList(u32 iInitialSize = 0) |
| 31 | + { |
| 32 | + pT = NULL; |
| 33 | + iSize = 0; |
| 34 | + iReservedSize = 0; |
| 35 | + if (iInitialSize>0) |
| 36 | + resize(iInitialSize); |
| 37 | + } |
| 38 | + |
| 39 | + // Destructor. |
| 40 | + ~ArbitraryList(void) |
| 41 | + { |
| 42 | + if (pT==NULL) |
| 43 | + { |
| 44 | + VERIFY(iReservedSize==0); |
| 45 | + VERIFY(iSize==0); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + VERIFY(iReservedSize>0); |
| 50 | + VERIFY(iSize>0); |
| 51 | + delete[] pT; |
| 52 | + pT = NULL; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Returns the pointer to the given item. |
| 57 | + IC T*item(u32 iItem) |
| 58 | + { |
| 59 | + VERIFY(iItem<iSize); |
| 60 | + return (&pT[iItem]); |
| 61 | + } |
| 62 | + |
| 63 | + // Returns the pointer to the first item. |
| 64 | + IC T*ptr() |
| 65 | + { |
| 66 | + return (pT); |
| 67 | + } |
| 68 | + |
| 69 | + // Returns the size of the list |
| 70 | + IC u32 size() const |
| 71 | + { |
| 72 | + return iSize; |
| 73 | + } |
| 74 | + |
| 75 | + // Grows or shrinks the list to this number of items. |
| 76 | + // Preserves existing items. |
| 77 | + // Items that fall off the end of a shrink may vanish. |
| 78 | + // Returns the pointer to the first item. |
| 79 | + IC T*resize(u32 iNum) |
| 80 | + { |
| 81 | + VERIFY(iNum>=0); |
| 82 | + iSize = iNum; |
| 83 | + if (iNum<=iReservedSize) |
| 84 | + { |
| 85 | + if (iNum==0) |
| 86 | + { |
| 87 | + // Shrunk to 0 - bin the memory. |
| 88 | + delete[] pT; |
| 89 | + pT = NULL; |
| 90 | + iReservedSize = 0; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + VERIFY(pT!=NULL); |
| 95 | + } |
| 96 | + return (pT); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + // Need to grow. Grow by 50% more than |
| 101 | + // needed to avoid constant regrows. |
| 102 | + u32 iNewSize = (iNum*3)>>1; |
| 103 | + if (pT==NULL) |
| 104 | + { |
| 105 | + VERIFY(iReservedSize==0); |
| 106 | + pT = new T [iNewSize]; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + VERIFY(iReservedSize!=0); |
| 111 | + |
| 112 | + T *pOldT = pT; |
| 113 | + pT = new T[iNewSize]; |
| 114 | + for (u32 i = 0; i<iReservedSize; i++) |
| 115 | + { |
| 116 | + pT[i] = pOldT[i]; |
| 117 | + } |
| 118 | + delete[] pOldT; |
| 119 | + } |
| 120 | + VERIFY(pT!=NULL); |
| 121 | + iReservedSize = iNewSize; |
| 122 | + return (pT); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Adds one item to the list and returns a pointer to that new item. |
| 127 | + IC T*append() |
| 128 | + { |
| 129 | + resize(iSize+1); |
| 130 | + return (&pT[iSize-1]); |
| 131 | + } |
| 132 | + |
| 133 | + // Adds one item to the list and returns a pointer to that new item. |
| 134 | + IC |
| 135 | + |
| 136 | + void push_back(T &val) |
| 137 | + { |
| 138 | + resize(iSize+1); |
| 139 | + pT[iSize-1] = val; |
| 140 | + } |
| 141 | + |
| 142 | + // Removes the given item number by copying the last item |
| 143 | + // to that position and shrinking the list. |
| 144 | + IC |
| 145 | + |
| 146 | + void erase_fast(u32 iItemNumber) |
| 147 | + { |
| 148 | + VERIFY(iItemNumber<iSize); |
| 149 | + pT[iItemNumber] = pT[iSize-1]; |
| 150 | + resize(iSize-1); |
| 151 | + } |
| 152 | + |
| 153 | + // Copy the specified data into the list. |
| 154 | + IC |
| 155 | + |
| 156 | + void insert(u32 iFirstItem, T *p, u32 iNumItems) |
| 157 | + { |
| 158 | + for (u32 i = 0; i<iNumItems; i++) |
| 159 | + *(Item(i+iFirstItem)) = p[i]; |
| 160 | + } |
| 161 | + |
| 162 | + // A copy from another arbitrary list of the same type. |
| 163 | + IC |
| 164 | + |
| 165 | + void insert(u32 iFirstItem, ArbitraryList<T> &other, u32 iFirstOtherItem, u32 iNumItems) |
| 166 | + { |
| 167 | + for (u32 i = 0; i<iNumItems; i++) |
| 168 | + *(item(i+iFirstItem)) = *(other.item(i+iFirstOtherItem)); |
| 169 | + } |
| 170 | + |
| 171 | + IC T&operator[](u32 id) |
| 172 | + { |
| 173 | + VERIFY(id<iSize); |
| 174 | + return pT[id]; |
| 175 | + } |
| 176 | + |
| 177 | + IC const T&operator[](u32 id) const |
| 178 | + { |
| 179 | + VERIFY(id<iSize); |
| 180 | + return pT[id]; |
| 181 | + } |
| 182 | + |
| 183 | + // Copy constructor. |
| 184 | + ArbitraryList(const ArbitraryList<T> &other) |
| 185 | + { |
| 186 | + u32 iNumItems = other.size(); |
| 187 | + |
| 188 | + pT = NULL; |
| 189 | + iSize = 0; |
| 190 | + iReservedSize = 0; |
| 191 | + if (iNumItems>0) |
| 192 | + resize(iNumItems); |
| 193 | + for (u32 i = 0; i<iNumItems; i++) |
| 194 | + *(item(i)) = other[i]; |
| 195 | + } |
149 | 196 | }; |
150 | 197 |
|
151 | 198 | #endif //#ifndef ArbitraryListH |
| 199 | + |
| 200 | + |
0 commit comments