BitArray.h
author sl
Mon, 07 Jul 2014 21:32:47 +0200
changeset 2 d8186e7b69bf
child 13 70907579a3b6
permissions -rw-r--r--
Implementing and C API to control our MiniDisplay.
     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 class BitArray;
    75 
    76 /**
    77 */
    78 class BitArrayIndex
    79 {
    80     public:
    81         BitArrayIndex(BitArray *array, const unsigned int index);
    82 
    83         /* assignment */
    84         void operator=(const bool src);
    85 
    86     private:
    87         BitArray *m_BitArray;        /* array index applies to */
    88         unsigned int m_Index;           /* index of bit in array */
    89 };
    90 
    91 /**
    92 TODO: Derive a Bit Frame class from this one for X Y access to pixels.
    93 */
    94 class BitArray
    95 {
    96     public:
    97         BitArray(const int numBits);
    98         BitArray(unsigned char *array, const int numBits, bool aOwnsBuffer);
    99 
   100         virtual ~BitArray(void);
   101 
   102         void Dump(std::ostream &outStream);
   103 
   104         const unsigned int SizeInBits() { return m_NumBits; }
   105         const unsigned int SizeInBytes() { return m_SizeInBytes; }
   106 
   107         /* set/clear functions */
   108         void SetAll(void);
   109         void ClearAll(void);
   110         void SetBit(const unsigned int bit);
   111 		void SetBitValue(const unsigned int bit, bool aValue);
   112         void ClearBit(const unsigned int bit);
   113 
   114         BitArrayIndex operator()(const unsigned int bit);
   115 
   116         /* boolean operator */
   117         bool operator[](const unsigned int bit) const;
   118         bool operator==(const BitArray &other) const;
   119         bool operator!=(const BitArray &other) const;
   120         bool operator<(const BitArray &other) const;
   121         bool operator<=(const BitArray &other) const;
   122         bool operator>(const BitArray &other) const;
   123         bool operator>=(const BitArray &other) const;
   124 
   125         /* bitwise operators */
   126         BitArray operator&(const BitArray &other) const;
   127         BitArray operator^(const BitArray &other) const;
   128         BitArray operator|(const BitArray &other) const;
   129         BitArray operator~(void) const;
   130 
   131         BitArray operator<<(const unsigned int count) const;
   132         BitArray operator>>(const unsigned int count) const;
   133 
   134         /* increment/decrement */
   135         BitArray& operator++(void);          /* prefix */
   136         BitArray& operator++(int dummy);     /* postfix */
   137         BitArray& operator--(void);          /* prefix */
   138         BitArray& operator--(int dummy);     /* postfix */
   139 
   140         /* assignments */
   141         BitArray& operator=(const BitArray &src);
   142 
   143         BitArray& operator&=(const BitArray &src);
   144         BitArray& operator^=(const BitArray &src);
   145         BitArray& operator|=(const BitArray &src);
   146         BitArray& Not(void);                 /* negate (~=) */
   147 
   148         BitArray& operator<<=(unsigned const int shifts);
   149         BitArray& operator>>=(unsigned const int shifts);
   150 
   151 		unsigned char* Ptr(){return m_Array;}
   152 		const unsigned char* Ptr() const{return m_Array;}
   153 
   154     protected:
   155         unsigned int m_NumBits;                 /* number of bits in the array */
   156 		unsigned int m_SizeInBytes;
   157         unsigned char *m_Array;                 /* vector of characters */
   158         bool m_OwnsBuffer;
   159 };
   160 
   161 #endif  /* ndef BIT_ARRAY_H */