用于操作位c ++的类

时间:2017-03-29 13:29:27

标签: c++ templates encryption bit-manipulation

我试图了解以下类的功能,特别是函数func。我查看了每条线的粗略内容。它通过移动它们肯定会操纵位,但我无法理解大局。

 template<class T>
    class classX
    {
        public:
            classX(int _z) : z(_z){}

            size_t operator()(T x) const
            {
                union { T a; size_t b; } u;
                u.b = 0;
                u.a = x;

                unsigned char rng1 = cvRNG(z*(z+1) + u.b);// cvRNG returns the input if number>0, else return  (uint64_t)(int64_t)-1
                return (size_t)( cvRandReal(&rng1)*(double)(UINT32_MAX) );// cvRandReal returns random floating-point number between 0 and 1
            }

        private:
            int  z;
    };

template<class T,class H=classX<T>>
class classY
{
    public:
    classY(int nb, int nh)
      : l_(0),c_(0),arr_(0)
    {    
        b_   = nb; 
        l_   = nb / 8 + 1; 
        arr_ = new unsigned char[l_];


        for(int i=1; i<=nh; i++)
            ff.push_back( H(i) );
    }
void func(const T& x)

        {
            for(size_t j=0; j<ff.size(); j++){
                size_t key = ff[j](x) % b_;
            arr_[ key / 8 ] |= (unsigned char)(1 << (key % 8));
        }
        c_++;
    }
bool func2(const T& x) const
{
    size_t z = 0;
    for(size_t j=0; j<ff.size(); j++){
        size_t key  = ff[j](x) % b_;
        z += (arr_[ key / 8 ] & (unsigned char)(1 << (key % 8)) ) > 0 ? 1 : 0;
    }
    return ( z == ff.size() );
}
private:
        unsigned char*    arr_;
        int               l_;
        int               c_;
        size_t            b_;
        std::vector<H>    ff;
    };

我试图了解以下类的功能,特别是函数func。我查看了每条线的粗略内容。它通过移动它们肯定会操纵位。

1 个答案:

答案 0 :(得分:1)

此代码为哈希集构建位图哈希。

// Calc Hash for a class 
template<class T>
    class classX
    {
        public:
            classX(int _z) : z(_z){}   // construct hash 

            // Method returns a hashcode for x based on seed z.
            size_t operator()(T x) const
            {
                // It is a nice try to read first 4 bytes form object x.
                // union share the memory for a & b
                union { T a; size_t b; } u;
                u.b = 0; // just clean the memory
                u.a = x; // since u.a share the memory with u.b, this line init u.b with first 4 bytes of x. 
                // If x is an instance if class with virtual methods, it will be pointer to vtable (same for all instances of the same calss). 
                //  If x is a struct, it will be fist 4 bytes of x data.
                // Most likely x is must be a struct.

                // rnd1 is a base seed for the cvRandReal function. Note, u.b not a 0!
                unsigned char rng1 = cvRNG(z*(z+1) + u.b);// cvRNG returns the input if number>0, else return  (uint64_t)(int64_t)-1
                // if rng1 a seed, line below just a hash function
                return (size_t)( cvRandReal(&rng1)*(double)(UINT32_MAX) );// cvRandReal returns random floating-point number between 0 and 1
            }

        private:
            int  z; // base seed
    };

// Bitmap Hash for Hash Set with Objects T, H - hash functions  
template<class T,class H=classX<T>>
class classY
{
    public:
    // nb: size of bitmap hash in bits
    // nh: number of hash functions.
    // Both this number suppose to reduce probability of hash collision
    classY(int nb, int nh)
      : l_(0),c_(0),arr_(0)
    {   
        b_   = nb;    // size of bitmap hash in bits
        l_   = nb / 8 + 1; // size of bitmap hash in bytes
        arr_ = new unsigned char[l_];  // bitmap array - hash data

        // init hash functions. Start from 1, because 0 seeder is not good.
        for(int i=1; i<=nh; i++)
            ff.push_back( H(i) );
    }
    // Add x into the hash bitmap (add x to the set)
    void func(const T& x)
    {
        // for all hash fucntions
        for(size_t j=0; j<ff.size(); j++)
        {
                size_t key = ff[j](x) % b_; // calc hash code and normalize it by number if bits in the map
                // key - is a bit number in the bitmap
                // Just a rise a key bit in the bitmap
                // key / 8  - byte number
                // key % 8  - bit number
                arr_[ key / 8 ] |= (unsigned char)(1 << (key % 8));
        }
        c_++; // increase number of object that was processed to build a hash
    }
    // Check if X into the set (Check if X was added with func before)
    // It return False if X wasn't added
    // It return True of X probably be added (high probability that X was added, but not 100%)
    bool func2(const T& x) const
    {
        size_t z = 0; // number of passed hash tests
        for(size_t j=0; j<ff.size(); j++){
            size_t key  = ff[j](x) % b_;   // calc hash code and normalize it by number if bits in the map, like in func()

            // Increment z (number of passed hash tests) if key bit is in the bitmask 
            z += (arr_[ key / 8 ] & (unsigned char)(1 << (key % 8)) ) > 0 ? 1 : 0;
        }
        return ( z == ff.size() ); // return true if all tests from hash functions was passed.
    }
private:
        unsigned char*    arr_; // hash bitmap
        int               l_;// size of bitmap in bytes
        int               c_;// number of object that was processed to build a hash
        size_t            b_;// size of bitmap in bits
        std::vector<H>    ff; // hash functions
    };

image