inc/BitArray.h
changeset 25 233a997193b8
child 27 ee1305f3a6bf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/inc/BitArray.h	Sat May 24 00:43:18 2014 +0200
     1.3 @@ -0,0 +1,159 @@
     1.4 +/***************************************************************************
     1.5 +*                         Arrays of Arbitrary Bit Length
     1.6 +*
     1.7 +*   File    : bitarray.h
     1.8 +*   Purpose : Header file for class supporting the creation and
     1.9 +*             manipulation of arbitrary length arrays of bits.
    1.10 +*   Author  : Michael Dipperstein
    1.11 +*   Date    : July 23, 2004
    1.12 +*
    1.13 +****************************************************************************
    1.14 +*   HISTORY
    1.15 +*
    1.16 +*   $Id: bitarray.h,v 1.5 2010/02/04 03:31:43 michael Exp $
    1.17 +*   $Log: bitarray.h,v $
    1.18 +*   Revision 1.5  2010/02/04 03:31:43  michael
    1.19 +*   Replaced vector<unsigned char> with an array of unsigned char.
    1.20 +*
    1.21 +*   Made updates for GCC 4.4.
    1.22 +*
    1.23 +*   Revision 1.4  2007/08/06 05:23:12  michael
    1.24 +*   Updated for LGPL Version 3.
    1.25 +*
    1.26 +*   All methods that don't modify object have been made
    1.27 +*   const to increase functionality of const bit_array_c.
    1.28 +*
    1.29 +*   All assignment operators return a reference to the object being assigned a value so that operator chaining will work.
    1.30 +*
    1.31 +*   Added >> and << operators.
    1.32 +*
    1.33 +*   Revision 1.3  2006/04/30 23:34:07  michael
    1.34 +*   Improved performance by incorporating Benjamin Schindler's
    1.35 +*   <bschindler@student.ethz.ch> changes to pass arguments as a reference.
    1.36 +*
    1.37 +*   Revision 1.2  2004/08/05 22:17:04  michael
    1.38 +*   Add overloads for bitwise operators returning values.
    1.39 +*   Add a more natural looking way to set bit values.
    1.40 +*
    1.41 +*   Revision 1.1.1.1  2004/08/04 13:28:20  michael
    1.42 +*   bit_array_c
    1.43 +*
    1.44 +****************************************************************************
    1.45 +*
    1.46 +* Bitarray: An ANSI C++ class for manipulating arbitrary length bit arrays
    1.47 +* Copyright (C) 2004, 2006-2007, 2010 by
    1.48 +*       Michael Dipperstein (mdipper@alumni.engr.ucsb.edu)
    1.49 +*
    1.50 +* This file is part of the bit array library.
    1.51 +*
    1.52 +* The bit array library is free software; you can redistribute it and/or
    1.53 +* modify it under the terms of the GNU Lesser General Public License as
    1.54 +* published by the Free Software Foundation; either version 3 of the
    1.55 +* License, or (at your option) any later version.
    1.56 +*
    1.57 +* The bit array library is distributed in the hope that it will be useful,
    1.58 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.59 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
    1.60 +* General Public License for more details.
    1.61 +*
    1.62 +* You should have received a copy of the GNU Lesser General Public License
    1.63 +* along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.64 +*
    1.65 +***************************************************************************/
    1.66 +#ifndef BIT_ARRAY_H
    1.67 +#define BIT_ARRAY_H
    1.68 +
    1.69 +/***************************************************************************
    1.70 +*                             INCLUDED FILES
    1.71 +***************************************************************************/
    1.72 +#include <ostream>
    1.73 +
    1.74 +/***************************************************************************
    1.75 +*                            TYPE DEFINITIONS
    1.76 +***************************************************************************/
    1.77 +class BitArray;
    1.78 +
    1.79 +/**
    1.80 +*/
    1.81 +class BitArrayIndex
    1.82 +{
    1.83 +    public:
    1.84 +        BitArrayIndex(BitArray *array, const unsigned int index);
    1.85 +
    1.86 +        /* assignment */
    1.87 +        void operator=(const bool src);
    1.88 +
    1.89 +    private:
    1.90 +        BitArray *m_BitArray;        /* array index applies to */
    1.91 +        unsigned int m_Index;           /* index of bit in array */
    1.92 +};
    1.93 +
    1.94 +/**
    1.95 +*/
    1.96 +class BitArray
    1.97 +{
    1.98 +    public:
    1.99 +        BitArray(const int numBits);
   1.100 +        BitArray(unsigned char *array, const int numBits);
   1.101 +
   1.102 +        virtual ~BitArray(void);
   1.103 +
   1.104 +        void Dump(std::ostream &outStream);
   1.105 +
   1.106 +        const unsigned int SizeInBits() { return m_NumBits; };
   1.107 +		const unsigned int SizeInBytes() { return m_SizeInBytes; };
   1.108 +
   1.109 +        /* set/clear functions */
   1.110 +        void SetAll(void);
   1.111 +        void ClearAll(void);
   1.112 +        void SetBit(const unsigned int bit);
   1.113 +		void SetBitValue(const unsigned int bit, bool aValue);
   1.114 +        void ClearBit(const unsigned int bit);
   1.115 +
   1.116 +        BitArrayIndex operator()(const unsigned int bit);
   1.117 +
   1.118 +        /* boolean operator */
   1.119 +        bool operator[](const unsigned int bit) const;
   1.120 +        bool operator==(const BitArray &other) const;
   1.121 +        bool operator!=(const BitArray &other) const;
   1.122 +        bool operator<(const BitArray &other) const;
   1.123 +        bool operator<=(const BitArray &other) const;
   1.124 +        bool operator>(const BitArray &other) const;
   1.125 +        bool operator>=(const BitArray &other) const;
   1.126 +
   1.127 +        /* bitwise operators */
   1.128 +        BitArray operator&(const BitArray &other) const;
   1.129 +        BitArray operator^(const BitArray &other) const;
   1.130 +        BitArray operator|(const BitArray &other) const;
   1.131 +        BitArray operator~(void) const;
   1.132 +
   1.133 +        BitArray operator<<(const unsigned int count) const;
   1.134 +        BitArray operator>>(const unsigned int count) const;
   1.135 +
   1.136 +        /* increment/decrement */
   1.137 +        BitArray& operator++(void);          /* prefix */
   1.138 +        BitArray& operator++(int dummy);     /* postfix */
   1.139 +        BitArray& operator--(void);          /* prefix */
   1.140 +        BitArray& operator--(int dummy);     /* postfix */
   1.141 +
   1.142 +        /* assignments */
   1.143 +        BitArray& operator=(const BitArray &src);
   1.144 +
   1.145 +        BitArray& operator&=(const BitArray &src);
   1.146 +        BitArray& operator^=(const BitArray &src);
   1.147 +        BitArray& operator|=(const BitArray &src);
   1.148 +        BitArray& Not(void);                 /* negate (~=) */
   1.149 +
   1.150 +        BitArray& operator<<=(unsigned const int shifts);
   1.151 +        BitArray& operator>>=(unsigned const int shifts);
   1.152 +
   1.153 +		unsigned char* Ptr(){return m_Array;}
   1.154 +		const unsigned char* Ptr() const{return m_Array;}
   1.155 +
   1.156 +    protected:
   1.157 +        unsigned int m_NumBits;                 /* number of bits in the array */
   1.158 +		unsigned int m_SizeInBytes;
   1.159 +        unsigned char *m_Array;                 /* vector of characters */
   1.160 +};
   1.161 +
   1.162 +#endif  /* ndef BIT_ARRAY_H */