Skip to content

Commit

Permalink
difference_type
Browse files Browse the repository at this point in the history
  • Loading branch information
keithalewis committed Jun 13, 2024
1 parent c2c7558 commit c2e5ef1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
58 changes: 46 additions & 12 deletions fms_iterable.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ namespace fms::iterable {

return !i && !j; // both done
}

/*
template<input I, input J>
constexpr J copy(I i, J j)
{
while (i && j) {
*j++ = *i++;
*j = *i;
++i;
++j;
}
return j;
}

*/
// length(i, length(j)) = length(i) + length(j)
template <input I>
constexpr std::size_t length(I i, std::size_t n = 0) noexcept
Expand Down Expand Up @@ -112,7 +114,7 @@ namespace fms::iterable {
}

// For use with STL
template <class I>
template <input I>
constexpr I begin(I i)
{
if constexpr (has_begin<I>) {
Expand Down Expand Up @@ -154,6 +156,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = typename I::difference_type;

constexpr interval(I b, I e)
: b(b), e(e)
Expand Down Expand Up @@ -217,6 +220,7 @@ namespace fms::iterable {
using iterator_category = std::input_iterator_tag;
using value_type = T;
using reference = T&;
using difference_type = typename std::vector<T>::difference_type;

vector()
: v{}, i(0)
Expand Down Expand Up @@ -307,11 +311,6 @@ namespace fms::iterable {
return _v;
}

operator++();

return _v;
}

// Multi-pass
vector& reset(size_t i_ = 0)
{
Expand Down Expand Up @@ -354,6 +353,7 @@ namespace fms::iterable {
struct empty {
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;

bool operator==(const empty&) const
{
Expand Down Expand Up @@ -387,6 +387,7 @@ namespace fms::iterable {
using iterator_category = std::input_iterator_tag;
using value_type = T;
using reference = T&;
using difference_type = std::ptrdiff_t;

constant(T c = 0) noexcept
: c(c)
Expand Down Expand Up @@ -427,6 +428,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;

iota(T t = 0) noexcept
: t(t)
Expand Down Expand Up @@ -466,6 +468,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;

power(T t, T tn = 1)
: t(t), tn(tn)
Expand Down Expand Up @@ -505,6 +508,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;

factorial(T t = 1)
: t(t), n(1)
Expand Down Expand Up @@ -544,6 +548,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;

choose(T n)
: n(n), k(0), nk(1)
Expand Down Expand Up @@ -587,6 +592,7 @@ namespace fms::iterable {
using iterator_category = std::input_iterator_tag;
using value_type = T;
using reference = T&;
using difference_type = std::ptrdiff_t;

// pointer() is empty iterator
pointer(T* p = nullptr) noexcept
Expand Down Expand Up @@ -632,6 +638,7 @@ namespace fms::iterable {
using iterator_category = std::input_iterator_tag;
using value_type = T;
using reference = T&;
using difference_type = std::ptrdiff_t;

null_terminated_pointer(T* p) noexcept
: p(p)
Expand Down Expand Up @@ -677,6 +684,7 @@ namespace fms::iterable {
using iterator_category = std::input_iterator_tag;
using value_type = T;
using reference = T&;
using difference_type = std::ptrdiff_t;

once(T t) noexcept
: t(t), b(true)
Expand Down Expand Up @@ -720,6 +728,7 @@ namespace fms::iterable {
using iterator_category = std::input_iterator_tag;
using value_type = typename I::value_type;
using reference = typename I::reference;
using difference_type = typename I::difference_type;

repeat(I i) noexcept
: i0(i), i(i)
Expand Down Expand Up @@ -769,13 +778,23 @@ namespace fms::iterable {
using iterator_category = std::input_iterator_tag;
using value_type = T;
using reference = T&;
using difference_type = typename I::difference_type;

take(const I& i, std::size_t n)
: i(i), n(n)
{ }

bool operator==(const take& t) const = default;

auto begin() const
{
return i;
}
auto end() const
{
return std::next(i, n);
}

explicit operator bool() const noexcept
{
return i && n > 0;
Expand All @@ -784,10 +803,12 @@ namespace fms::iterable {
{
return *i;
}
/*
reference operator*() noexcept
{
return *i;
}
*/
take& operator++() noexcept
{
if (n) {
Expand Down Expand Up @@ -822,6 +843,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::common_type_t<typename I0::difference_type, typename I1::difference_type>;

concatenate2() = default;
concatenate2(const I0& i0, const I1& i1)
Expand Down Expand Up @@ -881,6 +903,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::common_type_t<typename I0::difference_type, typename I0::difference_type>;

merge2(const I0& i0, const I1& i1)
: i0(i0), i1(i1)
Expand Down Expand Up @@ -983,6 +1006,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = void;

call(F&& f)
: f(f)
Expand All @@ -1004,13 +1028,15 @@ namespace fms::iterable {

// Apply a function to elements of an iterable.
// f(*i), f(*++i), f(*++i), ...
template <class F, input I, class T = typename I::value_type, class U = std::invoke_result_t<F, T>>
template <class F, input I, class T = typename I::value_type,
class U = std::invoke_result_t<F, T>>
class apply {
F f;
I i;
public:
using iterator_category = std::input_iterator_tag;
using value_type = U;
using difference_type = typename I::difference_type;

apply(const F& f, const I& i)
: f(f), i(i)
Expand Down Expand Up @@ -1074,14 +1100,16 @@ namespace fms::iterable {
// TODO: apply(f, *i0, *i1, ...), apply(f, {*++i0, *++i1, ...}), ...

// Apply a binary operation to elements of two iterable.
template <class BinOp, input I0, input I1, class T0 = typename I0::value_type, class T1 = typename I1::value_type, class T = std::invoke_result_t<BinOp, T0, T1>>
template <class BinOp, input I0, input I1, class T0 = typename I0::value_type, class T1 = typename I1::value_type,
class T = std::invoke_result_t<BinOp, T0, T1>>
class binop {
BinOp op;
I0 i0;
I1 i1;
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;

binop(const BinOp& op, I0 i0, I1 i1)
: op(op), i0(i0), i1(i1)
Expand Down Expand Up @@ -1159,6 +1187,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = typename I::difference_type;

filter(const filter& a)
: p(a.p), i(a.i)
Expand Down Expand Up @@ -1231,6 +1260,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = typename I::difference_type;

until(const P& p, const I& i)
: p(p), i(i)
Expand Down Expand Up @@ -1303,6 +1333,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;

fold(const BinOp& op, const I& i, T t = 0)
: op(op), i(i), t(t)
Expand Down Expand Up @@ -1393,7 +1424,8 @@ namespace fms::iterable {
// inline auto horner(I i, T x, T t = 1)

// d(i[1], i[0]), d(i[2], i[1]), ...
template <input I, class T = typename I::value_type, class D = std::minus<T>, typename U = std::invoke_result_t<D, T, T>>
template <input I, class T = typename I::value_type, class D = std::minus<T>,
typename U = std::invoke_result_t<D, T, T>>
class delta {
D d;
I i;
Expand All @@ -1409,6 +1441,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = U;
using difference_type = std::ptrdiff_t;

delta(const I& _i, const D& _d = std::minus<T>{})
: d(_d), i(_i), t{}, _t{}
Expand Down Expand Up @@ -1487,6 +1520,7 @@ namespace fms::iterable {
public:
using iterator_category = std::input_iterator_tag;
using value_type = std::pair<typename I::value_type, typename J::value_type>;
using difference_type = std::common_type<typename I::difference_type, typename J::difference_type>;

pair(I i, J j)
: i(i), j(j)
Expand Down
6 changes: 3 additions & 3 deletions fms_iterable.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int test_interval() {
interval c(v.begin(), v.end());
auto c2(c);
assert(c == c2);
// c = c2; // cannot reseat const reference
c = c2;
assert(!(c2 != c));

assert(c);
Expand All @@ -32,7 +32,7 @@ int test_interval() {
std::vector v{ 1, 2, 3 };
interval c(v.begin(), v.end());
int i = 1;
for (auto ci : c) {
for (auto ci : c) { // range for
assert(i == ci);
++i;
}
Expand All @@ -50,7 +50,7 @@ int test_interval() {
{
int i[] = { 1,2,3 };
int j[3];
auto jj = copy(array(i), array(j));
std::copy(begin(array(i)), end(array(i)), pointer(j));
assert(equal(array(i), array(j)));
}

Expand Down

0 comments on commit c2e5ef1

Please sign in to comment.