BitArray.h
author StephaneLenclud
Wed, 04 Feb 2015 21:53:06 +0100
changeset 26 f943cd500390
parent 0 0f874d9e4130
permissions -rw-r--r--
Merge
     1 /***************************************************************************
     2 *                         Arrays of Arbitrary Bit Length
     3 *
     4 *   File    : bitarray.h
     5 *   Purpose : Header file for class supporting the creation and
     6 *             manipulation of arbitrary length arrays of bits.
     7 *   Author  : Michael Dipperstein
     8 *   Date    : July 23, 2004
     9 *
    10 ****************************************************************************
    11 *   HISTORY
    12 *
    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.
    17 *
    18 *   Made updates for GCC 4.4.
    19 *
    20 *   Revision 1.4  2007/08/06 05:23:12  michael
    21 *   Updated for LGPL Version 3.
    22 *
    23 *   All methods that don't modify object have been made
    24 *   const to increase functionality of const bit_array_c.
    25 *
    26 *   All assignment operators return a reference to the object being assigned a value so that operator chaining will work.
    27 *
    28 *   Added >> and << operators.
    29 *
    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.
    33 *
    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.
    37 *
    38 *   Revision 1.1.1.1  2004/08/04 13:28:20  michael
    39 *   bit_array_c
    40 *
    41 ****************************************************************************
    42 *
    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)
    46 *
    47 * This file is part of the bit array library.
    48 *
    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.
    53 *
    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.
    58 *
    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/>.
    61 *
    62 ***************************************************************************/
    63 #ifndef BIT_ARRAY_H
    64 #define BIT_ARRAY_H
    65 
    66 /***************************************************************************
    67 *                             INCLUDED FILES
    68 ***************************************************************************/
    69 #include <ostream>
    70 
    71 /***************************************************************************
    72 *                            TYPE DEFINITIONS
    73 ***************************************************************************/
    74 typedef unsigned char (*TBitInChar)(const unsigned int aBit);
    75 template <TBitInChar F>
    76 class BitArray;
    77 
    78 
    79 //Bits are indexed: 76543210
    80 inline unsigned char HighBitHighIndex(const unsigned int aBit) { return (1 << (((aBit)  % CHAR_BIT)));};
    81 
    82 //Bits are indexed: 01234567
    83 inline unsigned char HighBitLowIndex(const unsigned int aBit) { return (1 << (CHAR_BIT - 1 - ((aBit)  % CHAR_BIT)));};
    84 
    85 
    86 
    87 /**
    88 */
    89 template <TBitInChar F>
    90 class BitArrayIndex
    91 {
    92     public:
    93         BitArrayIndex(BitArray<F> *array, const unsigned int index);
    94 
    95         /* assignment */
    96         void operator=(const bool src);
    97 
    98     private:
    99         BitArray<F> *m_BitArray;        /* array index applies to */
   100         unsigned int m_Index;           /* index of bit in array */
   101 };
   102 
   103 /**
   104 TODO: Derive a Bit Frame class from this one for X Y access to pixels.
   105 */
   106 template <TBitInChar F>
   107 class BitArray
   108 {
   109     public:
   110         BitArray(const int numBits);
   111         BitArray(unsigned char *array, const int numBits, bool aOwnsBuffer);
   112 
   113         virtual ~BitArray(void);
   114 
   115         void Dump(std::ostream &outStream);
   116 
   117         const unsigned int SizeInBits() { return m_NumBits; }
   118         const unsigned int SizeInBytes() { return m_SizeInBytes; }
   119 
   120         /* set/clear functions */
   121         void SetAll(void);
   122         void ClearAll(void);
   123         void SetBit(const unsigned int bit);
   124 		void SetBitValue(const unsigned int bit, bool aValue);
   125         void ClearBit(const unsigned int bit);
   126 
   127         BitArrayIndex<F> operator()(const unsigned int bit);
   128 
   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;
   137 
   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;
   143 
   144         BitArray<F> operator<<(const unsigned int count) const;
   145         BitArray<F> operator>>(const unsigned int count) const;
   146 
   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 */
   152 
   153         /* assignments */
   154         BitArray<F>& operator=(const BitArray<F> &src);
   155 
   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 (~=) */
   160 
   161         BitArray<F>& operator<<=(unsigned const int shifts);
   162         BitArray<F>& operator>>=(unsigned const int shifts);
   163 
   164 		unsigned char* Ptr(){return m_Array;}
   165 		const unsigned char* Ptr() const{return m_Array;}
   166 
   167     protected:
   168         unsigned int m_NumBits;                 /* number of bits in the array */
   169 		unsigned int m_SizeInBytes;
   170         unsigned char *m_Array;                 /* vector of characters */
   171         bool m_OwnsBuffer;
   172 };
   173 
   174 typedef BitArray<HighBitHighIndex> BitArrayHigh;
   175 typedef BitArray<HighBitLowIndex> BitArrayLow;
   176 
   177 #endif  /* ndef BIT_ARRAY_H */