Wordsearch Solver
mini_offsets.hpp
1 #ifndef COMPACT_TRIE2_MINI_OFFSETS_HPP
2 #define COMPACT_TRIE2_MINI_OFFSETS_HPP
3 
4 #include <range/v3/iterator/basic_iterator.hpp>
5 #include <range/v3/range/access.hpp>
6 #include <range/v3/view/facade.hpp>
7 
8 #include <cassert>
9 #include <cstddef>
10 #include <cstdint>
11 #include <cstring>
12 #include <utility>
13 
14 namespace compact_trie2 {
15 
19 template <class Rng>
20 class MiniOffsets : public ranges::view_facade<MiniOffsets<Rng>> {
21 public:
22  friend ranges::range_access;
23  MiniOffsets() = default;
24 
25  struct cursor {
26  friend ranges::range_access;
27  using I = ranges::iterator_t<Rng>;
28  struct mixin : ranges::basic_mixin<cursor> {
29  mixin() = default;
30  using ranges::basic_mixin<cursor>::basic_mixin;
31  explicit mixin(I it) : mixin{cursor{std::forward<I>(it)}} {}
32  I base() const { return this->get().it_; }
33  void write(const std::uint16_t val) {
34  // This assert is now trivially fulfilled
35  assert(val < (1 << 2 * 8));
36  // fmt::print("Write\n");
37  std::memcpy(&*this->get().it_, &val, 2);
38  }
39  };
40 
41  I it_ = I();
42 
43  explicit cursor(I it) : it_(std::forward<I>(it)) {}
44  void next() { ranges::advance(it_, 2); }
45  std::uint16_t read() const {
46  std::uint16_t n{};
47  std::memcpy(&n, &*it_, 2);
48  // fmt::print("Read\n");
49  return n;
50  }
51  bool equal(cursor const& that) const { return it_ == that.it_; }
52  void prev() { ranges::advance(it_, -2); }
53  void advance(ranges::iter_difference_t<I> n) {
54  ranges::advance(it_, 2 * n);
55  }
56  auto distance_to(cursor const& that) const {
57  assert((that.it_ - it_) % 2 == 0);
58  return (that.it_ - it_) / 2;
59  }
60 
61  public:
62  constexpr cursor() = default;
63  };
64 
65  using Iterator = cursor;
66 
67  auto begin_cursor() const { return Iterator{ranges::begin(rng_)}; }
68  auto end_cursor() const { return Iterator{ranges::end(rng_)}; }
69  explicit MiniOffsets(Rng&& rng) : rng_(std::forward<Rng>(rng)) {}
70 
71  Rng rng_;
72 };
73 
74 template <class Rng> MiniOffsets<Rng> make_mini_offsets(Rng&& rng) {
75  return MiniOffsets<Rng>{std::forward<Rng>(rng)};
76 }
77 
78 } // namespace compact_trie2
79 
80 #endif // COMPACT_TRIE2_MINI_OFFSETS_HPP
2 byte wide view onto the offsets of child nodes, relative to the CompactTrie2::next_row_offset().
Definition: mini_offsets.hpp:20
namespace compact_trie2
Definition: compact_trie2.hpp:19
Definition: mini_offsets.hpp:28
Definition: mini_offsets.hpp:25