Bridges-C++  3.1.1
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 {
44  template <typename E>
45  class MLelement : public SLelement<E> {
46 
47  protected:
48  MLelement<E> *sub_list = nullptr; // link to a sublist
49  bool tag = false;
50 
51  public:
52 
63  MLelement(const E& val = E(), const string& lab = string())
64  : SLelement<E> (nullptr, val, lab) {
65  sub_list = nullptr;
66  tag = false;
67  }
68 
78  MLelement (string label) : SLelement<E> (nullptr, E(), label) {
79  sub_list = nullptr;
80  tag = false;
81  }
82 
92  MLelement (E e, MLelement<E> *next) : SLelement<E> (next, e, string()) {
93  sub_list = nullptr;
94  tag = false;
95  }
96 
104  // Hmmm, need to think about removing link data from
105  // an existing sublist
106  this->sub_list = sl;
107  tag = true;
108 
109  // create link data from node to head of sublist
110  if (sl)
111  this->links[sl];
112 
113  // by default, color and shape sublist nodes to distinguish them
114  this->getVisualizer()->setColor(Color("red"));
115  this->getVisualizer()->setShape(SQUARE);
116  }
117 
124  return sub_list;
125  }
126 
133  MLelement<E> *getNext() override {
134  return (MLelement<E>*) this->next;
135  }
136 
143  MLelement<E> *getNext() const override {
144  return (MLelement<E>*) this->next;
145  }
146 
153  if (n) {
154  if (this->next != n) // remove old link data, possibly
155  this->links.erase(this->next);
156 
157  this->next = n;
158  this->links[this->next];
159  }
160  }
161 
167  void setTag(bool t) {
168  tag = t;
169  }
170 
176  bool getTag() {
177  return tag;
178  }
182  virtual const string getDStype() const override {
183  return "Multilist";
184  }
185  private:
191  virtual const string getDataStructureRepresentation() const override final {
192  vector<const MLelement<E>*> nodes;
193 
194  // get the list of nodes
195  this->getListElements(this, nodes);
196 
197  // generate the JSON string
198 
199  // map the nodes to a sequence of ids, 0...N-1
200  // then get the JSON string for nodes placeholder
201  // nullptr prevents insertion of other nullptrs
202  unordered_map<const MLelement*, int> node_map { {nullptr, -1} };
203 
204  string nodes_JSON, links_JSON;
205 
206  int i = 0; // get the JSON string for nodes
207  for (const auto* e : nodes) {
208  if (node_map.emplace(e, i).second) {
209  // successful emplacement
210  i++;
211  nodes_JSON += e->getElementRepresentation() + COMMA;
212  }
213  }
214  //Remove trailing comma and nullptr entry
215  node_map.erase(nullptr);
216  if (nodes_JSON.size()) {
217  nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
218  }
219 
220  // generate the links representation
221  for (int k = 0; k < nodes.size(); k++) {
222  if (nodes[k]->next != nullptr) { // link exists
223  links_JSON += this->getLinkRepresentation(nodes[k]->links.at(nodes[k]->next),
224  to_string(node_map[nodes[k]]),
225  to_string(node_map[nodes[k]->getNext()])) + COMMA;
226  }
227  if (nodes[k]->tag) { // sublist link
228 
229  if (nodes[k]->sub_list != nullptr) { // link exists
230  links_JSON += this->getLinkRepresentation(
231  nodes[k]->links.at(nodes[k]->sub_list),
232  to_string(node_map[nodes[k]]),
233  to_string(node_map[nodes[k]->sub_list])) + COMMA;
234  }
235  }
236  }
237 
238  //Remove trailing comma
239  if (links_JSON.size()) {
240  links_JSON = links_JSON.erase(links_JSON.size() - 1);
241  }
242 
243  string ml_list_json =
244  QUOTE + "nodes" + QUOTE + COLON +
245  OPEN_BOX + nodes_JSON + CLOSE_BOX + COMMA +
246  QUOTE + "links" + QUOTE + COLON + OPEN_BOX +
247  links_JSON + CLOSE_BOX +
248  CLOSE_CURLY;
249 
250  return ml_list_json;
251  }
257  void getListElements(const MLelement<E> *list,
258  vector<const MLelement<E>*>& nodes) const {
259 
260  auto it = list;
261  while (it != nullptr) {
262  nodes.push_back(it);
263  // if this node has a sublist, recurse
264  if (it->tag) {
265  getListElements(it->sub_list, nodes);
266  }
267  // iterate
268  it = (MLelement<E>*) it->next;
269  }
270  }
271  };
272  }
273 } // end namespace bridges
274 
275 #endif
MLelement< E > * sub_list
Definition: MLelement.h:48
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:256
MLelement< E > * getNext() const override
Definition: MLelement.h:143
MLelement< E > * getNext() override
Definition: MLelement.h:133
MLelement(const E &val=E(), const string &lab=string())
Definition: MLelement.h:63
const string COLON
Definition: DataStructure.h:51
void setNext(MLelement< E > *n)
Definition: MLelement.h:152
SLelement * next
Definition: SLelement.h:30
const string OPEN_BOX
Definition: DataStructure.h:54
virtual const string getDStype() const override
Definition: MLelement.h:182
MLelement * getSubList()
Definition: MLelement.h:123
void setColor(const Color &col)
Set the color to "col".
Definition: ElementVisualizer.h:79
MLelement(string label)
Definition: MLelement.h:78
const string CLOSE_CURLY
Definition: DataStructure.h:53
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:51
The singly linked list element, derived from Element.
Definition: SLelement.h:27
MLelement(E e, MLelement< E > *next)
Definition: MLelement.h:92
bool getTag()
Gets the tag of the element.
Definition: MLelement.h:176
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
void setSubList(MLelement< E > *sl)
Definition: MLelement.h:103
void setTag(bool t)
Sets the tag of the element.
Definition: MLelement.h:167
const string CLOSE_BOX
Definition: DataStructure.h:55
This class can be used to instantiate Multi-list Elements.
Definition: MLelement.h:45
ElementVisualizer * getVisualizer()
Definition: Element.h:136
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:87
void setShape(const Shape &shp)
Set the shape of the element.
Definition: ElementVisualizer.h:122
const string COMMA
Definition: DataStructure.h:50
bool tag
Definition: MLelement.h:49
const string QUOTE
Definition: DataStructure.h:49
Definition: ElementVisualizer.h:10