Bridges-C++ 3.5.0-dev1
Bridges(C++ API)
Loading...
Searching...
No Matches
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
8namespace 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 private:
171 const string generateJSON( const vector<const Element<E>*>& nodes) const {
172 if (MAX_ELEMENTS_ALLOWED <= nodes.size()) {
173 // cant exceed max number of elements
174 throw "Max allowed elements(for visualization) exceeded.. " +
175 to_string(nodes.size()) + " Must be less than " +
176 to_string(MAX_ELEMENTS_ALLOWED);
177 }
178 // map the nodes to a sequence of ids, 0...N-1
179 // then get the JSON string for nodes placeholder
180 // nullptr prevents insertion of other nullptrs
181 unordered_map<const Element<E>*, int> map{{nullptr, -1}};
182
183 string nodes_JSON;
184
185 int i = 0; // get the JSON string for nodes
186 for (const auto * e : nodes) {
187 if (map.emplace(e, i).second && ++i) {
188 // short circut only incriments i and gets rep
189 // upon successful emplacement
190 nodes_JSON += e->getElementRepresentation() + COMMA;
191 }
192 }
193 map.erase(nullptr); //Remove trailing comma and nullptr entry
194 if (nodes_JSON.size()) {
195 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
196 }
197
198 return nodes_JSON;
199 };
200
201 }; // Array
202 }
203}// end namespace bridges
204
205#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
Element< E > const & getElement(int index) const
Get the object at index index - 1D array.
Definition Array.h:119
virtual ~Array()
Definition Array.h:35
Element< E > & getElement(int index)
Definition Array.h:108
void getDimensions(int *dim)
Get the dimensions of the array.
Definition Array.h:80
Array(const Array &)=delete
Array & operator=(const Array &)=delete
int const * getDimensions() const
Definition Array.h:97
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:74
This is the fundamental building block for all data structures in BRIDGES.
Definition Element.h:51
std::string JSONencode(const T &d)
Definition JSONutil.h:38
constexpr int MAX_ELEMENTS_ALLOWED
Definition DataStructure.h:62
Support for drawing Bar charts.
Definition alltypes.h:4
const string COLON
Definition DataStructure.h:52
const string OPEN_BOX
Definition DataStructure.h:55
const string COMMA
Definition DataStructure.h:51
const string CLOSE_BOX
Definition DataStructure.h:56
const string CLOSE_CURLY
Definition DataStructure.h:54
const string QUOTE
Definition DataStructure.h:50