inc/BitArray.h
author sl
Tue, 27 May 2014 17:49:33 +0200
changeset 27 ee1305f3a6bf
parent 25 233a997193b8
permissions -rw-r--r--
Experimenting with our bitmap to bitarray convertion.
sl@25
     1
/***************************************************************************
sl@25
     2
*                         Arrays of Arbitrary Bit Length
sl@25
     3
*
sl@25
     4
*   File    : bitarray.h
sl@25
     5
*   Purpose : Header file for class supporting the creation and
sl@25
     6
*             manipulation of arbitrary length arrays of bits.
sl@25
     7
*   Author  : Michael Dipperstein
sl@25
     8
*   Date    : July 23, 2004
sl@25
     9
*
sl@25
    10
****************************************************************************
sl@25
    11
*   HISTORY
sl@25
    12
*
sl@25
    13
*   $Id: bitarray.h,v 1.5 2010/02/04 03:31:43 michael Exp $
sl@25
    14
*   $Log: bitarray.h,v $
sl@25
    15
*   Revision 1.5  2010/02/04 03:31:43  michael
sl@25
    16
*   Replaced vector<unsigned char> with an array of unsigned char.
sl@25
    17
*
sl@25
    18
*   Made updates for GCC 4.4.
sl@25
    19
*
sl@25
    20
*   Revision 1.4  2007/08/06 05:23:12  michael
sl@25
    21
*   Updated for LGPL Version 3.
sl@25
    22
*
sl@25
    23
*   All methods that don't modify object have been made
sl@25
    24
*   const to increase functionality of const bit_array_c.
sl@25
    25
*
sl@25
    26
*   All assignment operators return a reference to the object being assigned a value so that operator chaining will work.
sl@25
    27
*
sl@25
    28
*   Added >> and << operators.
sl@25
    29
*
sl@25
    30
*   Revision 1.3  2006/04/30 23:34:07  michael
sl@25
    31
*   Improved performance by incorporating Benjamin Schindler's
sl@25
    32
*   <bschindler@student.ethz.ch> changes to pass arguments as a reference.
sl@25
    33
*
sl@25
    34
*   Revision 1.2  2004/08/05 22:17:04  michael
sl@25
    35
*   Add overloads for bitwise operators returning values.
sl@25
    36
*   Add a more natural looking way to set bit values.
sl@25
    37
*
sl@25
    38
*   Revision 1.1.1.1  2004/08/04 13:28:20  michael
sl@25
    39
*   bit_array_c
sl@25
    40
*
sl@25
    41
****************************************************************************
sl@25
    42
*
sl@25
    43
* Bitarray: An ANSI C++ class for manipulating arbitrary length bit arrays
sl@25
    44
* Copyright (C) 2004, 2006-2007, 2010 by
sl@25
    45
*       Michael Dipperstein (mdipper@alumni.engr.ucsb.edu)
sl@25
    46
*
sl@25
    47
* This file is part of the bit array library.
sl@25
    48
*
sl@25
    49
* The bit array library is free software; you can redistribute it and/or
sl@25
    50
* modify it under the terms of the GNU Lesser General Public License as
sl@25
    51
* published by the Free Software Foundation; either version 3 of the
sl@25
    52
* License, or (at your option) any later version.
sl@25
    53
*
sl@25
    54
* The bit array library is distributed in the hope that it will be useful,
sl@25
    55
* but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@25
    56
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
sl@25
    57
* General Public License for more details.
sl@25
    58
*
sl@25
    59
* You should have received a copy of the GNU Lesser General Public License
sl@25
    60
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
sl@25
    61
*
sl@25
    62
***************************************************************************/
sl@25
    63
#ifndef BIT_ARRAY_H
sl@25
    64
#define BIT_ARRAY_H
sl@25
    65
sl@25
    66
/***************************************************************************
sl@25
    67
*                             INCLUDED FILES
sl@25
    68
***************************************************************************/
sl@25
    69
#include <ostream>
sl@25
    70
sl@25
    71
/***************************************************************************
sl@25
    72
*                            TYPE DEFINITIONS
sl@25
    73
***************************************************************************/
sl@25
    74
class BitArray;
sl@25
    75
sl@25
    76
/**
sl@25
    77
*/
sl@25
    78
class BitArrayIndex
sl@25
    79
{
sl@25
    80
    public:
sl@25
    81
        BitArrayIndex(BitArray *array, const unsigned int index);
sl@25
    82
sl@25
    83
        /* assignment */
sl@25
    84
        void operator=(const bool src);
sl@25
    85
sl@25
    86
    private:
sl@25
    87
        BitArray *m_BitArray;        /* array index applies to */
sl@25
    88
        unsigned int m_Index;           /* index of bit in array */
sl@25
    89
};
sl@25
    90
sl@25
    91
/**
sl@27
    92
TODO: Derive a Bit Frame class from this one for X Y access to pixels.
sl@25
    93
*/
sl@25
    94
class BitArray
sl@25
    95
{
sl@25
    96
    public:
sl@25
    97
        BitArray(const int numBits);
sl@25
    98
        BitArray(unsigned char *array, const int numBits);
sl@25
    99
sl@25
   100
        virtual ~BitArray(void);
sl@25
   101
sl@25
   102
        void Dump(std::ostream &outStream);
sl@25
   103
sl@25
   104
        const unsigned int SizeInBits() { return m_NumBits; };
sl@25
   105
		const unsigned int SizeInBytes() { return m_SizeInBytes; };
sl@25
   106
sl@25
   107
        /* set/clear functions */
sl@25
   108
        void SetAll(void);
sl@25
   109
        void ClearAll(void);
sl@25
   110
        void SetBit(const unsigned int bit);
sl@25
   111
		void SetBitValue(const unsigned int bit, bool aValue);
sl@25
   112
        void ClearBit(const unsigned int bit);
sl@25
   113
sl@25
   114
        BitArrayIndex operator()(const unsigned int bit);
sl@25
   115
sl@25
   116
        /* boolean operator */
sl@25
   117
        bool operator[](const unsigned int bit) const;
sl@25
   118
        bool operator==(const BitArray &other) const;
sl@25
   119
        bool operator!=(const BitArray &other) const;
sl@25
   120
        bool operator<(const BitArray &other) const;
sl@25
   121
        bool operator<=(const BitArray &other) const;
sl@25
   122
        bool operator>(const BitArray &other) const;
sl@25
   123
        bool operator>=(const BitArray &other) const;
sl@25
   124
sl@25
   125
        /* bitwise operators */
sl@25
   126
        BitArray operator&(const BitArray &other) const;
sl@25
   127
        BitArray operator^(const BitArray &other) const;
sl@25
   128
        BitArray operator|(const BitArray &other) const;
sl@25
   129
        BitArray operator~(void) const;
sl@25
   130
sl@25
   131
        BitArray operator<<(const unsigned int count) const;
sl@25
   132
        BitArray operator>>(const unsigned int count) const;
sl@25
   133
sl@25
   134
        /* increment/decrement */
sl@25
   135
        BitArray& operator++(void);          /* prefix */
sl@25
   136
        BitArray& operator++(int dummy);     /* postfix */
sl@25
   137
        BitArray& operator--(void);          /* prefix */
sl@25
   138
        BitArray& operator--(int dummy);     /* postfix */
sl@25
   139
sl@25
   140
        /* assignments */
sl@25
   141
        BitArray& operator=(const BitArray &src);
sl@25
   142
sl@25
   143
        BitArray& operator&=(const BitArray &src);
sl@25
   144
        BitArray& operator^=(const BitArray &src);
sl@25
   145
        BitArray& operator|=(const BitArray &src);
sl@25
   146
        BitArray& Not(void);                 /* negate (~=) */
sl@25
   147
sl@25
   148
        BitArray& operator<<=(unsigned const int shifts);
sl@25
   149
        BitArray& operator>>=(unsigned const int shifts);
sl@25
   150
sl@25
   151
		unsigned char* Ptr(){return m_Array;}
sl@25
   152
		const unsigned char* Ptr() const{return m_Array;}
sl@25
   153
sl@25
   154
    protected:
sl@25
   155
        unsigned int m_NumBits;                 /* number of bits in the array */
sl@25
   156
		unsigned int m_SizeInBytes;
sl@25
   157
        unsigned char *m_Array;                 /* vector of characters */
sl@25
   158
};
sl@25
   159
sl@25
   160
#endif  /* ndef BIT_ARRAY_H */