Bridges-C++  3.4.2
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 
45  Array(int sz)
46  : array_data(nullptr) {
47  int dim[] = {1, 1, sz};
48  setSize(sz, dim);
49  }
50 
57  void setSize(int nd, int *dim) {
58  if (dim[0] <= 0 || dim[1] <= 0 || dim[2] <= 0) {
59  cout << "Dimensions of array must be positive!" << endl
60  << "\tProvided dimensions: " << dim[0]
61  << "," << dim[1] << "," << dim[2] << endl;
62  throw;
63  }
64  dims[0] = dim[0];
65  dims[1] = dim[1];
66  dims[2] = dim[2];
67  num_dims = nd;
68  size = dim[0] * dim[1] * dim[2];
69 
70  // allocate space for the array
71  if (array_data != nullptr)
72  delete[] array_data;
73  array_data = new Element<E>[size];
74  }
80  void getDimensions(int *dim) {
81  switch (num_dims) {
82  case 1:
83  dim[0] = dims[0];
84  break;
85  case 2:
86  dim[0] = dims[0];
87  dim[1] = dims[1];
88  break;
89  case 3:
90  dim[0] = dims[0];
91  dim[1] = dims[1];
92  dim[2] = dims[2];
93  break;
94  }
95  }
96 
97  int const * getDimensions() const {
98  return dims;
99  }
108  Element<E>& getElement(int index) {
109  return array_data[index];
110  }
111 
119  Element<E> const & getElement(int index) const {
120  return array_data[index];
121  }
122 
129  void setElement(int ind, Element<E> el) {
130  array_data[ind] = el;
131  }
132  public:
138  virtual const string getDStype() const override {
139  return "Array";
140  }
141 
142  virtual const string getDataStructureRepresentation() const override final {
144 
145  vector<const Element<E>*> nodes;
146 
147  for (int k = 0; k < size; k++) {
148  nodes.push_back(&array_data[k]);
149  }
150  // first write out dimensions
151  string array_json =
152  QUOTE + "dims" + QUOTE + COLON +
153  OPEN_BOX +
154  JSONencode(dims[0]) + COMMA +
155  JSONencode(dims[1]) + COMMA +
156  JSONencode(dims[2]) +
157  CLOSE_BOX + COMMA +
158 
159  QUOTE + "nodes" + QUOTE + COLON +
160  OPEN_BOX +
161  generateJSON(nodes) +
163 
164  return array_json;
165  }
166 
167  Array(const Array&) = delete; //would be incorrect, so disabled.
168  Array& operator=(const Array&) = delete; //would be incorrect, so disabled.
169 
170 
171  private:
172  const string generateJSON( const vector<const Element<E>*>& nodes) const {
173  if (MAX_ELEMENTS_ALLOWED <= nodes.size()) {
174  // cant exceed max number of elements
175  throw "Max allowed elements(for visualization) exceeded.. " +
176  to_string(nodes.size()) + " Must be less than " +
177  to_string(MAX_ELEMENTS_ALLOWED);
178  }
179  // map the nodes to a sequence of ids, 0...N-1
180  // then get the JSON string for nodes placeholder
181  // nullptr prevents insertion of other nullptrs
182  unordered_map<const Element<E>*, int> map{{nullptr, -1}};
183 
184  string nodes_JSON;
185 
186  int i = 0; // get the JSON string for nodes
187  for (const auto* e : nodes) {
188  if (map.emplace(e, i).second && ++i) {
189  // short circut only incriments i and gets rep
190  // upon successful emplacement
191  nodes_JSON += e->getElementRepresentation() + COMMA;
192  }
193  }
194  map.erase(nullptr); //Remove trailing comma and nullptr entry
195  if (nodes_JSON.size()) {
196  nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
197  }
198 
199  return nodes_JSON;
200  };
201 
202 
203  }; // Array
204  }
205 }// end namespace bridges
206 
207 #endif
The foundation of BRIDGES array types. It is not meant to be used directly by students.
Definition: Array.h:21
void setElement(int ind, Element< E > el)
Set the Element at index ind - 1D array.
Definition: Array.h:129
virtual const string getDataStructureRepresentation() const override final
Definition: Array.h:142
Array()
Definition: Array.h:31
void setSize(int nd, int *dim)
Set the size of the array.
Definition: Array.h:57
virtual const string getDStype() const override
Definition: Array.h:138
virtual ~Array()
Definition: Array.h:35
Array & operator=(const Array &)=delete
int const * getDimensions() const
Definition: Array.h:97
void getDimensions(int *dim)
Get the dimensions of the array.
Definition: Array.h:80
Element< E > & getElement(int index)
Definition: Array.h:108
Array(const Array &)=delete
Element< E > const & getElement(int index) const
Get the object at index index - 1D array.
Definition: Array.h:119
Array(int sz)
builds an array given the size
Definition: Array.h:45
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
std::string JSONencode(const T &d)
Definition: JSONutil.h:37
constexpr int MAX_ELEMENTS_ALLOWED
Definition: DataStructure.h:61
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