Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement an iota range #77

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 2023/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
#include "solvelib/03/day03_test.hpp"
#include "solvelib/04/day04_test.hpp"
#include "solvelib/05/day05_test.hpp"

#include "xmaslib/iota/iota_test.hpp"
145 changes: 145 additions & 0 deletions 2023/xmaslib/iota/iota.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#pragma once

#include <cassert>
#include <compare>
#include <cstddef>
#include <format>
#include <iterator>
#include <limits>
#include <stdexcept>

namespace xmas {
namespace views {

// iota is a view that ranges over integers in [begin, end).
template <std::integral T> class iota {
public:
class iterator;

constexpr iota(T begin, T end) : m_begin(begin), m_end(end) {
assert(begin <= end);
}
constexpr iota(T end) : iota(0, end) {}
constexpr iota() : iota(0, std::numeric_limits<T>::max()) {}

constexpr iterator begin() const noexcept { return cbegin(); }
constexpr iterator end() const noexcept { return cend(); }

constexpr iterator cbegin() const noexcept { return iterator(m_begin); }
constexpr iterator cend() const noexcept { return iterator(m_end); }

constexpr std::size_t size() const noexcept {
return safe_cast<std::size_t>(m_end - m_begin);
}

constexpr T front() const noexcept { return m_begin; }
constexpr T back() const noexcept { return m_end - 1; }

constexpr T operator[](std::size_t index) const noexcept {
#ifndef NDEBUG
range_check(index);
#endif
return safe_cast<T>(m_begin + safe_cast<std::ptrdiff_t>(index));
}

constexpr T at(std::size_t index) const {
range_check(index);
return safe_cast<T>(m_begin + safe_cast<std::ptrdiff_t>(index));
}

private:
constexpr void range_check(std::size_t index) const {
if (index >= size()) {
throw std::runtime_error(
std::format("xmas::views::iota::range_check: index (which is %zu) "
">= this->size() (which is %zu)",
index, this->size()));
}
}

T m_begin;
T m_end;

template <std::integral Out, std::integral In>
constexpr static Out safe_cast(In in) {
assert(in == static_cast<In>(static_cast<Out>(in)));
return static_cast<Out>(in);
}

public:
class iterator {
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = void;
using reference = value_type &;

constexpr explicit iterator(value_type init = 0) noexcept : v{init} {}

constexpr iterator operator++() noexcept {
++v;
return *this;
}

constexpr iterator operator++(int) noexcept {
auto it = iterator{v + 1};
++*this;
return it;
}

constexpr iterator &operator+=(std::ptrdiff_t delta) noexcept {
v += safe_cast<value_type>(delta);
return *this;
}

constexpr iterator &operator-=(std::ptrdiff_t delta) noexcept {
v -= safe_cast<value_type>(delta);
return *this;
}

[[nodiscard]] constexpr iterator
operator+(std::ptrdiff_t delta) const noexcept {
auto it = *this;
return it += delta;
}

[[nodiscard]] constexpr iterator
operator-(std::ptrdiff_t delta) const noexcept {
auto it = *this;
return it -= delta;
}

[[nodiscard]] constexpr difference_type
operator-(iterator other) const noexcept {
return safe_cast<difference_type>(this->v) -
safe_cast<difference_type>(other.v);
}

[[nodiscard]] constexpr bool operator==(iterator other) const noexcept {
return v == other.v;
}

[[nodiscard]] constexpr bool operator!=(iterator other) const noexcept {
return !(*this == other);
}

[[nodiscard]] constexpr std::strong_ordering
operator<=>(iterator other) const noexcept {
return v <=> other.v;
}

[[nodiscard]] constexpr value_type operator*() const noexcept { return v; }

[[nodiscard]] constexpr value_type
operator[](std::size_t index) const noexcept {
return safe_cast<value_type>(v + safe_cast<std::ptrdiff_t>(index));
}

private:
T v;
};
};

} // namespace views
} // namespace xmas
61 changes: 61 additions & 0 deletions 2023/xmaslib/iota/iota_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "iota.hpp"

#include <doctest/doctest.h>

#include <algorithm>
#include <cstdint>
#include <functional>
#include <iterator>
#include <numeric>
#include <vector>

TEST_CASE("iota iterator") {

SUBCASE("Access") {
xmas::views::iota<std::int8_t> iota(3, 10);
REQUIRE_EQ(iota.size(), 7);

CHECK_EQ(iota.at(0), 3);
CHECK_EQ(iota.at(1), 4);
CHECK_EQ(iota.at(2), 5);
CHECK_EQ(iota.at(3), 6);
CHECK_EQ(iota.at(4), 7);
CHECK_EQ(iota.at(5), 8);
CHECK_EQ(iota.at(6), 9);

REQUIRE_THROWS(iota.at(7));

CHECK_EQ(iota[0], 3);
CHECK_EQ(iota[1], 4);
CHECK_EQ(iota[2], 5);
CHECK_EQ(iota[3], 6);
CHECK_EQ(iota[4], 7);
CHECK_EQ(iota[5], 8);
CHECK_EQ(iota[6], 9);
}

SUBCASE("Copy") {
xmas::views::iota<std::size_t> iota(11, 17);
std::vector<std::size_t> out;

std::copy(iota.cbegin(), iota.cend(), std::back_inserter(out));

REQUIRE_EQ(iota.size(), 6);
REQUIRE_EQ(out.size(), 6);

CHECK_EQ(out[0], 11);
CHECK_EQ(out[1], 12);
CHECK_EQ(out[2], 13);
CHECK_EQ(out[3], 14);
CHECK_EQ(out[4], 15);
CHECK_EQ(out[5], 16);
}

SUBCASE("Reduce") {
xmas::views::iota<int> iota(0, 10);
REQUIRE_EQ(iota.size(), 10u);

auto got = std::reduce(iota.begin(), iota.end(), 0, std::plus<int>{});
REQUIRE_EQ(got, 45);
}
}
Loading