rcolyer.net
RC Lib  Version 202403231100
Bitfield2D.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////
2 //
3 // RC Library, (c) 2011-2014, Ryan A. Colyer
4 // Distributed under the Boost Software License, v1.0. (LICENSE.txt)
5 //
6 /// \file Bitfield2D.h
7 /// Provides a two-dimensional structure of packed bits.
8 /////////////////////////////////////////////////////////////////////
9 
10 #ifndef RC_BITFIELD2D_H
11 #define RC_BITFIELD2D_H
12 
13 
14 #include "Bitfield.h"
15 #include "Data1D.h"
16 #include "Errors.h"
17 #include <stdlib.h>
18 
19 
20 namespace RC {
21  /// A bounds-safe two-dimensional structure of efficiently packed bits.
22  /** Note AxB and BxA dimensions will both take AB bits of storage with the
23  * total rounded up to the nearest 32 bit word.
24  * @see Bitfield
25  * @see Bitfield3D
26  */
27  class Bitfield2D {
28  protected:
29  /// @cond UNDOC
30  void Initialize(size_t new_b_size1, size_t new_b_size2) {
31  b_size1 = new_b_size1;
32  b_size2 = new_b_size2;
33 
34  data = Bitfield (b_size1 * b_size2);
35  }
36  /// @endcond
37 
38  public:
39  /// Default constructor which initializes to size 0, 0.
41  Initialize(0, 0);
42  }
43 
44  /// Constructor which sets the initial sizes.
45  /** Efficiency note: The first dimension, set by b_size1, should be the
46  * one which is iterated over most frequently in innermost loops for
47  * caching gains.
48  * @param b_size1 The size of the first dimension.
49  * @param b_size2 The size of the second dimension.
50  */
51  explicit Bitfield2D(size_t b_size1, size_t b_size2) {
52  Initialize(b_size1, b_size2);
53  }
54 
55 
56  /// Deletes the stored data, resets the sizes to 0, and releases the
57  /// memory.
58  inline void Delete() {
59  b_size1 = 0;
60  b_size2 = 0;
61  data.Delete();
62  }
63 
64 
65  /// True if the total size of the bits is 0.
66  inline bool IsEmpty() const {
67  return ( (b_size1 == 0) || (b_size2 == 0) );
68  }
69 
70 
71  /// Bounds-checked access of the indexed element.
72  /** Throws an ErrorMsgBounds exception if x or y is out of the bounds.
73  * @param x The dimension 1 index of the bit to access.
74  * @param y The dimension 2 index of the bit to access.
75  * @return A Bitfield::BitfieldBool structure that casts to a bool or
76  * can be assigned a bool value.
77  */
78  inline Bitfield::BitfieldBool operator() (size_t x, size_t y) {
79  Assert(x, y);
80  return data[y*b_size1 + x];
81  }
82 
83  /// Const version of operator()
84  inline Bitfield::BitfieldBoolConst operator() (size_t x, size_t y) const {
85  Assert(x, y);
86  return data[y*b_size1 + x];
87  }
88 
89  /// Identical to operator()
90  inline Bitfield::BitfieldBool At(size_t x, size_t y) {
91  Assert(x, y);
92  return data[y*b_size1 + x];
93  }
94 
95  /// Const version of At()
96  inline Bitfield::BitfieldBoolConst At(size_t x, size_t y) const {
97  Assert(x, y);
98  return data[y*b_size1 + x];
99  }
100 
101 
102  /// Return the extent in bits of dimension 1.
103  inline size_t size1() const { return b_size1; }
104  /// Return the extent in bits of dimension 2.
105  inline size_t size2() const { return b_size2; }
106 
107  /// Set all bits to 0 / false.
108  inline void Zero() {
109  data.Zero();
110  }
111 
112  /// Set all bits of dimension 1 to zero at y indexed dimension 2.
113  inline void Zero1D(const size_t y) {
114  size_t y_start;
115 
116  Assert(0, y);
117 
118  y_start = y*b_size1;
119 
120  data.ZeroRange(y_start, y_start + b_size1 - 1);
121  }
122 
123 
124  /// True if indices x and y are both in bounds.
125  inline bool Check(const size_t x, const size_t y) const {
126  if ( ! ((x < b_size1) && (y < b_size2)) ) {
127  return false;
128  }
129  else {
130  return true;
131  }
132  }
133 
134 
135  /// Throws ErrorMsgBounds if index x or y is out of bounds.
136  inline void Assert(const size_t x, const size_t y) const {
137  if ( ! Check(x, y) ) {
138  Throw_RC_Type(Bounds, "Out of bounds");
139  }
140  }
141 
142 
143  /// Swap the data in Bitfield2D a and b.
144  friend void swap (Bitfield2D &a, Bitfield2D &b) {
145  std::swap(a.b_size1, b.b_size1);
146  std::swap(a.b_size2, b.b_size2);
147  swap(a.data, b.data);
148  }
149 
150 
151  protected:
152  /// @cond PROTECTED
153 
154  size_t b_size1;
155  size_t b_size2;
156  Bitfield data;
157  /// @endcond
158  };
159 
160 
161  /// Outputs data to a stream as a character series of '1's and '0's, with
162  /// newlines trailing each entry of dimension 2.
163  /** Usage like Bitfield2D bf; std::cout << bf << std::endl;
164  */
165  inline std::ostream& operator<< (std::ostream &out, const Bitfield2D &b) {
166  size_t i, j;
167 
168  for (j=0; j<b.size2(); j++) {
169  for (i=0; i<b.size1(); i++) {
170  if (b(i,j)) {
171  out << '1';
172  }
173  else {
174  out << '0';
175  }
176  }
177  out << '\n';
178  }
179 
180  return out;
181  }
182 }
183 
184 
185 
186 #endif // RC_BITFIELD2D_H
187 
Provides a one-dimensional vector-like structure of packed bits.
Provides a one-dimensional vector-like structure.
Provides informative exception handling.
#define Throw_RC_Type(Type, err)
Use this to throw an RC:ErrorMsg subtype exception.
Definition: Errors.h:282
A bounds-safe two-dimensional structure of efficiently packed bits.
Definition: Bitfield2D.h:27
Bitfield2D(size_t b_size1, size_t b_size2)
Constructor which sets the initial sizes.
Definition: Bitfield2D.h:51
bool Check(const size_t x, const size_t y) const
True if indices x and y are both in bounds.
Definition: Bitfield2D.h:125
friend void swap(Bitfield2D &a, Bitfield2D &b)
Swap the data in Bitfield2D a and b.
Definition: Bitfield2D.h:144
size_t size1() const
Return the extent in bits of dimension 1.
Definition: Bitfield2D.h:103
void Delete()
Deletes the stored data, resets the sizes to 0, and releases the memory.
Definition: Bitfield2D.h:58
void Zero1D(const size_t y)
Set all bits of dimension 1 to zero at y indexed dimension 2.
Definition: Bitfield2D.h:113
bool IsEmpty() const
True if the total size of the bits is 0.
Definition: Bitfield2D.h:66
Bitfield2D()
Default constructor which initializes to size 0, 0.
Definition: Bitfield2D.h:40
void Zero()
Set all bits to 0 / false.
Definition: Bitfield2D.h:108
Bitfield::BitfieldBool operator()(size_t x, size_t y)
Bounds-checked access of the indexed element.
Definition: Bitfield2D.h:78
Bitfield::BitfieldBool At(size_t x, size_t y)
Identical to operator()
Definition: Bitfield2D.h:90
Bitfield::BitfieldBoolConst At(size_t x, size_t y) const
Const version of At()
Definition: Bitfield2D.h:96
size_t size2() const
Return the extent in bits of dimension 2.
Definition: Bitfield2D.h:105
void Assert(const size_t x, const size_t y) const
Throws ErrorMsgBounds if index x or y is out of bounds.
Definition: Bitfield2D.h:136
A temporary return-type that serves as a const interface to specific bit values.
Definition: Bitfield.h:132
A temporary return-type that serves as an interface to specific bit values.
Definition: Bitfield.h:79
A bounds-safe one-dimensional vector-like structure of efficiently packed bits.
Definition: Bitfield.h:27
Definition: APtr.h:25
std::ostream & operator<<(std::ostream &out, APtr< T > obj)
A convenience stream output for displaying the enclosed object.
Definition: APtr.h:120
void swap(RC::Data1D< T > &a, RC::Data1D< T > &b)
Efficiently swap all the contents of a and b.
Definition: Data1D.h:1184
email address
— (c) 2015