Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vbl_array_3d.h

Go to the documentation of this file.
00001 // This is core/vbl/vbl_array_3d.h
00002 #ifndef vbl_array_3dh
00003 #define vbl_array_3dh
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Contains class for templated 3d array
00010 // \author Paul Beardsley, Oxford University, UK
00011 // \date   29.03.96
00012 //
00013 // \verbatim
00014 // Modifications
00015 //  960926 AWF Converted to non-fascist C++ :-)
00016 //  970218 AWF Templated
00017 //  01 Mar 2001 fsm. Converted to fascist C++
00018 //  PDA (Manchester) 21/03/2001: Tidied up the documentation
00019 //  Peter Vanroose 3 Jan. 2002 added operator==
00020 //  Peter Vanroose 4 Jan. 2002 bug fix: 3rd arg row2_count_ --> row3_count_
00021 // \endverbatim
00022 
00023 
00024 #include <vcl_compiler.h>
00025 
00026 #ifdef __OPTIMIZE__
00027 # define RANGECHECK(i,j,k) ((void)0)
00028 #else
00029 # include <vcl_cassert.h>
00030 # define RANGECHECK(i,j,k) assert(((int)i < row1_count_) && \
00031                    ((int)j < row2_count_) && ((int)k < row3_count_))
00032 #endif
00033 
00034 //: Templated 3-dimensional array
00035 
00036 export template <class T>
00037 class vbl_array_3d
00038 {
00039  public:
00040   typedef T element_type;
00041   typedef T* iterator;
00042   typedef T const* const_iterator;
00043 
00044   vbl_array_3d(): element_(0), row1_count_(0), row2_count_(0), row3_count_(0)
00045   {}
00046 
00047   vbl_array_3d(int n1, int n2, int n3) { construct(n1, n2, n3); }
00048 
00049   vbl_array_3d(int n1, int n2, int n3, T const* init_values)
00050   {
00051     construct(n1, n2, n3); set(init_values);
00052   }
00053 
00054   vbl_array_3d(int n1, int n2, int n3, T const& fill_value)
00055   {
00056     construct(n1, n2, n3); fill(fill_value);
00057   }
00058 
00059   vbl_array_3d(vbl_array_3d<T> const& that)
00060   : element_(0), row1_count_(0), row2_count_(0), row3_count_(0)
00061   {
00062     if(that.element_){
00063       construct(that.row1_count_,that.row2_count_,that.row3_count_);
00064       set(that.data_block());
00065     }
00066   }
00067 
00068   ~vbl_array_3d () { destruct(); }
00069   vbl_array_3d<T>& operator=(vbl_array_3d<T> const& that) {
00070     resize(that.row1_count_, that.row2_count_, that.row3_count_);
00071     set(that.data_block());
00072     return *this;
00073   }
00074 
00075   //: Comparison
00076   bool operator==(vbl_array_3d<T> const& that) const {
00077     if (row1_count_ != that.row1_count_ ||
00078         row2_count_ != that.row2_count_ ||
00079         row3_count_ != that.row3_count_)
00080       return false;
00081     const_iterator i = this->begin();
00082     const_iterator j = that.begin();
00083     while (i != this->end())
00084     {
00085       if (!(*i == *j)) // do not assume we have operator!=(T)
00086         return false;
00087       ++i; ++j;
00088     }
00089     return true;
00090   }
00091 
00092   // Data Access---------------------------------------------------------------
00093 
00094   T      & operator() (unsigned i1, unsigned i2, unsigned i3)
00095   {
00096     RANGECHECK(i1,i2,i3);
00097     return element_ [i1][i2][i3];
00098   }
00099 
00100   T const& operator() (unsigned i1, unsigned i2, unsigned i3) const
00101   {
00102     RANGECHECK(i1,i2,i3);
00103     return element_ [i1][i2][i3];
00104   }
00105 
00106   T      * const* operator[](unsigned i1) { return element_[i1]; }
00107   T const* const* operator[](unsigned i1) const { return element_[i1]; }
00108 
00109   // dimensions
00110   int get_row1_count () const { return row1_count_; }
00111   int get_row2_count () const { return row2_count_; }
00112   int get_row3_count () const { return row3_count_; }
00113 
00114   // iterators
00115   unsigned size() const
00116   {
00117     return row1_count_ * row2_count_ * row3_count_;
00118   }
00119 
00120   iterator begin() { return element_[0][0]; }
00121   iterator end  () { return begin() + size(); }
00122   const_iterator begin() const { return element_[0][0]; }
00123   const_iterator end  () const { return begin() + size(); }
00124 
00125   // data_block will return all elements of the array in sequential storage.
00126   T      * data_block()       { return element_[0][0]; }
00127   T const* data_block() const { return element_[0][0]; }
00128 
00129   void resize(int n1, int n2, int n3); // no malloc unless size changes.
00130   void set(T const* array);
00131   void get(T* array) const;
00132   void fill(T const& value);
00133 
00134  protected:
00135   void construct(int, int, int);
00136   void destruct ();
00137 
00138  private:
00139   T ***element_;
00140   int row1_count_;
00141   int row2_count_;
00142   int row3_count_;
00143 };
00144 
00145 #undef RANGECHECK
00146 
00147 //
00148 // formatted I/O
00149 //
00150 #include <vcl_iosfwd.h>
00151 export template <class T> vcl_ostream& operator<<(vcl_ostream&,
00152                                                   vbl_array_3d<T >const&);
00153 
00154 export template <class T> vcl_istream& operator>>(vcl_istream&,
00155                                                   vbl_array_3d<T >&);
00156 
00157 #define VBL_ARRAY_3D_INSTANTIATE \
00158 extern "please include vbl/vbl_array_3d.txx instead"
00159 #define VBL_ARRAY_3D_IO_INSTANTIATE \
00160 extern "please include vbl/vbl_array_3d.txx instead"
00161 
00162 #endif // vbl_array_3dh

Generated on Thu Jan 10 14:39:08 2008 for core/vbl by  doxygen 1.4.4