snippets 0.1.0
Loading...
Searching...
No Matches
sorted_utils.hpp
Go to the documentation of this file.
1#ifndef SORTED_UTILS_H
2#define SORTED_UTILS_H
3
4#include <vector>
5#include <concepts>
6#include <optional>
7#include <functional>
8#include <tuple>
9
10namespace hsc_snippets {
11 template<std::totally_ordered T>
12 int numOfGreaterElements(const std::vector<T> &v, T value)
13 // v should be non-descreasing
14 {
15 auto it = std::lower_bound(v.cbegin(), v.cend(), value,
16 [](const T &value, const T &element) { return value <= element; });
17 if (it == v.cend()) {
18 return 0;
19 }
20 return v.cend() - it;
21 }
22
23 template<std::integral T>
24 // vec must be in ascending order. [start, end)
25 bool containsInRange(const std::vector<T> &vec, T start, T end) {
26 if (vec.empty() || start >= end) {
27 return false;
28 }
29
30 // Lower bound of the range
31 auto lower = std::lower_bound(vec.begin(), vec.end(), start);
32
33 // Check if the lower bound is not beyond the vector and less than 'end'
34 if (lower != vec.end() && *lower < end) {
35 return true;
36 }
37
38 return false;
39 }
40
41 template<typename T, std::integral I = int>
57 std::optional<I> deferred_binary_search(I low,
58 I high,
59 const T &value,
60 auto &&func) {
61 while (low <= high) {
62 I mid = low + (high - low) / 2;
63 T res = func(mid);
64 if (res == value) {
65 return mid;
66 } else if (res < value) {
67 low = mid + 1;
68 } else {
69 high = mid - 1;
70 }
71 }
72
73 return std::nullopt;
74 }
75
76
77 template<typename T, std::integral I = int>
89 I deferred_upper_bound(I low, I high, const T &value, auto &&func, auto &&comp) {
90 I it;
91 I count = high - low + 1;
92 I step;
93
94 while (count > 0) {
95 it = low;
96 step = count / 2;
97 it += step;
98
99 if (comp(value, func(it))) {
100 count = step;
101 } else {
102 low = ++it;
103 count -= step + 1;
104 }
105 }
106
107 return low;
108 }
109
110 template<typename T, std::integral I = int>
120 I deferred_upper_bound(I low, I high, const T &value, auto &&func) {
121 return deferred_upper_bound(low, high, value, func, std::less<T>());
122 }
123
124 template<typename T, std::integral I = int>
136 I deferred_lower_bound(I low, I high, const T &value, auto &&func, auto &&comp) {
137 I it;
138 I count = high - low + 1;
139 I step;
140
141 while (count > 0) {
142 it = low;
143 step = count / 2;
144 it += step;
145
146 if (comp(func(it), value)) {
147 low = ++it;
148 count -= step + 1;
149 } else {
150 count = step;
151 }
152 }
153
154 return low; // Returns the first index where the value is not less than the target value
155 }
156
157 template<typename T, std::integral I = int>
167 I deferred_lower_bound(I low, I high, const T &value, auto &&func) {
168 return deferred_lower_bound(low, high, value, func, std::less<T>());
169 }
170
171
180 template<std::integral T>
181 void sortThree(T &a, T &b, T &c) {
182 if (a > b) {
183 std::swap(a, b);
184 }
185 if (b > c) {
186 std::swap(b, c);
187 }
188 if (a > b) {
189 std::swap(a, b);
190 }
191 }
192
210 static std::tuple<std::function<int(int)>, int> create_compression_mapper(const std::vector<int> &nums) {
211 std::vector<int> compressed = nums;
212 std::sort(compressed.begin(), compressed.end());
213 compressed.erase(std::unique(compressed.begin(), compressed.end()), compressed.end());
214
215 auto func = [compressed](int value) {
216 return std::lower_bound(compressed.begin(), compressed.end(), value) - compressed.begin();
217 };
218
219 int count = compressed.size();
220 return std::make_tuple(func, count);
221 }
222}
223
224#endif // SORTED_UTILS_H
Definition big_integer.hpp:14
std::optional< I > deferred_binary_search(I low, I high, const T &value, auto &&func)
Definition sorted_utils.hpp:57
static std::tuple< std::function< int(int)>, int > create_compression_mapper(const std::vector< int > &nums)
Definition sorted_utils.hpp:210
bool containsInRange(const std::vector< T > &vec, T start, T end)
Definition sorted_utils.hpp:25
I deferred_upper_bound(I low, I high, const T &value, auto &&func, auto &&comp)
Definition sorted_utils.hpp:89
int numOfGreaterElements(const std::vector< T > &v, T value)
Definition sorted_utils.hpp:12
void sortThree(T &a, T &b, T &c)
Definition sorted_utils.hpp:181
I deferred_lower_bound(I low, I high, const T &value, auto &&func, auto &&comp)
Definition sorted_utils.hpp:136