Wordsearch Solver
node.hpp
1 #ifndef NODE_HPP
2 #define NODE_HPP
3 
4 #include <bitset>
5 #include <cstddef>
6 #include <cstdint>
7 #include <ostream>
8 
9 namespace compact_trie {
10 
13 class Node {
14 public:
15  using PrecedingType = std::uint32_t;
16  // using PrecedingType = unsigned short int;
17 
18  Node() = default;
19 
20  void add_char(char c);
21  void set_preceding(std::size_t preceding);
22  void set_is_end_of_word(bool is_end_of_word);
23 
28  std::size_t bits_on_before(std::size_t i) const;
29 
33  bool test(std::size_t i) const;
34 
37  bool any() const;
38 
39  bool is_end_of_word() const;
40 
44  PrecedingType preceding() const;
45 
46  friend std::ostream& operator<<(std::ostream& os, const Node& node);
47 
48 private:
49  std::bitset<26> bits_;
50  PrecedingType preceding_;
51  bool is_end_of_word_;
52 };
53 
54 // libstdc++ with gcc 8 on linux bitset implementation uses an unsigned long to
55 // back the bitset, so a bitset of size 1 is 8 bytes long. It in fact uses
56 // multiples of 8, ie. sizeof(std::bitset<65>) == 16
57 // Since Node will therefore be 8-byte aligned, may as well use std::uint32_t
58 // for PrecedingType
59 
60 // static_assert(sizeof(std::bitset<1>) == 8);
61 // static_assert(sizeof(std::bitset<26>) == 8);
62 // static_assert(sizeof(std::bitset<65>) == 16);
63 // static_assert(sizeof(Node) == 16);
64 
65 } // namespace compact_trie
66 
67 #endif // NODE_HPP
Bitset based node representing suffixes/letters.
Definition: node.hpp:13
bool test(std::size_t i) const
O(1) test if a suffix is present.
Definition: node.cpp:61
PrecedingType preceding() const
Definition: node.cpp:70
bool any() const
Definition: node.cpp:66
std::size_t bits_on_before(std::size_t i) const
Definition: node.cpp:48
namespace compact_trie
Definition: compact_trie.hpp:38