From 56d1bf495f9949313f9b7c71083b279093456ac1 Mon Sep 17 00:00:00 2001 From: Tristan Brindle Date: Thu, 22 Aug 2024 15:45:50 +0100 Subject: [PATCH] Use the new arithmetic operations everywhere --- include/flux/core/default_impls.hpp | 24 ++++++++++++------------ include/flux/op/adjacent.hpp | 2 +- include/flux/op/cartesian_base.hpp | 16 ++++++++-------- include/flux/op/chunk.hpp | 4 ++-- include/flux/op/compare.hpp | 2 +- include/flux/op/cycle.hpp | 10 +++++----- include/flux/op/detail/pdqsort.hpp | 4 ++-- include/flux/op/drop.hpp | 2 +- include/flux/op/reverse.hpp | 2 +- include/flux/op/scan.hpp | 2 +- include/flux/op/slide.hpp | 10 +++++----- include/flux/op/stride.hpp | 14 ++++++++------ include/flux/op/take.hpp | 12 ++++++------ include/flux/source/array_ptr.hpp | 6 +++--- include/flux/source/range.hpp | 2 +- 15 files changed, 57 insertions(+), 55 deletions(-) diff --git a/include/flux/core/default_impls.hpp b/include/flux/core/default_impls.hpp index 063d85d3..09fb0dd0 100644 --- a/include/flux/core/default_impls.hpp +++ b/include/flux/core/default_impls.hpp @@ -38,7 +38,7 @@ struct sequence_traits : default_sequence_traits { static constexpr auto inc(auto const&, index_t& idx) { FLUX_DEBUG_ASSERT(idx < N); - idx = num::checked_add(idx, distance_t{1}); + idx = num::add(idx, distance_t{1}); } static constexpr auto last(auto const&) -> index_t { return N; } @@ -46,19 +46,19 @@ struct sequence_traits : default_sequence_traits { static constexpr auto dec(auto const&, index_t& idx) { FLUX_DEBUG_ASSERT(idx > 0); - idx = num::checked_sub(idx, distance_t{1}); + idx = num::sub(idx, distance_t{1}); } static constexpr auto inc(auto const&, index_t& idx, distance_t offset) { - FLUX_DEBUG_ASSERT(num::checked_add(idx, offset) <= N); - FLUX_DEBUG_ASSERT(num::checked_add(idx, offset) >= 0); - idx = num::checked_add(idx, offset); + FLUX_DEBUG_ASSERT(num::add(idx, offset) <= N); + FLUX_DEBUG_ASSERT(num::add(idx, offset) >= 0); + idx = num::add(idx, offset); } static constexpr auto distance(auto const&, index_t from, index_t to) -> distance_t { - return num::checked_sub(to, from); + return num::sub(to, from); } static constexpr auto data(auto& self) -> auto* { return self; } @@ -186,7 +186,7 @@ struct sequence_traits : default_sequence_traits { static constexpr auto inc(auto& self, index_t& idx) { FLUX_DEBUG_ASSERT(idx < size(self)); - idx = num::checked_add(idx, distance_t{1}); + idx = num::add(idx, distance_t{1}); } static constexpr auto read_at(auto& self, index_t idx) -> decltype(auto) @@ -203,21 +203,21 @@ struct sequence_traits : default_sequence_traits { static constexpr auto dec(auto&, index_t& idx) { FLUX_DEBUG_ASSERT(idx > 0); - idx = num::checked_sub(idx, distance_t{1}); + idx = num::sub(idx, distance_t{1}); } static constexpr auto last(auto& self) -> index_t { return size(self); } static constexpr auto inc(auto& self, index_t& idx, distance_t offset) { - FLUX_DEBUG_ASSERT(num::checked_add(idx, offset) <= size(self)); - FLUX_DEBUG_ASSERT(num::checked_add(idx, offset) >= 0); - idx = num::checked_add(idx, offset); + FLUX_DEBUG_ASSERT(num::add(idx, offset) <= size(self)); + FLUX_DEBUG_ASSERT(num::add(idx, offset) >= 0); + idx = num::add(idx, offset); } static constexpr auto distance(auto&, index_t from, index_t to) -> distance_t { - return num::checked_sub(to, from); + return num::sub(to, from); } static constexpr auto size(auto& self) -> distance_t diff --git a/include/flux/op/adjacent.hpp b/include/flux/op/adjacent.hpp index ab55672f..1c65ab8d 100644 --- a/include/flux/op/adjacent.hpp +++ b/include/flux/op/adjacent.hpp @@ -112,7 +112,7 @@ struct adjacent_sequence_traits_base : default_sequence_traits { requires sized_sequence { auto s = (flux::size(self.base_) - N) + 1; - return (std::max)(s, distance_t{0}); + return (cmp::max)(s, distance_t{0}); } }; diff --git a/include/flux/op/cartesian_base.hpp b/include/flux/op/cartesian_base.hpp index e3faa9cc..1b32f99d 100644 --- a/include/flux/op/cartesian_base.hpp +++ b/include/flux/op/cartesian_base.hpp @@ -17,7 +17,7 @@ inline constexpr auto checked_pow = { T res{1}; for(U i{0}; i < exponent; i++) { - res = num::checked_mul(res, base, loc); + res = num::mul(res, base, loc); } return res; }; @@ -116,18 +116,18 @@ struct cartesian_traits_base_impl : default_sequence_traits { auto& base = get_base(self); const auto this_index = flux::distance(base, flux::first(base), std::get(cur)); - auto new_index = num::checked_add(this_index, offset); + auto new_index = num::add(this_index, offset); auto this_size = flux::size(base); // If the new index overflows the maximum or underflows zero, calculate the carryover and fix it. if (new_index < 0 || new_index >= this_size) { - offset = num::checked_div(new_index, this_size); - new_index = num::checked_mod(new_index, this_size); + offset = num::div(new_index, this_size); + new_index = num::mod(new_index, this_size); // Correct for negative index which may happen when underflowing. if (new_index < 0) { - new_index = num::checked_add(new_index, this_size); - offset = num::checked_sub(offset, flux::distance_t(1)); + new_index = num::add(new_index, this_size); + offset = num::sub(offset, flux::distance_t(1)); } // Call the next level down if necessary. @@ -138,7 +138,7 @@ struct cartesian_traits_base_impl : default_sequence_traits { } } - flux::inc(base, std::get(cur), num::checked_sub(new_index, this_index)); + flux::inc(base, std::get(cur), num::sub(new_index, this_index)); return cur; } @@ -235,7 +235,7 @@ struct cartesian_traits_base_impl : default_sequence_traits { { return std::apply([](auto& base0, auto&... bases) { distance_t sz = flux::size(base0); - ((sz = num::checked_mul(sz, flux::size(bases))), ...); + ((sz = num::mul(sz, flux::size(bases))), ...); return sz; }, self.bases_); } diff --git a/include/flux/op/chunk.hpp b/include/flux/op/chunk.hpp index 5a6d5d47..b1dadac6 100644 --- a/include/flux/op/chunk.hpp +++ b/include/flux/op/chunk.hpp @@ -286,9 +286,9 @@ struct chunk_adaptor : inline_sequence_base> { requires random_access_sequence { if (offset > 0) { - cur.missing = advance(self.base_, cur.cur, num::checked_mul(offset, self.chunk_sz_)) % self.chunk_sz_; + cur.missing = advance(self.base_, cur.cur, num::mul(offset, self.chunk_sz_)) % self.chunk_sz_; } else if (offset < 0) { - advance(self.base_, cur.cur, num::checked_add(num::checked_mul(offset, self.chunk_sz_), cur.missing)); + advance(self.base_, cur.cur, num::add(num::mul(offset, self.chunk_sz_), cur.missing)); cur.missing = 0; } } diff --git a/include/flux/op/compare.hpp b/include/flux/op/compare.hpp index e92a8327..a1b7bd3e 100644 --- a/include/flux/op/compare.hpp +++ b/include/flux/op/compare.hpp @@ -63,7 +63,7 @@ struct compare_fn { } else { auto const seq1_size = flux::usize(seq1); auto const seq2_size = flux::usize(seq2); - auto min_size = std::min(seq1_size, seq2_size); + auto min_size = (cmp::min)(seq1_size, seq2_size); int cmp_result = 0; if(min_size > 0) { diff --git a/include/flux/op/cycle.hpp b/include/flux/op/cycle.hpp index 3b1d6b82..5752ca88 100644 --- a/include/flux/op/cycle.hpp +++ b/include/flux/op/cycle.hpp @@ -168,7 +168,7 @@ struct cycle_adaptor : inline_sequence_base> { } auto off = flux::distance(self.base_, first, cur.base_cur); - off = num::checked_add(off, offset); + off = num::add(off, offset); cur.n += static_cast(off/sz); @@ -187,8 +187,8 @@ struct cycle_adaptor : inline_sequence_base> { sized_sequence { auto dist = num::checked_cast(to.n) - num::checked_cast(from.n); - dist = num::checked_mul(dist, flux::size(self.base_)); - return num::checked_add(dist, + dist = num::mul(dist, flux::size(self.base_)); + return num::add(dist, flux::distance(self.base_, from.base_cur, to.base_cur)); } @@ -203,8 +203,8 @@ struct cycle_adaptor : inline_sequence_base> { static constexpr auto size(auto& self) -> distance_t requires (!IsInfinite && sized_sequence) { - return num::checked_mul(flux::size(self.base_), - num::checked_cast(self.data_.count)); + return num::mul(flux::size(self.base_), + num::checked_cast(self.data_.count)); } }; }; diff --git a/include/flux/op/detail/pdqsort.hpp b/include/flux/op/detail/pdqsort.hpp index 0ba254bb..8eef39d2 100644 --- a/include/flux/op/detail/pdqsort.hpp +++ b/include/flux/op/detail/pdqsort.hpp @@ -312,7 +312,7 @@ partition_right_branchless(Seq& seq, Cur const begin, Cur const end, Comp& comp) } // Swap elements and update block sizes and first/last boundaries. - int num = (std::min)(num_l, num_r); + int num = (cmp::min)(num_l, num_r); swap_offsets(seq, first, last, offsets_l + start_l, offsets_r + start_r, num, num_l == num_r); num_l -= num; @@ -361,7 +361,7 @@ partition_right_branchless(Seq& seq, Cur const begin, Cur const end, Comp& comp) } } - int num = (std::min)(num_l, num_r); + int num = (cmp::min)(num_l, num_r); swap_offsets(seq, first, last, offsets_l + start_l, offsets_r + start_r, num, num_l == num_r); num_l -= num; diff --git a/include/flux/op/drop.hpp b/include/flux/op/drop.hpp index a763b1b5..d47d3123 100644 --- a/include/flux/op/drop.hpp +++ b/include/flux/op/drop.hpp @@ -44,7 +44,7 @@ struct drop_adaptor : inline_sequence_base> { static constexpr auto size(auto& self) requires sized_sequence { - return (cmp::max)(num::checked_sub(flux::size(self.base()), self.count_), + return (cmp::max)(num::sub(flux::size(self.base()), self.count_), distance_t{0}); } diff --git a/include/flux/op/reverse.hpp b/include/flux/op/reverse.hpp index 2ad036a2..006991d6 100644 --- a/include/flux/op/reverse.hpp +++ b/include/flux/op/reverse.hpp @@ -103,7 +103,7 @@ struct reverse_adaptor : inline_sequence_base> static constexpr auto inc(auto& self, cursor_type& cur, distance_t dist) -> void { - flux::inc(self.base_, cur.base_cur, num::checked_sub(distance_t{}, dist)); + flux::inc(self.base_, cur.base_cur, num::sub(distance_t{}, dist)); } static constexpr auto distance(auto& self, cursor_type const& from, cursor_type const& to) diff --git a/include/flux/op/scan.hpp b/include/flux/op/scan.hpp index 2de2668b..0742e36f 100644 --- a/include/flux/op/scan.hpp +++ b/include/flux/op/scan.hpp @@ -132,7 +132,7 @@ struct scan_adaptor : inline_sequence_base> { requires sized_sequence { if constexpr (Mode == scan_mode::exclusive) { - return num::checked_add(flux::size(self.base_), distance_t{1}); + return num::add(flux::size(self.base_), distance_t{1}); } else { return flux::size(self.base_); } diff --git a/include/flux/op/slide.hpp b/include/flux/op/slide.hpp index d1177856..a49e00f3 100644 --- a/include/flux/op/slide.hpp +++ b/include/flux/op/slide.hpp @@ -50,7 +50,7 @@ struct slide_adaptor : inline_sequence_base> { static constexpr auto first(auto& self) -> cursor_type { auto cur = flux::first(self.base_); auto end = cur; - advance(self.base_, end, self.win_sz_ - 1); + advance(self.base_, end, num::sub(self.win_sz_, distance_t{1})); return cursor_type{.from = std::move(cur), .to = std::move(end)}; } @@ -77,7 +77,7 @@ struct slide_adaptor : inline_sequence_base> { { auto end = flux::last(self.base_); auto cur = end; - advance(self.base_, cur, 1 - self.win_sz_); + advance(self.base_, cur, num::sub(distance_t{1}, self.win_sz_)); return cursor_type{.from = std::move(cur), .to = std::move(end)}; } @@ -105,8 +105,8 @@ struct slide_adaptor : inline_sequence_base> { static constexpr auto size(auto& self) -> distance_t requires sized_sequence { - auto s = (flux::size(self.base_) - self.win_sz_) + 1; - return std::max(s, distance_t{0}); + auto s = num::add(num::sub(flux::size(self.base_), self.win_sz_), distance_t{1}); + return (cmp::max)(s, distance_t{0}); } }; }; @@ -132,7 +132,7 @@ constexpr auto inline_sequence_base::slide(std::integral auto win_sz) && requires multipass_sequence { FLUX_ASSERT(win_sz > 0); - return flux::slide(std::move(derived()), std::move(win_sz)); + return flux::slide(std::move(derived()), num::checked_cast(win_sz)); } } // namespace slide diff --git a/include/flux/op/stride.hpp b/include/flux/op/stride.hpp index c0fe003e..5045773f 100644 --- a/include/flux/op/stride.hpp +++ b/include/flux/op/stride.hpp @@ -47,13 +47,14 @@ inline constexpr struct advance_fn { constexpr auto operator()(Seq& seq, cursor_t& cur, distance_t offset) const -> distance_t { if (offset > 0) { - auto dist = std::min(flux::distance(seq, cur, flux::last(seq)), offset); + auto dist = (cmp::min)(flux::distance(seq, cur, flux::last(seq)), offset); flux::inc(seq, cur, dist); - return offset - dist; + return num::sub(offset, dist); } else if (offset < 0) { - auto dist = -std::min(flux::distance(seq, flux::first(seq), cur), -offset); + auto dist = num::sub(distance_t{0}, + (cmp::min)(flux::distance(seq, flux::first(seq), cur), -offset)); flux::inc(seq, cur, dist); - return offset - dist; + return num::sub(offset, dist); } else { return 0; } @@ -225,9 +226,10 @@ struct stride_adaptor : inline_sequence_base> { requires random_access_sequence { if (offset > 0) { - cur.missing = advance(self.base_, cur.cur, num::checked_mul(offset, self.stride_)) % self.stride_; + cur.missing = num::mod(advance(self.base_, cur.cur, num::mul(offset, self.stride_)), + self.stride_); } else if (offset < 0) { - advance(self.base_, cur.cur, num::checked_add(num::checked_mul(offset, self.stride_), cur.missing)); + advance(self.base_, cur.cur, num::add(num::mul(offset, self.stride_), cur.missing)); cur.missing = 0; } } diff --git a/include/flux/op/take.hpp b/include/flux/op/take.hpp index 8234e9e9..44b52f79 100644 --- a/include/flux/op/take.hpp +++ b/include/flux/op/take.hpp @@ -57,7 +57,7 @@ struct take_adaptor : inline_sequence_base> static constexpr auto inc(auto& self, cursor_type& cur) { flux::inc(self.base_, cur.base_cur); - cur.length = num::checked_sub(cur.length, distance_t{1}); + cur.length = num::sub(cur.length, distance_t{1}); } static constexpr auto read_at(auto& self, cursor_type const& cur) @@ -88,22 +88,22 @@ struct take_adaptor : inline_sequence_base> requires bidirectional_sequence { flux::dec(self.base_, cur.base_cur); - cur.length = num::checked_add(cur.length, distance_t{1}); + cur.length = num::add(cur.length, distance_t{1}); } static constexpr auto inc(auto& self, cursor_type& cur, distance_t offset) requires random_access_sequence { flux::inc(self.base_, cur.base_cur, offset); - cur.length = num::checked_sub(cur.length, offset); + cur.length = num::sub(cur.length, offset); } static constexpr auto distance(auto& self, cursor_type const& from, cursor_type const& to) -> distance_t requires random_access_sequence { - return (std::min)(flux::distance(self.base_, from.base_cur, to.base_cur), - num::checked_sub(from.length, to.length)); + return (cmp::min)(flux::distance(self.base_, from.base_cur, to.base_cur), + num::sub(from.length, to.length)); } static constexpr auto data(auto& self) @@ -119,7 +119,7 @@ struct take_adaptor : inline_sequence_base> if constexpr (infinite_sequence) { return self.count_; } else { - return (std::min)(flux::size(self.base_), self.count_); + return (cmp::min)(flux::size(self.base_), self.count_); } } diff --git a/include/flux/source/array_ptr.hpp b/include/flux/source/array_ptr.hpp index ed4ae9e9..789658d7 100644 --- a/include/flux/source/array_ptr.hpp +++ b/include/flux/source/array_ptr.hpp @@ -90,7 +90,7 @@ struct array_ptr : inline_sequence_base> { static constexpr auto inc(array_ptr const& self, index_t& idx) -> void { FLUX_DEBUG_ASSERT(idx < self.sz_); - idx = num::checked_add(idx, distance_t{1}); + idx = num::add(idx, distance_t{1}); } static constexpr auto read_at(array_ptr const& self, index_t idx) -> T& @@ -115,7 +115,7 @@ struct array_ptr : inline_sequence_base> { static constexpr auto inc(array_ptr const& self, index_t& idx, distance_t offset) -> void { - index_t nxt = num::checked_add(idx, offset); + index_t nxt = num::add(idx, offset); FLUX_DEBUG_ASSERT(nxt >= 0); FLUX_DEBUG_ASSERT(nxt <= self.sz_); idx = nxt; @@ -124,7 +124,7 @@ struct array_ptr : inline_sequence_base> { static constexpr auto distance(array_ptr const&, index_t from, index_t to) -> distance_t { - return num::checked_sub(to, from); + return num::sub(to, from); } static constexpr auto size(array_ptr const& self) -> distance_t diff --git a/include/flux/source/range.hpp b/include/flux/source/range.hpp index e3d9e6b2..bf2479c0 100644 --- a/include/flux/source/range.hpp +++ b/include/flux/source/range.hpp @@ -117,7 +117,7 @@ struct range_sequence : inline_sequence_base> { requires std::ranges::random_access_range { if (offset < 0) { - bounds_check(num::checked_add(offset, distance(self, first(self), cur)) >= 0); + bounds_check(num::add(offset, distance(self, first(self), cur)) >= 0); } else if (offset > 0) { bounds_check(offset < distance(self, cur, last(self))); }