118 return m_data.crend(); |
118 return m_data.crend(); |
119 } |
119 } |
120 |
120 |
121 // ===================================================================== |
121 // ===================================================================== |
122 // |
122 // |
123 inline void RemoveAt (int pos) |
123 inline void removeAt (int pos) |
124 { |
124 { |
125 assert (pos < Size()); |
125 assert (pos < size()); |
126 m_data.erase (m_data.begin() + pos); |
126 m_data.erase (m_data.begin() + pos); |
127 } |
127 } |
128 |
128 |
129 // ===================================================================== |
129 // ===================================================================== |
130 // |
130 // |
131 ValueType& Prepend (const ValueType& value) |
131 ValueType& prepend (const ValueType& value) |
132 { |
132 { |
133 m_data.push_front (value); |
133 m_data.push_front (value); |
134 return m_data[0]; |
134 return m_data[0]; |
135 } |
135 } |
136 |
136 |
137 // ===================================================================== |
137 // ===================================================================== |
138 // |
138 // |
139 ValueType& Append (const ValueType& value) |
139 ValueType& append (const ValueType& value) |
140 { |
140 { |
141 m_data.push_back (value); |
141 m_data.push_back (value); |
142 return m_data[m_data.size() - 1]; |
142 return m_data[m_data.size() - 1]; |
143 } |
143 } |
144 |
144 |
145 // ===================================================================== |
145 // ===================================================================== |
146 // |
146 // |
147 void Merge (const SelfType& other) |
147 void merge (const Self& other) |
148 { |
148 { |
149 m_data.resize (Size() + other.Size()); |
149 resize (size() + other.size()); |
150 std::copy (other.begin(), other.end(), begin() + other.Size()); |
150 std::copy (other.begin(), other.end(), begin() + other.size()); |
151 } |
151 } |
152 |
152 |
153 // ===================================================================== |
153 // ===================================================================== |
154 // |
154 // |
155 bool Pop (T& val) |
155 bool pop (T& val) |
156 { |
156 { |
157 if (IsEmpty()) |
157 if (isEmpty()) |
158 return false; |
158 return false; |
159 |
159 |
160 val = m_data[Size() - 1]; |
160 val = m_data[size() - 1]; |
161 m_data.erase (m_data.end() - 1); |
161 m_data.erase (m_data.end() - 1); |
162 return true; |
162 return true; |
163 } |
163 } |
164 |
164 |
165 // ===================================================================== |
165 // ===================================================================== |
166 // |
166 // |
167 SelfType& operator<< (const T& value) |
167 Self& operator<< (const T& value) |
168 { |
168 { |
169 Append (value); |
169 append (value); |
170 return *this; |
170 return *this; |
171 } |
171 } |
172 |
172 |
173 // ===================================================================== |
173 // ===================================================================== |
174 // |
174 // |
175 void operator<< (const SelfType& vals) |
175 void operator<< (const Self& vals) |
176 { |
176 { |
177 Merge (vals); |
177 merge (vals); |
178 } |
178 } |
179 |
179 |
180 // ===================================================================== |
180 // ===================================================================== |
181 // |
181 // |
182 bool operator>> (T& value) |
182 bool operator>> (T& value) |
183 { |
183 { |
184 return Pop (value); |
184 return pop (value); |
185 } |
185 } |
186 |
186 |
187 // ===================================================================== |
187 // ===================================================================== |
188 // |
188 // |
189 SelfType Reverse() const |
189 Self reverse() const |
190 { |
190 { |
191 SelfType rev; |
191 Self rev; |
192 |
192 |
193 for (const T & val : *this) |
193 for (const T & val : *this) |
194 val >> rev; |
194 val >> rev; |
195 |
195 |
196 return rev; |
196 return rev; |
197 } |
197 } |
198 |
198 |
199 // ===================================================================== |
199 // ===================================================================== |
200 // |
200 // |
201 void Clear() |
201 void clear() |
202 { |
202 { |
203 m_data.clear(); |
203 m_data.clear(); |
204 } |
204 } |
205 |
205 |
206 // ===================================================================== |
206 // ===================================================================== |
207 // |
207 // |
208 void Insert (int pos, const ValueType& value) |
208 void insert (int pos, const ValueType& value) |
209 { |
209 { |
210 m_data.insert (m_data.begin() + pos, value); |
210 m_data.insert (m_data.begin() + pos, value); |
211 } |
211 } |
212 |
212 |
213 // ===================================================================== |
213 // ===================================================================== |
214 // |
214 // |
215 void RemoveDuplicates() |
215 void removeDuplicates() |
216 { |
216 { |
217 // Remove duplicate entries. For this to be effective, the vector must be |
217 // Remove duplicate entries. For this to be effective, the vector must be |
218 // sorted first. |
218 // sorted first. |
219 Sort(); |
219 sort(); |
220 Iterator pos = std::unique (begin(), end()); |
220 Iterator pos = std::unique (begin(), end()); |
221 Resize (std::distance (begin(), pos)); |
221 resize (std::distance (begin(), pos)); |
222 } |
222 } |
223 |
223 |
224 // ===================================================================== |
224 // ===================================================================== |
225 // |
225 // |
226 int Size() const |
226 int size() const |
227 { |
227 { |
228 return m_data.size(); |
228 return m_data.size(); |
229 } |
229 } |
230 |
230 |
231 // ===================================================================== |
231 // ===================================================================== |
232 // |
232 // |
233 ValueType& operator[] (int n) |
233 ValueType& operator[] (int n) |
234 { |
234 { |
235 assert (n < Size()); |
235 assert (n < size()); |
236 return m_data[n]; |
236 return m_data[n]; |
237 } |
237 } |
238 |
238 |
239 // ===================================================================== |
239 // ===================================================================== |
240 // |
240 // |
241 const ValueType& operator[] (int n) const |
241 const ValueType& operator[] (int n) const |
242 { |
242 { |
243 assert (n < Size()); |
243 assert (n < size()); |
244 return m_data[n]; |
244 return m_data[n]; |
245 } |
245 } |
246 |
246 |
247 // ===================================================================== |
247 // ===================================================================== |
248 // |
248 // |
249 void Resize (int size) |
249 void resize (int size) |
250 { |
250 { |
251 m_data.resize (size); |
251 m_data.resize (size); |
252 } |
252 } |
253 |
253 |
254 // ===================================================================== |
254 // ===================================================================== |
255 // |
255 // |
256 void Sort() |
256 void sort() |
257 { |
257 { |
258 std::sort (begin(), end()); |
258 std::sort (begin(), end()); |
259 } |
259 } |
260 |
260 |
261 // ===================================================================== |
261 // ===================================================================== |
262 // |
262 // |
263 int Find (const ValueType& needle) const |
263 int find (const ValueType& needle) const |
264 { |
264 { |
265 int i = 0; |
265 int i = 0; |
266 |
266 |
267 for (const ValueType & hay : *this) |
267 for (const ValueType & hay : *this) |
268 { |
268 { |
275 return -1; |
275 return -1; |
276 } |
276 } |
277 |
277 |
278 // ===================================================================== |
278 // ===================================================================== |
279 // |
279 // |
280 void Remove (const ValueType& it) |
280 void removeOne (const ValueType& it) |
281 { |
281 { |
282 int idx; |
282 int idx; |
283 |
283 |
284 if ((idx = Find (it)) != -1) |
284 if ((idx = find (it)) != -1) |
285 RemoveAt (idx); |
285 removeAt (idx); |
286 } |
286 } |
287 |
287 |
288 // ===================================================================== |
288 // ===================================================================== |
289 // |
289 // |
290 inline bool IsEmpty() const |
290 inline bool isEmpty() const |
291 { |
291 { |
292 return Size() == 0; |
292 return size() == 0; |
293 } |
293 } |
294 |
294 |
295 // ===================================================================== |
295 // ===================================================================== |
296 // |
296 // |
297 SelfType Mid (int a, int b) const |
297 Self splice (int a, int b) const |
298 { |
298 { |
299 assert (a >= 0 && b >= 0 && a < Size() && b < Size() && a <= b); |
299 assert (a >= 0 && b >= 0 && a < size() && b < size() && a <= b); |
300 SelfType result; |
300 Self result; |
301 |
301 |
302 for (int i = a; i <= b; ++i) |
302 for (int i = a; i <= b; ++i) |
303 result << operator[] (i); |
303 result << operator[] (i); |
304 |
304 |
305 return result; |
305 return result; |
306 } |
306 } |
307 |
307 |
308 // ===================================================================== |
308 // ===================================================================== |
309 // |
309 // |
310 inline const ListType& GetDeque() const |
310 inline const WrappedList& deque() const |
311 { |
311 { |
312 return m_data; |
312 return m_data; |
313 } |
313 } |
314 |
314 |
315 // ===================================================================== |
315 // ===================================================================== |
316 // |
316 // |
317 inline const ValueType& First() const |
317 inline const ValueType& first() const |
318 { |
318 { |
319 return *m_data.begin(); |
319 return *m_data.begin(); |
320 } |
320 } |
321 |
321 |
322 // ===================================================================== |
322 // ===================================================================== |
323 // |
323 // |
324 inline const ValueType& Last() const |
324 inline const ValueType& last() const |
325 { |
325 { |
326 return *(m_data.end() - 1); |
326 return *(m_data.end() - 1); |
327 } |
327 } |
328 |
328 |
329 // ===================================================================== |
329 // ===================================================================== |
330 // |
330 // |
331 inline bool Contains (const ValueType& a) const |
331 inline bool contains (const ValueType& a) const |
332 { |
332 { |
333 return Find (a) != -1; |
333 return find (a) != -1; |
334 } |
334 } |
335 |
335 |
336 // ===================================================================== |
336 // ===================================================================== |
337 // |
337 // |
338 SelfType operator+ (const SelfType& other) const |
338 Self operator+ (const Self& other) const |
339 { |
339 { |
340 SelfType out (*this); |
340 Self out (*this); |
341 out.Merge (other); |
341 out.merge (other); |
342 return out; |
342 return out; |
343 } |
343 } |
344 |
344 |
345 private: |
345 private: |
346 ListType m_data; |
346 WrappedList m_data; |
347 }; |
347 }; |
348 |
348 |
349 // ============================================================================= |
349 // ============================================================================= |
350 // |
350 // |
351 template<class T> |
351 template<typename T> |
352 List<T>& operator>> (const T& value, List<T>& haystack) |
352 List<T>& operator>> (const T& value, List<T>& haystack) |
353 { |
353 { |
354 haystack.push_front (value); |
354 haystack.prepend (value); |
355 return haystack; |
355 return haystack; |
356 } |
356 } |
357 |
357 |
358 #endif // BOTC_CONTAINERS_H |
358 #endif // BOTC_CONTAINERS_H |