/** * Template bitpattern ver. 1.0 * @author Piotr Pawlowski * @brief This template gives a bit pattern corresponding to a number of boolean functions run for the same argument. * @example * #include "bitpattern.h" * std:bitpattern bitpat; * bool fun0(float x){return x > 0.5;} * bool fun1(float x){return x > 0.75;} * bool fun2(float x){return x> 1.5;} * ..... * bitpat.join(0,&fun0); * bitpat.join(1,&fun1); * bitpat.join(2,&fun2); * ..... * bitpat.set(0.7); // now bitpat = 0b001 * bitpat.set(1.2); // now bitpat = 0b011 * bitpat.set(1.9); // now bitpat = 0b111 * **/ #ifndef bitpattern_h #define bitpattern_h #include #include namespace std{ template class bitpattern{ private: static const int Size = sizeof(output)*8; public: typedef bool function(input); typedef function * function_vector[sizeof(output)*8]; protected: output pattern; function_vector filter; public: bitpattern(const output n = 0):pattern(n){ memset(filter,0,sizeof(filter)); } bitpattern & operator=(const output n){ pattern = n; } void join(const function_vector fun){ for(int i=0; i=size()) printf("[Kratta TKratCalib:] Bitpattern: index out of range\n"); else filter[i] = f; } void join_mask(output w, function * f){ for(int n = 0; w; w/=2, n++) if(w & 1) join(n,f); } operator output()const{ return pattern; } int size()const{ return Size; } bool bit(int i)const{ return GetBit(pattern, i); } bool operator[](int i)const{ return bit(i); } output set(input inp){ for(int i=0; i>bit; } static void FlipBit(output & word, int bit){ if( GetBit(word, bit)) UnsetBit(bit); else SetBit(bit); } void SetBit(int i, bool b){ if(b) SetBit(pattern, i); else UnsetBit(pattern, i); } void FlipBit(int i){ FlipBit(pattern, i); } }; }; #endif