Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

2bitary.h

00001 // A one-dimensional array of two-bit entries.
00002 //
00003 // Copyright 2000 Andrew Kirmse.  All rights reserved.
00004 //
00005 // Permission is granted to use this code for any purpose, as long as this
00006 // copyright message remains intact.
00007 
00008 #ifndef _csTwoBitArray_H
00009 #define _csTwoBitArray_H
00010 
00011 #include "bitarray.h"
00012 
00013 class csTwoBitArray : private csBitArray
00014 {
00015    typedef csBitArray super;
00016    
00017 public:
00018 
00019    //
00020    // Two bit proxy (for operator[])
00021    //
00022    
00023    class TwoBitProxy
00024    {
00025    private:
00026       csTwoBitArray &mArray;
00027       unsigned     mPos;
00028    public:
00029       TwoBitProxy(csTwoBitArray &array, unsigned pos):
00030             mArray(array), mPos(pos)
00031       {}
00032 
00033       TwoBitProxy &operator=(const TwoBitProxy &that)
00034       {
00035          mArray.Set(mPos, that.mArray.Get(that.mPos));
00036          return *this;
00037       }
00038 
00039       TwoBitProxy &operator=(unsigned value)
00040       {
00041          mArray.Set(mPos, value);
00042          return *this;
00043       }
00044 
00045       operator unsigned() const
00046       {
00047          return mArray.Get(mPos);
00048       }
00049    };
00050 
00051    friend class TwoBitProxy;
00052    
00053    //
00054    // Constructors
00055    //
00056    
00057    explicit csTwoBitArray(unsigned size) : super(2 * size)
00058    {}
00059    
00060    csTwoBitArray(const csTwoBitArray &that) : super(that)
00061    {}
00062    
00063    //
00064    // Operators
00065    //
00066    
00067    csTwoBitArray &operator=(const csTwoBitArray &that)
00068    {
00069       super::operator=(that);
00070       return *this;
00071    }
00072 
00073    TwoBitProxy operator[](unsigned pos)
00074    {
00075       return TwoBitProxy(*this, pos);
00076    }
00077 
00078    const TwoBitProxy operator[](unsigned pos) const
00079    {
00080       return TwoBitProxy(CONST_CAST(csTwoBitArray&,*this), pos);
00081    }
00082    
00083    bool operator==(const csTwoBitArray &that) const
00084    {
00085       return super::operator==(that);
00086    }
00087 
00088    bool operator!=(const csTwoBitArray &that) const
00089    {
00090       return !(*this == that);
00091    }
00092 
00093    csTwoBitArray &operator&=(const csTwoBitArray &that)
00094    {
00095       super::operator&=(that);
00096       return *this;
00097    }
00098 
00099    csTwoBitArray operator|=(const csTwoBitArray &that)
00100    {
00101       super::operator|=(that);
00102       return *this;
00103    }
00104 
00105    csTwoBitArray operator^=(const csTwoBitArray &that)
00106    {
00107       super::operator^=(that);
00108       return *this;
00109    }
00110 
00111    csTwoBitArray operator~() const
00112    {
00113       return csTwoBitArray(*this).FlipAllBits();
00114    }
00115 
00116    friend csTwoBitArray operator&(const csTwoBitArray &a1, const csTwoBitArray &a2)
00117    {
00118       return csTwoBitArray(a1) &= a2;
00119    }
00120 
00121    friend csTwoBitArray operator|(const csTwoBitArray &a1, const csTwoBitArray &a2)
00122    {
00123       return csTwoBitArray(a1) |= a2;
00124    }
00125 
00126    friend csTwoBitArray operator^(const csTwoBitArray &a1, const csTwoBitArray &a2)
00127    {
00128       return csTwoBitArray(a1) ^= a2;
00129    }
00130 
00131    //
00132    // Plain English interface
00133    //
00134    
00135    // Set all bits to false.
00136    void Clear()
00137    {
00138       super::Clear();
00139    }
00140    
00141    // Toggle the bits at position pos.
00142    void FlipBits(unsigned pos) 
00143    {
00144       Set(pos, 3 ^ Get(pos));
00145    }
00146 
00147    // Set the bit at position pos to the given value.
00148    void Set(unsigned pos, unsigned val)
00149    {
00150       super::Set(2 * pos, val & 1);
00151       super::Set(2 * pos + 1, (val >> 1) & 1);
00152    }
00153 
00154    // Returns true iff the bit at position pos is true.
00155    unsigned Get(unsigned pos) const
00156    {
00157       return (unsigned(super::IsBitSet(2 * pos)) |
00158               (unsigned(super::IsBitSet(2 * pos + 1)) << 1));
00159    }
00160 
00161    // Returns true iff all bits are false.
00162    bool AllZero() const
00163    {
00164       return super::AllBitsFalse();
00165    }
00166 
00167    // Change value of all bits
00168    csTwoBitArray &FlipAllBits()
00169    {
00170       super::FlipAllBits();
00171       return *this;
00172    }
00173    
00174 private:
00175 };
00176 
00177 #endif

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000