|
1 | | -/* Copyright (C) Tom Forsyth, 2001. |
| 1 | +/* Copyright (C) Tom Forsyth, 2001. |
2 | 2 | * All rights reserved worldwide. |
3 | 3 | * |
4 | 4 | * This software is provided "as is" without express or implied |
|
9 | 9 | */ |
10 | 10 | // Some standard useful classes, templates, etc. |
11 | 11 |
|
12 | | - |
13 | 12 | #ifndef ArbitraryListH |
14 | 13 | #define ArbitraryListH |
15 | 14 |
|
16 | | - |
17 | 15 | // An arbitrary-sized list template class. |
18 | 16 | // Designed to hold _unsorted_ data, but only RemoveItem() |
19 | 17 | // actually disturbs the order, so you can use it for general arrays |
20 | 18 | // if you don't use that function. |
21 | 19 | template <class T> |
22 | 20 | class ArbitraryList |
23 | 21 | { |
24 | | - T *pT; // The list. |
25 | | - u32 iSize; // The current size of the list. |
26 | | - u32 iReservedSize; // The current reserved size of the list. |
| 22 | + T* pT; // The list. |
| 23 | + u32 iSize; // The current size of the list. |
| 24 | + u32 iReservedSize; // The current reserved size of the list. |
27 | 25 | public: |
| 26 | + // Constructor, with optional initial size setting. |
| 27 | + ArbitraryList(u32 iInitialSize = 0) |
| 28 | + { |
| 29 | + pT = NULL; |
| 30 | + iSize = 0; |
| 31 | + iReservedSize = 0; |
| 32 | + if (iInitialSize > 0) resize(iInitialSize); |
| 33 | + } |
| 34 | + // Destructor. |
| 35 | + ~ArbitraryList(void) |
| 36 | + { |
| 37 | + if (pT == NULL) { |
| 38 | + VERIFY(iReservedSize == 0); |
| 39 | + VERIFY(iSize == 0); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + VERIFY(iReservedSize > 0); |
| 44 | + VERIFY(iSize > 0); |
| 45 | + delete[] pT; |
| 46 | + pT = NULL; |
| 47 | + } |
| 48 | + } |
| 49 | + // Returns the pointer to the given item. |
| 50 | + IC T* item(u32 iItem) |
| 51 | + { |
| 52 | + VERIFY(iItem < iSize); |
| 53 | + return (&pT[iItem]); |
| 54 | + } |
| 55 | + // Returns the pointer to the first item. |
| 56 | + IC T* ptr() { return (pT); } |
| 57 | + // Returns the size of the list |
| 58 | + IC u32 size() const { return iSize; } |
| 59 | + // Grows or shrinks the list to this number of items. |
| 60 | + // Preserves existing items. |
| 61 | + // Items that fall off the end of a shrink may vanish. |
| 62 | + // Returns the pointer to the first item. |
| 63 | + IC T* resize(u32 iNum) |
| 64 | + { |
| 65 | + VERIFY(iNum >= 0); |
| 66 | + iSize = iNum; |
| 67 | + if (iNum <= iReservedSize) { |
| 68 | + if (iNum == 0) { |
| 69 | + // Shrunk to 0 - bin the memory. |
| 70 | + delete[] pT; |
| 71 | + pT = NULL; |
| 72 | + iReservedSize = 0; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + VERIFY(pT != NULL); |
| 77 | + } |
| 78 | + return (pT); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + // Need to grow. Grow by 50% more than |
| 83 | + // needed to avoid constant regrows. |
| 84 | + u32 iNewSize = (iNum * 3) >> 1; |
| 85 | + if (pT == NULL) { |
| 86 | + VERIFY(iReservedSize == 0); |
| 87 | + pT = new T[iNewSize]; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + VERIFY(iReservedSize != 0); |
28 | 92 |
|
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]; } |
| 93 | + T* pOldT = pT; |
| 94 | + pT = new T[iNewSize]; |
| 95 | + for (u32 i = 0; i < iReservedSize; i++) |
| 96 | + { |
| 97 | + pT[i] = pOldT[i]; |
| 98 | + } |
| 99 | + delete[] pOldT; |
| 100 | + } |
| 101 | + VERIFY(pT != NULL); |
| 102 | + iReservedSize = iNewSize; |
| 103 | + return (pT); |
| 104 | + } |
| 105 | + } |
| 106 | + // Adds one item to the list and returns a pointer to that new item. |
| 107 | + IC T* append() |
| 108 | + { |
| 109 | + resize(iSize + 1); |
| 110 | + return (&pT[iSize - 1]); |
| 111 | + } |
| 112 | + // Adds one item to the list and returns a pointer to that new item. |
| 113 | + IC void push_back(T& val) |
| 114 | + { |
| 115 | + resize(iSize + 1); |
| 116 | + pT[iSize - 1] = val; |
| 117 | + } |
| 118 | + // Removes the given item number by copying the last item |
| 119 | + // to that position and shrinking the list. |
| 120 | + IC void erase_fast(u32 iItemNumber) |
| 121 | + { |
| 122 | + VERIFY(iItemNumber < iSize); |
| 123 | + pT[iItemNumber] = pT[iSize - 1]; |
| 124 | + resize(iSize - 1); |
| 125 | + } |
| 126 | + // Copy the specified data into the list. |
| 127 | + IC void insert(u32 iFirstItem, T* p, u32 iNumItems) |
| 128 | + { |
| 129 | + for (u32 i = 0; i < iNumItems; i++) |
| 130 | + *(Item(i + iFirstItem)) = p[i]; |
| 131 | + } |
| 132 | + // A copy from another arbitrary list of the same type. |
| 133 | + IC void insert(u32 iFirstItem, ArbitraryList<T>& other, u32 iFirstOtherItem, u32 iNumItems) |
| 134 | + { |
| 135 | + for (u32 i = 0; i < iNumItems; i++) |
| 136 | + *(item(i + iFirstItem)) = *(other.item(i + iFirstOtherItem)); |
| 137 | + } |
| 138 | + IC T& operator[](u32 id) |
| 139 | + { |
| 140 | + VERIFY(id < iSize); |
| 141 | + return pT[id]; |
| 142 | + } |
| 143 | + IC const T& operator[](u32 id) const |
| 144 | + { |
| 145 | + VERIFY(id < iSize); |
| 146 | + return pT[id]; |
| 147 | + } |
135 | 148 |
|
136 | | - // Copy constructor. |
137 | | - ArbitraryList ( const ArbitraryList<T> &other ) |
138 | | - { |
139 | | - u32 iNumItems = other.size(); |
| 149 | + // Copy constructor. |
| 150 | + ArbitraryList(const ArbitraryList<T>& other) |
| 151 | + { |
| 152 | + u32 iNumItems = other.size(); |
140 | 153 |
|
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 | | - } |
| 154 | + pT = NULL; |
| 155 | + iSize = 0; |
| 156 | + iReservedSize = 0; |
| 157 | + if (iNumItems > 0) resize(iNumItems); |
| 158 | + for (u32 i = 0; i < iNumItems; i++) |
| 159 | + *(item(i)) = other[i]; |
| 160 | + } |
149 | 161 | }; |
150 | 162 |
|
151 | | -#endif //#ifndef ArbitraryListH |
| 163 | +#endif //#ifndef ArbitraryListH |
0 commit comments