Skip to content

Commit

Permalink
Improvements in xmaslib
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardGomezEscandell committed Dec 24, 2023
1 parent cb5b6e9 commit f4e2d4b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions 2023/xmaslib/integer_range/integer_range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions 2023/xmaslib/matrix/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> data() noexcept {
return m_data;
}
Expand Down
16 changes: 10 additions & 6 deletions 2023/xmaslib/matrix/text_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
namespace xmas {
namespace views {

std::pair<std::size_t, std::size_t> text_matrix::dimensions(
std::string_view input) {
std::pair<std::size_t, std::size_t> 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);

Expand All @@ -33,10 +31,16 @@ view<std::string::iterator> text_matrix::row(std::size_t i) {
text.begin() + static_cast<std::ptrdiff_t>(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<std::ptrdiff_t>(pos),
text.begin() + static_cast<std::ptrdiff_t>(pos + n_cols)};
}

strided<std::string::iterator> text_matrix::col(std::size_t j) {
assert(j < n_cols);
return {
text.begin() + static_cast<std::ptrdiff_t>(j), text.end(), n_cols + 1};
return {text.begin() + static_cast<std::ptrdiff_t>(j), text.end(), n_cols + 1};
}

char text_matrix::at(std::size_t i, std::size_t j) const {
Expand Down
7 changes: 7 additions & 0 deletions 2023/xmaslib/matrix/text_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct text_matrix {
text_matrix(std::string& data);

[[nodiscard]] view<std::string::iterator> row(std::size_t i);
[[nodiscard]] std::string_view line(std::size_t i) const;

[[nodiscard]] strided<std::string::iterator> col(std::size_t j);

[[nodiscard]] char at(std::size_t i, std::size_t j) const;
Expand All @@ -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); });
Expand Down

0 comments on commit f4e2d4b

Please sign in to comment.