sources/list.h

changeset 69
eb4c25284a19
parent 51
481073b016a9
child 71
4f7c2c944637
equal deleted inserted replaced
68:202e74157de5 69:eb4c25284a19
47 using ConstIterator = typename C::const_iterator; 47 using ConstIterator = typename C::const_iterator;
48 using ReverseIterator = typename C::reverse_iterator; 48 using ReverseIterator = typename C::reverse_iterator;
49 using ConstReverseIterator = typename C::const_reverse_iterator; 49 using ConstReverseIterator = typename C::const_reverse_iterator;
50 using Self = Container<T, C>; 50 using Self = Container<T, C>;
51 51
52 Container(); 52 Container(){}
53 Container (int numvalues); 53
54 Container (const C& a); 54 Container (int numvalues) :
55 Container (std::initializer_list<T>&& a); 55 m_container (numvalues) {}
56 56
57 auto append (const T& value) -> T&; 57 Container (const C& other) :
58 auto begin() -> Iterator; 58 m_container (other) {}
59 auto begin() const -> ConstIterator; 59
60 auto clear() -> void; 60 Container (std::initializer_list<T>&& a) :
61 auto contains (const T& a) const -> bool; 61 m_container (a) {}
62 auto crbegin() const -> ConstReverseIterator; 62
63 auto crend() const -> ConstReverseIterator; 63 T& append (const T& value)
64 auto container() const -> const C&; 64 {
65 auto end() -> Iterator; 65 m_container.push_back (value);
66 auto end() const -> ConstIterator; 66 return m_container[m_container.size() - 1];
67 auto find (const T& needle) -> Iterator; 67 }
68 auto find (const T& needle) const -> ConstIterator; 68
69 auto find (Function<bool (T const&)> func) -> Iterator; 69 Iterator begin()
70 auto find (Function<bool (T const&)> func) const -> ConstIterator; 70 {
71 auto first() -> T&; 71 return m_container.begin();
72 auto first() const -> const T&; 72 }
73 auto insert (int pos, const T& value) -> void; 73
74 auto is_empty() const -> bool; 74 ConstIterator begin() const
75 auto last() -> T&; 75 {
76 auto last() const -> const T&; 76 return m_container.cbegin();
77 auto merge (const Self& other) -> void; 77 }
78 auto pop (T& val) -> bool; 78
79 auto prepend (const T& value) -> T&; 79 void clear()
80 auto rbegin() -> ReverseIterator; 80 {
81 auto remove_at (int pos) -> void; 81 m_container.clear();
82 auto remove_duplicates() -> void; 82 }
83 auto remove_one (const T& it) -> void; 83
84 auto rend() -> ReverseIterator; 84 bool contains (const T& a) const
85 auto resize (int size) -> void; 85 {
86 auto reverse() const -> Self; 86 return std::find (m_container.cbegin(), m_container.cend(), a) != m_container.end();
87 auto size() const -> int; 87 }
88 auto sort() -> void; 88
89 auto splice (int a, int b) const -> Self; 89 ConstReverseIterator crbegin() const
90 auto splice (const Range<int>& a) const -> Self; 90 {
91 91 return m_container.crbegin();
92 auto operator<< (const T& value) -> Self&; 92 }
93 auto operator<< (const Self& vals) -> Self&; 93
94 auto operator[] (int n) -> T&; 94 ConstReverseIterator crend() const
95 auto operator[] (int n) const -> const T&; 95 {
96 auto operator[] (Range<int> const& n) const -> Self; 96 return m_container.crbegin();
97 auto operator+ (const Self& other) const -> Self; 97 }
98
99 const C& container() const
100 {
101 return m_container;
102 }
103
104 Iterator end()
105 {
106 return m_container.end();
107 }
108
109 ConstIterator end() const
110 {
111 return m_container.cend();
112 }
113
114 Iterator find (const T& needle)
115 {
116 auto it = std::find (m_container.begin(), m_container.end(), needle);
117
118 if (it == m_container.end())
119 return end();
120
121 return it;
122 }
123
124 ConstIterator find (const T& needle) const
125 {
126 auto it = std::find (m_container.cbegin(), m_container.cend(), needle);
127
128 if (it == m_container.cend())
129 return end();
130
131 return it;
132 }
133
134 Iterator find (Function<bool (T const&)> func)
135 {
136 for (Iterator it = begin(); it != end(); ++it)
137 {
138 if (func (*it))
139 return it;
140 }
141
142 return end();
143 }
144
145 ConstIterator find (Function<bool (T const&)> func) const
146 {
147 for (ConstIterator it = begin(); it != end(); ++it)
148 {
149 if (func (*it))
150 return it;
151 }
152
153 return end();
154 }
155
156 T& first()
157 {
158 return *begin();
159 }
160
161 const T& first() const
162 {
163 return *begin();
164 }
165
166 void insert (int pos, const T& value)
167 {
168 m_container.insert (m_container.begin() + pos, value);
169 }
170
171 bool is_empty() const
172 {
173 return size() == 0;
174 }
175
176 T& last()
177 {
178 return *(end() - 1);
179 }
180
181 const T& last() const
182 {
183 return *(end() - 1);
184 }
185
186 void merge (const Self& other)
187 {
188 int oldsize = size();
189 resize (size() + other.size());
190 std::copy (other.begin(), other.end(), begin() + oldsize);
191 }
192
193 bool pop (T& val)
194 {
195 if (is_empty())
196 return false;
197
198 val = m_container[size() - 1];
199 m_container.erase (m_container.end() - 1);
200 return true;
201 }
202
203 T& prepend (const T& value)
204 {
205 m_container.push_front (value);
206 return m_container[0];
207 }
208
209 ReverseIterator rbegin()
210 {
211 return m_container.rbegin();
212 }
213
214 void remove_at (int pos)
215 {
216 assert (pos < size());
217 m_container.erase (m_container.begin() + pos);
218 }
219
220 void remove_duplicates()
221 {
222 sort();
223 resize (std::distance (begin(), std::unique (begin(), end())));
224 }
225
226 void remove_one (const T& valueToRemove)
227 {
228 auto it = std::find (m_container.begin(), m_container.end(), valueToRemove);
229
230 if (it != m_container.end())
231 m_container.erase (it);
232 }
233
234 ReverseIterator rend()
235 {
236 return m_container.rend();
237 }
238
239 void resize (int size)
240 {
241 m_container.resize (size);
242 }
243
244 Self reverse() const
245 {
246 Self rev;
247 std::copy (rbegin(), rend(), rev.begin());
248 return rev;
249 }
250
251 int size() const
252 {
253 return m_container.size();
254 }
255
256 void sort()
257 {
258 std::sort (begin(), end());
259 }
260
261 Self splice (int a, int b) const
262 {
263 if (a < 0 or b >= size() or b < a)
264 return Self();
265
266 Self result;
267
268 for (int i = a; i <= b; ++i)
269 result << operator[] (i);
270
271 return result;
272 }
273
274 Self splice (const Range<int>& a) const
275 {
276 return splice (a.min(), a.max());
277 }
278
279 Self& operator<< (const T& value)
280 {
281 append (value);
282 return *this;
283 }
284
285 Self& operator<< (const Self& vals)
286 {
287 merge (vals);
288 return *this;
289 }
290
291 T& operator[] (int n)
292 {
293 assert (n < size());
294 return m_container[n];
295 }
296
297 const T& operator[] (int n) const
298 {
299 assert (n < size());
300 return m_container[n];
301 }
302
303 Self operator[] (Range<int> const& n) const
304 {
305 return splice (n);
306 }
307
308 Self operator+ (const Self& other) const
309 {
310 Self out (*this);
311 out.merge (other);
312 return out;
313 }
98 314
99 protected: 315 protected:
100 C m_container; 316 C m_container;
101 }; 317 };
102 318
319 // -------------------------------------------------------------------------------------------------
320 //
103 template<typename T, typename C> 321 template<typename T, typename C>
104 Container<T, C>& operator>> (const T& value, Container<T, C>& haystack); 322 Container<T, C>& operator>> (const T& value, Container<T, C>& haystack)
323 {
324 haystack.prepend (value);
325 return haystack;
326 }
327
328 // -------------------------------------------------------------------------------------------------
329 //
105 330
106 template<typename T> 331 template<typename T>
107 using List = Container<T, std::deque<T>>; 332 using List = Container<T, std::deque<T>>;
333
334 // -------------------------------------------------------------------------------------------------
335 //
108 336
109 template<typename T> 337 template<typename T>
110 class Vector : public Container<T, std::vector<T>> 338 class Vector : public Container<T, std::vector<T>>
111 { 339 {
112 public: 340 public:
113 using Super = Container<T, std::vector<T>>; 341 using Super = Container<T, std::vector<T>>;
114 342 using Super::Container;
115 Vector() {} 343
116 Vector (int numvalues) : Super (numvalues) {} 344 Vector(){}
117 Vector (const Vector<T>& a) : Super (a) {} 345
118 Vector (std::initializer_list<T>&& a) : Super (a) {} 346 Vector (T* data, size_t length) :
119 Vector (T* data, size_t length) : Super (std::vector<T> (data, data + length)) {} 347 Super (std::vector<T> (data, data + length)) {}
120 348
121 auto data() -> T* 349 T* data()
122 { 350 {
123 return Super::m_container.data(); 351 return Super::m_container.data();
124 } 352 }
125 353
126 auto data() const -> const T* 354 const T* data() const
127 { 355 {
128 return Super::m_container.data(); 356 return Super::m_container.data();
129 } 357 }
130 358
131 operator const T*() const 359 operator const T*() const
132 { 360 {
133 return data(); 361 return data();
134 } 362 }
135 }; 363 };
136
137 //
138 // -------------------------------------------------------------------------------------------------
139 //
140 // IMPLEMENTATIONS
141 //
142
143 template<typename T, typename C>
144 Container<T, C>::Container() {}
145
146 // -------------------------------------------------------------------------------------------------
147 //
148 template<typename T, typename C>
149 Container<T, C>::Container (const C& other) :
150 m_container (other) {}
151
152 // -------------------------------------------------------------------------------------------------
153 //
154 template<typename T, typename C>
155 Container<T, C>::Container (std::initializer_list<T> && a) :
156 m_container (a) {}
157
158 // -------------------------------------------------------------------------------------------------
159 //
160 template<typename T, typename C>
161 Container<T, C>::Container (int numvalues) :
162 m_container (numvalues) {}
163
164 // -------------------------------------------------------------------------------------------------
165 //
166 template<typename T, typename C>
167 auto Container<T, C>::begin() -> Iterator
168 {
169 return m_container.begin();
170 }
171
172 // -------------------------------------------------------------------------------------------------
173 //
174 template<typename T, typename C>
175 auto Container<T, C>::begin() const -> ConstIterator
176 {
177 return m_container.cbegin();
178 }
179
180 // -------------------------------------------------------------------------------------------------
181 //
182 template<typename T, typename C>
183 auto Container<T, C>::end() -> Iterator
184 {
185 return m_container.end();
186 }
187
188 // -------------------------------------------------------------------------------------------------
189 //
190 template<typename T, typename C>
191 auto Container<T, C>::end() const -> ConstIterator
192 {
193 return m_container.cend();
194 }
195
196 // -------------------------------------------------------------------------------------------------
197 //
198 template<typename T, typename C>
199 auto Container<T, C>::rbegin() -> ReverseIterator
200 {
201 return m_container.rbegin();
202 }
203
204 // -------------------------------------------------------------------------------------------------
205 //
206 template<typename T, typename C>
207 auto Container<T, C>::crbegin() const -> ConstReverseIterator
208 {
209 return m_container.crbegin();
210 }
211
212 // -------------------------------------------------------------------------------------------------
213 //
214 template<typename T, typename C>
215 auto Container<T, C>::rend() -> ReverseIterator
216 {
217 return m_container.rend();
218 }
219
220 // -------------------------------------------------------------------------------------------------
221 //
222 template<typename T, typename C>
223 auto Container<T, C>::crend() const -> ConstReverseIterator
224 {
225 return m_container.crend();
226 }
227
228 // -------------------------------------------------------------------------------------------------
229 //
230 template<typename T, typename C>
231 auto Container<T, C>::remove_at (int pos) -> void
232 {
233 assert (pos < size());
234 m_container.erase (m_container.begin() + pos);
235 }
236
237 // -------------------------------------------------------------------------------------------------
238 //
239 template<typename T, typename C>
240 auto Container<T, C>::prepend (const T& value) -> T&
241 {
242 m_container.push_front (value);
243 return m_container[0];
244 }
245
246 // -------------------------------------------------------------------------------------------------
247 //
248 template<typename T, typename C>
249 auto Container<T, C>::append (const T& value) -> T&
250 {
251 m_container.push_back (value);
252 return m_container[m_container.size() - 1];
253 }
254
255 // -------------------------------------------------------------------------------------------------
256 //
257 template<typename T, typename C>
258 auto Container<T, C>::merge (const Self& other) -> void
259 {
260 int oldsize = size();
261 resize (size() + other.size());
262 std::copy (other.begin(), other.end(), begin() + oldsize);
263 }
264
265 // -------------------------------------------------------------------------------------------------
266 //
267 template<typename T, typename C>
268 auto Container<T, C>::pop (T& val) -> bool
269 {
270 if (is_empty())
271 return false;
272
273 val = m_container[size() - 1];
274 m_container.erase (m_container.end() - 1);
275 return true;
276 }
277
278 // -------------------------------------------------------------------------------------------------
279 //
280 template<typename T, typename C>
281 auto Container<T, C>::operator<< (const T& value) -> Self&
282 {
283 append (value);
284 return *this;
285 }
286
287 // -------------------------------------------------------------------------------------------------
288 //
289 template<typename T, typename C>
290 auto Container<T, C>::operator<< (const Self& vals) -> Self&
291 {
292 merge (vals);
293 return *this;
294 }
295
296 // -------------------------------------------------------------------------------------------------
297 //
298 template<typename T, typename C>
299 auto Container<T, C>::reverse() const -> Self
300 {
301 Self rev;
302 std::copy (rbegin(), rend(), rev.begin());
303 return rev;
304 }
305
306 // -------------------------------------------------------------------------------------------------
307 //
308 template<typename T, typename C>
309 auto Container<T, C>::clear() -> void
310 {
311 m_container.clear();
312 }
313
314 // -------------------------------------------------------------------------------------------------
315 //
316 template<typename T, typename C>
317 auto Container<T, C>::insert (int pos, const T& value) -> void
318 {
319 m_container.insert (m_container.begin() + pos, value);
320 }
321
322 // -------------------------------------------------------------------------------------------------
323 //
324 template<typename T, typename C>
325 auto Container<T, C>::remove_duplicates() -> void
326 {
327 sort();
328 resize (std::distance (begin(), std::unique (begin(), end())));
329 }
330
331 // -------------------------------------------------------------------------------------------------
332 //
333 template<typename T, typename C>
334 auto Container<T, C>::size() const -> int
335 {
336 return m_container.size();
337 }
338
339 // -------------------------------------------------------------------------------------------------
340 //
341 template<typename T, typename C>
342 auto Container<T, C>::operator[] (int n) -> T&
343 {
344 assert (n < size());
345 return m_container[n];
346 }
347
348 // -------------------------------------------------------------------------------------------------
349 //
350 template<typename T, typename C>
351 auto Container<T, C>::operator[] (int n) const -> const T&
352 {
353 assert (n < size());
354 return m_container[n];
355 }
356
357 // -------------------------------------------------------------------------------------------------
358 //
359 template<typename T, typename C>
360 auto Container<T, C>::operator[] (const Range<int>& n) const -> Self
361 {
362 return splice (n);
363 }
364
365 // -------------------------------------------------------------------------------------------------
366 //
367 template<typename T, typename C>
368 auto Container<T, C>::resize (int size) -> void
369 {
370 m_container.resize (size);
371 }
372
373 // -------------------------------------------------------------------------------------------------
374 //
375 template<typename T, typename C>
376 auto Container<T, C>::sort() -> void
377 {
378 std::sort (begin(), end());
379 }
380
381 // -------------------------------------------------------------------------------------------------
382 //
383 template<typename T, typename C>
384 auto Container<T, C>::find (const T& needle) -> Iterator
385 {
386 auto it = std::find (m_container.begin(), m_container.end(), needle);
387
388 if (it == m_container.end())
389 return end();
390
391 return it;
392 }
393
394 // -------------------------------------------------------------------------------------------------
395 //
396 template<typename T, typename C>
397 auto Container<T, C>::find (const T& needle) const -> ConstIterator
398 {
399 auto it = std::find (m_container.cbegin(), m_container.cend(), needle);
400
401 if (it == m_container.cend())
402 return end();
403
404 return it;
405 }
406
407 // -------------------------------------------------------------------------------------------------
408 //
409 template<typename T, typename C>
410 auto Container<T, C>::find (Function<bool (T const&)> func) -> Iterator
411 {
412 for (Iterator it = begin(); it != end(); ++it)
413 {
414 if (func (*it))
415 return it;
416 }
417
418 return end();
419 }
420
421 // -------------------------------------------------------------------------------------------------
422 //
423 template<typename T, typename C>
424 auto Container<T, C>::find (Function<bool (T const&)> func) const -> ConstIterator
425 {
426 for (ConstIterator it = begin(); it != end(); ++it)
427 {
428 if (func (*it))
429 return it;
430 }
431
432 return end();
433 }
434
435 // -------------------------------------------------------------------------------------------------
436 //
437 template<typename T, typename C>
438 auto Container<T, C>::remove_one (const T& a) -> void
439 {
440 auto it = std::find (m_container.begin(), m_container.end(), a);
441
442 if (it != m_container.end())
443 m_container.erase (it);
444 }
445
446 // -------------------------------------------------------------------------------------------------
447 //
448 template<typename T, typename C>
449 auto Container<T, C>::is_empty() const -> bool
450 {
451 return size() == 0;
452 }
453
454 // -------------------------------------------------------------------------------------------------
455 //
456 template<typename T, typename C>
457 auto Container<T, C>::splice (int a, int b) const -> Self
458 {
459 if (a < 0 or b >= size() or b < a)
460 return Self();
461
462 Self result;
463
464 for (int i = a; i <= b; ++i)
465 result << operator[] (i);
466
467 return result;
468 }
469
470 // -------------------------------------------------------------------------------------------------
471 //
472 template<typename T, typename C>
473 auto Container<T, C>::splice (const Range<int>& a) const -> Self
474 {
475 return splice (a.min(), a.max());
476 }
477
478 // -------------------------------------------------------------------------------------------------
479 //
480 template<typename T, typename C>
481 auto Container<T, C>::container() const -> const C&
482 {
483 return m_container;
484 }
485
486 // -------------------------------------------------------------------------------------------------
487 //
488 template<typename T, typename C>
489 auto Container<T, C>::first() -> T&
490 {
491 return *m_container.begin();
492 }
493
494 // -------------------------------------------------------------------------------------------------
495 //
496 template<typename T, typename C>
497 auto Container<T, C>::first() const -> const T&
498 {
499 return *m_container.cbegin();
500 }
501
502 // -------------------------------------------------------------------------------------------------
503 //
504 template<typename T, typename C>
505 auto Container<T, C>::last() -> T&
506 {
507 return *(m_container.end() - 1);
508 }
509
510 // -------------------------------------------------------------------------------------------------
511 //
512 template<typename T, typename C>
513 auto Container<T, C>::last() const -> const T&
514 {
515 return *(m_container.cend() - 1);
516 }
517
518 // -------------------------------------------------------------------------------------------------
519 //
520 template<typename T, typename C>
521 auto Container<T, C>::contains (const T& a) const -> bool
522 {
523 return std::find (m_container.cbegin(), m_container.cend(), a) != m_container.end();
524 }
525
526 // -------------------------------------------------------------------------------------------------
527 //
528 template<typename T, typename C>
529 auto Container<T, C>::operator+ (const Self& other) const -> Self
530 {
531 Self out (*this);
532 out.merge (other);
533 return out;
534 }
535
536 // -------------------------------------------------------------------------------------------------
537 //
538 template<typename T, typename C>
539 auto operator>> (const T& value, Container<T, C>& haystack) -> Container<T, C>&
540 {
541 haystack.prepend (value);
542 return haystack;
543 }

mercurial