Tree Compression with Top Trees Revisited
testnav.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iostream>
3 #include <string>
4 
5 // Data structures
6 #include "Edges.h"
7 #include "Nodes.h"
8 #include "OrderedTree.h"
9 #include "TopDag.h"
10 #include "TopTree.h"
11 
12 // Algorithms
13 #include "TopDagConstructor.h"
14 #include "TopTreeUnpacker.h"
15 #include "RePairCombiner.h"
16 #include "TopDagUnpacker.h"
17 #include "Navigation.h"
18 #include "NavTest.h"
19 
20 // Utils
21 #include "ArgParser.h"
22 #include "DotGraphExporter.h"
23 #include "Timer.h"
24 #include "XML.h"
25 
26 
27 using std::cout;
28 using std::endl;
29 using std::string;
30 
31 int main(int argc, char **argv) {
33  ArgParser argParser(argc, argv);
34  const bool useRePair = argParser.isSet("r");
35  string filename = "data/1998statistics.xml";
36  if (argParser.numDataArgs() > 0) {
37  filename = argParser.getDataArg(0);
38  } else if (useRePair) {
39  // if used as "./test -r foo.xml", it will match the foo.xml to the "-r" which is unfortunate
40  string arg = argParser.get<string>("r", "");
41  filename = (arg == "") ? filename : arg;
42  }
43  const bool print = argParser.isSet("p");
44 
45  Labels<string> labels;
46  XmlParser<OrderedTree<TreeNode, TreeEdge>>::parse(filename, t, labels);
47  cout << t.summary() << "; Height: " << t.height() << " Avg depth: " << t.avgDepth() << endl;
48 
49  const int treeEdges = t._numEdges;
50  TopDag<string> dag(t._numNodes, labels);
51 
52  Timer timer;
53  if (useRePair) {
54  RePairCombiner<OrderedTree<TreeNode, TreeEdge>, string> topDagConstructor(t, dag);
55  topDagConstructor.construct();
56  } else {
57  TopDagConstructor<OrderedTree<TreeNode, TreeEdge>, string> topDagConstructor(t, dag);
58  topDagConstructor.construct();
59  }
60  cout << "Top DAG construction took " << timer.getAndReset() << "ms";
61  //, avg node depth " << topTree.avgDepth() << " (min " << topTree.minDepth() << "); took " << timer.getAndReset() << " ms" << endl;
62 
63  const int edges = dag.countEdges();
64  const double percentage = (edges * 100.0) / treeEdges;
65  const double ratio = ((int)(1000 / percentage)) / 10.0;
66  cout << "Top dag has " << dag.nodes.size() - 1 << " nodes, " << edges << " edges (" << percentage
67  << "% of original tree, " << ratio << ":1)" << endl;
68 
69  /*Navigator<string> nav(dag);
70 
71  cout << "isLeaf: " << nav.isLeaf() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
72  cout << "firstChild: " << nav.firstChild() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
73  cout << "firstChild: " << nav.firstChild() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
74  cout << "firstChild: " << nav.firstChild() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
75  cout << "nextSibling: " << nav.nextSibling() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
76  cout << "nextSibling: " << nav.nextSibling() << "; label: " << nav.getLabel() << " = " << std::endl << std::endl;
77 
78  cout << "parent: " << nav.parent() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
79  cout << "nextSibling: " << nav.nextSibling() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
80  cout << "firstChild: " << nav.firstChild() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
81  cout << "parent: " << nav.parent() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
82  cout << "parent: " << nav.parent() << "; label: " << nav.getLabel() << " = " << std::flush << *nav.getLabel() << std::endl << std::endl;
83 */
84 
85  timer.reset();
86  PreorderTraversal<string> trav(dag, print);
87  long long maxTreeStackSize = trav.run();
88  cout << "Preorder traversal took " << timer.get() << "ms, max tree stack size = " << maxTreeStackSize << " Bytes" << endl;
89 
90  return 0;
91 }
int height() const
Definition: OrderedTree.h:571
Transform a tree into its top tree.
vector< DagNode< DataType > > nodes
Definition: TopDag.h:177
Traverse an in-memory Top DAG in preorder.
Definition: NavTest.h:10
Ordered tree data structure.
Definition: OrderedTree.h:37
string getDataArg(const int index) const
get a data argument by its index (among the data arguments)
Definition: ArgParser.h:68
double avgDepth() const
Definition: OrderedTree.h:579
bool isSet(const string &arg) const
check whether an argument was set
Definition: ArgParser.h:58
ReturnType get() const
Definition: Timer.h:23
void reset()
Definition: Timer.h:19
uint numDataArgs() const
the number of unnamed data arguments
Definition: ArgParser.h:63
Read an XML file into a tree, using RapidXml.
Definition: XML.h:23
Parse command-line arguments.
Definition: ArgParser.h:21
void construct(DebugInfo *debugInfo=NULL)
T get(const string &key, const T defaultValue=T())
Definition: ArgParser.h:44
Transform a tree into its top tree.
int main(int argc, char **argv)
Definition: testnav.cpp:31
string summary() const
A one-line summary of the tree.
Definition: OrderedTree.h:348
A flexible timer.
Definition: Timer.h:14
long long run()
Do the traversal and print an XML representation to stdout.
Definition: NavTest.h:15
int countEdges() const
Count the number of edges in the DAG.
Definition: TopDag.h:87
ReturnType getAndReset()
Definition: Timer.h:29
A binary DAG that is specialised to be a top tree's minimal DAG.
Definition: TopDag.h:46
void construct(DebugInfo *debugInfo=NULL, const double minRatio=1.2)
A key-value label storage.
Definition: Labels.h:107