|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include "subspace/marker/unsafe.h" |
| 18 | +#include "subspace/num/integer_concepts.h" |
| 19 | +#include "subspace/num/types.h" |
| 20 | + |
| 21 | +namespace sus::option { |
| 22 | +template <class T> |
| 23 | +class Option; |
| 24 | +} |
| 25 | + |
| 26 | +namespace sus::num { |
| 27 | +struct usize; |
| 28 | +} |
| 29 | + |
| 30 | +namespace sus::iter::__private { |
| 31 | + |
| 32 | +template <::sus::num::Integer T> |
| 33 | +constexpr T step_forward(T l) noexcept { |
| 34 | + // SAFETY: All `Integer` can hold `1`. |
| 35 | + return l + T::from_unchecked(::sus::marker::unsafe_fn, 1); |
| 36 | +} |
| 37 | +template <::sus::num::Integer T> |
| 38 | +constexpr T step_backward(T l) noexcept { |
| 39 | + // SAFETY: All `Integer` can hold `1`. |
| 40 | + return l - T::from_unchecked(::sus::marker::unsafe_fn, 1); |
| 41 | +} |
| 42 | +template <::sus::num::Integer T> |
| 43 | +constexpr Option<T> step_forward_checked(T l) noexcept { |
| 44 | + // SAFETY: All `Integer` can hold `1`. |
| 45 | + return l.checked_add(T::from_unchecked(::sus::marker::unsafe_fn, 1)); |
| 46 | +} |
| 47 | +template <::sus::num::Integer T> |
| 48 | +constexpr Option<T> step_backward_checked(T l) noexcept { |
| 49 | + // SAFETY: All `Integer` can hold `1`. |
| 50 | + return l.checked_sub(T::from_unchecked(::sus::marker::unsafe_fn, 1)); |
| 51 | +} |
| 52 | +template <::sus::num::Integer T> |
| 53 | +constexpr T step_forward_by(T l, ::sus::num::usize steps) noexcept { |
| 54 | + return l + T::from(steps); |
| 55 | +} |
| 56 | +template <::sus::num::Integer T> |
| 57 | +constexpr T step_backward_by(T l, ::sus::num::usize steps) noexcept { |
| 58 | + return l - T::from(steps); |
| 59 | +} |
| 60 | +template <::sus::num::Integer T> |
| 61 | +constexpr Option<T> step_forward_by_checked(T l, |
| 62 | + ::sus::num::usize steps) noexcept { |
| 63 | + return T::try_from(steps).ok().and_then( |
| 64 | + [&l](T steps) { return l.checked_add(::sus::marker::unsafe_fn, steps); }); |
| 65 | +} |
| 66 | +template <::sus::num::Integer T> |
| 67 | +constexpr Option<T> step_backward_by_checked(T l, |
| 68 | + ::sus::num::usize steps) noexcept { |
| 69 | + return T::try_from(steps).ok().and_then( |
| 70 | + [&l](T steps) { return l.checked_sub(::sus::marker::unsafe_fn, steps); }); |
| 71 | +} |
| 72 | +template <::sus::num::Integer T> |
| 73 | +constexpr Option<::sus::num::usize> steps_between(const T& l, |
| 74 | + const T& r) noexcept { |
| 75 | + return r.checked_sub(l).and_then( |
| 76 | + [](T steps) { return ::sus::num::usize::try_from(steps).ok(); }); |
| 77 | +} |
| 78 | + |
| 79 | +/// Objects that have a notion of successor and predecessor operations. |
| 80 | +/// |
| 81 | +/// The successor operations move towards values that compare greater. The |
| 82 | +/// predecessor operations moves toward values that compare lesser. |
| 83 | +template <class T> |
| 84 | +concept Step = requires(const T& t, ::sus::num::usize n) { |
| 85 | + // Required methods. |
| 86 | + { ::sus::iter::__private::step_forward(t) } -> std::same_as<T>; |
| 87 | + { ::sus::iter::__private::step_backward(t) } -> std::same_as<T>; |
| 88 | + { |
| 89 | + ::sus::iter::__private::step_forward_checked(t) |
| 90 | + } -> std::same_as<::sus::option::Option<T>>; |
| 91 | + { |
| 92 | + ::sus::iter::__private::step_backward_checked(t) |
| 93 | + } -> std::same_as<::sus::option::Option<T>>; |
| 94 | + { ::sus::iter::__private::step_forward_by(t, n) } -> std::same_as<T>; |
| 95 | + { ::sus::iter::__private::step_backward_by(t, n) } -> std::same_as<T>; |
| 96 | + { |
| 97 | + ::sus::iter::__private::step_forward_by_checked(t, n) |
| 98 | + } -> std::same_as<::sus::option::Option<T>>; |
| 99 | + { |
| 100 | + ::sus::iter::__private::step_backward_by_checked(t, n) |
| 101 | + } -> std::same_as<::sus::option::Option<T>>; |
| 102 | + { |
| 103 | + ::sus::iter::__private::steps_between(t, t) |
| 104 | + } -> std::same_as<::sus::option::Option<::sus::num::usize>>; |
| 105 | +}; |
| 106 | + |
| 107 | +} // namespace sus::iter::__private |
0 commit comments