Classes | Public Member Functions | Public Attributes | List of all members
dune::HuffTree Struct Reference

#include <FelixCompress.hh>

Classes

struct  Node
 

Public Member Functions

bool valuecomp (const Node &one, const Node &other) const
 
Nodeoperator() (const uint16_t value)
 
void print (Node *loc)
 
void print ()
 
void printFile (std::string &filename)
 
void generate_codes (Node *loc, size_t buff=0, uint8_t len=0)
 
void generate_codes ()
 
std::unordered_map< uint16_t, Node * > make_tree (std::vector< Node > nodevec)
 
 ~HuffTree ()
 

Public Attributes

Noderoot
 
Nodenodelist
 
std::unordered_map< uint16_t, Node * > nodes
 

Detailed Description

Definition at line 29 of file FelixCompress.hh.

Constructor & Destructor Documentation

dune::HuffTree::~HuffTree ( )
inline

Definition at line 190 of file FelixCompress.hh.

190 { delete[] nodelist; }

Member Function Documentation

void dune::HuffTree::generate_codes ( Node loc,
size_t  buff = 0,
uint8_t  len = 0 
)
inline

Definition at line 114 of file FelixCompress.hh.

114  {
115  // Assign a string when a leaf is reached. Otherwise go one layer deeper.
116  if (loc->left == NULL) {
117  loc->huffcode = buff;
118  loc->hufflength = len;
119  nodes[loc->value] = loc;
120  } else {
121  // Move one node down.
122  generate_codes(loc->left, buff, len + 1);
123  generate_codes(loc->right, buff | (1 << len), len + 1);
124  }
125 
126  // Move one node up.
127  return;
128  }
QMapNodeBase * left
Definition: qmap.h:51
std::unordered_map< uint16_t, Node * > nodes
QMapNodeBase * right
Definition: qmap.h:52
void dune::HuffTree::generate_codes ( )
inline

Definition at line 129 of file FelixCompress.hh.

129 { return generate_codes(root); }
std::unordered_map<uint16_t, Node*> dune::HuffTree::make_tree ( std::vector< Node nodevec)
inline

Definition at line 132 of file FelixCompress.hh.

132  {
133  // Order vector to get the same results every time.
134  std::sort(nodevec.begin(), nodevec.end());
135  // Create a new buffer to hold the leaves and N-1 new nodes.
136  const unsigned totlen = 2 * nodevec.size() - 1;
137  Node* begin = &nodevec[0];
138  nodelist = new Node[totlen];
139  std::copy(begin, begin + nodevec.size(), nodelist);
140 
141  // Continue until the buffer is filled.
142  for (unsigned i = 0; i < totlen - nodevec.size(); ++i) {
143  // Find the lowest-frequency non-parented nodes.
144  Node* lowest = nodelist;
145  while (lowest->hasParent) {
146  ++lowest;
147  }
148  Node* sec_lowest = lowest + 1;
149  while (sec_lowest->hasParent) {
150  ++sec_lowest;
151  }
152  for (Node* n = nodelist; n != nodelist + nodevec.size() + i; ++n) {
153  // Disregard parented nodes.
154  if (n->hasParent) {
155  continue;
156  }
157  if (n->frequency < lowest->frequency) {
158  sec_lowest = lowest;
159  lowest = n;
160  } else if (n->frequency < sec_lowest->frequency && n != lowest) {
161  sec_lowest = n;
162  }
163  }
164 
165  // Make lowest value the left node in case of equal frequency.
166  if (lowest->frequency == sec_lowest->frequency &&
167  lowest->value > sec_lowest->value) {
168  Node* tmp = lowest;
169  lowest = sec_lowest;
170  sec_lowest = tmp;
171  }
172 
173  // Link the lowest frequency nodes to a new one in the buffer.
174  Node* newNode = &nodelist[nodevec.size() + i];
175  newNode->left = lowest;
176  newNode->right = sec_lowest;
177  newNode->frequency = lowest->frequency + sec_lowest->frequency;
178  lowest->hasParent = true;
179  sec_lowest->hasParent = true;
180  }
181 
182  // Assign root to the last unparented node and generate codes.
183  root = &nodelist[totlen - 1];
184  generate_codes();
185 
186  // Output the generated map.
187  return nodes;
188  }
QMapNodeBase * left
Definition: qmap.h:51
std::unordered_map< uint16_t, Node * > nodes
std::void_t< T > n
string tmp
Definition: languages.py:63
T copy(T const &v)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
QMapNodeBase * right
Definition: qmap.h:52
Node* dune::HuffTree::operator() ( const uint16_t  value)
inline

Definition at line 68 of file FelixCompress.hh.

68 { return nodes[value]; }
std::unordered_map< uint16_t, Node * > nodes
void dune::HuffTree::print ( Node loc)
inline

Definition at line 71 of file FelixCompress.hh.

71  {
72  if (loc->left == NULL) {
73  std::cout << "Node with value " << loc->value << ", frequency "
74  << loc->frequency << " and huffcode "
75  << std::bitset<32>(loc->huffcode) << " length "
76  << (unsigned)loc->hufflength << '\n';
77  } else {
78  print(loc->left);
79  print(loc->right);
80  }
81  return;
82  }
QMapNodeBase * left
Definition: qmap.h:51
std::void_t< T > n
QMapNodeBase * right
Definition: qmap.h:52
void dune::HuffTree::print ( )
inline

Definition at line 83 of file FelixCompress.hh.

83 { return print(root); }
void dune::HuffTree::printFile ( std::string filename)
inline

Definition at line 86 of file FelixCompress.hh.

86  {
87  std::ofstream ofile(filename);
88 
89  ofile << "Value\tFrequency\tCode\tLength\n";
90  std::vector<Node*> local_nlist;
91 
92  for(auto p : nodes) {
93  Node* n = p.second;
94  if(n->frequency == 0) continue;
95  local_nlist.push_back(n);
96  }
97 
98  std::sort(local_nlist.begin(), local_nlist.end());
99 
100  for(unsigned i = 0; i < local_nlist.size(); ++i) {
101  Node* n = local_nlist[i];
102  if(n->value > (1<<16)/2) {
103  ofile << (int)n->value-(1<<16) << '\t' << n->frequency << '\t' << n->huffcode << '\t' << (unsigned)n->hufflength << '\n';
104  } else {
105  if(n->value > 1000) continue; // Disregard start values.
106  ofile << n->value << '\t' << n->frequency << '\t' << n->huffcode << '\t' << (unsigned)n->hufflength << '\n';
107  }
108  }
109 
110  ofile.close();
111  }
std::unordered_map< uint16_t, Node * > nodes
string filename
Definition: train.py:213
std::void_t< T > n
p
Definition: test.py:223
bool dune::HuffTree::valuecomp ( const Node one,
const Node other 
) const
inline

Definition at line 59 of file FelixCompress.hh.

59  {
60  return one.value < other.value;
61  }

Member Data Documentation

Node* dune::HuffTree::nodelist

Definition at line 64 of file FelixCompress.hh.

std::unordered_map<uint16_t, Node*> dune::HuffTree::nodes

Definition at line 65 of file FelixCompress.hh.

Node* dune::HuffTree::root

Definition at line 63 of file FelixCompress.hh.


The documentation for this struct was generated from the following file: