libwordring
trie_heap_iterator.hpp
1 #pragma once
2 
3 #include <wordring/trie/trie_heap.hpp>
4 
5 #include <optional>
6 
7 namespace wordring::detail
8 {
9  template <typename Container>
11  {
12  public:
13  using difference_type = std::ptrdiff_t;
14  using value_type = std::uint8_t;
15  using pointer = value_type*;
16  using reference = value_type&;
17  using iterator_category = std::input_iterator_tag;
18 
19  protected:
20  using index_type = typename trie_node::index_type;
21  using node_type = trie_node;
22  using container = Container const;
23 
24  static constexpr std::uint16_t null_value = 256;
25 
26  protected:
28  : m_c(nullptr)
29  , m_index(0)
30  {
31  }
32 
33  const_trie_heap_iterator(container& c, index_type index)
34  : m_c(std::addressof(c))
35  , m_index(index)
36  {
37  }
38 
41  value_type value() const
42  {
43  assert(base() <= m_index && m_index < base() + null_value);
44 
45  return m_index - base();
46  }
47 
50  index_type at_index(value_type label) const
51  {
52  node_type const* d = m_c->data();
53 
54  index_type base = (d + m_index)->m_base;
55  index_type idx = 0;
56 
57  if (1 <= base)
58  {
59  index_type i = base + label;
60  if (i < limit() && (d + i)->m_check == m_index) idx = i;
61  }
62 
63  return idx;
64  }
65 
68  void advance() { m_index = find(m_index + 1, base() + null_value, mother()); }
69 
73  index_type parent_index() const
74  {
75  assert(1 <= m_index);
76 
77  return (m_c->data() + m_index)->m_check;
78  }
79 
84  index_type begin_index() const
85  {
86  assert(1 <= m_index);
87 
88  index_type idx = (m_c->data() + m_index)->m_base;
89  return (1 <= idx)
90  ? find(idx, idx + null_value, m_index)
91  : 0;
92  }
93 
94  index_type end_index() const { return 0; }
95 
102  index_type find(index_type first, index_type last, index_type check) const
103  {
104  index_type tail = std::min(last, limit());
105  for (; first < tail; ++first) if ((m_c->data() + first)->m_check == check) break;
106 
107  return (first != tail) ? first : 0;
108  }
109 
112  index_type mother() const
113  {
114  assert(1 <= m_index && m_index < limit());
115 
116  node_type const* d = m_c->data();
117  index_type parent = (d + m_index)->m_check;
118 
119  return (1 <= parent)
120  ? parent
121  : 0;
122  }
123 
127  index_type base() const
128  {
129  node_type const* d = m_c->data();
130 
131  index_type parent = mother();
132  assert(parent != 0);
133 
134  index_type idx = (parent != 0) ? (d + parent)->m_base : 0;
135  assert(1 <= idx); // thisが子である以上、親のBASEが存在する。
136 
137  return idx;
138  }
139 
143  index_type limit() const { return m_c->size(); }
144 
147  bool has_null() const
148  {
149  if (m_index == 1) return false;
150 
151  node_type const* d = m_c->data();
152  index_type idx = (d + m_index)->m_base + null_value;
153 
154  return (idx < limit() && (d + idx)->m_check == m_index);
155  }
156 
159  bool has_sibling() const
160  {
161  if (m_index == 1) return false;
162 
163  index_type check = (m_c->data() + m_index)->m_check;
164 
165  index_type first = base();
166  index_type last = first + null_value;
167 
168  while (true)
169  {
170  first = find(first, last, check);
171  if (first == 0) return false;
172  if (first != m_index) return true;
173  ++first;
174  }
175 
176  return false;
177  }
178 
179  protected:
180  container* m_c;
181  index_type m_index;
182  };
183 }
wordring::detail::const_trie_heap_iterator::advance
void advance()
Definition: trie_heap_iterator.hpp:68
wordring::detail::const_trie_heap_iterator::value
value_type value() const
Definition: trie_heap_iterator.hpp:41
wordring::detail::const_trie_heap_iterator::begin_index
index_type begin_index() const
Definition: trie_heap_iterator.hpp:84
wordring::detail::const_trie_heap_iterator::parent_index
index_type parent_index() const
Definition: trie_heap_iterator.hpp:73
wordring::detail::const_trie_heap_iterator::limit
index_type limit() const
Definition: trie_heap_iterator.hpp:143
wordring::detail::const_trie_heap_iterator::at_index
index_type at_index(value_type label) const
Definition: trie_heap_iterator.hpp:50
wordring::detail::const_trie_heap_iterator::has_null
bool has_null() const
Definition: trie_heap_iterator.hpp:147
wordring::detail::trie_node
Definition: trie_heap.hpp:23
wordring::detail::const_trie_heap_iterator::has_sibling
bool has_sibling() const
Definition: trie_heap_iterator.hpp:159
wordring::detail::const_trie_heap_iterator::find
index_type find(index_type first, index_type last, index_type check) const
Definition: trie_heap_iterator.hpp:102
wordring::detail
wordring::detail::const_trie_heap_iterator
Definition: trie_heap_iterator.hpp:10
wordring::detail::const_trie_heap_iterator::base
index_type base() const
Definition: trie_heap_iterator.hpp:127
wordring::detail::const_trie_heap_iterator::mother
index_type mother() const
Definition: trie_heap_iterator.hpp:112