pieceMask.h
Go to the documentation of this file.
00001 #ifndef _PIECE_MASK_H
00002 #define _PIECE_MASK_H
00003 #include "osl/config.h"
00004 
00005 #include "osl/misc/mask.h"
00006 #include "osl/ptypeTraits.h"
00007 #if OSL_WORDSIZE == 64
00008 #  include "osl/container/pieceMask64.h"
00009 #elif OSL_WORDSIZE == 32
00010 #  include "osl/container/pieceMask32.h"
00011 #endif
00012 
00013 #include <iosfwd>
00014 
00015 namespace osl
00016 {
00017   namespace container
00018   {
00019 #if OSL_WORDSIZE == 64
00020   typedef PieceMask64 PieceMaskBase;
00021 #elif OSL_WORDSIZE == 32
00022   typedef PieceMask32 PieceMaskBase;
00023 #endif
00024 
00031   class PieceMask : public PieceMaskBase
00032   {
00033   public:
00034     PieceMask() {}
00035     PieceMask(const PieceMaskBase& base) : PieceMaskBase(base) {}
00036     static const mask_t numToMask(int num) { 
00037       return mask_t::makeDirect(1) << PieceMask::numToOffset(num); 
00038     }
00039     void setMask(int index,mask_t val) {
00040       mutableMask(index)=val;
00041     }
00042   private:
00043     mask_t& mutableMaskNum(int num) {
00044       return mutableMask(numToIndex(num));
00045     }
00046     const mask_t getMaskNum(int num) const {
00047       return getMask(numToIndex(num));
00048     }
00049   public:
00050     void xorMask(int index,mask_t val) {
00051       mutableMask(index)^=val;
00052     }
00053     void orMask(int index,mask_t val) {
00054       mutableMask(index)|=val;
00055     }
00056     bool test(int num) const {
00057       return (getMaskNum(num)&numToMask(num)).any();
00058     }
00059     void set(int num) {
00060       mutableMaskNum(num)|=numToMask(num);
00061     }
00062     void flip(int num) {
00063       mutableMaskNum(num)^=numToMask(num);
00064     }
00065     void reset(int num) {
00066       mutableMaskNum(num)&= ~numToMask(num);
00067     }
00068     bool any() const { return ! none(); }
00069 
00070     const mask_t getMask(int num) const { return PieceMaskBase::getMask(num); }
00072     template <Ptype PTYPE>
00073     const mask_t getMask() const { return getMask(PtypeFuns<PTYPE>::indexNum); }
00074     
00076     template <Ptype PTYPE>
00077     const mask_t selectBit() const 
00078     {
00079       mask_t mask = getMask<PTYPE>();
00080       mask &= mask_t::makeDirect(PtypeFuns<PtypeFuns<PTYPE>::basicType>::indexMask);
00081       return mask;
00082     }
00084     template <Ptype PTYPE>
00085     void clearBit()
00086     {
00087       mask_t& mask = mutableMask(PtypeFuns<PTYPE>::indexNum);
00088       mask &= ~mask_t::makeDirect(PtypeFuns<PtypeFuns<PTYPE>::basicType>::indexMask);
00089     }
00091     template <Ptype PTYPE>
00092     void setBit()
00093     {
00094       mask_t& mask = mutableMask(PtypeFuns<PTYPE>::indexNum);
00095       mask |= mask_t::makeDirect(PtypeFuns<PtypeFuns<PTYPE>::basicType>::indexMask);
00096     }
00097   };
00098 
00099   // TODO: PieceMask32 と PieceMask64 に分離
00100   inline const PieceMask operator&(const PieceMask &m1, const PieceMask &m2) {
00101 #if OSL_WORDSIZE == 64
00102     return PieceMask64(m1.getMask(0)&m2.getMask(0));
00103 #elif OSL_WORDSIZE == 32
00104     return PieceMask32(m1.getMask(0)&m2.getMask(0),
00105                        m1.getMask(1)&m2.getMask(1));
00106 #endif
00107   }
00108 
00109   // TODO: PieceMask32 と PieceMask64 に分離
00110   inline const PieceMask operator|(const PieceMask &m1, const PieceMask &m2) {
00111 #if OSL_WORDSIZE == 64
00112     return PieceMask64(m1.getMask(0)|m2.getMask(0));
00113 #elif OSL_WORDSIZE == 32
00114     return PieceMask32(m1.getMask(0)|m2.getMask(0),
00115                        m1.getMask(1)|m2.getMask(1));
00116 #endif
00117   }
00118   // TODO: PieceMask32 と PieceMask64 に分離
00119   inline const PieceMask operator~(const PieceMask &m1) {
00120 #if OSL_WORDSIZE == 64
00121     return PieceMask64(~m1.getMask(0));
00122 #elif OSL_WORDSIZE == 32
00123     return PieceMask32(~m1.getMask(0),~m1.getMask(1));
00124 #endif
00125   }
00126 
00127   inline bool operator==(const PieceMask &m1, const PieceMask &m2){
00128     return m1.getMask(0)==m2.getMask(0) && m1.getMask(1)==m2.getMask(1);
00129   }
00130   inline bool operator!=(const PieceMask &m1, const PieceMask &m2)
00131   {
00132     return ! (m1 == m2);
00133   }
00134   std::ostream& operator<<(std::ostream& os,PieceMask const& pieceMask);
00135   } // namespace container
00136   using container::PieceMask;
00137 } // namespace osl
00138 
00139 
00140 #endif /* _PIECE_MASK_H */
00141 // ;;; Local Variables:
00142 // ;;; mode:c++
00143 // ;;; c-basic-offset:2
00144 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines