Bridges-C++  3.1.1
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 
8 namespace bridges {
9  namespace datastructure {
23  template <typename E>
24  class TreeElement : public Element<E>, public DataStructure {
25  private:
26  vector<TreeElement*> children;
27  public:
36  TreeElement(const E& e = E(), const string& lab = string())
37  : Element<E>(e, lab) {
38  }
43  virtual const string getDStype() const override {
44  return "Tree";
45  }
50  vector<TreeElement*>& getChildren() {
51  return children;
52  }
59  const vector<TreeElement*>& getChildren() const {
60  return children;
61  }
68  TreeElement* getChild(const int& n) {
69  return (n >= children.size() || n < 0 ) ? nullptr : children.at(n);
70  }
79  const TreeElement* getChild(const int& n) const {
80  return (n >= children.size() || n < 0) ? nullptr : children.at(n);
81  }
87  void addChild(TreeElement* child) {
88  children.push_back(child);
89  if (child) {
90  this->links[child];
91  }
92  }
99  void setChild(const size_t& index, TreeElement* kid) {
108  if ((index < children.size()) && index >= 0) {
109  children[index] = kid;
110  this->links.erase(children[index]); // remove any existing links
111  this->links[kid]; // initialize this element in the map
112  }
113  }
114  private:
128  virtual const string getDataStructureRepresentation() const override final {
129  //TODO: Check for exceeding max node
130  string tree_json =
131  QUOTE + "nodes" + QUOTE + COLON +
132  (OPEN_CURLY +
133  this->preOrder((TreeElement<E>*)this)
135 
136  return tree_json;
137  }
138 
145  string preOrder(TreeElement<E>* root) const {
147  string json_str = "", children = "", link_props = "", elem_rep = "";
148  string t_str;
149  if (root != NULL) {
150  // first get the node representation
151  elem_rep = root->getElementRepresentation();
152  // remove surrounding curly braces
153  t_str = elem_rep.substr(1, elem_rep.size() - 2);
154  json_str += t_str + COMMA;
155  // now get the children
156  if (root->children.size())
157  json_str += QUOTE + "children" + QUOTE + COLON + OPEN_BOX ;
158  for (int k = 0; k < root->children.size(); k++) {
159  if (root->children[k] == NULL) {
160  json_str += OPEN_CURLY + QUOTE + "name" + QUOTE + COLON +
161  QUOTE + "NULL" + QUOTE + CLOSE_CURLY + COMMA;
162  }
163  else {
164  LinkVisualizer *lv =
165  root->getLinkVisualizer(root->children[k]);
166  json_str += OPEN_CURLY;
167  if (lv) {
168  Color c = lv->getColor();
169  json_str += QUOTE + "linkProperties" + QUOTE + COLON + OPEN_CURLY +
170  QUOTE + "color" + QUOTE + COLON +
171  c.getCSSRepresentation() + COMMA +
172  QUOTE + "thickness" + QUOTE + COLON +
173  JSONencode(lv->getThickness()) +
174  // QUOTE + "weight" + QUOTE + COLON +
175  // JSONencode(lv->getWeight()) +
176  CLOSE_CURLY + COMMA;
177  }
178  else
179  json_str += "linkProperties" + COLON + "{}" + COMMA;
180  // process its children
181  json_str += preOrder(root->children[k]);
182  json_str += CLOSE_CURLY + COMMA;
183  }
184  }
185  // remove last comma
186  json_str = json_str.substr(0, json_str.size() - 1);
187  // end of children
188  if (root->children.size())
189  json_str += CLOSE_BOX;
190  }
191 
192  return json_str;
193  }
194  }; //end of TreeElement class
195 
196  }
197 }//end of bridges namespace
198 #endif
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:44
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
const string COLON
Definition: DataStructure.h:51
const string OPEN_BOX
Definition: DataStructure.h:54
const vector< TreeElement * > & getChildren() const
Definition: TreeElement.h:59
TreeElement(const E &e=E(), const string &lab=string())
Definition: TreeElement.h:36
void setChild(const size_t &index, TreeElement *kid)
Definition: TreeElement.h:99
vector< TreeElement * > & getChildren()
Definition: TreeElement.h:50
const string CLOSE_CURLY
Definition: DataStructure.h:53
This class can be used to create tree elements, derived from Element.
Definition: TreeElement.h:24
TreeElement * getChild(const int &n)
Definition: TreeElement.h:68
virtual const string getElementRepresentation() const
Definition: Element.h:221
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:51
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 OPEN_CURLY
Definition: DataStructure.h:52
const string CLOSE_BOX
Definition: DataStructure.h:55
const string getCSSRepresentation() const
Definition: Color.h:404
virtual const string getDStype() const override
Definition: TreeElement.h:43
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:87
const TreeElement * getChild(const int &n) const
Definition: TreeElement.h:79
const string COMMA
Definition: DataStructure.h:50
const string QUOTE
Definition: DataStructure.h:49
void addChild(TreeElement *child)
Definition: TreeElement.h:87
std::string JSONencode(const T &d)
Definition: JSONutil.h:37
LinkVisualizer * getLinkVisualizer(const Element *el)
Definition: Element.h:154