sources/list.h

branch
protocol5
changeset 106
7b156b764d11
parent 88
08ccaf26cffd
child 109
e4966d7e615d
equal deleted inserted replaced
104:a76af67a3a4b 106:7b156b764d11
30 30
31 #pragma once 31 #pragma once
32 #include "basics.h" 32 #include "basics.h"
33 #include <algorithm> 33 #include <algorithm>
34 #include <deque> 34 #include <deque>
35 #include <initializer_list>
36 #include <functional> 35 #include <functional>
37 #include <cassert> 36 #include <cassert>
37 #include <vector>
38 #include "range.h" 38 #include "range.h"
39 BEGIN_ZFC_NAMESPACE
39 40
40 // ------------------------------------------------------------------------------------------------- 41 // -------------------------------------------------------------------------------------------------
41 // 42 //
42 template<typename T, typename C> 43 template<typename T, typename C>
43 class Container 44 class Container
44 { 45 {
45 public: 46 public:
46 using Iterator = typename C::iterator; 47 typedef typename C::iterator Iterator;
47 using ConstIterator = typename C::const_iterator; 48 typedef typename C::const_iterator ConstIterator;
48 using ReverseIterator = typename C::reverse_iterator; 49 typedef typename C::reverse_iterator ReverseIterator;
49 using ConstReverseIterator = typename C::const_reverse_iterator; 50 typedef typename C::const_reverse_iterator ConstReverseIterator;
50 using Self = Container<T, C>; 51 typedef Container<T, C> Self;
51 52
52 Container(){} 53 Container(){}
53 54
54 Container (int numvalues) : 55 Container (int numvalues) :
55 m_container (numvalues) {} 56 m_container (numvalues) {}
56 57
57 Container (const C& other) : 58 Container (const C& other) :
58 m_container (other) {} 59 m_container (other) {}
59 60
60 Container (std::initializer_list<T>&& a) :
61 m_container (a) {}
62
63 T& append (const T& value) 61 T& append (const T& value)
64 { 62 {
65 m_container.push_back (value); 63 m_container.push_back (value);
66 return m_container[m_container.size() - 1]; 64 return m_container[m_container.size() - 1];
67 } 65 }
71 return m_container.begin(); 69 return m_container.begin();
72 } 70 }
73 71
74 ConstIterator begin() const 72 ConstIterator begin() const
75 { 73 {
76 return m_container.cbegin(); 74 return m_container.begin();
77 } 75 }
78 76
79 void clear() 77 void clear()
80 { 78 {
81 m_container.clear(); 79 m_container.clear();
82 } 80 }
83 81
84 bool contains (const T& a) const 82 bool contains (const T& a) const
85 { 83 {
86 return std::find (m_container.cbegin(), m_container.cend(), a) != m_container.end(); 84 return std::find (m_container.begin(), m_container.end(), a) != m_container.end();
87 } 85 }
88 86
89 ConstReverseIterator crbegin() const 87 ConstReverseIterator crbegin() const
90 { 88 {
91 return m_container.crbegin(); 89 return m_container.rbegin();
92 } 90 }
93 91
94 ConstReverseIterator crend() const 92 ConstReverseIterator crend() const
95 { 93 {
96 return m_container.crbegin(); 94 return m_container.rend();
97 } 95 }
98 96
99 const C& container() const 97 const C& container() const
100 { 98 {
101 return m_container; 99 return m_container;
106 return m_container.end(); 104 return m_container.end();
107 } 105 }
108 106
109 ConstIterator end() const 107 ConstIterator end() const
110 { 108 {
111 return m_container.cend(); 109 return m_container.end();
112 } 110 }
113 111
114 Iterator find (const T& needle) 112 Iterator find (const T& needle)
115 { 113 {
116 auto it = std::find (m_container.begin(), m_container.end(), needle); 114 auto it = std::find (m_container.begin(), m_container.end(), needle);
121 return it; 119 return it;
122 } 120 }
123 121
124 ConstIterator find (const T& needle) const 122 ConstIterator find (const T& needle) const
125 { 123 {
126 auto it = std::find (m_container.cbegin(), m_container.cend(), needle); 124 auto it = std::find (m_container.begin(), m_container.end(), needle);
127 125
128 if (it == m_container.cend()) 126 if (it == m_container.end())
129 return end(); 127 return end();
130 128
131 return it; 129 return it;
132 } 130 }
133 131
134 Iterator find (Function<bool (T const&)> func) 132 Iterator find (bool (*func)(T const&))
135 { 133 {
136 for (Iterator it = begin(); it != end(); ++it) 134 for (Iterator it = begin(); it != end(); ++it)
137 { 135 {
138 if (func (*it)) 136 if (func (*it))
139 return it; 137 return it;
140 } 138 }
141 139
142 return end(); 140 return end();
143 } 141 }
144 142
145 ConstIterator find (Function<bool (T const&)> func) const 143 ConstIterator find (bool (*func)(T const&)) const
146 { 144 {
147 for (ConstIterator it = begin(); it != end(); ++it) 145 for (ConstIterator it = begin(); it != end(); ++it)
148 { 146 {
149 if (func (*it)) 147 if (func (*it))
150 return it; 148 return it;
327 325
328 // ------------------------------------------------------------------------------------------------- 326 // -------------------------------------------------------------------------------------------------
329 // 327 //
330 328
331 template<typename T> 329 template<typename T>
332 using List = Container<T, std::deque<T>>; 330 class List : public Container<T, std::deque<T> >
331 {
332 public:
333 typedef Container<T, std::deque<T> > Super;
334
335 List(){}
336
337 List (int numvalues) :
338 Super (numvalues) {}
339
340 List (const Super& other) :
341 Super (other) {}
342 };
333 343
334 // ------------------------------------------------------------------------------------------------- 344 // -------------------------------------------------------------------------------------------------
335 // 345 //
336 346
337 template<typename T> 347 template<typename T>
338 class Vector : public Container<T, std::vector<T>> 348 class Vector : public Container<T, std::vector<T> >
339 { 349 {
340 public: 350 public:
341 using Super = Container<T, std::vector<T>>; 351 typedef Container<T, std::vector<T> > Super;
342 using typename Super::Container;
343 352
344 Vector(){} 353 Vector(){}
345 354
346 Vector (T* data, size_t length) : 355 Vector (T* data, size_t length) :
347 Super (std::vector<T> (data, data + length)) {} 356 Super (std::vector<T> (data, data + length)) {}
359 operator const T*() const 368 operator const T*() const
360 { 369 {
361 return data(); 370 return data();
362 } 371 }
363 }; 372 };
373
374 END_ZFC_NAMESPACE

mercurial