1 /***************************************************************************
2 * Arrays of Arbitrary Bit Length
5 * Purpose : Header file for class supporting the creation and
6 * manipulation of arbitrary length arrays of bits.
7 * Author : Michael Dipperstein
10 ****************************************************************************
13 * $Id: bitarray.h,v 1.5 2010/02/04 03:31:43 michael Exp $
14 * $Log: bitarray.h,v $
15 * Revision 1.5 2010/02/04 03:31:43 michael
16 * Replaced vector<unsigned char> with an array of unsigned char.
18 * Made updates for GCC 4.4.
20 * Revision 1.4 2007/08/06 05:23:12 michael
21 * Updated for LGPL Version 3.
23 * All methods that don't modify object have been made
24 * const to increase functionality of const bit_array_c.
26 * All assignment operators return a reference to the object being assigned a value so that operator chaining will work.
28 * Added >> and << operators.
30 * Revision 1.3 2006/04/30 23:34:07 michael
31 * Improved performance by incorporating Benjamin Schindler's
32 * <bschindler@student.ethz.ch> changes to pass arguments as a reference.
34 * Revision 1.2 2004/08/05 22:17:04 michael
35 * Add overloads for bitwise operators returning values.
36 * Add a more natural looking way to set bit values.
38 * Revision 1.1.1.1 2004/08/04 13:28:20 michael
41 ****************************************************************************
43 * Bitarray: An ANSI C++ class for manipulating arbitrary length bit arrays
44 * Copyright (C) 2004, 2006-2007, 2010 by
45 * Michael Dipperstein (mdipper@alumni.engr.ucsb.edu)
47 * This file is part of the bit array library.
49 * The bit array library is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU Lesser General Public License as
51 * published by the Free Software Foundation; either version 3 of the
52 * License, or (at your option) any later version.
54 * The bit array library is distributed in the hope that it will be useful,
55 * but WITHOUT ANY WARRANTY; without even the implied warranty of
56 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
57 * General Public License for more details.
59 * You should have received a copy of the GNU Lesser General Public License
60 * along with this program. If not, see <http://www.gnu.org/licenses/>.
62 ***************************************************************************/
66 /***************************************************************************
68 ***************************************************************************/
71 /***************************************************************************
73 ***************************************************************************/
74 typedef unsigned char (*TBitInChar)(const unsigned int aBit);
75 template <TBitInChar F>
79 //Bits are indexed: 76543210
80 inline unsigned char HighBitHighIndex(const unsigned int aBit) { return (1 << (((aBit) % CHAR_BIT)));};
82 //Bits are indexed: 01234567
83 inline unsigned char HighBitLowIndex(const unsigned int aBit) { return (1 << (CHAR_BIT - 1 - ((aBit) % CHAR_BIT)));};
89 template <TBitInChar F>
93 BitArrayIndex(BitArray<F> *array, const unsigned int index);
96 void operator=(const bool src);
99 BitArray<F> *m_BitArray; /* array index applies to */
100 unsigned int m_Index; /* index of bit in array */
104 TODO: Derive a Bit Frame class from this one for X Y access to pixels.
106 template <TBitInChar F>
110 BitArray(const int numBits);
111 BitArray(unsigned char *array, const int numBits, bool aOwnsBuffer);
113 virtual ~BitArray(void);
115 void Dump(std::ostream &outStream);
117 const unsigned int SizeInBits() { return m_NumBits; }
118 const unsigned int SizeInBytes() { return m_SizeInBytes; }
120 /* set/clear functions */
123 void SetBit(const unsigned int bit);
124 void SetBitValue(const unsigned int bit, bool aValue);
125 void ClearBit(const unsigned int bit);
127 BitArrayIndex<F> operator()(const unsigned int bit);
129 /* boolean operator */
130 bool operator[](const unsigned int bit) const;
131 bool operator==(const BitArray<F> &other) const;
132 bool operator!=(const BitArray<F> &other) const;
133 bool operator<(const BitArray<F> &other) const;
134 bool operator<=(const BitArray<F> &other) const;
135 bool operator>(const BitArray<F> &other) const;
136 bool operator>=(const BitArray<F> &other) const;
138 /* bitwise operators */
139 BitArray<F> operator&(const BitArray<F> &other) const;
140 BitArray<F> operator^(const BitArray<F> &other) const;
141 BitArray<F> operator|(const BitArray<F> &other) const;
142 BitArray<F> operator~(void) const;
144 BitArray<F> operator<<(const unsigned int count) const;
145 BitArray<F> operator>>(const unsigned int count) const;
147 /* increment/decrement */
148 BitArray<F>& operator++(void); /* prefix */
149 BitArray<F>& operator++(int dummy); /* postfix */
150 BitArray<F>& operator--(void); /* prefix */
151 BitArray<F>& operator--(int dummy); /* postfix */
154 BitArray<F>& operator=(const BitArray<F> &src);
156 BitArray<F>& operator&=(const BitArray<F> &src);
157 BitArray<F>& operator^=(const BitArray<F> &src);
158 BitArray<F>& operator|=(const BitArray<F> &src);
159 BitArray<F>& Not(void); /* negate (~=) */
161 BitArray<F>& operator<<=(unsigned const int shifts);
162 BitArray<F>& operator>>=(unsigned const int shifts);
164 unsigned char* Ptr(){return m_Array;}
165 const unsigned char* Ptr() const{return m_Array;}
168 unsigned int m_NumBits; /* number of bits in the array */
169 unsigned int m_SizeInBytes;
170 unsigned char *m_Array; /* vector of characters */
174 typedef BitArray<HighBitHighIndex> BitArrayHigh;
175 typedef BitArray<HighBitLowIndex> BitArrayLow;
177 #endif /* ndef BIT_ARRAY_H */