Bridges-C++  3.4.5-dev1-6-g935685a
Bridges(C++ API)
MLelement.h
Go to the documentation of this file.
1 #ifndef ML_ELEMENT_H
2 
3 #define ML_ELEMENT_H
4 
5 #include "SLelement.h"
6 
7 namespace bridges {
8  namespace datastructure {
45  template <typename E>
46  class MLelement : public SLelement<E> {
47 
48  protected:
49  MLelement<E> *sub_list = nullptr; // link to a sublist
50  bool tag = false;
51 
52  public:
53 
65  MLelement(const E& val = E(), const string& lab = string())
66  : SLelement<E> (nullptr, val, lab) {
67  sub_list = nullptr;
68  tag = false;
69  }
70 
82  MLelement (string label) : SLelement<E> (nullptr, E(), label) {
83  sub_list = nullptr;
84  tag = false;
85  }
86 
97  MLelement (E e, MLelement<E> *next) : SLelement<E> (next, e, string()) {
98  sub_list = nullptr;
99  tag = false;
100  }
101 
109  // Hmmm, need to think about removing link data from
110  // an existing sublist
111  this->sub_list = sl;
112  tag = true;
113 
114  // create link data from node to head of sublist
115  if (sl)
116  this->links[sl];
117 
118  // by default, color and shape sublist nodes to distinguish them
119  this->getVisualizer()->setColor(Color("red"));
120  this->getVisualizer()->setShape(SQUARE);
121  }
122 
129  return sub_list;
130  }
131 
138  MLelement<E> *getNext() override {
139  return (MLelement<E>*) this->next;
140  }
141 
148  MLelement<E> *getNext() const override {
149  return (MLelement<E>*) this->next;
150  }
151 
158  if (n) {
159  if (this->next != n) // remove old link data, possibly
160  this->links.erase(this->next);
161 
162  this->next = n;
163  this->links[this->next];
164  }
165  }
166 
172  void setTag(bool t) {
173  tag = t;
174  }
175 
181  bool getTag() {
182  return tag;
183  }
189  virtual const string getDStype() const override {
190  return "Multilist";
191  }
192  private:
198  virtual const string getDataStructureRepresentation() const override final {
199  vector<const MLelement<E>*> nodes;
200 
201  // get the list of nodes
202  this->getListElements(this, nodes);
203 
204  // generate the JSON string
205 
206  // map the nodes to a sequence of ids, 0...N-1
207  // then get the JSON string for nodes placeholder
208  // nullptr prevents insertion of other nullptrs
209  unordered_map<const MLelement*, int> node_map { {nullptr, -1} };
210 
211  string nodes_JSON, links_JSON;
212 
213  int i = 0; // get the JSON string for nodes
214  for (const auto * e : nodes) {
215  if (node_map.emplace(e, i).second) {
216  // successful emplacement
217  i++;
218  nodes_JSON += e->getElementRepresentation() + COMMA;
219  }
220  }
221  //Remove trailing comma and nullptr entry
222  node_map.erase(nullptr);
223  if (nodes_JSON.size()) {
224  nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
225  }
226 
227  // generate the links representation
228  for (int k = 0; k < nodes.size(); k++) {
229  if (nodes[k]->next != nullptr) { // link exists
230  links_JSON += this->getLinkRepresentation(nodes[k]->links.at(nodes[k]->next),
231  to_string(node_map[nodes[k]]),
232  to_string(node_map[nodes[k]->getNext()])) + COMMA;
233  }
234  if (nodes[k]->tag) { // sublist link
235 
236  if (nodes[k]->sub_list != nullptr) { // link exists
237  links_JSON += this->getLinkRepresentation(
238  nodes[k]->links.at(nodes[k]->sub_list),
239  to_string(node_map[nodes[k]]),
240  to_string(node_map[nodes[k]->sub_list])) + COMMA;
241  }
242  }
243  }
244 
245  //Remove trailing comma
246  if (links_JSON.size()) {
247  links_JSON = links_JSON.erase(links_JSON.size() - 1);
248  }
249 
250  string ml_list_json =
251  QUOTE + "nodes" + QUOTE + COLON +
252  OPEN_BOX + nodes_JSON + CLOSE_BOX + COMMA +
253  QUOTE + "links" + QUOTE + COLON + OPEN_BOX +
254  links_JSON + CLOSE_BOX +
255  CLOSE_CURLY;
256 
257  return ml_list_json;
258  }
259 
260  // virtual void getDataStructureRepresentation(rapidjson::Document& d) const override {
261  // }
268  void getListElements(const MLelement<E> *list,
269  vector<const MLelement<E>*>& nodes) const {
270 
271  auto it = list;
272  while (it != nullptr) {
273  nodes.push_back(it);
274  // if this node has a sublist, recurse
275  if (it->tag) {
276  getListElements(it->sub_list, nodes);
277  }
278  // iterate
279  it = (MLelement<E>*) it->next;
280  }
281  }
282  };
283  }
284 } // end namespace bridges
285 
286 #endif
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:50
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:310
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:92
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:141
void setShape(const Shape &shp)
Set the shape of the element.
Definition: ElementVisualizer.h:140
void setColor(const Color &col)
Set the color to "col".
Definition: ElementVisualizer.h:94
This class can be used to instantiate Multi-list Elements.
Definition: MLelement.h:46
void setNext(MLelement< E > *n)
Sets the element to point to the next MLelement.
Definition: MLelement.h:157
void setTag(bool t)
Sets the tag of the element.
Definition: MLelement.h:172
bool getTag()
Gets the tag of the element.
Definition: MLelement.h:181
MLelement< E > * getNext() override
Retrieves the element following this element.
Definition: MLelement.h:138
void setSubList(MLelement< E > *sl)
Sets the start of a new sublist to the MLelement "next".
Definition: MLelement.h:108
MLelement< E > * getNext() const override
Retrieves the element following this element - const version.
Definition: MLelement.h:148
virtual const string getDStype() const override
Return the string representaion of element.
Definition: MLelement.h:189
MLelement * getSubList()
Gets the sublist at this node, if it exists.
Definition: MLelement.h:128
MLelement(string label)
Creates an MLelement object.
Definition: MLelement.h:82
MLelement(E e, MLelement< E > *next)
Creates an MLelement object.
Definition: MLelement.h:97
bool tag
Definition: MLelement.h:50
MLelement(const E &val=E(), const string &lab=string())
constructor
Definition: MLelement.h:65
MLelement< E > * sub_list
Definition: MLelement.h:49
The singly linked list element, derived from Element.
Definition: SLelement.h:26
SLelement * next
Definition: SLelement.h:29
@ SQUARE
Definition: ElementVisualizer.h:10
Support for drawing Bar charts.
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 CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54
const string QUOTE
Definition: DataStructure.h:50