23#ifndef FROZEN_LETITGO_BITS_ALGORITHMS_H
24#define FROZEN_LETITGO_BITS_ALGORITHMS_H
37 constexpr auto trip_count = std::numeric_limits<
decltype(v)>::digits;
39 for(std::size_t i = 1; i < trip_count; i <<= 1)
46auto constexpr log(T v) {
56 return (n <= 8*
sizeof(
unsigned int))
57 + (n <= 8*
sizeof(
unsigned long))
58 + (n <= 8*
sizeof(
unsigned long long))
65template<std::
size_t N>
67 static_assert(N < 2,
"unsupported type size");
72template<std::
size_t N>
75template <
typename Iter,
typename Compare>
77 Compare
const &compare) {
79 while (begin != end) {
80 if (compare(*begin, *result)) {
89constexpr void cswap(T &a, T &b) {
95template <
class T,
class U>
96constexpr void cswap(std::pair<T, U> & a, std::pair<T, U> & b) {
97 cswap(a.first, b.first);
98 cswap(a.second, b.second);
101template <
class... Tys, std::size_t... Is>
102constexpr void cswap(std::tuple<Tys...> &a, std::tuple<Tys...> &b, std::index_sequence<Is...>) {
103 using swallow =
int[];
104 (void) swallow{(
cswap(std::get<Is>(a), std::get<Is>(b)), 0)...};
107template <
class... Tys>
108constexpr void cswap(std::tuple<Tys...> &a, std::tuple<Tys...> &b) {
109 cswap(a, b, std::make_index_sequence<
sizeof...(Tys)>());
112template <
typename Iter>
117template <
typename Iterator,
class Compare>
118constexpr Iterator
partition(Iterator left, Iterator right, Compare
const &compare) {
119 auto pivot = left + (right - left) / 2;
122 for (
auto it = left; 0 < right - it; ++it) {
123 if (compare(*it, *pivot)) {
133template <
typename Iterator,
class Compare>
134constexpr void quicksort(Iterator left, Iterator right, Compare
const &compare) {
135 while (0 < right - left) {
138 left = new_pivot + 1;
142template <
typename Container,
class Compare>
144 Compare
const &compare) {
145 Container res = array;
146 quicksort(res.begin(), res.end() - 1, compare);
156 template <
class ForwardIt>
158 std::integral_constant<std::size_t, 0>) {
162 template <
class ForwardIt, std::
size_t N>
164 std::integral_constant<std::size_t, N>) {
165 auto constexpr step = N / 2;
166 static_assert(N/2 == N - N / 2 - 1,
"power of two minus 1");
167 auto it = first + step;
169 return doit_fast(next_it, std::integral_constant<std::size_t, N / 2>{});
172 template <
class ForwardIt, std::
size_t N>
173 inline constexpr ForwardIt
doitfirst(ForwardIt first, std::integral_constant<std::size_t, N>, std::integral_constant<bool, true>) {
174 return doit_fast(first, std::integral_constant<std::size_t, N>{});
177 template <
class ForwardIt, std::
size_t N>
178 inline constexpr ForwardIt
doitfirst(ForwardIt first, std::integral_constant<std::size_t, N>, std::integral_constant<bool, false>) {
180 auto constexpr next_start = next_power / 2 - 1;
181 auto it = first + next_start;
183 auto constexpr next = N - next_start - 1;
187 return doit_fast(first, std::integral_constant<std::size_t, next_start>{});
190 template <
class ForwardIt>
191 inline constexpr ForwardIt
doitfirst(ForwardIt first, std::integral_constant<std::size_t, 1>, std::integral_constant<bool, false>) {
192 return doit_fast(first, std::integral_constant<std::size_t, 1>{});
196template <std::
size_t N,
class ForwardIt,
class T,
class Compare>
197constexpr ForwardIt
lower_bound(ForwardIt first,
const T &value, Compare
const &compare) {
201template <std::
size_t N,
class Compare,
class ForwardIt,
class T>
203 Compare
const &compare) {
205 return (!(where == first + N) && !(compare(value, *where)));
209template<
class InputIt1,
class InputIt2>
210constexpr bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
212 for (; first1 != last1; ++first1, ++first2) {
213 if (!(*first1 == *first2)) {
220template<
class InputIt1,
class InputIt2>
223 for (; (first1 != last1) && (first2 != last2); ++first1, ++first2) {
224 if (*first1 < *first2)
226 if (*first2 < *first1)
229 return (first1 == last1) && (first2 != last2);
Definition algorithms.h:33
constexpr auto min_element(Iter begin, const Iter end, Compare const &compare)
Definition algorithms.h:76
constexpr void iter_swap(Iter a, Iter b)
Definition algorithms.h:113
constexpr bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
Definition algorithms.h:210
unsigned int select_uint_least(std::integral_constant< std::size_t, 4 >)
constexpr ForwardIt lower_bound(ForwardIt first, const T &value, Compare const &compare)
Definition algorithms.h:197
constexpr Iterator partition(Iterator left, Iterator right, Compare const &compare)
Definition algorithms.h:118
auto constexpr log(T v)
Definition algorithms.h:46
auto constexpr next_highest_power_of_two(std::size_t v)
Definition algorithms.h:35
constexpr std::size_t bit_weight(std::size_t n)
Definition algorithms.h:55
constexpr void cswap(T &a, T &b)
Definition algorithms.h:89
decltype(select_uint_least(std::integral_constant< std::size_t, bit_weight(N)>())) select_uint_least_t
Definition algorithms.h:73
constexpr void quicksort(Iterator left, Iterator right, Compare const &compare)
Definition algorithms.h:134
constexpr bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
Definition algorithms.h:221
constexpr bool binary_search(ForwardIt first, const T &value, Compare const &compare)
Definition algorithms.h:202
Definition algorithm.h:30
Definition algorithms.h:150
T const & value_
Definition algorithms.h:151
constexpr ForwardIt doit_fast(ForwardIt first, std::integral_constant< std::size_t, N >)
Definition algorithms.h:163
constexpr ForwardIt doit_fast(ForwardIt first, std::integral_constant< std::size_t, 0 >)
Definition algorithms.h:157
constexpr ForwardIt doitfirst(ForwardIt first, std::integral_constant< std::size_t, N >, std::integral_constant< bool, true >)
Definition algorithms.h:173
Compare const & compare_
Definition algorithms.h:152
constexpr ForwardIt doitfirst(ForwardIt first, std::integral_constant< std::size_t, 1 >, std::integral_constant< bool, false >)
Definition algorithms.h:191
constexpr LowerBound(T const &value, Compare const &compare)
Definition algorithms.h:153
constexpr ForwardIt doitfirst(ForwardIt first, std::integral_constant< std::size_t, N >, std::integral_constant< bool, false >)
Definition algorithms.h:178