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