Skip to content

Commit

Permalink
Use the new arithmetic operations everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
tcbrindle committed Aug 22, 2024
1 parent 7982c1e commit 56d1bf4
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 55 deletions.
24 changes: 12 additions & 12 deletions include/flux/core/default_impls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ struct sequence_traits<T[N]> : 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; }

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; }
Expand Down Expand Up @@ -186,7 +186,7 @@ struct sequence_traits<R> : 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)
Expand All @@ -203,21 +203,21 @@ struct sequence_traits<R> : 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
Expand Down
2 changes: 1 addition & 1 deletion include/flux/op/adjacent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct adjacent_sequence_traits_base : default_sequence_traits {
requires sized_sequence<Base>
{
auto s = (flux::size(self.base_) - N) + 1;
return (std::max)(s, distance_t{0});
return (cmp::max)(s, distance_t{0});
}
};

Expand Down
16 changes: 8 additions & 8 deletions include/flux/op/cartesian_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -116,18 +116,18 @@ struct cartesian_traits_base_impl : default_sequence_traits {

auto& base = get_base<I>(self);
const auto this_index = flux::distance(base, flux::first(base), std::get<I>(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.
Expand All @@ -138,7 +138,7 @@ struct cartesian_traits_base_impl : default_sequence_traits {
}
}

flux::inc(base, std::get<I>(cur), num::checked_sub(new_index, this_index));
flux::inc(base, std::get<I>(cur), num::sub(new_index, this_index));

return cur;
}
Expand Down Expand Up @@ -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_);
}
Expand Down
4 changes: 2 additions & 2 deletions include/flux/op/chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ struct chunk_adaptor<Base> : inline_sequence_base<chunk_adaptor<Base>> {
requires random_access_sequence<Base>
{
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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/flux/op/compare.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions include/flux/op/cycle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct cycle_adaptor : inline_sequence_base<cycle_adaptor<Base, IsInfinite>> {
}

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<std::size_t>(off/sz);

Expand All @@ -187,8 +187,8 @@ struct cycle_adaptor : inline_sequence_base<cycle_adaptor<Base, IsInfinite>> {
sized_sequence<decltype(self.base_)>
{
auto dist = num::checked_cast<distance_t>(to.n) - num::checked_cast<distance_t>(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));
}

Expand All @@ -203,8 +203,8 @@ struct cycle_adaptor : inline_sequence_base<cycle_adaptor<Base, IsInfinite>> {
static constexpr auto size(auto& self) -> distance_t
requires (!IsInfinite && sized_sequence<Base>)
{
return num::checked_mul(flux::size(self.base_),
num::checked_cast<flux::distance_t>(self.data_.count));
return num::mul(flux::size(self.base_),
num::checked_cast<flux::distance_t>(self.data_.count));
}
};
};
Expand Down
4 changes: 2 additions & 2 deletions include/flux/op/detail/pdqsort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/flux/op/drop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct drop_adaptor : inline_sequence_base<drop_adaptor<Base>> {
static constexpr auto size(auto& self)
requires sized_sequence<Base>
{
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});
}

Expand Down
2 changes: 1 addition & 1 deletion include/flux/op/reverse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct reverse_adaptor : inline_sequence_base<reverse_adaptor<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)
Expand Down
2 changes: 1 addition & 1 deletion include/flux/op/scan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct scan_adaptor : inline_sequence_base<scan_adaptor<Base, Func, R, Mode>> {
requires sized_sequence<Base>
{
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_);
}
Expand Down
10 changes: 5 additions & 5 deletions include/flux/op/slide.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct slide_adaptor : inline_sequence_base<slide_adaptor<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)};
}
Expand All @@ -77,7 +77,7 @@ struct slide_adaptor : inline_sequence_base<slide_adaptor<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)};
}

Expand Down Expand Up @@ -105,8 +105,8 @@ struct slide_adaptor : inline_sequence_base<slide_adaptor<Base>> {
static constexpr auto size(auto& self) -> distance_t
requires sized_sequence<Base>
{
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});
}
};
};
Expand All @@ -132,7 +132,7 @@ constexpr auto inline_sequence_base<D>::slide(std::integral auto win_sz) &&
requires multipass_sequence<D>
{
FLUX_ASSERT(win_sz > 0);
return flux::slide(std::move(derived()), std::move(win_sz));
return flux::slide(std::move(derived()), num::checked_cast<distance_t>(win_sz));
}

} // namespace slide
Expand Down
14 changes: 8 additions & 6 deletions include/flux/op/stride.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ inline constexpr struct advance_fn {
constexpr auto operator()(Seq& seq, cursor_t<Seq>& 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;
}
Expand Down Expand Up @@ -225,9 +226,10 @@ struct stride_adaptor<Base> : inline_sequence_base<stride_adaptor<Base>> {
requires random_access_sequence<Base>
{
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;
}
}
Expand Down
12 changes: 6 additions & 6 deletions include/flux/op/take.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct take_adaptor : inline_sequence_base<take_adaptor<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)
Expand Down Expand Up @@ -88,22 +88,22 @@ struct take_adaptor : inline_sequence_base<take_adaptor<Base>>
requires bidirectional_sequence<Base>
{
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<Base>
{
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<Base>
{
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)
Expand All @@ -119,7 +119,7 @@ struct take_adaptor : inline_sequence_base<take_adaptor<Base>>
if constexpr (infinite_sequence<Base>) {
return self.count_;
} else {
return (std::min)(flux::size(self.base_), self.count_);
return (cmp::min)(flux::size(self.base_), self.count_);
}
}

Expand Down
6 changes: 3 additions & 3 deletions include/flux/source/array_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct array_ptr : inline_sequence_base<array_ptr<T>> {
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&
Expand All @@ -115,7 +115,7 @@ struct array_ptr : inline_sequence_base<array_ptr<T>> {
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;
Expand All @@ -124,7 +124,7 @@ struct array_ptr : inline_sequence_base<array_ptr<T>> {
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
Expand Down
2 changes: 1 addition & 1 deletion include/flux/source/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct range_sequence : inline_sequence_base<range_sequence<R, IsConst>> {
requires std::ranges::random_access_range<R>
{
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)));
}
Expand Down

0 comments on commit 56d1bf4

Please sign in to comment.