21 #ifndef _MINLIB_STL_ALGORITHM_H_
22 #define _MINLIB_STL_ALGORITHM_H_
39 return __builtin_popcount (v);
79 void
copy(const
T* src, const
T* last,
T* dest) {
89 void
move_n(const
T* from,
size_t n,
T* dest) {
90 assert(from != dest || n == 0);
92 if (dest + n >= from && dest < from + n) {
100 inline
void move(const
T* src, const
T* last,
T* dest) {
101 assert(src != dest || src == last);
102 const size_t n =
reinterpret_cast<uintptr_t
>(last) -
reinterpret_cast<uintptr_t
>(src);
103 const unsigned char* destEnd =
reinterpret_cast<const unsigned char*
>(dest) + n;
105 if (destEnd >=
reinterpret_cast<const unsigned char*
>(src) && dest < last) {
112 template <
typename T>
115 memmove (advance_ptr(result,-nBytes), first, nBytes);
135 inline
void fill(
T* src,
T* last, const
T& val) {
136 while (src != last) {
137 *src = val; src = src + 1;
142 inline
void fill_n(
T* src,
size_t n, const
T& val) {
144 while (src != last) {
146 case 0: *src = val; ++src;
147 case 7: *src = val; ++src;
148 case 6: *src = val; ++src;
149 case 5: *src = val; ++src;
150 case 4: *src = val; ++src;
151 case 3: *src = val; ++src;
152 case 2: *src = val; ++src;
153 case 1: *src = val; ++src;
160 constexpr TIter
lower_bound (TIter first, TIter last, const TPred& value) {
162 while (first != last) {
165 if (value < *mid) first = mid + 1;
172 constexpr TIter
lower_bound(TIter src, TIter last, const
T& val, const TPred& pred) {
178 const int halfDist = dist >> 1;
182 src = ++mid, dist -= halfDist + 1;
191 constexpr TIter
upper_bound (TIter first, TIter last, const TPred& value) {
193 while (first != last) {
196 if (value < *mid) last = mid;
197 else first = mid + 1;
203 constexpr TIter
upper_bound(TIter src, TIter last, const
T& val, const TPred& pred) {
209 const int halfDist = dist >> 1;
213 src = ++mid, dist -= halfDist + 1;
220 template <
typename TIter,
typename TComp>
221 inline constexpr
bool binary_search (TIter first, TIter last,
const TComp& value) {
223 return found != last && !(value < *found);
230 TIter
find(TIter src, TIter last, const
T& val) {
231 while (src != last) {
232 if ((*src) == val)
return src;
239 TIter
find_if(TIter src, TIter last, const
T& val, const TPred& pred) {
240 while (src != last) {
250 while (src != last) {
257 return t >=
T(0) ? t : -t;
260 inline int abs(
const int& x) {
261 const int y = x >> 31;
265 inline short abs(
const short& x) {
266 const short y = x >> 15;
273 inline bool islower(
char ch) {
return is_range<char>(ch, 0x61, 0x7A); }
275 inline bool isupper(
char ch) {
return is_range<char>(ch, 0x41, 0x5A); }
279 inline bool isdigit(
char ch) {
return is_range<char>(ch, 0x30, 0x39); }
281 inline bool iscntrl(
char ch) {
return is_range<char>(ch, 0x00, 0x1F) || ch == 0x7F; }
283 inline bool isspace(
char ch) {
return is_range<char>(ch, 0x09, 0x0D) || ch == 0x20; }
285 inline bool isblank(
char ch) {
return ch == 0x09 || ch == 0x20; }
287 inline bool isgraph(
char ch) {
return is_range<char>(ch, 0x21, 0x7E); }
291 inline bool isxdigit(
char ch){
return isdigit(ch) || is_range<char>(ch, 0x41, 0x46) || is_range<char>(ch, 0x61, 0x66); }
303 return x > y ? x : y;
308 return x < y ? x : y;
312 void
swap(TAssignable& a, TAssignable& b) {
320 inline
void swap(TAssignable (&x)[N], TAssignable (&y)[N]) {
321 for (
size_t i = 0; i < N; i++)
332 for (; a != b; ++a, ++c)
337 template <
typename TRandomAccessIterator,
typename TRandFunc>
338 void random_shuffle (TRandomAccessIterator first, TRandomAccessIterator last,
const TRandFunc& func) {
339 for (; first != last; ++ first)
345 enum { size =
sizeof(
T) * 8 };
349 enum { size =
sizeof(
T) };
#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
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
char hex2char(char ch)
Definition: mn_algorithm.hpp:295
bool islower(char ch)
Definition: mn_algorithm.hpp:273
void fill_n(T *src, size_t n, const T &val)
Definition: mn_algorithm.hpp:142
void construct_n(T *src, size_t n)
Definition: mn_algorithm.hpp:127
bool isdigit(char ch)
Definition: mn_algorithm.hpp:279
bool isspace(char ch)
Definition: mn_algorithm.hpp:283
void iter_swap(fIt1 a, fIt2 b)
Definition: mn_algorithm.hpp:326
void destruct(T *mem)
Definition: mn_algorithm.hpp:61
void move_n(const T *from, size_t n, T *dest)
Definition: mn_algorithm.hpp:89
constexpr bool binary_search(TIter first, TIter last, const TComp &value)
Definition: mn_algorithm.hpp:221
T * copy_backward(const T *first, const T *last, T *result) noexcept
Definition: mn_algorithm.hpp:113
void destruct_n(T *src, size_t n)
Definition: mn_algorithm.hpp:131
T max(const T x, const T y)
Definition: mn_algorithm.hpp:302
size_t popcount(uint32_t v)
Definition: mn_algorithm.hpp:38
TIter find(TIter src, TIter last, const T &val)
Definition: mn_algorithm.hpp:230
constexpr iterator_traits< TIter >::difference_type distance(TIter first, TIter last)
Definition: mn_iterator.hpp:100
T min(const T x, const T y)
Definition: mn_algorithm.hpp:307
bool isgraph(char ch)
Definition: mn_algorithm.hpp:287
bool is_range(const T ch, const T min, const T max)
Definition: mn_algorithm.hpp:271
bool isalnum(char ch)
Definition: mn_algorithm.hpp:293
bool isxdigit(char ch)
Definition: mn_algorithm.hpp:291
void accumulate(TIter src, TIter last, T &dest)
Definition: mn_algorithm.hpp:249
bool isalpha(char ch)
Definition: mn_algorithm.hpp:277
TIter find_if(TIter src, TIter last, const T &val, const TPred &pred)
Definition: mn_algorithm.hpp:239
bool isprint(char ch)
Definition: mn_algorithm.hpp:289
void swap(TAssignable &a, TAssignable &b)
Definition: mn_algorithm.hpp:312
bool isblank(char ch)
Definition: mn_algorithm.hpp:285
void copy_construct(T *mem, const T &orig)
Definition: mn_algorithm.hpp:45
fIt2 swap_ranges(fIt1 a, fIt1 b, fIt2 c)
Definition: mn_algorithm.hpp:331
void random_shuffle(TRandomAccessIterator first, TRandomAccessIterator last, const TRandFunc &func)
Definition: mn_algorithm.hpp:338
constexpr TIter upper_bound(TIter first, TIter last, const TPred &value)
Definition: mn_algorithm.hpp:191
TFn foreach(TIter src, TIter last, TFn fn)
Definition: mn_algorithm.hpp:119
void copy_construct_n(T *src, size_t n, T *dest)
Definition: mn_algorithm.hpp:84
void copy(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:79
T abs(const T &t)
Definition: mn_algorithm.hpp:256
void fill(T *src, T *last, const T &val)
Definition: mn_algorithm.hpp:135
constexpr void advance(TIter &iter, TDistance d)
Definition: mn_iterator.hpp:105
void construct(T *mem)
Definition: mn_algorithm.hpp:55
constexpr TIter lower_bound(TIter first, TIter last, const TPred &value)
Definition: mn_algorithm.hpp:160
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100
bool isupper(char ch)
Definition: mn_algorithm.hpp:275
void copy_n(const T *src, size_t n, T *dest)
Copy N elements from src to dest.
Definition: mn_algorithm.hpp:74
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
bool iscntrl(char ch)
Definition: mn_algorithm.hpp:281
Definition: mn_typetraits.hpp:361
Definition: mn_typetraits.hpp:365
Definition: mn_typetraits.hpp:373
Definition: mn_inttokey.hpp:25
Definition: mn_utils.hpp:175
Definition: mn_algorithm.hpp:348
Definition: mn_algorithm.hpp:344