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@13
|
74 |
typedef unsigned char (*TBitInChar)(const unsigned int aBit);
|
sl@13
|
75 |
template <TBitInChar F>
|
sl@0
|
76 |
class BitArray;
|
sl@0
|
77 |
|
sl@13
|
78 |
|
sl@13
|
79 |
//Bits are indexed: 76543210
|
sl@13
|
80 |
inline unsigned char HighBitHighIndex(const unsigned int aBit) { return (1 << (((aBit) % CHAR_BIT)));};
|
sl@13
|
81 |
|
sl@13
|
82 |
//Bits are indexed: 01234567
|
sl@13
|
83 |
inline unsigned char HighBitLowIndex(const unsigned int aBit) { return (1 << (CHAR_BIT - 1 - ((aBit) % CHAR_BIT)));};
|
sl@13
|
84 |
|
sl@13
|
85 |
|
sl@13
|
86 |
|
sl@0
|
87 |
/**
|
sl@0
|
88 |
*/
|
sl@13
|
89 |
template <TBitInChar F>
|
sl@0
|
90 |
class BitArrayIndex
|
sl@0
|
91 |
{
|
sl@0
|
92 |
public:
|
sl@13
|
93 |
BitArrayIndex(BitArray<F> *array, const unsigned int index);
|
sl@0
|
94 |
|
sl@0
|
95 |
/* assignment */
|
sl@0
|
96 |
void operator=(const bool src);
|
sl@0
|
97 |
|
sl@0
|
98 |
private:
|
sl@13
|
99 |
BitArray<F> *m_BitArray; /* array index applies to */
|
sl@0
|
100 |
unsigned int m_Index; /* index of bit in array */
|
sl@0
|
101 |
};
|
sl@0
|
102 |
|
sl@0
|
103 |
/**
|
sl@0
|
104 |
TODO: Derive a Bit Frame class from this one for X Y access to pixels.
|
sl@0
|
105 |
*/
|
sl@13
|
106 |
template <TBitInChar F>
|
sl@0
|
107 |
class BitArray
|
sl@0
|
108 |
{
|
sl@0
|
109 |
public:
|
sl@0
|
110 |
BitArray(const int numBits);
|
sl@0
|
111 |
BitArray(unsigned char *array, const int numBits, bool aOwnsBuffer);
|
sl@0
|
112 |
|
sl@0
|
113 |
virtual ~BitArray(void);
|
sl@0
|
114 |
|
sl@0
|
115 |
void Dump(std::ostream &outStream);
|
sl@0
|
116 |
|
sl@0
|
117 |
const unsigned int SizeInBits() { return m_NumBits; }
|
sl@0
|
118 |
const unsigned int SizeInBytes() { return m_SizeInBytes; }
|
sl@0
|
119 |
|
sl@0
|
120 |
/* set/clear functions */
|
sl@0
|
121 |
void SetAll(void);
|
sl@0
|
122 |
void ClearAll(void);
|
sl@0
|
123 |
void SetBit(const unsigned int bit);
|
sl@0
|
124 |
void SetBitValue(const unsigned int bit, bool aValue);
|
sl@0
|
125 |
void ClearBit(const unsigned int bit);
|
sl@0
|
126 |
|
sl@13
|
127 |
BitArrayIndex<F> operator()(const unsigned int bit);
|
sl@0
|
128 |
|
sl@0
|
129 |
/* boolean operator */
|
sl@0
|
130 |
bool operator[](const unsigned int bit) const;
|
sl@13
|
131 |
bool operator==(const BitArray<F> &other) const;
|
sl@13
|
132 |
bool operator!=(const BitArray<F> &other) const;
|
sl@13
|
133 |
bool operator<(const BitArray<F> &other) const;
|
sl@13
|
134 |
bool operator<=(const BitArray<F> &other) const;
|
sl@13
|
135 |
bool operator>(const BitArray<F> &other) const;
|
sl@13
|
136 |
bool operator>=(const BitArray<F> &other) const;
|
sl@0
|
137 |
|
sl@0
|
138 |
/* bitwise operators */
|
sl@13
|
139 |
BitArray<F> operator&(const BitArray<F> &other) const;
|
sl@13
|
140 |
BitArray<F> operator^(const BitArray<F> &other) const;
|
sl@13
|
141 |
BitArray<F> operator|(const BitArray<F> &other) const;
|
sl@13
|
142 |
BitArray<F> operator~(void) const;
|
sl@0
|
143 |
|
sl@13
|
144 |
BitArray<F> operator<<(const unsigned int count) const;
|
sl@13
|
145 |
BitArray<F> operator>>(const unsigned int count) const;
|
sl@0
|
146 |
|
sl@0
|
147 |
/* increment/decrement */
|
sl@13
|
148 |
BitArray<F>& operator++(void); /* prefix */
|
sl@13
|
149 |
BitArray<F>& operator++(int dummy); /* postfix */
|
sl@13
|
150 |
BitArray<F>& operator--(void); /* prefix */
|
sl@13
|
151 |
BitArray<F>& operator--(int dummy); /* postfix */
|
sl@0
|
152 |
|
sl@0
|
153 |
/* assignments */
|
sl@13
|
154 |
BitArray<F>& operator=(const BitArray<F> &src);
|
sl@0
|
155 |
|
sl@13
|
156 |
BitArray<F>& operator&=(const BitArray<F> &src);
|
sl@13
|
157 |
BitArray<F>& operator^=(const BitArray<F> &src);
|
sl@13
|
158 |
BitArray<F>& operator|=(const BitArray<F> &src);
|
sl@13
|
159 |
BitArray<F>& Not(void); /* negate (~=) */
|
sl@0
|
160 |
|
sl@13
|
161 |
BitArray<F>& operator<<=(unsigned const int shifts);
|
sl@13
|
162 |
BitArray<F>& operator>>=(unsigned const int shifts);
|
sl@0
|
163 |
|
sl@0
|
164 |
unsigned char* Ptr(){return m_Array;}
|
sl@0
|
165 |
const unsigned char* Ptr() const{return m_Array;}
|
sl@0
|
166 |
|
sl@0
|
167 |
protected:
|
sl@0
|
168 |
unsigned int m_NumBits; /* number of bits in the array */
|
sl@0
|
169 |
unsigned int m_SizeInBytes;
|
sl@0
|
170 |
unsigned char *m_Array; /* vector of characters */
|
sl@0
|
171 |
bool m_OwnsBuffer;
|
sl@0
|
172 |
};
|
sl@0
|
173 |
|
sl@13
|
174 |
typedef BitArray<HighBitHighIndex> BitArrayHigh;
|
sl@13
|
175 |
typedef BitArray<HighBitLowIndex> BitArrayLow;
|
sl@13
|
176 |
|
sl@0
|
177 |
#endif /* ndef BIT_ARRAY_H */
|