vbl_bit_array_2d.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_bit_array_2d.h
2 #ifndef vbl_bit_array_2d_h_
3 #define vbl_bit_array_2d_h_
4 //:
5 // \file
6 // \brief Contains class for a 2d bit array; interface as vbl_array_2d<T>
7 // \author Geoffrey Cross
8 //
9 // \verbatim
10 // Modifications
11 // Peter Vanroose - 4dec01 - adapted interface to that of vbl_array_2d<T>
12 // Peter Vanroose - 26 Aug 2004 - adapted interface to that of vbl_array_2d<T>
13 // \endverbatim
14 //-----------------------------------------------------------------------------
15 
16 #include <iosfwd>
17 #ifdef _MSC_VER
18 # include <vcl_msvc_warnings.h>
19 #endif
20 
21 //: simple 2D bit array
22 // essentially identical to vbl_array_2d<bool> but more efficiently stored
23 class vbl_bit_array_2d
24 {
25  public:
26  // Default constructor
27  vbl_bit_array_2d() : data_(nullptr), num_rows_(0), num_cols_(0) {}
28  //: Construct num_rows x num_cols array and leave data uninitialised
29  vbl_bit_array_2d(unsigned int m, unsigned int n) { construct(m,n); }
30  //: Construct num_rows x num_cols array and fill all cells with v
31  vbl_bit_array_2d(unsigned int m, unsigned int n, bool v) { construct(m,n); fill(v); }
32  //: Construct num_rows x num_cols array and fill all cells with v
33  vbl_bit_array_2d(unsigned int m, unsigned int n, bool v[]);
34  //: Copy constructor
36  // Destructor
38 
39  //: Assignment operator
41 
42  //: Comparison
43  bool operator==(vbl_bit_array_2d const &a) const;
44  //:
45  bool operator!=(vbl_bit_array_2d const &a) const { return ! operator==(a); }
46 
47  // Operations----------------------------------------------------------------
48 
49  //: Fill with value
50  void fill(bool value);
51  //: Delete contents and resize to m rows x n cols
52  void resize(unsigned int m, unsigned int n) { destruct(); construct(m,n); }
53  //: Resizes and pads with zeros; keeps existing data
54  void enlarge(unsigned int m, unsigned int n);
55  //: make as if default-constructed.
56  void clear() { if (data_) { destruct(); construct(0,0); } }
57 
58  // Data Access---------------------------------------------------------------
59  bool operator() (unsigned int i, unsigned int j) const;
60  bool operator() (unsigned int i, unsigned int j);
61 
62  void put(unsigned int i, unsigned int j, bool const &x);
63  bool get(unsigned int i, unsigned int j) const;
64  //: Set the value of a cell; default is to set the value on
65  void set(unsigned int i, unsigned int j, bool v=true) { put(i, j, v); }
66  //: Change the value of a cell
67  void flip(unsigned int i, unsigned int j) { put(i, j, !get(i,j)); }
68 
69  inline unsigned int rows() const { return num_rows_; }
70  inline unsigned int cols() const { return num_cols_; }
71  inline unsigned int columns() const { return num_cols_; }
72  //: Number of bytes allocated by the data
73  unsigned long size() const;
74 
75  private:
76  unsigned char *data_;
77  unsigned int num_rows_;
78  unsigned int num_cols_;
79 
80  void destruct() { delete[] data_; data_=nullptr; }
81  void construct(unsigned int m, unsigned int n);
82 
83  //helper
84  void index( unsigned int x, unsigned int y, unsigned long &byteindex, unsigned int &bitindex) const;
85 };
86 
87 std::ostream& operator<< (std::ostream& os, const vbl_bit_array_2d &v);
88 
89 #endif // vbl_bit_array_2d_h_
void enlarge(unsigned int m, unsigned int n)
Resizes and pads with zeros; keeps existing data.
bool operator!=(vbl_bit_array_2d const &a) const
vbl_bit_array_2d & operator=(vbl_bit_array_2d const &)
Assignment operator.
bool operator()(unsigned int i, unsigned int j) const
unsigned char * data_
unsigned int num_rows_
bool operator==(vbl_bit_array_2d const &a) const
Comparison.
std::ostream & operator<<(std::ostream &os, const vbl_bit_array_2d &v)
void clear()
make as if default-constructed.
bool get(unsigned int i, unsigned int j) const
void construct(unsigned int m, unsigned int n)
void resize(unsigned int m, unsigned int n)
Delete contents and resize to m rows x n cols.
unsigned int cols() const
simple 2D bit array.
void flip(unsigned int i, unsigned int j)
Change the value of a cell.
unsigned long size() const
Number of bytes allocated by the data.
unsigned int num_cols_
void put(unsigned int i, unsigned int j, bool const &x)
void set(unsigned int i, unsigned int j, bool v=true)
Set the value of a cell; default is to set the value on.
void index(unsigned int x, unsigned int y, unsigned long &byteindex, unsigned int &bitindex) const
void fill(bool value)
Fill with value.
unsigned int columns() const
unsigned int rows() const