sl@0
|
1 |
/***************************************************************************
|
sl@0
|
2 |
* Arrays of Arbitrary Bit Length
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* File : bitarray.h
|
sl@0
|
5 |
* Purpose : Header file for class supporting the creation and
|
sl@0
|
6 |
* manipulation of arbitrary length arrays of bits.
|
sl@0
|
7 |
* Author : Michael Dipperstein
|
sl@0
|
8 |
* Date : July 23, 2004
|
sl@0
|
9 |
*
|
sl@0
|
10 |
****************************************************************************
|
sl@0
|
11 |
* HISTORY
|
sl@0
|
12 |
*
|
sl@0
|
13 |
* $Id: bitarray.h,v 1.5 2010/02/04 03:31:43 michael Exp $
|
sl@0
|
14 |
* $Log: bitarray.h,v $
|
sl@0
|
15 |
* Revision 1.5 2010/02/04 03:31:43 michael
|
sl@0
|
16 |
* Replaced vector<unsigned char> with an array of unsigned char.
|
sl@0
|
17 |
*
|
sl@0
|
18 |
* Made updates for GCC 4.4.
|
sl@0
|
19 |
*
|
sl@0
|
20 |
* Revision 1.4 2007/08/06 05:23:12 michael
|
sl@0
|
21 |
* Updated for LGPL Version 3.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* All methods that don't modify object have been made
|
sl@0
|
24 |
* const to increase functionality of const bit_array_c.
|
sl@0
|
25 |
*
|
sl@0
|
26 |
* All assignment operators return a reference to the object being assigned a value so that operator chaining will work.
|
sl@0
|
27 |
*
|
sl@0
|
28 |
* Added >> and << operators.
|
sl@0
|
29 |
*
|
sl@0
|
30 |
* Revision 1.3 2006/04/30 23:34:07 michael
|
sl@0
|
31 |
* Improved performance by incorporating Benjamin Schindler's
|
sl@0
|
32 |
* <bschindler@student.ethz.ch> changes to pass arguments as a reference.
|
sl@0
|
33 |
*
|
sl@0
|
34 |
* Revision 1.2 2004/08/05 22:17:04 michael
|
sl@0
|
35 |
* Add overloads for bitwise operators returning values.
|
sl@0
|
36 |
* Add a more natural looking way to set bit values.
|
sl@0
|
37 |
*
|
sl@0
|
38 |
* Revision 1.1.1.1 2004/08/04 13:28:20 michael
|
sl@0
|
39 |
* bit_array_c
|
sl@0
|
40 |
*
|
sl@0
|
41 |
****************************************************************************
|
sl@0
|
42 |
*
|
sl@0
|
43 |
* Bitarray: An ANSI C++ class for manipulating arbitrary length bit arrays
|
sl@0
|
44 |
* Copyright (C) 2004, 2006-2007, 2010 by
|
sl@0
|
45 |
* Michael Dipperstein (mdipper@alumni.engr.ucsb.edu)
|
sl@0
|
46 |
*
|
sl@0
|
47 |
* This file is part of the bit array library.
|
sl@0
|
48 |
*
|
sl@0
|
49 |
* The bit array library is free software; you can redistribute it and/or
|
sl@0
|
50 |
* modify it under the terms of the GNU Lesser General Public License as
|
sl@0
|
51 |
* published by the Free Software Foundation; either version 3 of the
|
sl@0
|
52 |
* License, or (at your option) any later version.
|
sl@0
|
53 |
*
|
sl@0
|
54 |
* The bit array library is distributed in the hope that it will be useful,
|
sl@0
|
55 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
56 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
|
sl@0
|
57 |
* General Public License for more details.
|
sl@0
|
58 |
*
|
sl@0
|
59 |
* You should have received a copy of the GNU Lesser General Public License
|
sl@0
|
60 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
sl@0
|
61 |
*
|
sl@0
|
62 |
***************************************************************************/
|
sl@0
|
63 |
#ifndef BIT_ARRAY_H
|
sl@0
|
64 |
#define BIT_ARRAY_H
|
sl@0
|
65 |
|
sl@0
|
66 |
/***************************************************************************
|
sl@0
|
67 |
* INCLUDED FILES
|
sl@0
|
68 |
***************************************************************************/
|
sl@0
|
69 |
#include <ostream>
|
sl@0
|
70 |
|
sl@0
|
71 |
/***************************************************************************
|
sl@0
|
72 |
* TYPE DEFINITIONS
|
sl@0
|
73 |
***************************************************************************/
|
sl@0
|
74 |
class BitArray;
|
sl@0
|
75 |
|
sl@0
|
76 |
/**
|
sl@0
|
77 |
*/
|
sl@0
|
78 |
class BitArrayIndex
|
sl@0
|
79 |
{
|
sl@0
|
80 |
public:
|
sl@0
|
81 |
BitArrayIndex(BitArray *array, const unsigned int index);
|
sl@0
|
82 |
|
sl@0
|
83 |
/* assignment */
|
sl@0
|
84 |
void operator=(const bool src);
|
sl@0
|
85 |
|
sl@0
|
86 |
private:
|
sl@0
|
87 |
BitArray *m_BitArray; /* array index applies to */
|
sl@0
|
88 |
unsigned int m_Index; /* index of bit in array */
|
sl@0
|
89 |
};
|
sl@0
|
90 |
|
sl@0
|
91 |
/**
|
sl@0
|
92 |
TODO: Derive a Bit Frame class from this one for X Y access to pixels.
|
sl@0
|
93 |
*/
|
sl@0
|
94 |
class BitArray
|
sl@0
|
95 |
{
|
sl@0
|
96 |
public:
|
sl@0
|
97 |
BitArray(const int numBits);
|
sl@0
|
98 |
BitArray(unsigned char *array, const int numBits, bool aOwnsBuffer);
|
sl@0
|
99 |
|
sl@0
|
100 |
virtual ~BitArray(void);
|
sl@0
|
101 |
|
sl@0
|
102 |
void Dump(std::ostream &outStream);
|
sl@0
|
103 |
|
sl@0
|
104 |
const unsigned int SizeInBits() { return m_NumBits; }
|
sl@0
|
105 |
const unsigned int SizeInBytes() { return m_SizeInBytes; }
|
sl@0
|
106 |
|
sl@0
|
107 |
/* set/clear functions */
|
sl@0
|
108 |
void SetAll(void);
|
sl@0
|
109 |
void ClearAll(void);
|
sl@0
|
110 |
void SetBit(const unsigned int bit);
|
sl@0
|
111 |
void SetBitValue(const unsigned int bit, bool aValue);
|
sl@0
|
112 |
void ClearBit(const unsigned int bit);
|
sl@0
|
113 |
|
sl@0
|
114 |
BitArrayIndex operator()(const unsigned int bit);
|
sl@0
|
115 |
|
sl@0
|
116 |
/* boolean operator */
|
sl@0
|
117 |
bool operator[](const unsigned int bit) const;
|
sl@0
|
118 |
bool operator==(const BitArray &other) const;
|
sl@0
|
119 |
bool operator!=(const BitArray &other) const;
|
sl@0
|
120 |
bool operator<(const BitArray &other) const;
|
sl@0
|
121 |
bool operator<=(const BitArray &other) const;
|
sl@0
|
122 |
bool operator>(const BitArray &other) const;
|
sl@0
|
123 |
bool operator>=(const BitArray &other) const;
|
sl@0
|
124 |
|
sl@0
|
125 |
/* bitwise operators */
|
sl@0
|
126 |
BitArray operator&(const BitArray &other) const;
|
sl@0
|
127 |
BitArray operator^(const BitArray &other) const;
|
sl@0
|
128 |
BitArray operator|(const BitArray &other) const;
|
sl@0
|
129 |
BitArray operator~(void) const;
|
sl@0
|
130 |
|
sl@0
|
131 |
BitArray operator<<(const unsigned int count) const;
|
sl@0
|
132 |
BitArray operator>>(const unsigned int count) const;
|
sl@0
|
133 |
|
sl@0
|
134 |
/* increment/decrement */
|
sl@0
|
135 |
BitArray& operator++(void); /* prefix */
|
sl@0
|
136 |
BitArray& operator++(int dummy); /* postfix */
|
sl@0
|
137 |
BitArray& operator--(void); /* prefix */
|
sl@0
|
138 |
BitArray& operator--(int dummy); /* postfix */
|
sl@0
|
139 |
|
sl@0
|
140 |
/* assignments */
|
sl@0
|
141 |
BitArray& operator=(const BitArray &src);
|
sl@0
|
142 |
|
sl@0
|
143 |
BitArray& operator&=(const BitArray &src);
|
sl@0
|
144 |
BitArray& operator^=(const BitArray &src);
|
sl@0
|
145 |
BitArray& operator|=(const BitArray &src);
|
sl@0
|
146 |
BitArray& Not(void); /* negate (~=) */
|
sl@0
|
147 |
|
sl@0
|
148 |
BitArray& operator<<=(unsigned const int shifts);
|
sl@0
|
149 |
BitArray& operator>>=(unsigned const int shifts);
|
sl@0
|
150 |
|
sl@0
|
151 |
unsigned char* Ptr(){return m_Array;}
|
sl@0
|
152 |
const unsigned char* Ptr() const{return m_Array;}
|
sl@0
|
153 |
|
sl@0
|
154 |
protected:
|
sl@0
|
155 |
unsigned int m_NumBits; /* number of bits in the array */
|
sl@0
|
156 |
unsigned int m_SizeInBytes;
|
sl@0
|
157 |
unsigned char *m_Array; /* vector of characters */
|
sl@0
|
158 |
bool m_OwnsBuffer;
|
sl@0
|
159 |
};
|
sl@0
|
160 |
|
sl@0
|
161 |
#endif /* ndef BIT_ARRAY_H */
|