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