mn_utils.hpp
Go to the documentation of this file.
1 
19 #ifndef MINLIB_STL_UTILS_H_
20 #define MINLIB_STL_UTILS_H_
21 
22 #include <string.h>
23 
24 #include "mn_inttokey.hpp"
25 #include "../mn_defines.hpp"
26 #include "../mn_def.hpp"
27 
28 #include "mn_random.hpp"
29 #include "mn_random_lfsr.hpp"
30 #include "mn_ramdom_xorshift.hpp"
31 
32 namespace mn {
33  namespace internal {
34 
35  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
36  void copy_n(const T* first, mn::size_t n, T* result, int_to_type<false>) {
37  const T* last = first + n;
38 
39  while (first != last) {
40  switch (n & 0x3) {
41  case 0: *result++ = *first++;
42  case 3: *result++ = *first++;
43  case 2: *result++ = *first++;
44  case 1: *result++ = *first++;
45  }
46  }
47  }
48 
49  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
50  void copy_n(const T* first, mn::size_t n, T* result, int_to_type<true>) {
51  assert(result >= first + n || result < first);
52  memcpy(result, first, n * sizeof(T));
53  }
54 
55  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
56  void copy(const T* first, const T* last, T* result, int_to_type<false>) {
57  T* localResult = result;
58  while (first != last)
59  *localResult++ = *first++;
60  }
61  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
62  void copy(const T* first, const T* last, T* result, int_to_type<true>) {
63  const mn::size_t n = reinterpret_cast<const char*>(last) - reinterpret_cast<const char*>(first);
64  memcpy(result, first, n);
65  }
66 
67  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
68  inline void move_n(const T* from, mn::size_t n, T* result, int_to_type<false>) {
69  for (int i = int(n) - 1; i >= 0; --i)
70  result[i] = from[i];
71  }
72 
73  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
74  inline void move_n(const T* first, mn::size_t n, T* result, int_to_type<true>) {
75  memmove(result, first, n * sizeof(T));
76  }
77 
78  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
79  inline void move(const T* first, const T* last, T* result, int_to_type<false>) {
80  result += (last - first);
81  while (--last >= first)
82  *(--result) = *last;
83  }
84  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
85  inline void move(const T* first, const T* last, T* result, int_to_type<true>) {
86  const mn::size_t n = reinterpret_cast<uintptr_t>(last) - reinterpret_cast<uintptr_t>(first);
87  memmove(result, first, n);
88  }
89 
90 
91  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
92  void copy_construct_n(const T* first, mn::size_t n, T* result, int_to_type<false>) {
93  for (size_t i = 0; i < n; ++i)
94  new (result + i) T(first[i]);
95  }
96 
97  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
98  void copy_construct_n(const T* first, mn::size_t n, T* result, int_to_type<true>) {
99  assert(result >= first + n || result < first);
100  memcpy(result, first, n * sizeof(T));
101  }
102 
103  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
104  void destruct_n(T* first, mn::size_t n, int_to_type<false>) {
105  sizeof(first);
106  for (size_t i = 0; i < n; ++i)
107  (first + i)->~T();
108  }
109 
110  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
111  inline void destruct_n(T*, mn::size_t, int_to_type<true>) {
112  // Nothing to do, no destructor needed.
113  }
114 
115  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
116  void destruct(T* mem, int_to_type<false>) {
117  sizeof(mem);
118  mem->~T();
119  }
120 
121  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
122  inline void destruct(T*, int_to_type<true>) {
123  // Nothing to do, no destructor needed.
124  }
125 
126  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
127  void construct(T* mem, int_to_type<false>) {
128  new (mem) T();
129  }
130 
131  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
132  inline void construct(T*, int_to_type<true>) {
133  // Nothing to do
134  }
135 
136  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
137  inline void copy_construct(T* mem, const T& orig, int_to_type<false>) {
138  new (mem) T(orig);
139  }
140 
141  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
142  inline void copy_construct(T* mem, const T& orig, int_to_type<true>) {
143  mem[0] = orig;
144  }
145 
146  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
147  void construct_n(T* to, mn::size_t count, int_to_type<false>) {
148  sizeof(to);
149  for (size_t i = 0; i < count; ++i)
150  new (to + i) T();
151  }
152 
153  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
154  inline void construct_n(T*, int, int_to_type<true>) {
155  // trivial ctor, nothing to do.
156  }
157 
158  MN_TEMPLATE_FULL_DECL_TWO(class, TIter, class, TPred)
159  void test_ordering(TIter first, TIter last, const TPred& pred) {
160  sizeof(first); sizeof(last); sizeof(pred);
161  }
162 
163  MN_TEMPLATE_FULL_DECL_THREE(typename, T1, typename, T2, class, TPred)
164  inline bool debug_pred(const TPred& pred, const T1& a, const T2& b) {
165  return pred(a, b);
166  }
167 
168  struct in_place_type_tag {};
170 
171  struct in_place_type {};
172  } // Intarnal
173 
174  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
175  struct less {
176  bool operator()(const T& lhs, const T& rhs) const noexcept {
177  return lhs < rhs;
178  }
179  };
180  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
181  struct greater {
182  bool operator()(const T& lhs, const T& rhs) const noexcept {
183  return lhs > rhs;
184  }
185  };
186  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
187  struct equal_to {
188  bool operator()(const T& lhs, const T& rhs) const noexcept {
189  return lhs == rhs;
190  }
191  };
192  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
193  struct not_equal_to {
194  bool operator()(const T& lhs, const T& rhs) const noexcept {
195  return lhs != rhs;
196  }
197  };
198 
199 
200  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
201  struct greater_equal {
202  bool operator()(const T& lhs, const T& rhs) const noexcept {
203  return lhs >= rhs;
204  }
205  };
206 
207  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
208  struct less_equal {
209  bool operator()(const T& lhs, const T& rhs) const noexcept {
210  return lhs <= rhs;
211  }
212  };
213 
214  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
215  struct negate {
216  T operator()(const T& a) const noexcept {
217  return -a;
218  }
219  };
220 
221  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
222  struct plus {
223  T operator()(const T& a, const T& b) const noexcept {
224  return a + b;
225  }
226  };
227 
228  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
229  struct minus {
230  T operator()(const T& a, const T& b) const noexcept {
231  return a - b;
232  }
233  };
234 
235  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
236  struct multiplies {
237  T operator()(const T& a, const T& b) const noexcept {
238  return a * b;
239  }
240  };
241 
242  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
243  struct divides {
244  T operator()(const T& a, const T& b) const noexcept {
245  return a / b;
246  }
247  };
248 
249  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
250  struct modulus {
251  T operator()(const T& a, const T& b) const noexcept {
252  return a % b;
253  }
254  };
255 
256 
257 
258  MN_TEMPLATE_FULL_DECL_ONE(typename, T)
259  T nexthigher(T k) noexcept {
260  k--;
261  for (unsigned int i=1; i< sizeof(T) * 8; i <<= 1)
262  k |= (k >> i);
263  return k+1;
264  }
265 
266 
267  template <typename T>
268  struct inc {
269  using value_type = T;
270  value_type operator()(value_type x) const { return ++x; }
271  };
272 
273  template <typename T>
274  struct dec {
275  using value_type = T;
276  value_type operator()(value_type x) const { return --x; }
277  };
278 
279  template <typename T, typename R>
280  inline R _reinterpret_cast(T* p) {
281  return static_cast<R>(static_cast<void*>(p));
282  }
283 
284  template <typename T, typename R>
285  inline const R _const_reinterpret_cast(const T* p) {
286  return static_cast<const R>(static_cast<const void*>(p));
287  }
288 
291 
293 }
294 
295 #endif
Pseudro-Random Number Generator based on xor_shift128++ generator.
Definition: mn_ramdom_xorshift.hpp:36
Pseudro-Random Number Generator based on a 32-bit linear-feedback shift register.
Definition: mn_random_lfsr.hpp:36
#define MN_TEMPLATE_FULL_DECL_THREE(d1, t1, d2, t2, d3, t3)
Definition: mn_defines.hpp:27
#define MN_TEMPLATE_FULL_DECL_ONE(d1, t1)
Definition: mn_defines.hpp:25
#define MN_TEMPLATE_FULL_DECL_TWO(d1, t1, d2, t2)
Definition: mn_defines.hpp:26
This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
void construct_n(T *to, mn::size_t count, int_to_type< false >)
Definition: mn_utils.hpp:147
void destruct_n(T *first, mn::size_t n, int_to_type< false >)
Definition: mn_utils.hpp:104
void move_n(const T *from, mn::size_t n, T *result, int_to_type< false >)
Definition: mn_utils.hpp:68
bool debug_pred(const TPred &pred, const T1 &a, const T2 &b)
Definition: mn_utils.hpp:164
void copy_construct(T *mem, const T &orig, int_to_type< false >)
Definition: mn_utils.hpp:137
void copy_n(const T *first, mn::size_t n, T *result, int_to_type< false >)
Definition: mn_utils.hpp:36
void construct(T *mem, int_to_type< false >)
Definition: mn_utils.hpp:127
void move(const T *first, const T *last, T *result, int_to_type< false >)
Definition: mn_utils.hpp:79
void copy(const T *first, const T *last, T *result, int_to_type< false >)
Definition: mn_utils.hpp:56
void copy_construct_n(const T *first, mn::size_t n, T *result, int_to_type< false >)
Definition: mn_utils.hpp:92
void test_ordering(TIter first, TIter last, const TPred &pred)
Definition: mn_utils.hpp:159
void destruct(T *mem, int_to_type< false >)
Definition: mn_utils.hpp:116
Definition: mn_utils.hpp:169
Definition: mn_utils.hpp:171
Definition: mn_utils.hpp:168
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
const R _const_reinterpret_cast(const T *p)
Definition: mn_utils.hpp:285
R _reinterpret_cast(T *p)
Definition: mn_utils.hpp:280
basic_ramdom_xorshift random_xorshift
Definition: mn_utils.hpp:289
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
T nexthigher(T k) noexcept
Definition: mn_utils.hpp:259
Definition: mn_utils.hpp:274
value_type operator()(value_type x) const
Definition: mn_utils.hpp:276
T value_type
Definition: mn_utils.hpp:275
Definition: mn_utils.hpp:243
T operator()(const T &a, const T &b) const noexcept
Definition: mn_utils.hpp:244
Definition: mn_utils.hpp:187
bool operator()(const T &lhs, const T &rhs) const noexcept
Definition: mn_utils.hpp:188
Definition: mn_utils.hpp:201
bool operator()(const T &lhs, const T &rhs) const noexcept
Definition: mn_utils.hpp:202
Definition: mn_utils.hpp:181
bool operator()(const T &lhs, const T &rhs) const noexcept
Definition: mn_utils.hpp:182
Definition: mn_utils.hpp:268
value_type operator()(value_type x) const
Definition: mn_utils.hpp:270
T value_type
Definition: mn_utils.hpp:269
Definition: mn_inttokey.hpp:25
Definition: mn_utils.hpp:208
bool operator()(const T &lhs, const T &rhs) const noexcept
Definition: mn_utils.hpp:209
Definition: mn_utils.hpp:175
bool operator()(const T &lhs, const T &rhs) const noexcept
Definition: mn_utils.hpp:176
Definition: mn_utils.hpp:229
T operator()(const T &a, const T &b) const noexcept
Definition: mn_utils.hpp:230
Definition: mn_utils.hpp:250
T operator()(const T &a, const T &b) const noexcept
Definition: mn_utils.hpp:251
Definition: mn_utils.hpp:236
T operator()(const T &a, const T &b) const noexcept
Definition: mn_utils.hpp:237
Definition: mn_utils.hpp:215
T operator()(const T &a) const noexcept
Definition: mn_utils.hpp:216
Definition: mn_utils.hpp:193
bool operator()(const T &lhs, const T &rhs) const noexcept
Definition: mn_utils.hpp:194
Definition: mn_utils.hpp:222
T operator()(const T &a, const T &b) const noexcept
Definition: mn_utils.hpp:223