core/vbl/vbl_bit_array_2d.h
Go to the documentation of this file.
00001 // This is core/vbl/vbl_bit_array_2d.h
00002 #ifndef vbl_bit_array_2d_h_
00003 #define vbl_bit_array_2d_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Contains class for a 2d bit array; interface as vbl_array_2d<T>
00010 // \author Geoffrey Cross
00011 //
00012 // \verbatim
00013 // Modifications
00014 // Peter Vanroose - 4dec01 - adapted interface to that of vbl_array_2d<T>
00015 // Peter Vanroose - 26 Aug 2004 - adapted interface to that of vbl_array_2d<T>
00016 // \endverbatim
00017 //-----------------------------------------------------------------------------
00018 
00019 #include <vcl_iosfwd.h>
00020 
00021 //: simple 2D bit array
00022 // essentially identical to vbl_array_2d<bool> but more efficiently stored
00023 class vbl_bit_array_2d
00024 {
00025  public:
00026   // Default constructor
00027   vbl_bit_array_2d() : data_(0), num_rows_(0), num_cols_(0) {}
00028   //: Construct num_rows x num_cols array and leave data uninitialised
00029   vbl_bit_array_2d(unsigned int m, unsigned int n) { construct(m,n); }
00030   //: Construct num_rows x num_cols array and fill all cells with v
00031   vbl_bit_array_2d(unsigned int m, unsigned int n, bool v) { construct(m,n); fill(v); }
00032   //: Construct num_rows x num_cols array and fill all cells with v
00033   vbl_bit_array_2d(unsigned int m, unsigned int n, bool v[]);
00034   //: Copy constructor
00035   vbl_bit_array_2d(vbl_bit_array_2d const &);
00036   // Destructor
00037  ~vbl_bit_array_2d() { destruct(); }
00038 
00039   //: Assignment operator
00040   vbl_bit_array_2d& operator=(vbl_bit_array_2d const&);
00041 
00042   //: Comparison
00043   bool operator==(vbl_bit_array_2d const &a) const;
00044   //:
00045   bool operator!=(vbl_bit_array_2d const &a) const { return ! operator==(a); }
00046 
00047   // Operations----------------------------------------------------------------
00048 
00049   //: Fill with value
00050   void fill(bool value);
00051   //: Delete contents and resize to m rows x n cols
00052   void resize(unsigned int m, unsigned int n) { destruct(); construct(m,n); }
00053   //: Resizes and pads with zeros; keeps existing data
00054   void enlarge(unsigned int m, unsigned int n);
00055   //: make as if default-constructed.
00056   void clear() { if (data_) { destruct(); construct(0,0); } }
00057 
00058   // Data Access---------------------------------------------------------------
00059   bool operator() (unsigned int i, unsigned int j) const;
00060   bool operator() (unsigned int i, unsigned int j);
00061 
00062   void put(unsigned int i, unsigned int j, bool const &x);
00063   bool get(unsigned int i, unsigned int j) const;
00064   //: Set the value of a cell; default is to set the value on
00065   void set(unsigned int i, unsigned int j, bool v=true) { put(i, j, v); }
00066   //: Change the value of a cell
00067   void flip(unsigned int i, unsigned int j) { put(i, j, !get(i,j)); }
00068 
00069   inline unsigned int rows() const { return num_rows_; }
00070   inline unsigned int cols() const { return num_cols_; }
00071   inline unsigned int columns() const { return num_cols_; }
00072   //: Number of bytes allocated by the data
00073   unsigned long size() const;
00074 
00075  private:
00076   unsigned char *data_;
00077   unsigned int num_rows_;
00078   unsigned int num_cols_;
00079 
00080   void destruct() { delete[] data_; data_=0; }
00081   void construct(unsigned int m, unsigned int n);
00082 
00083   //helper
00084   void index( unsigned int x, unsigned int y, unsigned long &byteindex, unsigned int &bitindex) const;
00085 };
00086 
00087 vcl_ostream& operator<< (vcl_ostream& os, const vbl_bit_array_2d &v);
00088 
00089 #endif // vbl_bit_array_2d_h_