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