Bridges-C++ 3.5.0-dev2-1-ge3e57bf
Bridges(C++ API)
TreeElement.h
Go to the documentation of this file.
1#ifndef TREE_ELEMENT_H
2#define TREE_ELEMENT_H
3
4#include <vector>
5
6#include "Element.h" //string, using std
7
8namespace bridges {
9 namespace datastructure {
23 template <typename E>
24 class TreeElement : public Element<E>, public DataStructure {
25 private:
26 vector<TreeElement*> children;
27 public:
38 TreeElement(const E& e = E(), const string& lab = string())
39 : Element<E>(e, lab) {
40 }
45 virtual const string getDStype() const override {
46 return "Tree";
47 }
52 vector<TreeElement*>& getChildren() {
53 return children;
54 }
61 const vector<TreeElement*>& getChildren() const {
62 return children;
63 }
73 TreeElement* getChild(const int& n) {
74 return (n >= children.size() || n < 0 ) ? nullptr : children.at(n);
75 }
86 const TreeElement* getChild(const int& n) const {
87 return (n >= children.size() || n < 0) ? nullptr : children.at(n);
88 }
94 void addChild(TreeElement* child) {
95 children.push_back(child);
96 if (child) {
97 this->links[child];
98 }
99 }
108 void setChild(const size_t& index, TreeElement* kid) {
117 if ((index < children.size()) && index >= 0) {
118 children[index] = kid;
119 this->links.erase(children[index]); // remove any existing links
120 this->links[kid]; // initialize this element in the map
121 }
122 }
123 private:
137 virtual const string getDataStructureRepresentation() const override final {
138 //TODO: Check for exceeding max node
139 string tree_json =
140 QUOTE + "nodes" + QUOTE + COLON +
141 (OPEN_CURLY +
142 this->preOrder((TreeElement<E>*)this)
144
145 return tree_json;
146 }
147
154 string preOrder(TreeElement<E>* root) const {
156 string json_str = "", children = "", link_props = "", elem_rep = "";
157 string t_str;
158 if (root != NULL) {
159 // first get the node representation
160 elem_rep = root->getElementRepresentation();
161 // remove surrounding curly braces
162 t_str = elem_rep.substr(1, elem_rep.size() - 2);
163 json_str += t_str + COMMA;
164 // now get the children
165 if (root->children.size())
166 json_str += QUOTE + "children" + QUOTE + COLON + OPEN_BOX ;
167 for (int k = 0; k < root->children.size(); k++) {
168 if (root->children[k] == NULL) {
169 json_str += OPEN_CURLY + QUOTE + "name" + QUOTE + COLON +
170 QUOTE + "NULL" + QUOTE + CLOSE_CURLY + COMMA;
171 }
172 else {
173 LinkVisualizer *lv =
174 root->getLinkVisualizer(root->children[k]);
175 json_str += OPEN_CURLY;
176 if (lv) {
177 Color c = lv->getColor();
178 json_str += QUOTE + "linkProperties" + QUOTE + COLON + OPEN_CURLY +
179 QUOTE + "color" + QUOTE + COLON +
180 c.getCSSRepresentation() + COMMA +
181 QUOTE + "thickness" + QUOTE + COLON +
182 JSONencode(lv->getThickness()) + COMMA +
183 // (!lv->getLabel().empty() ?
184 QUOTE + "label" + QUOTE + COLON +
185 JSONencode(lv->getLabel()) +
186
187 // QUOTE + "weight" + QUOTE + COLON +
188 // JSONencode(lv->getWeight()) +
190 }
191 else
192 json_str += "linkProperties" + COLON + "{}" + COMMA;
193 // process its children
194 json_str += preOrder(root->children[k]);
195 json_str += CLOSE_CURLY + COMMA;
196 }
197 }
198 // remove last comma
199 json_str = json_str.substr(0, json_str.size() - 1);
200 // end of children
201 if (root->children.size())
202 json_str += CLOSE_BOX;
203 }
204
205 return json_str;
206 }
207 }; //end of TreeElement class
208
209 }
210}//end of bridges namespace
211#endif
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:74
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:51
virtual const string getElementRepresentation() const
Gets the JSON string of the element representation.
Definition: Element.h:235
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:162
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:92
This class can be used to create tree elements, derived from Element.
Definition: TreeElement.h:24
TreeElement(const E &e=E(), const string &lab=string())
Construct a TreeElement from given values.
Definition: TreeElement.h:38
TreeElement * getChild(const int &n)
Gets the nth child of this node.
Definition: TreeElement.h:73
vector< TreeElement * > & getChildren()
Get the children of this node.
Definition: TreeElement.h:52
void addChild(TreeElement *child)
Adds a child to children.
Definition: TreeElement.h:94
virtual const string getDStype() const override
Get the data structure name.
Definition: TreeElement.h:45
const vector< TreeElement * > & getChildren() const
Get the children of this node.
Definition: TreeElement.h:61
void setChild(const size_t &index, TreeElement *kid)
Sets child at index to "kid".
Definition: TreeElement.h:108
const TreeElement * getChild(const int &n) const
Gets the nth child of this node - constant version.
Definition: TreeElement.h:86
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string OPEN_CURLY
Definition: DataStructure.h:53
const string CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54
const string QUOTE
Definition: DataStructure.h:50