vbl_array_3d.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_array_3d.h
2 #ifndef vbl_array_3dh
3 #define vbl_array_3dh
4 //:
5 // \file
6 // \brief Contains class for templated 3d array
7 // \author Paul Beardsley, Oxford University, UK
8 // \date 29 Mar 1996
9 //
10 // \verbatim
11 // Modifications
12 // 1996-09-26 AWF Converted to non-fascist C++ :-)
13 // 1997-02-18 AWF Templated
14 // 01 Mar 2001 fsm. Converted to fascist C++
15 // PDA (Manchester) 21 Mar 2001: Tidied up the documentation
16 // Peter Vanroose 3 Jan. 2002 added operator==
17 // Peter Vanroose 4 Jan. 2002 bug fix: 3rd arg row2_count_ --> row3_count_
18 // \endverbatim
19 
20 
21 #include <cstddef>
22 #include <iosfwd>
23 #ifdef _MSC_VER
24 # include <vcl_msvc_warnings.h>
25 #endif
26 
27 #ifdef __OPTIMIZE__
28 # define RANGECHECK(i,j,k) ((void)0)
29 #else
30 #include <cassert>
31 # define RANGECHECK(i,j,k) assert(((size_type)(i) < row1_count_) && \
32  ((size_type)(j) < row2_count_) && ((size_type)(k) < row3_count_))
33 #endif
34 
35 //: Templated 3-dimensional array
36 
37 template <class T>
39 {
40  public:
41  typedef std::size_t size_type;
42  typedef T element_type;
43 
44  private:
49 
50  public:
51  typedef T* iterator;
52  typedef T const* const_iterator;
53 
54  typedef T &reference;
55  typedef T const &const_reference;
56  public:
57 
59  {}
60 
62  { construct(n1, n2, n3); }
63 
64  vbl_array_3d(size_type n1, size_type n2, size_type n3, T const* init_values)
65  {
66  construct(n1, n2, n3); set(init_values);
67  }
68 
69  vbl_array_3d(size_type n1, size_type n2, size_type n3, T const& fill_value)
70  {
71  construct(n1, n2, n3); fill(fill_value);
72  }
73 
75  : element_(nullptr), row1_count_(0), row2_count_(0), row3_count_(0)
76  {
77  if (that.element_) {
79  set(that.data_block());
80  }
81  }
82 
85  resize(that.row1_count_, that.row2_count_, that.row3_count_);
87  return *this;
88  set(that.data_block());
89  return *this;
90  }
91 
92  //: Comparison
93  bool operator==(vbl_array_3d<T> const& that) const {
94  if (row1_count_ != that.row1_count_ ||
95  row2_count_ != that.row2_count_ ||
96  row3_count_ != that.row3_count_)
97  return false;
98  const_iterator i = this->begin();
99  const_iterator j = that.begin();
100  while (i != this->end())
101  {
102  if (!(*i == *j)) // do not assume we have operator!=(T)
103  return false;
104  ++i; ++j;
105  }
106  return true;
107  }
108 
109  // Data Access---------------------------------------------------------------
110 
112  {
113  RANGECHECK(i1,i2,i3);
114  return element_ [i1][i2][i3];
115  }
116 
118  {
119  RANGECHECK(i1,i2,i3);
120  return element_ [i1][i2][i3];
121  }
122 
123  T * const* operator[](size_type i1) { return element_[i1]; }
124  T const* const* operator[](size_type i1) const { return element_[i1]; }
125 
126  // dimensions
127  size_type get_row1_count () const { return row1_count_; }
128  size_type get_row2_count () const { return row2_count_; }
129  size_type get_row3_count () const { return row3_count_; }
130 
132  size_type capacity() const { return size(); }
133 
134  // iterators
135  iterator begin() { return element_[0][0]; }
136  iterator end () { return begin() + size(); }
137  const_iterator begin() const { return element_[0][0]; }
138  const_iterator end () const { return begin() + size(); }
139 
140  // data_block will return all elements of the array in sequential storage.
141  T * data_block() { return element_[0][0]; }
142  T const* data_block() const { return element_[0][0]; }
143 
144  void resize(size_type n1, size_type n2, size_type n3); // no malloc unless size changes.
145  void set(T const* array);
146  void get(T* array) const;
147  void fill(T const& value);
148 
149  protected:
151  void destruct();
152 };
153 
154 #undef RANGECHECK
155 
156 //
157 // formatted I/O
158 //
159 template <class T> std::ostream& operator<<(std::ostream&,
160  vbl_array_3d<T >const&);
161 
162 template <class T> std::istream& operator>>(std::istream&,
164 
165 #define VBL_ARRAY_3D_INSTANTIATE \
166 extern "please include vbl/vbl_array_3d.hxx instead"
167 #define VBL_ARRAY_3D_IO_INSTANTIATE \
168 extern "please include vbl/vbl_array_3d.hxx instead"
169 
170 #endif // vbl_array_3dh
T *const * operator[](size_type i1)
Definition: vbl_array_3d.h:123
size_type capacity() const
Definition: vbl_array_3d.h:132
void resize(size_type n1, size_type n2, size_type n3)
std::istream & operator>>(std::istream &, vbl_array_3d< T > &)
element_type *** element_
Definition: vbl_array_3d.h:45
size_type row3_count_
Definition: vbl_array_3d.h:48
vbl_array_3d< T > & operator=(vbl_array_3d< T > const &that)
Definition: vbl_array_3d.h:84
size_type row1_count_
Definition: vbl_array_3d.h:46
T const & const_reference
Definition: vbl_array_3d.h:55
std::ostream & operator<<(std::ostream &, vbl_array_3d< T >const &)
size_type get_row3_count() const
Definition: vbl_array_3d.h:129
vbl_array_3d(size_type n1, size_type n2, size_type n3, T const *init_values)
Definition: vbl_array_3d.h:64
vbl_array_3d(size_type n1, size_type n2, size_type n3)
Definition: vbl_array_3d.h:61
size_type size() const
Definition: vbl_array_3d.h:131
const_iterator end() const
Definition: vbl_array_3d.h:138
void set(T const *array)
Fill from static array of Ts.
vbl_array_3d(size_type n1, size_type n2, size_type n3, T const &fill_value)
Definition: vbl_array_3d.h:69
size_type row2_count_
Definition: vbl_array_3d.h:47
T const * data_block() const
Definition: vbl_array_3d.h:142
iterator begin()
Definition: vbl_array_3d.h:135
Templated 3-dimensional array.
Definition: vbl_array_3d.h:38
T * data_block()
Definition: vbl_array_3d.h:141
T const * const_iterator
Definition: vbl_array_3d.h:52
#define RANGECHECK(i, j, k)
Definition: vbl_array_3d.h:31
vbl_array_3d(vbl_array_3d< T > const &that)
Definition: vbl_array_3d.h:74
iterator end()
Definition: vbl_array_3d.h:136
size_type get_row2_count() const
Definition: vbl_array_3d.h:128
void fill(T const &value)
Fill with constant.
void construct(size_type, size_type, size_type)
Constructor utility.
const_iterator begin() const
Definition: vbl_array_3d.h:137
bool operator==(vbl_array_3d< T > const &that) const
Comparison.
Definition: vbl_array_3d.h:93
T const *const * operator[](size_type i1) const
Definition: vbl_array_3d.h:124
size_type get_row1_count() const
Definition: vbl_array_3d.h:127
void get(T *array) const
Get into array.
reference operator()(size_type i1, size_type i2, size_type i3)
Definition: vbl_array_3d.h:111
std::size_t size_type
Definition: vbl_array_3d.h:41