Bridges-C++  3.1.1
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:
42  SLelement(SLelement* next, const E& val = E(), const string& lab =
43  string()) : Element<E>(val, lab) {
44  setNext(next);
45  }
54  SLelement(const E& val = E(), const string& lab = string())
55  : SLelement(nullptr, val, lab) {
56  }
62  virtual const string getDStype() const override {
63  return "SinglyLinkedList";
64  }
65 
70  virtual SLelement* getNext() {
71  return next;
72  }
73 
79  virtual const SLelement* getNext() const {
80  return next;
81  }
82 
88  void setNext(SLelement* n) {
89  //if different, remove old link data
90  if (next != n) {
91  this->links.erase(next);
92  }
93 
94  next = n;
95  this->links[next]; //this creates an entry in links if it does not exist
96  }
97 
98  private:
104  virtual const string getDataStructureRepresentation() const override {
105 
106  vector<const SLelement<E>*> nodes;
107  // get the list of nodes
108  getListElements(nodes);
109  // generate the JSON string
110 
111  if (MAX_ELEMENTS_ALLOWED <= nodes.size()) {
112  // cant exceed max number of elements
113  throw "Max allowed elements(for visualization) exceeded.. " +
114  to_string(nodes.size()) + " Must be less than " +
115  to_string(MAX_ELEMENTS_ALLOWED);
116  }
117 
118  pair<string, string> json_nodes_links = generateJSON(nodes);
119 
120  string sl_list_json =
121  QUOTE + "nodes" + QUOTE + COLON +
122  OPEN_BOX + json_nodes_links.first + CLOSE_BOX + COMMA +
123  QUOTE + "links" + QUOTE + COLON + OPEN_BOX +
124  json_nodes_links.second + CLOSE_BOX +
125  CLOSE_CURLY;
126 
127 
128  return sl_list_json;
129  }
130  protected:
136  virtual const pair<string, string> generateJSON(
137  vector<const SLelement<E>*> nodes) const {
138  // map the nodes to a sequence of ids, 0...N-1
139  // then get the JSON string for nodes placeholder
140  // nullptr prevents insertion of other nullptrs
141  unordered_map<const SLelement*, int> node_map { {nullptr, -1} };
142 
143  string nodes_JSON, links_JSON;
144 
145  int i = 0; // get the JSON string for nodes
146  for (const auto* e : nodes) {
147  if (node_map.emplace(e, i).second) {
148  // successful emplacement
149  i++;
150  nodes_JSON += e->getElementRepresentation() + COMMA;
151  }
152  }
153  //Remove trailing comma and nullptr entry
154  node_map.erase(nullptr);
155  if (nodes_JSON.size()) {
156  nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
157  }
158  // for each pair<SLelement*,int> in map
159  for (unsigned int k = 0; k < nodes.size(); k++) {
160  if (nodes[k]->next != nullptr) { // link exists
161  links_JSON += this->getLinkRepresentation(nodes[k]->links.at(nodes[k]->next),
162  to_string(node_map[nodes[k]]),
163  to_string(node_map[nodes[k]->next]) ) + COMMA;
164  }
165  }
166 
167  //Remove trailing comma
168  if (links_JSON.size()) {
169  links_JSON = links_JSON.erase(links_JSON.size() - 1);
170  }
171 
172  return pair<string, string> (nodes_JSON, links_JSON);
173  }
174  protected:
180  virtual void getListElements(vector<const SLelement<E>*>& nodes) const {
181 
182  //prevents potential infinite loop
183  unordered_set<const SLelement<E>*> visited;
184  auto it = this;
185 
186  // using the visited array handles both regular and
187  // circular lists
188  while (it != nullptr && visited.emplace(it).second) {
189  nodes.push_back(it);
190  it = it->next;
191  }
192  }
193 
194  public:
195 
199 
201  typename bridges::datastructure::SLelement<E> * start;
202 
203  public:
205  : start(s)
206  {}
207 
208  class iterator {
209  typename bridges::datastructure::SLelement<E> *current;
210  public:
212  : current(c)
213  {}
214 
215  bool operator!= (const iterator& it) const {
216  return this->current != it.current;
217  }
218 
219  E const & operator* () const {
220  return current->getValue();
221  }
222 
223  E & operator* () {
224  return current->getValue();
225  }
226 
228  current = current->getNext();
229  return *this;
230  }
232  iterator clone(*this);
233  current = current->getNext();
234  return clone;
235  }
236  };
237 
239  return iterator(start);
240  }
241 
243  return iterator(nullptr);
244  }
245  };
246 
250  typename bridges::datastructure::SLelement<E> const * start;
251 
252  public:
254  : start(s)
255  {}
256 
257  class iterator {
258 
259  typename bridges::datastructure::SLelement< E > const * current;
260  public:
262  : current(c)
263  {}
264 
265  bool operator!=(const iterator& it) const {
266  return this->current != it.current;
267  }
268 
269  E const & operator*() const {
270  return current->getValue();
271  }
272 
274  current = current->getNext();
275  return *this;
276  }
277  };
278 
280  return iterator(start);
281  }
282 
284  return iterator(nullptr);
285  }
286  };
287 
288 
289  }; //end of SLelement class
290  // use some aliases for accessing iterators
291  template <class E>
293  template <class E>
295  }
296 }//end of bridges namespace
297 #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
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:256
constexpr int MAX_ELEMENTS_ALLOWED
Definition: DataStructure.h:61
bool operator!=(const iterator &it) const
Definition: SLelement.h:265
typename SLelement< E >::SLelement_constlisthelper SLelement_ConstList
Definition: SLelement.h:294
iterator begin()
Definition: SLelement.h:238
virtual const pair< string, string > generateJSON(vector< const SLelement< E > *> nodes) const
Definition: SLelement.h:136
E const & operator*() const
Definition: SLelement.h:219
const string COLON
Definition: DataStructure.h:51
E const & getValue() const
Definition: Element.h:195
iterator(typename bridges::datastructure::SLelement< E > const *c)
Definition: SLelement.h:261
SLelement * next
Definition: SLelement.h:30
SLelement_listhelper(typename bridges::datastructure::SLelement< E > *s)
Definition: SLelement.h:204
const string OPEN_BOX
Definition: DataStructure.h:54
iterator(typename bridges::datastructure::SLelement< E > *c)
Definition: SLelement.h:211
const string CLOSE_CURLY
Definition: DataStructure.h:53
virtual const SLelement * getNext() const
Definition: SLelement.h:79
these are helper classes for SLelement for easy iteration in a range for loop. It is not meant to be ...
Definition: SLelement.h:249
virtual void getListElements(vector< const SLelement< E > *> &nodes) const
Definition: SLelement.h:180
The singly linked list element, derived from Element.
Definition: SLelement.h:27
bool operator!=(const iterator &it) const
Definition: SLelement.h:215
SLelement_constlisthelper(typename bridges::datastructure::SLelement< E > const *s)
Definition: SLelement.h:253
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 CLOSE_BOX
Definition: DataStructure.h:55
typename SLelement< E >::SLelement_listhelper SLelement_List
Definition: SLelement.h:292
virtual SLelement * getNext()
Definition: SLelement.h:70
these are helper classes for SLelement for easy iteration in a range for loop. It is not meant to be ...
Definition: SLelement.h:200
SLelement(const E &val=E(), const string &lab=string())
Definition: SLelement.h:54
E const & operator*() const
Definition: SLelement.h:269
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:87
virtual const string getDStype() const override
Definition: SLelement.h:62
iterator end()
Definition: SLelement.h:242
const string COMMA
Definition: DataStructure.h:50
const string QUOTE
Definition: DataStructure.h:49
SLelement(SLelement *next, const E &val=E(), const string &lab=string())
Definition: SLelement.h:42
void setNext(SLelement *n)
Definition: SLelement.h:88