vbl_array_2d.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_array_2d.h
2 #ifndef vbl_array_2d_h_
3 #define vbl_array_2d_h_
4 //:
5 // \file
6 // \brief Contains class for a templated 2d array
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 05 Aug 96
9 //
10 // \verbatim
11 // Modifications
12 // Peter Vanroose -13nov98- added copy constructor and assignment operator
13 // AWF 10/12/98 Added row/column store. The old split was just too pedantic.
14 // PDA (Manchester) 21/03/2001: Tidied up the documentation
15 // \endverbatim
16 
17 #include <iosfwd>
18 #include <cstddef>
19 #ifdef _MSC_VER
20 # include <vcl_msvc_warnings.h>
21 #endif
22 
23 //: simple 2D array
24 template <class T>
26 {
27  public:
28  typedef std::size_t size_type;
29  typedef T element_type;
30 
31  private:
35 
36  public:
37  typedef T *iterator;
38  typedef T const *const_iterator;
39 
40  typedef T &reference;
41  typedef T const &const_reference;
42  public:
43 
44  //: Default constructor
46 
47  //: Construct m-by-n array.
49 
50  //: Construct and fill an m-by-n array.
51  vbl_array_2d(size_type m, size_type n, const T &v) { construct(m, n); fill(v);}
52 
53  //: Construct from a 2d array
55  construct(that.rows(), that.cols());
56  operator=(that);
57  }
58 
59  //: Destructor
61 
62  //: Assignment
64  resize(that.rows(), that.cols());
65  for (size_type i=0; i<num_rows_; ++i)
66  for (size_type j=0; j<num_cols_; ++j)
67  rows_[i][j] = that.rows_[i][j];
68  return *this;
69  }
70 
71  //: Comparison
72  bool operator==(vbl_array_2d<T> const &that) const {
73  if (num_rows_ != that.num_rows_ || num_cols_ != that.num_cols_)
74  return false;
75  for (size_type i=0; i<num_rows_; ++i)
76  for (size_type j=0; j<num_cols_; ++j)
77  if (!( rows_[i][j] == that.rows_[i][j] )) // do not assume we have operator!=
78  return false;
79  return true;
80  }
81 
82  //:
83  bool operator!=(vbl_array_2d<T> const &that) const {
84  return ! operator==(that);
85  }
86 
87  //: fill with `value'
88  void fill(T value) {
89  for (size_type i=0; i<num_rows_; ++i)
90  for (size_type j=0; j<num_cols_; ++j)
91  rows_[i][j] = value;
92  }
93 
94  //: change size.
95  void resize(size_type m, size_type n) {
96  if (m != num_rows_ || n != num_cols_) {
97  destruct();
98  construct(m, n);
99  }
100  }
101 
102  //: make as if default-constructed.
103  void clear() {
104  if (rows_) {
105  destruct();
106  construct();
107  }
108  }
109 
110  // Data Access---------------------------------------------------------------
111  const_reference operator() (size_type i, size_type j) const { return rows_[i][j]; }
113 
114  void put(size_type i, size_type j, T const &x) { rows_[i][j] = x; }
115  T get(size_type i, size_type j) const { return rows_[i][j]; }
116 
117  T const* operator[] (size_type i) const { return rows_[i]; }
118  T * operator[] (size_type i) { return rows_[i]; }
119 
120 
121  //: Return number of rows
122  size_type rows() const { return num_rows_; }
123 
124  //: Return number of columns
125  size_type cols() const { return num_cols_; }
126 
127  //: Return number of columns
128  size_type columns() const { return num_cols_; }
129 
130  //: Return size = (number of rows) * (number of columns)
131  size_type size() const { return num_rows_ * num_cols_; }
132  size_type capacity() const { return size(); }
133 
134  T * * get_rows() { return rows_; }
135  T const* const* get_rows() const { return rows_; }
136 
137  // iterators
138  iterator begin() { return rows_[0]; }
139  iterator end () { return rows_[0] + num_cols_ * num_rows_; }
140 
141  const_iterator begin() const { return rows_[0]; }
142  const_iterator end () const { return rows_[0] + num_cols_ * num_rows_; }
143 
144  private:
145  void construct() {
146  rows_ = nullptr;
147  num_rows_ = 0;
148  num_cols_ = 0;
149  }
150 
152  num_rows_ = m;
153  num_cols_ = n;
154  if (m && n) {
155  rows_ = new T * [m];
156  T* p = new T[m * n];
157  for (size_type i = 0; i < m; ++i)
158  rows_[i] = p + i * n;
159  }
160  else {
161  rows_ = nullptr;
162  }
163  }
164 
165  void destruct() {
166  if (rows_) {
167  delete [] rows_[0];
168  delete [] rows_;
169  }
170  }
171 };
172 
173 template <class T>
174 std::ostream& operator<<(std::ostream &, vbl_array_2d<T> const &);
175 
176 #define VBL_ARRAY_2D_INSTANTIATE \
177 extern "please include vbl/vbl_array_2d.hxx instead"
178 
179 #endif // vbl_array_2d_h_
element_type ** rows_
Definition: vbl_array_2d.h:32
simple 2D array.
Definition: vbl_array_2d.h:25
T ** get_rows()
Definition: vbl_array_2d.h:134
void destruct()
Definition: vbl_array_2d.h:165
size_type num_rows_
Definition: vbl_array_2d.h:33
std::size_t size_type
Definition: vbl_array_2d.h:28
size_type size() const
Return size = (number of rows) * (number of columns).
Definition: vbl_array_2d.h:131
iterator begin()
Definition: vbl_array_2d.h:138
bool operator!=(vbl_array_2d< T > const &that) const
Definition: vbl_array_2d.h:83
iterator end()
Definition: vbl_array_2d.h:139
vbl_array_2d(size_type m, size_type n)
Construct m-by-n array.
Definition: vbl_array_2d.h:48
size_type rows() const
Return number of rows.
Definition: vbl_array_2d.h:122
T const * operator[](size_type i) const
Definition: vbl_array_2d.h:117
void construct()
Definition: vbl_array_2d.h:145
size_type cols() const
Return number of columns.
Definition: vbl_array_2d.h:125
void clear()
make as if default-constructed.
Definition: vbl_array_2d.h:103
vbl_array_2d()
Default constructor.
Definition: vbl_array_2d.h:45
vbl_array_2d(size_type m, size_type n, const T &v)
Construct and fill an m-by-n array.
Definition: vbl_array_2d.h:51
bool operator==(vbl_array_2d< T > const &that) const
Comparison.
Definition: vbl_array_2d.h:72
void construct(size_type m, size_type n)
Definition: vbl_array_2d.h:151
size_type columns() const
Return number of columns.
Definition: vbl_array_2d.h:128
T const *const * get_rows() const
Definition: vbl_array_2d.h:135
void fill(T value)
fill with ‘value’.
Definition: vbl_array_2d.h:88
std::ostream & operator<<(std::ostream &, vbl_array_2d< T > const &)
T get(size_type i, size_type j) const
Definition: vbl_array_2d.h:115
vbl_array_2d< T > & operator=(vbl_array_2d< T > const &that)
Assignment.
Definition: vbl_array_2d.h:63
T const * const_iterator
Definition: vbl_array_2d.h:38
const_iterator begin() const
Definition: vbl_array_2d.h:141
const_iterator end() const
Definition: vbl_array_2d.h:142
const_reference operator()(size_type i, size_type j) const
Definition: vbl_array_2d.h:111
size_type capacity() const
Definition: vbl_array_2d.h:132
void resize(size_type m, size_type n)
change size.
Definition: vbl_array_2d.h:95
T const & const_reference
Definition: vbl_array_2d.h:41
size_type num_cols_
Definition: vbl_array_2d.h:34
~vbl_array_2d()
Destructor.
Definition: vbl_array_2d.h:60
vbl_array_2d(vbl_array_2d< T > const &that)
Construct from a 2d array.
Definition: vbl_array_2d.h:54
void put(size_type i, size_type j, T const &x)
Definition: vbl_array_2d.h:114