Skip to content

Commit e60f0b1

Browse files
cameroncusterCameron Custerweb-flow
authored
standardize on c++20: ranges/views and <bit> used consistently (#221)
* use c++20 ranges where shorter * [auto-verifier] verify commit 8735283 * use c++20 ranges and views in remaining full-range spots * [auto-verifier] verify commit 53a7fc4 * use c++20 idioms in doc examples: nullptr over NULL, ll() over (ll) cast * Revert "use c++20 idioms in doc examples: nullptr over NULL, ll() over (ll) cast" This reverts commit 77bba36. * use functional cast in hungarian doc example * revert non-strict-win ranges conversions (views::drop/take, ranges::unique) * Revert "revert non-strict-win ranges conversions (views::drop/take, ranges::unique)" This reverts commit 80a247a. * use ranges/views and <bit> consistently: offset ranges via views::drop/take, __lg -> bit_width/bit_floor * suppress cppcheck mismatchingContainerExpression false positive on ranges::unique in virtual_tree * revert subrange-returning ranges algos (unique/remove_if/partition) to iterator form; drop cppcheck suppression --------- Co-authored-by: Cameron Custer <cam@augmentcode.com> Co-authored-by: GitHub <noreply@github.com>
1 parent 9adb9e9 commit e60f0b1

20 files changed

Lines changed: 39 additions & 32 deletions

File tree

library/data_structures_[l,r)/rmq.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ template<class T, class F> struct RMQ {
2323
}
2424
T query(int l, int r) {
2525
assert(l < r);
26-
int lg = __lg(r - l);
26+
int lg = bit_width(r - l + 0u) - 1;
2727
return op(dp[lg][l], dp[lg][r - (1 << lg)]);
2828
}
2929
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22
//! https://codeforces.com/blog/entry/112755
33
int split(int tl, int tr) {
4-
int pw2 = 1 << __lg(tr - tl);
4+
int pw2 = bit_floor(tr - tl + 0u);
55
return min(tl + pw2, tr - pw2 / 2);
66
}

library/data_structures_[l,r)/seg_tree_uncommon/max_right.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
void max_right(int l, int r, auto f) {
22
for (T x = unit; l < r;) {
3-
int u = l + n, v = __lg(min(u & -u, r - l)),
3+
int u = l + n,
4+
v = bit_width(min(u & -u, r - l) + 0u) - 1,
45
m = l + (1 << v);
56
if (T y = op(x, s[u >> v]); f(m, y)) l = m, x = y;
67
else r = m - 1;

library/data_structures_[l,r)/seg_tree_uncommon/min_left.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
void min_left(int l, int r, auto f) {
22
for (T x = unit; l < r;) {
3-
int u = r + n, v = __lg(min(u & -u, r - l)),
3+
int u = r + n,
4+
v = bit_width(min(u & -u, r - l) + 0u) - 1,
45
m = r - (1 << v);
56
if (T y = op(s[(u - 1) >> v], x); f(m, y))
67
r = m, x = y;

library/data_structures_[l,r)/uncommon/deque_op.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ template<class T, class F> struct deq {
3131
void pop_back() {
3232
if (empty(r)) {
3333
vector<T> a(sz(l));
34-
transform(all(l), rbegin(a),
34+
ranges::transform(l, rbegin(a),
3535
[](dt& x) { return x[0]; });
3636
rebuild(a, sz(a) / 2);
3737
}
@@ -44,7 +44,7 @@ template<class T, class F> struct deq {
4444
void pop_front() {
4545
if (empty(l)) {
4646
vector<T> a(sz(r));
47-
transform(all(r), begin(a),
47+
ranges::transform(r, begin(a),
4848
[](dt& x) { return x[0]; });
4949
rebuild(a, (sz(a) + 1) / 2);
5050
}
@@ -58,9 +58,10 @@ template<class T, class F> struct deq {
5858
partial_sum(sz_le + all(a), begin(presum) + sz_le, op);
5959
l.resize(sz_le);
6060
r.resize(sz(a) - sz_le);
61-
transform(begin(a), begin(a) + sz_le, begin(presum),
61+
ranges::transform(a | views::take(sz_le), presum,
6262
rbegin(l), [](T x, T y) { return dt{x, y}; });
63-
transform(sz_le + all(a), begin(presum) + sz_le,
64-
begin(r), [](T x, T y) { return dt{x, y}; });
63+
ranges::transform(a | views::drop(sz_le),
64+
presum | views::drop(sz_le), begin(r),
65+
[](T x, T y) { return dt{x, y}; });
6566
}
6667
};

library/data_structures_[l,r)/uncommon/priority_queue_of_updates.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ template<class DS, class... ARGS> struct pq_updates {
4141
auto it = remove_if(idx + all(upd_st), [&](auto& cur) {
4242
return cur.second->first >= lowest_pri;
4343
});
44-
reverse_copy(all(extra), it);
44+
ranges::reverse_copy(extra, it);
4545
rep(i, idx, sz(upd_st)) ds.undo();
4646
upd_st.pop_back();
4747
mp.erase(prev(end(mp)));

library/data_structures_[l,r]/disjoint_rmq.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ template<class T, class F> struct disjoint_rmq {
2727
}
2828
T query(int l, int r) {
2929
if (l == r) return dp[0][l];
30-
int lg = __lg(l ^ r);
30+
int lg = bit_width((l ^ r) + 0u) - 1;
3131
return op(dp[lg][l], dp[lg][r]);
3232
}
3333
};

library/data_structures_[l,r]/rmq.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ template<class T, class F> struct RMQ {
2323
}
2424
T query(int l, int r) {
2525
assert(l <= r);
26-
int lg = __lg(r - l + 1);
26+
int lg = bit_width(r - l + 1u) - 1;
2727
return op(dp[lg][l], dp[lg][r - (1 << lg) + 1]);
2828
}
2929
};

library/data_structures_[l,r]/seg_tree.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
//! @time O(n + q log n)
1818
//! @space O(n)
1919
int nxt(int& l, int r) {
20-
int u = l, v = __lg(min(l & -l, r - l + 1));
20+
int u = l,
21+
v = bit_width(min(l & -l, r - l + 1) + 0u) - 1;
2122
return l += 1 << v, u >> v;
2223
}
2324
template<class T, class F> struct tree {

library/data_structures_[l,r]/seg_tree_uncommon/max_right.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
void max_right(int l, int r, auto f) {
22
if (T x = s[l + n]; f(l, x))
33
for (l++; l <= r;) {
4-
int u = l + n, v = __lg(min(u & -u, r - l + 1)),
4+
int u = l + n,
5+
v = bit_width(min(u & -u, r - l + 1) + 0u) - 1,
56
m = l + (1 << v) - 1;
67
if (T y = op(x, s[u >> v]); f(m, y))
78
l = m + 1, x = y;

0 commit comments

Comments
 (0)