tudocomp
– The TU Dortmund Compression Framework
RollingTrie.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/Algorithm.hpp>
4 #include <tudocomp/util/Hash.hpp>
6 
7 namespace tdc {
8 
9 namespace lz78 {
10 
11 template<
12  typename HashRoller = ZBackupRollingHash,
13  typename HashProber = LinearProber,
14  typename HashManager = SizeManagerPrime,
15  typename HashFunction = NoopHasher
16 >
17 class RollingTrie : public Algorithm, public LZ78Trie<> {
18  typedef typename HashRoller::key_type key_type;
19  mutable HashRoller m_roller;
21 
22  inline key_type hash_node(uliteral_t c) const {
23  m_roller += c;
24  return m_roller();
25  }
26 
27 public:
28  inline static Meta meta() {
29  Meta m("lz78trie", "rolling", "Rolling Hash Trie");
30  m.option("hash_roller").templated<HashRoller, ZBackupRollingHash>("hash_roller");
31  m.option("hash_prober").templated<HashProber, LinearProber>("hash_prober");
32  m.option("hash_manager").templated<HashManager, SizeManagerPrime>("hash_manager");
33  m.option("load_factor").dynamic(30);
34  m.option("hash_function").templated<HashFunction, NoopHasher>("hash_function");
35  return m;
36  }
37  inline RollingTrie(Env&& env, const size_t n, const size_t& remaining_characters, factorid_t reserve = 0)
38  : Algorithm(std::move(env))
39  , LZ78Trie(n,remaining_characters)
40  , m_roller(this->env().env_for_option("hash_roller"))
41  , m_table(this->env(), n, remaining_characters) {
42  m_table.max_load_factor(this->env().option("load_factor").as_integer()/100.0f );
43  if(reserve > 0) {
44  m_table.reserve(reserve);
45  }
46  }
47 
48  IF_STATS(
49  MoveGuard m_guard;
50  inline ~RollingTrie() {
51  if (m_guard) {
52  m_table.collect_stats(env());
53  }
54  }
55  )
56  RollingTrie(RollingTrie&& other) = default;
57  RollingTrie& operator=(RollingTrie&& other) = default;
58 
60  m_table.insert(std::make_pair<key_type,factorid_t>(hash_node(c), size()));
61  m_roller.clear();
62  return node_t(size() - 1, true);
63  }
64 
65  inline node_t get_rootnode(uliteral_t c) const {
66  hash_node(c);
67  return node_t(c, false);
68  }
69 
70  inline void clear() {
71 // m_table.clear();
72  }
73 
74  inline node_t find_or_insert(const node_t&, uliteral_t c) {
75  const factorid_t newleaf_id = size();
76 
77  auto ret = m_table.insert(std::make_pair(hash_node(c), newleaf_id));
78  if(ret.second) {
79  m_roller.clear();
80  return node_t(newleaf_id, true); // added a new node
81  }
82  return node_t(ret.first.value(), false);
83  }
84 
85  inline size_t size() const {
86  return m_table.entries();
87  }
88 };
89 
90 }} //ns
91 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
LZ78TrieNode node_t
Definition: LZ78Trie.hpp:43
RollingTrie & operator=(RollingTrie &&other)=default
uint8_t uliteral_t
Type to represent signed single literals.
Definition: def.hpp:131
std::pair< Iterator, bool > insert(std::pair< key_t, value_t > &&value)
Definition: Hash.hpp:518
void reserve(len_t hint)
Definition: Hash.hpp:461
node_t add_rootnode(uliteral_t c)
Definition: RollingTrie.hpp:59
RollingTrie(Env &&env, const size_t n, const size_t &remaining_characters, factorid_t reserve=0)
Definition: RollingTrie.hpp:37
node_t get_rootnode(uliteral_t c) const
Definition: RollingTrie.hpp:65
node_t find_or_insert(const node_t &, uliteral_t c)
Definition: RollingTrie.hpp:74
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
Default return type of find_or_insert.
Definition: LZ78Trie.hpp:22
void templated(const std::string &accepted_type)
Declares that this option accepts values of the specified Algorithm type T.
Definition: Meta.hpp:93
uint32_t factorid_t
Type for the factor indices, bounded by the number of LZ78 trie nodes.
Definition: LZ78Trie.hpp:11
OptionBuilder option(const std::string &name)
Declares an accepted option for this algorithm.
Definition: Meta.hpp:216
float max_load_factor() const noexcept
Definition: Hash.hpp:409
Local environment for a compression/encoding/decompression call.
len_t entries() const
Definition: Hash.hpp:514
IF_STATS(MoveGuard m_guard;inline ~RollingTrie() { if(m_guard) { m_table.collect_stats(env());} }) RollingTrie(RollingTrie &&other)=default
Interface for algorithms.
Definition: Algorithm.hpp:15
void dynamic()
Declares that this option accepts values of a simple type that can be parsed from a string (e...
Definition: Meta.hpp:150
size_t size() const
Definition: RollingTrie.hpp:85