Skip to content

Commit a76ca2f

Browse files
prefer ranges algorithms over iterator-pair std calls (library + tests) (#244)
* use ranges::iota over std::iota for full-range calls Convert the full-range std::iota(begin, end, v) calls to ranges::iota(range, v). This favors the ranges algorithms already used throughout the library and removes an all(...) macro expansion on main. Partial-range calls (e.g. iterator sub-ranges) and std::partial_sum are left as-is: partial_sum has no ranges equivalent, and partial ranges read more clearly with explicit iterators. * use ranges::unique and ranges::partition for full-range calls Convert the remaining full-range std algorithm calls to their ranges:: equivalents: - virtual_tree: unique(all(subset)) -> begin(ranges::unique(subset)) - edge_cd: partition(all(g[u]), ...) -> begin(ranges::partition(g[u], ...)) ranges::unique / ranges::partition return a subrange, so begin(...) yields the iterator the erase / offset logic needs. Partial-range calls (iterator sub-ranges) and std::partial_sum are still left as-is: partial_sum has no ranges equivalent, and partial ranges read more clearly with explicit iterators. * use ranges algorithms in tests for full-range std calls Apply the same "prefer ranges" convention to the test suite: - iota(all(x), v) -> ranges::iota(x, v) - unique(all(x)) -> begin(ranges::unique(x)) (erase / == end idiom) - remove_if(all(x), pred) -> begin(ranges::remove_if(x, pred)) (erase idiom) - next_permutation(all(x)) -> ranges::next_permutation(x).found accumulate(all(x), ...) is left as-is: there is no ranges::accumulate in C++23 (ranges::fold_left would change the idiom), matching how partial_sum is left in the library. * [auto-verifier] verify commit 69ac3da * [auto-verifier] verify commit e6325f1 --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 7bf45aa commit a76ca2f

23 files changed

Lines changed: 73 additions & 73 deletions

.verify-helper/timestamps.remote.json

Lines changed: 44 additions & 44 deletions
Large diffs are not rendered by default.

library/dsu/line_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct line_tree {
2020
vi p, last;
2121
vector<pii> edge;
2222
line_tree(int n): p(n, -1), last(n), edge(n, {-1, -1}) {
23-
iota(all(last), 0);
23+
ranges::iota(last, 0);
2424
}
2525
int size(int u) { return -p[f(u)]; }
2626
int f(int u) { return p[u] < 0 ? u : p[u] = f(p[u]); }

library/graphs/mst.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pair<ll, vi> mst(const vector<array<int, 3>>& w_eds,
1111
int n) {
1212
vi order(sz(w_eds));
13-
iota(all(order), 0);
13+
ranges::iota(order, 0);
1414
ranges::sort(order, {},
1515
[&](int i) { return w_eds[i][2]; });
1616
DSU dsu(n);

library/graphs/strongly_connected_components/offline_incremental_scc.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ vi offline_incremental_scc(vector<array<int, 2>> eds,
1414
int n) {
1515
int m = sz(eds);
1616
vi ids(n, -1), joins(m, m), idx(m), vs(n), scc_id;
17-
iota(all(idx), 0);
17+
ranges::iota(idx, 0);
1818
vector<vi> g;
1919
auto dnc = [&](this auto&& dnc, auto el, auto er, int tl,
2020
int tr) {

library/graphs/uncommon/complement_graph_ccs.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
vi get_complement_graph_ccs(const auto& g) {
1313
int n = sz(g);
1414
vi cc_id(n), unseen(n);
15-
iota(all(unseen), 0);
15+
ranges::iota(unseen, 0);
1616
vector<bool> is_g(n);
1717
for (int cnt = 0; !empty(unseen); cnt++) {
1818
int s = unseen.back();

library/math/prime_sieve/calc_linear_sieve.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! @time O(mx)
33
//! @space O(mx)
44
vi primes, sieve(1001); //!< min prime factor
5-
iota(all(sieve), 0);
5+
ranges::iota(sieve, 0);
66
rep(i, 2, sz(sieve)) {
77
if (sieve[i] == i) primes.push_back(i);
88
for (int prime : primes) {

library/math/prime_sieve/calc_sieve.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! @time O(mx * log(log mx))
33
//! @space O(mx)
44
vi sieve(1001); //!< min prime factor
5-
iota(all(sieve), 0);
5+
ranges::iota(sieve, 0);
66
for (int i = 2; i * i < sz(sieve); i++)
77
if (sieve[i] == i)
88
for (int j = i * i; j < sz(sieve); j += i)

library/strings/longest_common_subsequence/lcs_dp.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
template<class T> struct lcs_dp {
1111
T t;
1212
vi dp;
13-
lcs_dp(const T& t): t(t), dp(sz(t)) { iota(all(dp), 0); }
13+
lcs_dp(const T& t): t(t), dp(sz(t)) { ranges::iota(dp, 0); }
1414
void push_onto_s(int c) {
1515
int v = -1;
1616
rep(i, 0, sz(t)) if (c == t[i] || dp[i] < v)

library/strings/manacher/longest_palindrome_query.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct longest_pal_query {
1111
//! @space O(n log n) for rmq, everything else is O(n)
1212
longest_pal_query(const auto& s): man(manacher(s)) {
1313
vi init(sz(man));
14-
iota(all(init), 0);
14+
ranges::iota(init, 0);
1515
rmq = {init, [&](int i1, int i2) {
1616
return len(i1) < len(i2) ? i2 : i1;
1717
}};

library/strings/suffix_array/suffix_array.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
auto get_sa(const auto& s, int max_num) {
3939
int n = sz(s);
4040
vi sa(n), sa_inv(all(s)), lcp(n - 1);
41-
iota(all(sa), 0);
41+
ranges::iota(sa, 0);
4242
for (int i = 0; i < n; i = max(1, 2 * i)) {
4343
vi y(sa_inv), freq(max_num);
44-
iota(all(sa_inv), n - i);
44+
ranges::iota(sa_inv, n - i);
4545
ranges::copy_if(sa, begin(sa_inv) + i,
4646
[&](int& x) { return (x -= i) >= 0; });
4747
for (int x : y) freq[x]++;

0 commit comments

Comments
 (0)