Bridges-C++  3.2.0
Bridges(C++API)
Array2D.h
Go to the documentation of this file.
1 #ifndef ARRAY_2D_H
2 #define ARRAY_2D_H
3 
4 
5 #include "Element.h"
6 #include "Array.h"
7 
8 namespace bridges {
9  namespace datastructure {
42  template <typename E>
43  class Array2D : public Array<E> {
44 
45  private:
46  int num_rows;
47  int num_cols;
48 
49  Array2D()
50  : num_rows(0), num_cols(0) {
51  }
52  public:
53 
54  virtual ~Array2D() {
55  }
56 
61 
62  Array2D(int rows, int cols)
63  : num_rows(rows), num_cols(cols) {
64  int dims[] = {cols, rows, 1};
65  this->setSize (2, dims);
66  }
71  int getNumRows () {
72  return num_rows;
73  }
79  int getNumColumns () {
80  return num_cols;
81  }
82 
92  Element<E>& getElement(int row, int col) {
93  return Array<E>::getElement(row * num_cols + col);
94  }
95 
104  void setElement(int row, int col, Element<E> el) {
105  setElement(row * num_cols + col, el);
106  }
107 
109  struct Bracket_helper {
111  int row;
112  Bracket_helper(Array2D<E>& a, int r) : arr(a), row(r) {}
113 
114  E& operator[] (int col) {
115  return arr.getElement(row, col).getValue();
116  }
117  };
118 
121  return Bracket_helper(*this, index);
122  }
123 
126  Array2D<E> const& arr;
127  int row;
128  Bracket_helper_const(Array2D<E>& a, int r) : arr(a), row(r) {}
129 
130  E const & operator[] (int col) const {
131  return arr.getElement(row, col).getValue();
132  }
133  };
134 
137  return Bracket_helper_const(*this, index);
138  }
139 
140  }; // Array2D
141  }
142 }// end namespace bridges
143 
144 #endif
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:52
E & operator[](int col)
Definition: Array2D.h:114
int getNumRows()
Definition: Array2D.h:71
void setElement(int row, int col, Element< E > el)
Definition: Array2D.h:104
Element< E > & getElement(int row, int col)
Definition: Array2D.h:92
helper class to make [][] operators work on array 2d. You should never use it directly ...
Definition: Array2D.h:125
helper class to make [][] operators work on array 2d. You should never use it directly ...
Definition: Array2D.h:109
The foundation of BRIDGES array types. It is not meant to be used directly by students.
Definition: Array.h:21
Array2D< E > const & arr
Definition: Array2D.h:126
Bracket_helper_const(Array2D< E > &a, int r)
Definition: Array2D.h:128
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:57
int getNumColumns()
Gets the number of columns of the 2D array.
Definition: Array2D.h:79
Bracket_helper(Array2D< E > &a, int r)
Definition: Array2D.h:112
A BRIDGES array type.
Definition: Array2D.h:43
Array2D< E > & arr
Definition: Array2D.h:110
Array2D(int rows, int cols)
builds an array of given dimensions
Definition: Array2D.h:62
Element< E > & getElement(int index)
Definition: Array.h:108
virtual ~Array2D()
Definition: Array2D.h:54