tudocomp
– The TU Dortmund Compression Framework
JudyTrie.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/config.h>
4 #ifdef JUDY_H_AVAILABLE
5 
6 #include <tudocomp/Algorithm.hpp>
9 #include <Judy.h> // Judy Array
10 
11 namespace tdc {
12 namespace lz78 {
13 
19 class JudyTrie : public Algorithm, public LZ78Trie<> {
20 
21  Pvoid_t m_dict; // judy array
22  size_t m_size;
23 
24  inline factorid_t& find(const squeeze_node_t& node) {
25  void* pvalue;
26  JLI(pvalue, m_dict, node); //lookup node in dict, store value in pvalue
27  DCHECK_NE(pvalue, PJERR);
28  return *reinterpret_cast<factorid_t*>(pvalue);
29  }
30 
31 public:
32  inline static Meta meta() {
33  Meta m("lz78trie", "judy", "Lempel-Ziv 78 Judy Array");
34  return m;
35  }
36  inline JudyTrie(Env&& env, size_t n, const size_t& remaining_characters, factorid_t = 0)
37  : Algorithm(std::move(env))
38  , LZ78Trie(n,remaining_characters)
39  , m_dict(static_cast<Pvoid_t>(nullptr))
40  , m_size(0)
41  {
42  }
43 
44  inline node_t get_rootnode(uliteral_t c) const {
45  DCHECK_LT(create_node(0,c), std::numeric_limits<factorid_t>::max());
46  return node_t(create_node(0,c), false);
47  }
48 
49  inline node_t add_rootnode(uliteral_t c) {
50  DCHECK_EQ(find(create_node(0, c)), 0);
51  find(create_node(0, c)) = size();
52  ++m_size;
53  return node_t(m_size-1, true);
54  }
55 
56  inline void clear() {
57  if(m_dict != nullptr) {
58  int i;
59  JLFA(i, m_dict);
60  }
61  m_size = 0;
62  }
63 
64  inline node_t find_or_insert(const node_t& parent, uliteral_t c) {
65  const squeeze_node_t node = create_node(parent.id(), c);
66 
67  factorid_t& id = find(node);
68 
69  if(id == 0) {
70  const factorid_t newleaf_id = size()+1;
71  id = newleaf_id;
72  ++m_size;
73  return node_t(newleaf_id, true);
74  }
75  return node_t(id-1, false);
76  }
77 
78  inline size_t size() const {
79  return m_size;
80  }
81 };
82 
83 }} //ns
84 
85 #endif// JUDY_H_AVAILABLE
86 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
uint_t< 40 > squeeze_node_t
uint8_t uliteral_t
Type to represent signed single literals.
Definition: def.hpp:131
uint32_t factorid_t
Type for the factor indices, bounded by the number of LZ78 trie nodes.
Definition: LZ78Trie.hpp:11
squeeze_node_t create_node(factorid_t id, uliteral_t c)