Bridges-C++  3.4.2
Bridges(C++ API)
SLelement.h
Go to the documentation of this file.
1 #ifndef SLELEMENT_H
2 #define SLELEMENT_H
3 
4 #include "DataStructure.h"
5 #include "Element.h" //string, using std
6 
7 
8 namespace bridges {
9  namespace datastructure {
26  template <typename E>
27  class SLelement : public Element<E>, public DataStructure {
28 
29  protected:
30  SLelement* next = nullptr;
31 
32  public:
44  SLelement(SLelement* next, const E& val = E(), const string& lab =
45  string()) : Element<E>(val, lab) {
46  setNext(next);
47  }
59  SLelement(const E& val = E(), const string& lab = string())
60  : SLelement(nullptr, val, lab) {
61  }
67  virtual const string getDStype() const override {
68  return "SinglyLinkedList";
69  }
70 
75  virtual SLelement* getNext() {
76  return next;
77  }
78 
84  virtual const SLelement* getNext() const {
85  return next;
86  }
87 
93  void setNext(SLelement* n) {
94  //if different, remove old link data
95  if (next != n) {
96  this->links.erase(next);
97  }
98 
99  next = n;
100  this->links[next]; //this creates an entry in links if it does not exist
101  }
102 
103  private:
109  virtual const string getDataStructureRepresentation() const override {
110 
111  vector<const SLelement<E>*> nodes;
112  // get the list of nodes
113  getListElements(nodes);
114  // generate the JSON string
115 
116  if (MAX_ELEMENTS_ALLOWED <= nodes.size()) {
117  // cant exceed max number of elements
118  throw "Max allowed elements(for visualization) exceeded.. " +
119  to_string(nodes.size()) + " Must be less than " +
120  to_string(MAX_ELEMENTS_ALLOWED);
121  }
122 
123  pair<string, string> json_nodes_links = generateJSON(nodes);
124 
125  string sl_list_json =
126  QUOTE + "nodes" + QUOTE + COLON +
127  OPEN_BOX + json_nodes_links.first + CLOSE_BOX + COMMA +
128  QUOTE + "links" + QUOTE + COLON + OPEN_BOX +
129  json_nodes_links.second + CLOSE_BOX +
130  CLOSE_CURLY;
131 
132 
133  return sl_list_json;
134  }
135  protected:
143  virtual const pair<string, string> generateJSON(
144  vector<const SLelement<E>*> nodes) const {
145  // map the nodes to a sequence of ids, 0...N-1
146  // then get the JSON string for nodes placeholder
147  // nullptr prevents insertion of other nullptrs
148  unordered_map<const SLelement*, int> node_map { {nullptr, -1} };
149 
150  string nodes_JSON, links_JSON;
151 
152  int i = 0; // get the JSON string for nodes
153  for (const auto* e : nodes) {
154  if (node_map.emplace(e, i).second) {
155  // successful emplacement
156  i++;
157  nodes_JSON += e->getElementRepresentation() + COMMA;
158  }
159  }
160  //Remove trailing comma and nullptr entry
161  node_map.erase(nullptr);
162  if (nodes_JSON.size()) {
163  nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
164  }
165  // for each pair<SLelement*,int> in map
166  for (unsigned int k = 0; k < nodes.size(); k++) {
167  if (nodes[k]->next != nullptr) { // link exists
168  links_JSON += this->getLinkRepresentation(nodes[k]->links.at(nodes[k]->next),
169  to_string(node_map[nodes[k]]),
170  to_string(node_map[nodes[k]->next]) ) + COMMA;
171  }
172  }
173 
174  //Remove trailing comma
175  if (links_JSON.size()) {
176  links_JSON = links_JSON.erase(links_JSON.size() - 1);
177  }
178 
179  return pair<string, string> (nodes_JSON, links_JSON);
180  }
181  protected:
187  virtual void getListElements(vector<const SLelement<E>*>& nodes) const {
188 
189  //prevents potential infinite loop
190  unordered_set<const SLelement<E>*> visited;
191  auto it = this;
192 
193  // using the visited array handles both regular and
194  // circular lists
195  while (it != nullptr && visited.emplace(it).second) {
196  nodes.push_back(it);
197  it = it->next;
198  }
199  }
200 
201  public:
202 
214  typename bridges::datastructure::SLelement<E> * start;
215 
216  public:
218  : start(s)
219  {}
220 
221  class iterator {
222  typename bridges::datastructure::SLelement<E> *current;
223  public:
225  : current(c)
226  {}
227 
228  bool operator!= (const iterator& it) const {
229  return this->current != it.current;
230  }
231 
232  E const & operator* () const {
233  return current->getValue();
234  }
235 
236  E & operator* () {
237  return current->getValue();
238  }
239 
241  current = current->getNext();
242  return *this;
243  }
245  iterator clone(*this);
246  current = current->getNext();
247  return clone;
248  }
249  };
250 
252  return iterator(start);
253  }
254 
256  return iterator(nullptr);
257  }
258  };
259 
260 
271  typename bridges::datastructure::SLelement<E> const * start;
272 
273  public:
275  : start(s)
276  {}
277 
278  class iterator {
279 
280  typename bridges::datastructure::SLelement< E > const * current;
281  public:
283  : current(c)
284  {}
285 
286  bool operator!=(const iterator& it) const {
287  return this->current != it.current;
288  }
289 
290  E const & operator*() const {
291  return current->getValue();
292  }
293 
295  current = current->getNext();
296  return *this;
297  }
298  };
299 
301  return iterator(start);
302  }
303 
305  return iterator(nullptr);
306  }
307  };
308 
309 
310  }; //end of SLelement class
311  // use some aliases for accessing iterators
312  template <class E>
314  template <class E>
316  }
317 }//end of bridges namespace
318 #endif
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:52
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:273
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:95
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:210
iterator(typename bridges::datastructure::SLelement< E > const *c)
Definition: SLelement.h:282
bool operator!=(const iterator &it) const
Definition: SLelement.h:286
E const & operator*() const
Definition: SLelement.h:290
These are helper classes for SLelement for easy iteration.
Definition: SLelement.h:270
SLelement_constlisthelper(typename bridges::datastructure::SLelement< E > const *s)
Definition: SLelement.h:274
iterator(typename bridges::datastructure::SLelement< E > *c)
Definition: SLelement.h:224
E const & operator*() const
Definition: SLelement.h:232
bool operator!=(const iterator &it) const
Definition: SLelement.h:228
These are helper classes for SLelement for easy iteration.
Definition: SLelement.h:213
iterator begin()
Definition: SLelement.h:251
iterator end()
Definition: SLelement.h:255
SLelement_listhelper(typename bridges::datastructure::SLelement< E > *s)
Definition: SLelement.h:217
The singly linked list element, derived from Element.
Definition: SLelement.h:27
SLelement(const E &val=E(), const string &lab=string())
Constructs an slelement with the provided value.
Definition: SLelement.h:59
virtual const string getDStype() const override
Returns the data structure name.
Definition: SLelement.h:67
virtual const SLelement * getNext() const
Returns the next element in the list - Constant version.
Definition: SLelement.h:84
virtual const pair< string, string > generateJSON(vector< const SLelement< E > * > nodes) const
Generates the JSON representation of the element.
Definition: SLelement.h:143
SLelement(SLelement *next, const E &val=E(), const string &lab=string())
Constructs an slelement with the provided value.
Definition: SLelement.h:44
void setNext(SLelement *n)
Definition: SLelement.h:93
virtual void getListElements(vector< const SLelement< E > * > &nodes) const
Get the list of nodes.
Definition: SLelement.h:187
virtual SLelement * getNext()
Returns the next element in the list.
Definition: SLelement.h:75
SLelement * next
Definition: SLelement.h:30
typename SLelement< E >::SLelement_listhelper SLelement_List
Definition: SLelement.h:313
constexpr int MAX_ELEMENTS_ALLOWED
Definition: DataStructure.h:61
typename SLelement< E >::SLelement_constlisthelper SLelement_ConstList
Definition: SLelement.h:315
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:51
const string OPEN_BOX
Definition: DataStructure.h:54
const string COMMA
Definition: DataStructure.h:50
const string CLOSE_BOX
Definition: DataStructure.h:55
const string CLOSE_CURLY
Definition: DataStructure.h:53
const string QUOTE
Definition: DataStructure.h:49