1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | // |
---|
12 | // The Array class |
---|
13 | // --------------- |
---|
14 | // A very simple Array class not unlike the std::vector. |
---|
15 | // |
---|
16 | |
---|
17 | #if !defined __LOL_ARRAY_H__ |
---|
18 | #define __LOL_ARRAY_H__ |
---|
19 | |
---|
20 | #include <stdint.h> |
---|
21 | |
---|
22 | namespace lol |
---|
23 | { |
---|
24 | |
---|
25 | template<typename T1, typename T2 = void, typename T3 = void> class Array |
---|
26 | { |
---|
27 | public: |
---|
28 | typedef struct { T1 m1; T2 m2; T3 m3; } Element; |
---|
29 | |
---|
30 | inline Array() : m_data(0), m_count(0), m_reserved(0) {} |
---|
31 | inline ~Array() { delete[] m_data; } |
---|
32 | |
---|
33 | inline Element& operator[](int n) |
---|
34 | { |
---|
35 | return m_data[n]; |
---|
36 | } |
---|
37 | |
---|
38 | inline Element const& operator[](int n) const |
---|
39 | { |
---|
40 | return m_data[n]; |
---|
41 | } |
---|
42 | |
---|
43 | inline Array<T1, T2, T3> const& operator<<(Element const &x) |
---|
44 | { |
---|
45 | if (m_count >= m_reserved) |
---|
46 | { |
---|
47 | /* Protect ourselves against insertion of an element that is |
---|
48 | * already in m_data. */ |
---|
49 | Element tmp = x; |
---|
50 | Reserve(m_count * 13 / 8 + 8); |
---|
51 | m_data[m_count++] = tmp; |
---|
52 | } |
---|
53 | else |
---|
54 | m_data[m_count++] = x; |
---|
55 | return *this; |
---|
56 | } |
---|
57 | |
---|
58 | inline void Append(T1 const &m1, T2 const &m2, T3 const &m3) |
---|
59 | { |
---|
60 | if (m_count >= m_reserved) |
---|
61 | { |
---|
62 | T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; |
---|
63 | Reserve(m_count * 13 / 8 + 8); |
---|
64 | m_data[m_count].m1 = tmp1; |
---|
65 | m_data[m_count].m2 = tmp2; |
---|
66 | m_data[m_count].m3 = tmp3; |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | m_data[m_count].m1 = m1; |
---|
71 | m_data[m_count].m2 = m2; |
---|
72 | m_data[m_count].m3 = m3; |
---|
73 | } |
---|
74 | ++m_count; |
---|
75 | } |
---|
76 | |
---|
77 | void Remove(int pos) |
---|
78 | { |
---|
79 | memmove(m_data + pos, m_data + pos + 1, m_count - pos - 1); |
---|
80 | m_count--; |
---|
81 | } |
---|
82 | |
---|
83 | void Remove(int pos, int count) |
---|
84 | { |
---|
85 | memmove(m_data + pos, m_data + pos + count, m_count - pos - count); |
---|
86 | m_count -= count; |
---|
87 | } |
---|
88 | |
---|
89 | void Reserve(int count) |
---|
90 | { |
---|
91 | if (count <= (int)m_reserved) |
---|
92 | return; |
---|
93 | |
---|
94 | Element *tmp = new Element[count]; |
---|
95 | if (m_data) |
---|
96 | { |
---|
97 | memcpy(tmp, m_data, m_count * sizeof(Element)); |
---|
98 | delete[] m_data; |
---|
99 | } |
---|
100 | m_data = tmp; |
---|
101 | m_reserved = count; |
---|
102 | } |
---|
103 | |
---|
104 | inline int Count() const { return m_count; } |
---|
105 | inline int Bytes() const { return m_count * sizeof(Element); } |
---|
106 | |
---|
107 | private: |
---|
108 | Element *m_data; |
---|
109 | int m_count, m_reserved; |
---|
110 | }; |
---|
111 | |
---|
112 | template<typename T1, typename T2> class Array<T1, T2, void> |
---|
113 | { |
---|
114 | public: |
---|
115 | typedef struct { T1 m1; T2 m2; } Element; |
---|
116 | |
---|
117 | inline Array() : m_data(0), m_count(0), m_reserved(0) {} |
---|
118 | inline ~Array() { delete[] m_data; } |
---|
119 | |
---|
120 | inline Element& operator[](int n) |
---|
121 | { |
---|
122 | return m_data[n]; |
---|
123 | } |
---|
124 | |
---|
125 | inline Element const& operator[](int n) const |
---|
126 | { |
---|
127 | return m_data[n]; |
---|
128 | } |
---|
129 | |
---|
130 | inline Array<T1, T2> const& operator<<(Element const &x) |
---|
131 | { |
---|
132 | if (m_count >= m_reserved) |
---|
133 | { |
---|
134 | /* Protect ourselves against insertion of an element that is |
---|
135 | * already in m_data. */ |
---|
136 | Element tmp = x; |
---|
137 | Reserve(m_count * 13 / 8 + 8); |
---|
138 | m_data[m_count++] = tmp; |
---|
139 | } |
---|
140 | else |
---|
141 | m_data[m_count++] = x; |
---|
142 | return *this; |
---|
143 | } |
---|
144 | |
---|
145 | inline void Append(T1 const &m1, T2 const &m2) |
---|
146 | { |
---|
147 | if (m_count >= m_reserved) |
---|
148 | { |
---|
149 | T1 tmp1 = m1; T2 tmp2 = m2; |
---|
150 | Reserve(m_count * 13 / 8 + 8); |
---|
151 | m_data[m_count].m1 = tmp1; |
---|
152 | m_data[m_count].m2 = tmp2; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | m_data[m_count].m1 = m1; |
---|
157 | m_data[m_count].m2 = m2; |
---|
158 | } |
---|
159 | ++m_count; |
---|
160 | } |
---|
161 | |
---|
162 | void Remove(int pos) |
---|
163 | { |
---|
164 | memmove(m_data + pos, m_data + pos + 1, m_count - pos - 1); |
---|
165 | m_count--; |
---|
166 | } |
---|
167 | |
---|
168 | void Remove(int pos, int count) |
---|
169 | { |
---|
170 | memmove(m_data + pos, m_data + pos + count, m_count - pos - count); |
---|
171 | m_count -= count; |
---|
172 | } |
---|
173 | |
---|
174 | void Reserve(int count) |
---|
175 | { |
---|
176 | if (count <= (int)m_reserved) |
---|
177 | return; |
---|
178 | |
---|
179 | Element *tmp = new Element[count]; |
---|
180 | if (m_data) |
---|
181 | { |
---|
182 | memcpy(tmp, m_data, m_count * sizeof(Element)); |
---|
183 | delete[] m_data; |
---|
184 | } |
---|
185 | m_data = tmp; |
---|
186 | m_reserved = count; |
---|
187 | } |
---|
188 | |
---|
189 | inline int Count() const { return m_count; } |
---|
190 | inline int Bytes() const { return m_count * sizeof(Element); } |
---|
191 | |
---|
192 | private: |
---|
193 | Element *m_data; |
---|
194 | int m_count, m_reserved; |
---|
195 | }; |
---|
196 | |
---|
197 | template<typename T1> class Array<T1, void, void> |
---|
198 | { |
---|
199 | public: |
---|
200 | inline Array() : m_data(0), m_count(0), m_reserved(0) {} |
---|
201 | inline ~Array() { delete[] m_data; } |
---|
202 | |
---|
203 | inline T1& operator[](int n) |
---|
204 | { |
---|
205 | return m_data[n]; |
---|
206 | } |
---|
207 | |
---|
208 | inline T1 const& operator[](int n) const |
---|
209 | { |
---|
210 | return m_data[n]; |
---|
211 | } |
---|
212 | |
---|
213 | inline Array<T1> const& operator<<(T1 const &x) |
---|
214 | { |
---|
215 | if (m_count >= m_reserved) |
---|
216 | { |
---|
217 | T1 tmp = x; |
---|
218 | Reserve(m_count * 13 / 8 + 8); |
---|
219 | m_data[m_count++] = tmp; |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | m_data[m_count++] = x; |
---|
224 | } |
---|
225 | return *this; |
---|
226 | } |
---|
227 | |
---|
228 | inline void Append(T1 const &x) |
---|
229 | { |
---|
230 | *this << x; |
---|
231 | } |
---|
232 | |
---|
233 | void Remove(int pos) |
---|
234 | { |
---|
235 | memmove(m_data + pos, m_data + pos + 1, m_count - pos - 1); |
---|
236 | m_count--; |
---|
237 | } |
---|
238 | |
---|
239 | void Remove(int pos, int count) |
---|
240 | { |
---|
241 | memmove(m_data + pos, m_data + pos + count, m_count - pos - count); |
---|
242 | m_count -= count; |
---|
243 | } |
---|
244 | |
---|
245 | void Reserve(int count) |
---|
246 | { |
---|
247 | if (count <= (int)m_reserved) |
---|
248 | return; |
---|
249 | |
---|
250 | T1 *tmp = new T1[count]; |
---|
251 | if (m_data) |
---|
252 | { |
---|
253 | memcpy(tmp, m_data, m_count * sizeof(T1)); |
---|
254 | delete[] m_data; |
---|
255 | } |
---|
256 | m_data = tmp; |
---|
257 | m_reserved = count; |
---|
258 | } |
---|
259 | |
---|
260 | inline int Count() const { return m_count; } |
---|
261 | inline int Bytes() const { return m_count * sizeof(T1); } |
---|
262 | |
---|
263 | private: |
---|
264 | T1 *m_data; |
---|
265 | int m_count, m_reserved; |
---|
266 | }; |
---|
267 | |
---|
268 | } /* namespace lol */ |
---|
269 | |
---|
270 | #endif // __LOL_ARRAY_H__ |
---|
271 | |
---|