Mercurial > LightClone
annotate LightClone/Source/ArrayList.h @ 19:51718795f019
Adding event handling & drag and drop to GuiInterface
author | koryspansel |
---|---|
date | Thu, 15 Sep 2011 12:13:40 -0700 |
parents | ee1c2510096d |
children | b4dc5d674e22 |
rev | line source |
---|---|
12 | 1 /* |
2 * ArrayList | |
3 */ | |
4 | |
5 #ifndef __ARRAYLIST_H__ | |
6 #define __ARRAYLIST_H__ | |
7 | |
8 #include "Core.h" | |
9 | |
10 /* | |
11 * ArrayList | |
12 */ | |
13 template<typename Type> | |
14 class ArrayList | |
15 { | |
16 /* | |
17 * pList | |
18 */ | |
19 Type* pList; | |
20 | |
21 /* | |
22 * nCapacity | |
23 */ | |
24 uint32 nCapacity; | |
25 | |
26 /* | |
27 * nSize | |
28 */ | |
29 uint32 nSize; | |
30 | |
31 public: | |
32 | |
33 /* | |
34 * ArrayList | |
35 */ | |
36 ArrayList() : pList(0), nCapacity(0), nSize(0) | |
37 { | |
38 } | |
39 | |
40 /* | |
41 * ~ArrayList | |
42 */ | |
43 ~ArrayList() | |
44 { | |
45 delete[] pList; | |
46 } | |
47 | |
48 /* | |
49 * Add | |
50 */ | |
51 ErrorCode Add(Type& kValue) | |
52 { | |
53 ErrorCode eCode = Resize(nSize + 1); | |
54 if(eCode == Error_Success) | |
55 { | |
56 pList[nSize++] = kValue; | |
57 } | |
58 | |
59 return eCode; | |
60 } | |
61 | |
62 /* | |
63 * Remove | |
64 */ | |
65 ErrorCode Remove(uint32 nIndex) | |
66 { | |
67 ErrorCode eCode = Error_Fail; | |
68 | |
69 if(nIndex < nSize) | |
70 { | |
15
ee1c2510096d
Work on GUI system
koryspansel <koryspansel@bendbroadband.com>
parents:
12
diff
changeset
|
71 --nSize; |
12 | 72 |
15
ee1c2510096d
Work on GUI system
koryspansel <koryspansel@bendbroadband.com>
parents:
12
diff
changeset
|
73 for(uint32 i = nIndex; i < nSize; ++i) |
12 | 74 { |
75 pList[i] = pList[i + 1]; | |
76 } | |
77 | |
15
ee1c2510096d
Work on GUI system
koryspansel <koryspansel@bendbroadband.com>
parents:
12
diff
changeset
|
78 eCode = Error_Success; |
12 | 79 } |
80 | |
81 return eCode; | |
82 } | |
83 | |
84 /* | |
19
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
85 * Remove |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
86 */ |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
87 ErrorCode Remove(Type& kItem) |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
88 { |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
89 for(uint32 i = 0; i < nSize; ++i) |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
90 { |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
91 if(pList[i] == kItem) |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
92 { |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
93 --nSize; |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
94 |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
95 for(; i < nSize; ++i) |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
96 pList[i] = pList[i + 1]; |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
97 |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
98 return Error_Success; |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
99 } |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
100 } |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
101 |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
102 return Error_Fail; |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
103 } |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
104 |
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
105 /* |
12 | 106 * Clear |
107 */ | |
108 void Clear() | |
109 { | |
110 nSize = 0; | |
111 } | |
112 | |
113 /* | |
19
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
114 * Size |
12 | 115 */ |
19
51718795f019
Adding event handling & drag and drop to GuiInterface
koryspansel
parents:
15
diff
changeset
|
116 uint32 Size() const |
12 | 117 { |
118 return nSize; | |
119 } | |
120 | |
121 /* | |
122 * Find | |
123 */ | |
124 int32 Find(Type& kValue) const | |
125 { | |
126 for(uint32 i = 0; i < nSize; ++i) | |
127 { | |
128 if(pList[i] == kValue) | |
129 { | |
130 return (int32)i; | |
131 } | |
132 } | |
133 | |
134 return -1; | |
135 } | |
136 | |
137 /* | |
138 * operator [] | |
139 */ | |
140 Type& operator[](int nIndex) | |
141 { | |
142 //ASSERT(nIndex < nSize); | |
143 return pList[nIndex]; | |
144 } | |
145 | |
146 /* | |
147 * operator [] | |
148 */ | |
149 const Type& operator[](int nIndex) const | |
150 { | |
151 //ASSERT(nIndex < nSize); | |
152 return pList[nIndex]; | |
153 } | |
154 | |
155 private: | |
156 | |
157 /* | |
158 * Resize | |
159 */ | |
160 ErrorCode Resize(uint32 nAmount) | |
161 { | |
162 if(nAmount > nCapacity) | |
163 { | |
164 Type* pArray = new Type[2 * nAmount]; | |
165 | |
166 if(pList) | |
167 { | |
168 for(uint32 i = 0; i < nSize; ++i) | |
169 { | |
170 pArray[i] = pList[i]; | |
171 } | |
172 | |
173 delete[] pList; | |
174 } | |
175 | |
176 pList = pArray; | |
177 nCapacity = 2 * nAmount; | |
178 } | |
179 | |
180 return Error_Success; | |
181 } | |
182 }; | |
183 | |
184 #endif //__ARRAYLIST_H__ |