1 #include "Tree.h"
2 #include "nodedumper.h"
3 #include "printutils.h"
4 
5 #include <assert.h>
6 #include <algorithm>
7 #include <sstream>
8 #include <tuple>
9 
~Tree()10 Tree::~Tree()
11 {
12 	this->nodecachemap.clear();
13 }
14 
15 /*!
16 	Returns the cached string representation of the subtree rooted by \a node.
17 	If node is not cached, the cache will be rebuilt.
18 */
getString(const AbstractNode & node,const std::string & indent) const19 const std::string Tree::getString(const AbstractNode &node, const std::string &indent) const
20 {
21 	assert(this->root_node);
22 	bool idString = false;
23 
24 	// Retrieve a nodecache given a tuple of NodeDumper constructor options
25 	NodeCache &nodecache = this->nodecachemap[std::make_tuple(indent,idString)];
26 
27 	if (!nodecache.contains(node)) {
28 		NodeDumper dumper(nodecache, this->root_node, indent, idString);
29 		dumper.traverse(*this->root_node);
30 		assert(nodecache.contains(*this->root_node) &&
31 					 "NodeDumper failed to create a cache");
32 	}
33 	return nodecache[node];
34 }
35 
36 /*!
37 	Returns the cached ID string representation of the subtree rooted by \a node.
38 	If node is not cached, the cache will be rebuilt.
39 
40 	The difference between this method and getString() is that the ID string
41 	is stripped for whitespace. Especially indentation whitespace is important to
42 	strip to enable cache hits for equivalent nodes from different scopes.
43 */
getIdString(const AbstractNode & node) const44 const std::string Tree::getIdString(const AbstractNode &node) const
45 {
46 	assert(this->root_node);
47 	const std::string indent = "";
48 	const bool idString = true;
49 
50 	// Retrieve a nodecache given a tuple of NodeDumper constructor options
51 	NodeCache &nodecache = this->nodecachemap[make_tuple(indent,idString)];
52 
53 	if (!nodecache.contains(node)) {
54 		nodecache.clear();
55 		NodeDumper dumper(nodecache, this->root_node, indent, idString);
56 		dumper.traverse(*this->root_node);
57 		assert(nodecache.contains(*this->root_node) &&
58 					 "NodeDumper failed to create id cache");
59 	}
60 	return nodecache[node];
61 }
62 
63 /*!
64 	Sets a new root. Will clear the existing cache.
65  */
setRoot(const AbstractNode * root)66 void Tree::setRoot(const AbstractNode *root)
67 {
68 	this->root_node = root;
69 	this->nodecachemap.clear();
70 }
71 
setDocumentPath(const std::string path)72 void Tree::setDocumentPath(const std::string path){
73 	this->document_path = path;
74 }
75 
getDocumentPath() const76 const std::string Tree::getDocumentPath() const
77 {
78 	return this->document_path;
79 }
80