CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/PhysicsTools/MVAComputer/interface/BitSet.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_MVAComputer_BitSet_h
00002 #define PhysicsTools_MVAComputer_BitSet_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     MVAComputer
00006 // Class  :     BitSet
00007 //
00008 
00009 //
00010 // Author:      Christophe Saout <christophe.saout@cern.ch>
00011 // Created:     Sat Apr 24 15:18 CEST 2007
00012 // $Id: BitSet.h,v 1.5 2009/05/09 12:23:46 saout Exp $
00013 //
00014 
00015 #include <string.h>
00016 #include <cstddef>
00017 #include <cstring>
00018 
00019 namespace PhysicsTools {
00020 
00030 class BitSet {
00031     public:
00032         typedef std::size_t size_t;
00033 
00034     protected:
00035         typedef unsigned int Word_t;
00036 
00037     public:
00047         struct Manipulator {
00048             public:
00049                 inline Manipulator(const Manipulator &orig) :
00050                         word(orig.word), mask(orig.mask) {}
00051                 inline ~Manipulator() {}
00052 
00054                 inline operator bool() const { return *word & mask; }
00055 
00057                 inline bool operator = (bool bit)
00058                 { *word = (*word & ~mask) | (bit ? mask : 0); return bit; }
00059 
00060             protected:
00061                 friend class BitSet;
00062 
00063                 inline Manipulator(Word_t *word, unsigned int bit) :
00064                         word(word), mask((Word_t)1 << bit) {}
00065 
00066             private:
00067                 Word_t          *word;
00068                 Word_t          mask;
00069         };
00070 
00078         struct Iterator {
00079             public:
00081                 inline operator bool() const { return store < end; }
00082 
00084                 inline size_t operator () () const
00085                 { return (store - begin) * wordSize + pos; }
00086 
00088                 Iterator &operator ++ ()
00089                 {
00090                         if (++pos < wordSize) {
00091                                 Word_t word = *store & -(1 << pos);
00092                                 if (word) {
00093                                         pos = ffs(word) - 1;
00094                                         return *this;
00095                                 }
00096                         }
00097 
00098                         pos = 0;
00099                         for(;;) {
00100                                 if (++store >= end)
00101                                         break;
00102                                 else if (*store) {
00103                                         pos = ffs(*store) - 1;
00104                                         break;
00105                                 }
00106                         }
00107 
00108                         return *this;
00109                 }
00110 
00112                 inline Iterator operator ++ (int dummy)
00113                 { Iterator orig = *this; ++*this; return orig; }
00114 
00115             protected:
00116                 friend class BitSet;
00117 
00118                 Iterator(Word_t *begin, Word_t *end) :
00119                         begin(begin), store(begin), end(end), pos(0)
00120                 { if (store < end && !(*store & 1)) ++*this; }
00121 
00122             private:
00123                 Word_t          *begin, *store, *end;
00124                 unsigned int    pos;
00125         };
00126 
00127         BitSet() : store(0), bits_(0) {}
00128 
00129         BitSet(const BitSet &orig) : bits_(orig.bits_)
00130         {
00131                 std::size_t words = bitsToWords(bits_);
00132                 if (words) {
00133                         store = new Word_t[words];
00134                         std::memcpy(store, orig.store, words * sizeof(Word_t));
00135                 } else
00136                         store = 0;
00137         }
00138 
00140         BitSet(size_t bits) : bits_(bits)
00141         {
00142                 std::size_t words = bitsToWords(bits);
00143                 if (words) {
00144                         store = new Word_t[words];
00145                         std::memset(store, 0, sizeof(Word_t) * words);
00146                 } else
00147                         store = 0;
00148         }
00149 
00150         inline ~BitSet() { delete[] store; }
00151 
00152         BitSet &operator = (const BitSet &orig)
00153         {
00154                 delete[] store;
00155                 bits_ = orig.bits_;
00156                 std::size_t words = bitsToWords(bits_);
00157                 if (words) {
00158                         store = new Word_t[words];
00159                         std::memcpy(store, orig.store, words * sizeof(Word_t));
00160                 } else
00161                         store = 0;
00162                 return *this;
00163         }
00164 
00166         inline Manipulator operator [] (size_t bit)
00167         { return Manipulator(&store[bit / wordSize], bit % wordSize); }
00168 
00170         inline const Manipulator operator [] (size_t bit) const
00171         { return Manipulator(&store[bit / wordSize], bit % wordSize); }
00172 
00174         inline size_t size() const { return bits_; }
00175 
00177         size_t bits() const;
00178 
00180         inline Iterator iter() const
00181         { return Iterator(store, store + bitsToWords(bits_)); }
00182 
00183     private:
00184         static inline size_t bitsToWords(std::size_t bits)
00185         { return (bits + wordSize - 1) / wordSize; }
00186 
00187         static const unsigned int wordSize = sizeof(Word_t) * 8;
00188 
00189         Word_t  *store;
00190         size_t  bits_;
00191 };
00192 
00193 namespace Calibration {
00194         class BitSet;
00195 
00197         PhysicsTools::BitSet convert(const BitSet &bitSet);
00199         BitSet convert(const PhysicsTools::BitSet &bitSet);
00200 }
00201 
00202 } // namespace PhysicsTools
00203 
00204 #endif // PhysicsTools_MVAComputer_BitSet_h