Bridges-C++  3.1.1
Bridges(C++API)
Array.h
Go to the documentation of this file.
1 #ifndef ARRAY_H
2 #define ARRAY_H
3 
4 #include "Element.h"
5 
6 #include "Bridges.h"
7 
8 namespace bridges {
9  namespace datastructure {
20  template <typename E>
21  class Array : public DataStructure {
22 
23  friend class Element<E>;
24  private:
25  Element<E> *array_data;
26  int num_dims;
27  int dims[3];
28  int size;
29 
30  protected:
32  : array_data(nullptr), num_dims(0), dims{0, 0, 0}, size(0) {
33  }
34 
35  virtual ~Array() {
36  if (array_data != nullptr)
37  delete [] array_data;
38  }
39 
43  Array(int sz)
44  : array_data(nullptr) {
45  int dim[] = {1, 1, sz};
46  setSize(sz, dim);
47  }
48 
55  void setSize(int nd, int *dim) {
56  if (dim[0] <= 0 || dim[1] <= 0 || dim[2] <= 0) {
57  cout << "Dimensions of array must be positive!" << endl
58  << "\tProvided dimensions: " << dim[0]
59  << "," << dim[1] << "," << dim[2] << endl;
60  throw;
61  }
62  dims[0] = dim[0];
63  dims[1] = dim[1];
64  dims[2] = dim[2];
65  num_dims = nd;
66  size = dim[0] * dim[1] * dim[2];
67 
68  // allocate space for the array
69  if (array_data != nullptr)
70  delete[] array_data;
71  array_data = new Element<E>[size];
72  }
78  void getDimensions(int *dim) {
79  switch (num_dims) {
80  case 1:
81  dim[0] = dims[0];
82  break;
83  case 2:
84  dim[0] = dims[0];
85  dim[1] = dims[1];
86  break;
87  case 3:
88  dim[0] = dims[0];
89  dim[1] = dims[1];
90  dim[2] = dims[2];
91  break;
92  }
93  }
94 
95  int const * getDimensions() const {
96  return dims;
97  }
106  Element<E>& getElement(int index) {
107  return array_data[index];
108  }
109 
117  Element<E> const & getElement(int index) const {
118  return array_data[index];
119  }
120 
127  void setElement(int ind, Element<E> el) {
128  array_data[ind] = el;
129  }
130  public:
136  virtual const string getDStype() const override {
137  return "Array";
138  }
139 
140  virtual const string getDataStructureRepresentation() const override final {
142 
143  vector<const Element<E>*> nodes;
144 
145  for (int k = 0; k < size; k++) {
146  nodes.push_back(&array_data[k]);
147  }
148  // first write out dimensions
149  string array_json =
150  QUOTE + "dims" + QUOTE + COLON +
151  OPEN_BOX +
152  JSONencode(dims[0]) + COMMA +
153  JSONencode(dims[1]) + COMMA +
154  JSONencode(dims[2]) +
155  CLOSE_BOX + COMMA +
156 
157  QUOTE + "nodes" + QUOTE + COLON +
158  OPEN_BOX +
159  generateJSON(nodes) +
161 
162  return array_json;
163  }
164 
165  Array(const Array&) = delete; //would be incorrect, so disabled.
166  Array& operator=(const Array&) = delete; //would be incorrect, so disabled.
167 
168 
169  private:
170  const string generateJSON( const vector<const Element<E>*>& nodes) const {
171  if (MAX_ELEMENTS_ALLOWED <= nodes.size()) {
172  // cant exceed max number of elements
173  throw "Max allowed elements(for visualization) exceeded.. " +
174  to_string(nodes.size()) + " Must be less than " +
175  to_string(MAX_ELEMENTS_ALLOWED);
176  }
177  // map the nodes to a sequence of ids, 0...N-1
178  // then get the JSON string for nodes placeholder
179  // nullptr prevents insertion of other nullptrs
180  unordered_map<const Element<E>*, int> map{{nullptr, -1}};
181 
182  string nodes_JSON;
183 
184  int i = 0; // get the JSON string for nodes
185  for (const auto* e : nodes) {
186  if (map.emplace(e, i).second && ++i) {
187  // short circut only incriments i and gets rep
188  // upon successful emplacement
189  nodes_JSON += e->getElementRepresentation() + COMMA;
190  }
191  }
192  map.erase(nullptr); //Remove trailing comma and nullptr entry
193  if (nodes_JSON.size()) {
194  nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
195  }
196 
197  return nodes_JSON;
198  };
199 
200 
201  }; // Array
202  }
203 }// end namespace bridges
204 
205 #endif
int const * getDimensions() const
Definition: Array.h:95
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
constexpr int MAX_ELEMENTS_ALLOWED
Definition: DataStructure.h:61
Array(int sz)
builds an array given the dimensions
Definition: Array.h:43
virtual const string getDStype() const override
Definition: Array.h:136
const string COLON
Definition: DataStructure.h:51
virtual const string getDataStructureRepresentation() const override final
Definition: Array.h:140
The foundation of BRIDGES array types. It is not meant to be used directly by students.
Definition: Array.h:21
const string OPEN_BOX
Definition: DataStructure.h:54
void setElement(int ind, Element< E > el)
Set the Element at index ind - 1D array.
Definition: Array.h:127
const string CLOSE_CURLY
Definition: DataStructure.h:53
void getDimensions(int *dim)
Get the dimensions of the array.
Definition: Array.h:78
virtual ~Array()
Definition: Array.h:35
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
void setSize(int nd, int *dim)
Set the size of the array.
Definition: Array.h:55
const string CLOSE_BOX
Definition: DataStructure.h:55
Element< E > & getElement(int index)
Definition: Array.h:106
Array()
Definition: Array.h:31
Array & operator=(const Array &)=delete
const string COMMA
Definition: DataStructure.h:50
Element< E > const & getElement(int index) const
Get the object at index index - 1D array.
Definition: Array.h:117
const string QUOTE
Definition: DataStructure.h:49
std::string JSONencode(const T &d)
Definition: JSONutil.h:37