diff --git a/2023/xmaslib/integer_range/integer_range.hpp b/2023/xmaslib/integer_range/integer_range.hpp index 82569c6..ee38fbf 100644 --- a/2023/xmaslib/integer_range/integer_range.hpp +++ b/2023/xmaslib/integer_range/integer_range.hpp @@ -22,6 +22,11 @@ struct integer_range { assert(begin <= end); } + constexpr integer_range(auto pair) { + const auto& [begin, end] = pair; + integer_range(begin, end); + } + constexpr bool empty() const noexcept { return begin == end; } diff --git a/2023/xmaslib/matrix/matrix.hpp b/2023/xmaslib/matrix/matrix.hpp index 7799349..4d5b861 100644 --- a/2023/xmaslib/matrix/matrix.hpp +++ b/2023/xmaslib/matrix/matrix.hpp @@ -77,6 +77,11 @@ struct matrix { return row(r); } + auto operator[](auto rc) const { + auto const& [r, c] = rc; + return row(r)[c]; + } + std::span data() noexcept { return m_data; } diff --git a/2023/xmaslib/matrix/text_matrix.cpp b/2023/xmaslib/matrix/text_matrix.cpp index b67cd1b..fbd75ce 100644 --- a/2023/xmaslib/matrix/text_matrix.cpp +++ b/2023/xmaslib/matrix/text_matrix.cpp @@ -9,11 +9,9 @@ namespace xmas { namespace views { -std::pair text_matrix::dimensions( - std::string_view input) { +std::pair text_matrix::dimensions(std::string_view input) { const std::size_t ncols = - 1 + - std::size_t(std::find(input.cbegin(), input.cend(), '\n') - input.cbegin()); + 1 + std::size_t(std::find(input.cbegin(), input.cend(), '\n') - input.cbegin()); const std::size_t nrows = input.size() / ncols; xlog::debug("Detected {} rows and {} columns", nrows, ncols); @@ -33,10 +31,16 @@ view text_matrix::row(std::size_t i) { text.begin() + static_cast(pos + n_cols)}; } +std::string_view text_matrix::line(std::size_t i) const { + assert(i < n_rows); + const std::size_t pos = i * (n_rows + 1); + return {text.begin() + static_cast(pos), + text.begin() + static_cast(pos + n_cols)}; +} + strided text_matrix::col(std::size_t j) { assert(j < n_cols); - return { - text.begin() + static_cast(j), text.end(), n_cols + 1}; + return {text.begin() + static_cast(j), text.end(), n_cols + 1}; } char text_matrix::at(std::size_t i, std::size_t j) const { diff --git a/2023/xmaslib/matrix/text_matrix.hpp b/2023/xmaslib/matrix/text_matrix.hpp index 5438145..eaa584f 100644 --- a/2023/xmaslib/matrix/text_matrix.hpp +++ b/2023/xmaslib/matrix/text_matrix.hpp @@ -14,6 +14,8 @@ struct text_matrix { text_matrix(std::string& data); [[nodiscard]] view row(std::size_t i); + [[nodiscard]] std::string_view line(std::size_t i) const; + [[nodiscard]] strided col(std::size_t j); [[nodiscard]] char at(std::size_t i, std::size_t j) const; @@ -35,6 +37,11 @@ struct text_matrix { std::ranges::views::transform([this](std::size_t r) { return row(r); }); } + auto lines() const { + return std::ranges::views::iota(std::size_t{0}, n_rows) | + std::ranges::views::transform([this](std::size_t r) { return line(r); }); + } + auto cols() { return std::ranges::views::iota(std::size_t{0}, n_cols) | std::ranges::views::transform([this](std::size_t c) { return col(c); });