rcolyer.net
RC Lib  Version 202403231100
Bitfield3D.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 Bitfield3D.h
7 /// Provides a three-dimensional structure of packed bits.
8 /////////////////////////////////////////////////////////////////////
9 
10 #ifndef RC_BITFIELD3D_H
11 #define RC_BITFIELD3D_H
12 
13 
14 #include "Bitfield.h"
15 #include "Bitfield2D.h"
16 #include "Data1D.h"
17 #include "Errors.h"
18 #include <stdlib.h>
19 
20 
21 namespace RC {
22  /// A bounds-safe three-dimensional structure of efficiently packed bits.
23  /** Note AxBxC dimensions take ABC bits of storage with the total rounded
24  * up to the nearest 32 bit word.
25  * @see Bitfield
26  * @see Bitfield2D
27  */
28  class Bitfield3D {
29  protected:
30  /// @cond UNDOC
31  void Initialize(size_t new_b_size1, size_t new_b_size2,
32  size_t new_b_size3) {
33  b_size1 = new_b_size1;
34  b_size2 = new_b_size2;
35  b_size3 = new_b_size3;
36 
37  data = Bitfield (b_size1 * b_size2 * b_size3);
38  }
39  /// @endcond
40 
41  public:
42  /// Default constructor which initializes to size 0, 0, 0.
44  Initialize(0, 0, 0);
45  }
46 
47  /// Cosntructor which sets the initial sizes.
48  /** Efficiency note: the first dimension, set by b_size1, should be the
49  * one which is iterated over most frequently in innermost loops for
50  * caching gains.
51  * @param b_size1 The size of the first dimension.
52  * @param b_size2 The size of the second dimension.
53  * @param b_size3 The size of the third dimension.
54  */
55  explicit Bitfield3D(size_t b_size1, size_t b_size2, size_t b_size3) {
56  Initialize(b_size1, b_size2, b_size3);
57  }
58 
59 
60  /// Deletes the stored data, resets the sizes to 0, and releases the
61  /// memory.
62  inline void Delete() {
63  b_size1 = 0;
64  b_size2 = 0;
65  b_size3 = 0;
66  data.Delete();
67  }
68 
69 
70  /// True of the total size of the bits is 0.
71  inline bool IsEmpty() const {
72  return ( (b_size1 == 0) || (b_size2 == 0) || (b_size3 == 0) );
73  }
74 
75 
76  /// Bounds-checked access of the indexed element.
77  /** Throws an ErrorMsgBounds exception if x, y, or z is out of the bounds.
78  * @param x The dimension 1 index of the bit to access.
79  * @param y The dimension 2 index of the bit to access.
80  * @param z The dimension 3 index of the bit to access.
81  * @return A Bitfield::BitfieldBool structure that casts to a bool or
82  * can be assigned a bool value.
83  */
84  inline Bitfield::BitfieldBool operator() (size_t x, size_t y, size_t z) {
85  Assert(x, y, z);
86  return data[z*b_size2*b_size1 + y*b_size1 + x];
87  }
88 
89  /// Const version of operator()
90  inline Bitfield::BitfieldBoolConst operator()
91  (size_t x, size_t y, size_t z) const {
92  Assert(x, y, z);
93  return data[z*b_size2*b_size1 + y*b_size1 + x];
94  }
95 
96  /// Identical to operator()
97  inline Bitfield::BitfieldBool At(size_t x, size_t y, size_t z) {
98  Assert(x, y, z);
99  return data[z*b_size2*b_size1 + y*b_size1 + x];
100  }
101 
102  /// Const version of At()
103  inline Bitfield::BitfieldBoolConst At(size_t x, size_t y, size_t z) const {
104  Assert(x, y, z);
105  return data[z*b_size2*b_size1 + y*b_size1 + x];
106  }
107 
108 
109  /// Return the extent in bits of dimension 1.
110  inline size_t size1() const { return b_size1; }
111  /// Return the extent in bits of dimension 2.
112  inline size_t size2() const { return b_size2; }
113  /// Return the extent in bits of dimension 3.
114  inline size_t size3() const { return b_size3; }
115 
116 
117  /// Set all bits to 0 / false.
118  inline void Zero() {
119  data.Zero();
120  }
121 
122  /// Set all bits of dimensions 1 and 2 to zero at z indexed dimension 3.
123  inline void Zero2D(const size_t z) {
124  size_t z_start, z_fact;
125 
126  Assert(0, 0, z);
127 
128  z_fact = b_size2*b_size1;
129  z_start = z * z_fact;
130 
131  data.ZeroRange(z_start, z_start+z_fact-1);
132  }
133 
134  /// Set all bits of dimension 1 to zero at y and z indexed dimensions 2
135  /// and 3.
136  inline void Zero1D(const size_t y, const size_t z) {
137  size_t y_start;
138 
139  Assert(0, y, z);
140 
141  y_start = z * b_size2 * b_size1 + y * b_size1;
142 
143  data.ZeroRange(y_start, y_start + b_size1 - 1);
144  }
145 
146 
147  /// True of indices x, y, and z are all in bounds.
148  inline bool Check(const size_t x, const size_t y, const size_t z) const {
149  if ( ! ((x < b_size1) && (y < b_size2) && (z < b_size3)) ) {
150  return false;
151  }
152  else {
153  return true;
154  }
155  }
156 
157 
158  /// Throws ErrorMsgBounds if index x, y, or z is out of bounds.
159  inline void Assert(const size_t x, const size_t y, const size_t z) const {
160  if ( ! Check(x, y, z) ) {
161  Throw_RC_Type(Bounds, "Out of bounds");
162  }
163  }
164 
165 
166  /// Swap the data in Bitfield3D a and b.
167  friend void swap (Bitfield3D &a, Bitfield3D &b) {
168  std::swap(a.b_size1, b.b_size1);
169  std::swap(a.b_size2, b.b_size2);
170  std::swap(a.b_size3, b.b_size3);
171  swap(a.data, b.data);
172  }
173 
174 
175  protected:
176  /// @cond PROTECTED
177 
178  size_t b_size1;
179  size_t b_size2;
180  size_t b_size3;
181  Bitfield data;
182  /// @endcond
183  };
184 
185 
186  /// Outputs data to a stream as a character series of '1's and '0's, with
187  /// newlines trailing each entry of dimension 2, and double-spaces after
188  /// entries of dimension 3.
189  /** Usage like Bitfield3D bf; std::cout << bf << std::endl;
190  */
191  inline std::ostream& operator<< (std::ostream &out, const Bitfield3D &b) {
192  size_t i, j, k;
193 
194  for (k=0; k<b.size3(); k++) {
195  for (j=0; j<b.size2(); j++) {
196  for (i=0; i<b.size1(); i++) {
197  if (b(i,j,k)) {
198  out << '1';
199  }
200  else {
201  out << '0';
202  }
203  }
204  out << '\n';
205  }
206  out << '\n';
207  }
208 
209  return out;
210  }
211 }
212 
213 
214 #endif // RC_BITFIELD3D_H
215 
Provides a two-dimensional structure of packed bits.
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 three-dimensional structure of efficiently packed bits.
Definition: Bitfield3D.h:28
Bitfield3D()
Default constructor which initializes to size 0, 0, 0.
Definition: Bitfield3D.h:43
bool IsEmpty() const
True of the total size of the bits is 0.
Definition: Bitfield3D.h:71
Bitfield::BitfieldBoolConst At(size_t x, size_t y, size_t z) const
Const version of At()
Definition: Bitfield3D.h:103
void Zero1D(const size_t y, const size_t z)
Set all bits of dimension 1 to zero at y and z indexed dimensions 2 and 3.
Definition: Bitfield3D.h:136
size_t size1() const
Return the extent in bits of dimension 1.
Definition: Bitfield3D.h:110
friend void swap(Bitfield3D &a, Bitfield3D &b)
Swap the data in Bitfield3D a and b.
Definition: Bitfield3D.h:167
Bitfield::BitfieldBool operator()(size_t x, size_t y, size_t z)
Bounds-checked access of the indexed element.
Definition: Bitfield3D.h:84
void Delete()
Deletes the stored data, resets the sizes to 0, and releases the memory.
Definition: Bitfield3D.h:62
void Zero()
Set all bits to 0 / false.
Definition: Bitfield3D.h:118
bool Check(const size_t x, const size_t y, const size_t z) const
True of indices x, y, and z are all in bounds.
Definition: Bitfield3D.h:148
size_t size3() const
Return the extent in bits of dimension 3.
Definition: Bitfield3D.h:114
Bitfield::BitfieldBool At(size_t x, size_t y, size_t z)
Identical to operator()
Definition: Bitfield3D.h:97
void Assert(const size_t x, const size_t y, const size_t z) const
Throws ErrorMsgBounds if index x, y, or z is out of bounds.
Definition: Bitfield3D.h:159
size_t size2() const
Return the extent in bits of dimension 2.
Definition: Bitfield3D.h:112
Bitfield3D(size_t b_size1, size_t b_size2, size_t b_size3)
Cosntructor which sets the initial sizes.
Definition: Bitfield3D.h:55
void Zero2D(const size_t z)
Set all bits of dimensions 1 and 2 to zero at z indexed dimension 3.
Definition: Bitfield3D.h:123
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