]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2015-2016. | |
4 | // Distributed under the Boost Software License, Version 1.0. | |
5 | // (See accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt) | |
7 | // | |
8 | // See http://www.boost.org/libs/move for documentation. | |
9 | // | |
10 | ////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef BOOST_MOVE_TEST_ORDER_TYPE_HPP | |
13 | #define BOOST_MOVE_TEST_ORDER_TYPE_HPP | |
14 | ||
15 | #include <boost/config.hpp> | |
b32b8144 | 16 | #include <boost/move/core.hpp> |
92f5a8d4 | 17 | #include <boost/move/detail/iterator_traits.hpp> |
7c673cae FG |
18 | #include <cstddef> |
19 | #include <cstdio> | |
20 | ||
b32b8144 | 21 | struct order_perf_type |
7c673cae FG |
22 | { |
23 | public: | |
24 | std::size_t key; | |
25 | std::size_t val; | |
26 | ||
b32b8144 | 27 | order_perf_type() |
1e59de90 | 28 | : key(), val() |
7c673cae FG |
29 | { |
30 | ++num_elements; | |
31 | } | |
32 | ||
b32b8144 | 33 | order_perf_type(const order_perf_type& other) |
7c673cae FG |
34 | : key(other.key), val(other.val) |
35 | { | |
36 | ++num_elements; | |
37 | ++num_copy; | |
38 | } | |
39 | ||
b32b8144 | 40 | order_perf_type & operator=(const order_perf_type& other) |
7c673cae FG |
41 | { |
42 | ++num_copy; | |
43 | key = other.key; | |
44 | val = other.val; | |
45 | return *this; | |
46 | } | |
47 | ||
b32b8144 | 48 | ~order_perf_type () |
7c673cae FG |
49 | { |
50 | --num_elements; | |
51 | } | |
52 | ||
b32b8144 FG |
53 | static void reset_stats() |
54 | { | |
55 | num_compare=0; | |
56 | num_copy=0; | |
57 | } | |
58 | ||
59 | friend bool operator< (const order_perf_type& left, const order_perf_type& right) | |
60 | { ++num_compare; return left.key < right.key; } | |
61 | ||
7c673cae FG |
62 | static boost::ulong_long_type num_compare; |
63 | static boost::ulong_long_type num_copy; | |
64 | static boost::ulong_long_type num_elements; | |
65 | }; | |
66 | ||
b32b8144 FG |
67 | boost::ulong_long_type order_perf_type::num_compare = 0; |
68 | boost::ulong_long_type order_perf_type::num_copy = 0; | |
69 | boost::ulong_long_type order_perf_type::num_elements = 0; | |
70 | ||
71 | ||
72 | struct order_move_type | |
73 | { | |
74 | BOOST_MOVABLE_BUT_NOT_COPYABLE(order_move_type) | |
75 | ||
76 | public: | |
77 | std::size_t key; | |
78 | std::size_t val; | |
79 | ||
11fdf7f2 TL |
80 | static const std::size_t moved_constr_mark = std::size_t(-1); |
81 | static const std::size_t moved_assign_mark = std::size_t(-2); | |
82 | ||
b32b8144 FG |
83 | order_move_type() |
84 | : key(0u), val(0u) | |
85 | {} | |
86 | ||
87 | order_move_type(BOOST_RV_REF(order_move_type) other) | |
88 | : key(other.key), val(other.val) | |
89 | { | |
90 | other.key = other.val = std::size_t(-1); | |
91 | } | |
92 | ||
93 | order_move_type & operator=(BOOST_RV_REF(order_move_type) other) | |
94 | { | |
95 | key = other.key; | |
96 | val = other.val; | |
97 | other.key = other.val = std::size_t(-2); | |
98 | return *this; | |
99 | } | |
100 | ||
101 | friend bool operator< (const order_move_type& left, const order_move_type& right) | |
102 | { return left.key < right.key; } | |
103 | ||
104 | ~order_move_type () | |
105 | { | |
106 | key = val = std::size_t(-3); | |
107 | } | |
108 | }; | |
7c673cae | 109 | |
7c673cae FG |
110 | struct order_type_less |
111 | { | |
11fdf7f2 TL |
112 | template<class T, class U> |
113 | bool operator()(const T &a, U const &b) const | |
b32b8144 | 114 | { return a < b; } |
7c673cae FG |
115 | }; |
116 | ||
117 | template<class T> | |
118 | inline bool is_order_type_ordered(T *elements, std::size_t element_count, bool stable = true) | |
119 | { | |
120 | for(std::size_t i = 1; i < element_count; ++i){ | |
b32b8144 | 121 | if(order_type_less()(elements[i], elements[i-1])){ |
7c673cae FG |
122 | std::printf("\n Ord KO !!!!"); |
123 | return false; | |
124 | } | |
b32b8144 | 125 | if( stable && !(order_type_less()(elements[i-1], elements[i])) && (elements[i-1].val > elements[i].val) ){ |
7c673cae FG |
126 | std::printf("\n Stb KO !!!! "); |
127 | return false; | |
128 | } | |
129 | } | |
130 | return true; | |
131 | } | |
132 | ||
b32b8144 FG |
133 | namespace boost { |
134 | namespace movelib { | |
135 | namespace detail_adaptive { | |
136 | ||
137 | ||
138 | ||
139 | }}} | |
140 | ||
141 | template<class T> | |
142 | inline bool is_key(T *elements, std::size_t element_count) | |
143 | { | |
144 | for(std::size_t i = 1; i < element_count; ++i){ | |
145 | if(elements[i].key >= element_count){ | |
146 | std::printf("\n Key.key KO !!!!"); | |
147 | return false; | |
148 | } | |
149 | if(elements[i].val != std::size_t(-1)){ | |
150 | std::printf("\n Key.val KO !!!!"); | |
151 | return false; | |
152 | } | |
153 | } | |
154 | return true; | |
155 | } | |
156 | ||
157 | template<class T> | |
158 | inline bool is_buffer(T *elements, std::size_t element_count) | |
159 | { | |
160 | for(std::size_t i = 1; i < element_count; ++i){ | |
161 | if(elements[i].key != std::size_t(-1)){ | |
162 | std::printf("\n Buf.key KO !!!!"); | |
163 | return false; | |
164 | } | |
165 | if(elements[i].val >= element_count){ | |
166 | std::printf("\n Buf.val KO !!!!"); | |
167 | return false; | |
168 | } | |
169 | } | |
170 | return true; | |
171 | } | |
172 | ||
173 | ||
92f5a8d4 TL |
174 | //size_type iterator |
175 | template <class T, class D> | |
176 | class randit | |
177 | { | |
178 | public: | |
179 | typedef std::random_access_iterator_tag iterator_category; | |
180 | typedef T value_type; | |
181 | typedef D difference_type; | |
182 | typedef T* pointer; | |
183 | typedef T& reference; | |
184 | ||
185 | private: | |
186 | T* m_ptr; | |
187 | ||
188 | public: | |
189 | explicit randit(T* ptr) | |
190 | : m_ptr(ptr) | |
191 | {} | |
192 | ||
193 | public: | |
194 | ||
195 | //Constructors | |
196 | randit() | |
197 | : m_ptr() //Value initialization to achieve "null iterators" (N3644) | |
198 | {} | |
199 | ||
200 | randit(const randit& other) | |
201 | : m_ptr(other.m_ptr) | |
202 | {} | |
203 | ||
204 | randit & operator=(const randit& other) | |
205 | { m_ptr = other.m_ptr; return *this; } | |
206 | ||
207 | //T* like operators | |
208 | reference operator*() const | |
209 | { return *m_ptr; } | |
210 | ||
211 | pointer operator->() const | |
212 | { return m_ptr; } | |
213 | ||
214 | reference operator[](difference_type off) const | |
215 | { return m_ptr[off]; } | |
216 | ||
217 | //Increment / Decrement | |
218 | randit& operator++() | |
219 | { ++m_ptr; return *this; } | |
220 | ||
221 | randit operator++(int) | |
222 | { return randit(m_ptr++); } | |
223 | ||
224 | randit& operator--() | |
225 | { --m_ptr; return *this; } | |
226 | ||
227 | randit operator--(int) | |
228 | { return randit(m_ptr--); } | |
229 | ||
230 | //Arithmetic | |
231 | randit& operator+=(difference_type off) | |
232 | { m_ptr += off; return *this; } | |
233 | ||
234 | randit& operator-=(difference_type off) | |
235 | { m_ptr -= off; return *this; } | |
236 | ||
237 | friend randit operator+(const randit &x, difference_type off) | |
238 | { return randit(x.m_ptr+off); } | |
239 | ||
240 | friend randit operator+(difference_type off, randit right) | |
241 | { right.m_ptr += off; return right; } | |
242 | ||
243 | friend randit operator-(randit left, difference_type off) | |
244 | { left.m_ptr -= off; return left; } | |
245 | ||
246 | friend difference_type operator-(const randit &left, const randit& right) | |
247 | { return difference_type(left.m_ptr - right.m_ptr); } | |
248 | ||
249 | //Comparison operators | |
250 | friend bool operator== (const randit& l, const randit& r) | |
251 | { return l.m_ptr == r.m_ptr; } | |
252 | ||
253 | friend bool operator!= (const randit& l, const randit& r) | |
254 | { return l.m_ptr != r.m_ptr; } | |
255 | ||
256 | friend bool operator< (const randit& l, const randit& r) | |
257 | { return l.m_ptr < r.m_ptr; } | |
258 | ||
259 | friend bool operator<= (const randit& l, const randit& r) | |
260 | { return l.m_ptr <= r.m_ptr; } | |
261 | ||
262 | friend bool operator> (const randit& l, const randit& r) | |
263 | { return l.m_ptr > r.m_ptr; } | |
264 | ||
265 | friend bool operator>= (const randit& l, const randit& r) | |
266 | { return l.m_ptr >= r.m_ptr; } | |
267 | }; | |
268 | ||
269 | struct less_int | |
270 | { | |
271 | bool operator()(int l, int r) | |
272 | { return l < r; } | |
273 | }; | |
274 | ||
275 | ||
7c673cae | 276 | #endif //BOOST_MOVE_TEST_ORDER_TYPE_HPP |