sl@0: /*************************************************************************** sl@0: * Arrays of Arbitrary Bit Length sl@0: * sl@0: * File : bitarray.h sl@0: * Purpose : Header file for class supporting the creation and sl@0: * manipulation of arbitrary length arrays of bits. sl@0: * Author : Michael Dipperstein sl@0: * Date : July 23, 2004 sl@0: * sl@0: **************************************************************************** sl@0: * HISTORY sl@0: * sl@0: * $Id: bitarray.h,v 1.5 2010/02/04 03:31:43 michael Exp $ sl@0: * $Log: bitarray.h,v $ sl@0: * Revision 1.5 2010/02/04 03:31:43 michael sl@0: * Replaced vector with an array of unsigned char. sl@0: * sl@0: * Made updates for GCC 4.4. sl@0: * sl@0: * Revision 1.4 2007/08/06 05:23:12 michael sl@0: * Updated for LGPL Version 3. sl@0: * sl@0: * All methods that don't modify object have been made sl@0: * const to increase functionality of const bit_array_c. sl@0: * sl@0: * All assignment operators return a reference to the object being assigned a value so that operator chaining will work. sl@0: * sl@0: * Added >> and << operators. sl@0: * sl@0: * Revision 1.3 2006/04/30 23:34:07 michael sl@0: * Improved performance by incorporating Benjamin Schindler's sl@0: * changes to pass arguments as a reference. sl@0: * sl@0: * Revision 1.2 2004/08/05 22:17:04 michael sl@0: * Add overloads for bitwise operators returning values. sl@0: * Add a more natural looking way to set bit values. sl@0: * sl@0: * Revision 1.1.1.1 2004/08/04 13:28:20 michael sl@0: * bit_array_c sl@0: * sl@0: **************************************************************************** sl@0: * sl@0: * Bitarray: An ANSI C++ class for manipulating arbitrary length bit arrays sl@0: * Copyright (C) 2004, 2006-2007, 2010 by sl@0: * Michael Dipperstein (mdipper@alumni.engr.ucsb.edu) sl@0: * sl@0: * This file is part of the bit array library. sl@0: * sl@0: * The bit array library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public License as sl@0: * published by the Free Software Foundation; either version 3 of the sl@0: * License, or (at your option) any later version. sl@0: * sl@0: * The bit array library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser sl@0: * General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public License sl@0: * along with this program. If not, see . sl@0: * sl@0: ***************************************************************************/ sl@0: #ifndef BIT_ARRAY_H sl@0: #define BIT_ARRAY_H sl@0: sl@0: /*************************************************************************** sl@0: * INCLUDED FILES sl@0: ***************************************************************************/ sl@0: #include sl@0: sl@0: /*************************************************************************** sl@0: * TYPE DEFINITIONS sl@0: ***************************************************************************/ sl@13: typedef unsigned char (*TBitInChar)(const unsigned int aBit); sl@13: template sl@0: class BitArray; sl@0: sl@13: sl@13: //Bits are indexed: 76543210 sl@13: inline unsigned char HighBitHighIndex(const unsigned int aBit) { return (1 << (((aBit) % CHAR_BIT)));}; sl@13: sl@13: //Bits are indexed: 01234567 sl@13: inline unsigned char HighBitLowIndex(const unsigned int aBit) { return (1 << (CHAR_BIT - 1 - ((aBit) % CHAR_BIT)));}; sl@13: sl@13: sl@13: sl@0: /** sl@0: */ sl@13: template sl@0: class BitArrayIndex sl@0: { sl@0: public: sl@13: BitArrayIndex(BitArray *array, const unsigned int index); sl@0: sl@0: /* assignment */ sl@0: void operator=(const bool src); sl@0: sl@0: private: sl@13: BitArray *m_BitArray; /* array index applies to */ sl@0: unsigned int m_Index; /* index of bit in array */ sl@0: }; sl@0: sl@0: /** sl@0: TODO: Derive a Bit Frame class from this one for X Y access to pixels. sl@0: */ sl@13: template sl@0: class BitArray sl@0: { sl@0: public: sl@0: BitArray(const int numBits); sl@0: BitArray(unsigned char *array, const int numBits, bool aOwnsBuffer); sl@0: sl@0: virtual ~BitArray(void); sl@0: sl@0: void Dump(std::ostream &outStream); sl@0: sl@0: const unsigned int SizeInBits() { return m_NumBits; } sl@0: const unsigned int SizeInBytes() { return m_SizeInBytes; } sl@0: sl@0: /* set/clear functions */ sl@0: void SetAll(void); sl@0: void ClearAll(void); sl@0: void SetBit(const unsigned int bit); sl@0: void SetBitValue(const unsigned int bit, bool aValue); sl@0: void ClearBit(const unsigned int bit); sl@0: sl@13: BitArrayIndex operator()(const unsigned int bit); sl@0: sl@0: /* boolean operator */ sl@0: bool operator[](const unsigned int bit) const; sl@13: bool operator==(const BitArray &other) const; sl@13: bool operator!=(const BitArray &other) const; sl@13: bool operator<(const BitArray &other) const; sl@13: bool operator<=(const BitArray &other) const; sl@13: bool operator>(const BitArray &other) const; sl@13: bool operator>=(const BitArray &other) const; sl@0: sl@0: /* bitwise operators */ sl@13: BitArray operator&(const BitArray &other) const; sl@13: BitArray operator^(const BitArray &other) const; sl@13: BitArray operator|(const BitArray &other) const; sl@13: BitArray operator~(void) const; sl@0: sl@13: BitArray operator<<(const unsigned int count) const; sl@13: BitArray operator>>(const unsigned int count) const; sl@0: sl@0: /* increment/decrement */ sl@13: BitArray& operator++(void); /* prefix */ sl@13: BitArray& operator++(int dummy); /* postfix */ sl@13: BitArray& operator--(void); /* prefix */ sl@13: BitArray& operator--(int dummy); /* postfix */ sl@0: sl@0: /* assignments */ sl@13: BitArray& operator=(const BitArray &src); sl@0: sl@13: BitArray& operator&=(const BitArray &src); sl@13: BitArray& operator^=(const BitArray &src); sl@13: BitArray& operator|=(const BitArray &src); sl@13: BitArray& Not(void); /* negate (~=) */ sl@0: sl@13: BitArray& operator<<=(unsigned const int shifts); sl@13: BitArray& operator>>=(unsigned const int shifts); sl@0: sl@0: unsigned char* Ptr(){return m_Array;} sl@0: const unsigned char* Ptr() const{return m_Array;} sl@0: sl@0: protected: sl@0: unsigned int m_NumBits; /* number of bits in the array */ sl@0: unsigned int m_SizeInBytes; sl@0: unsigned char *m_Array; /* vector of characters */ sl@0: bool m_OwnsBuffer; sl@0: }; sl@0: sl@13: typedef BitArray BitArrayHigh; sl@13: typedef BitArray BitArrayLow; sl@13: sl@0: #endif /* ndef BIT_ARRAY_H */