Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类来操作位c++;_C++_Templates_Encryption_Bit Manipulation - Fatal编程技术网

C++ 类来操作位c++;

C++ 类来操作位c++;,c++,templates,encryption,bit-manipulation,C++,Templates,Encryption,Bit Manipulation,我试图理解以下类的功能,特别是functionfunc。我查了一下每一行的大致功能。它肯定是通过移位来操纵位,但我无法理解全局 template<class T> class classX { public: classX(int _z) : z(_z){} size_t operator()(T x) const { union { T a;

我试图理解以下类的功能,特别是function
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;
    };
模板
类别X
{
公众:
类x(int_z):z(z){}
size_t运算符()(t x)常量
{
联合体{T a;大小{u T b;}u;
u、 b=0;
u、 a=x;
无符号字符rng1=cvRNG(z*(z+1)+u.b);//如果数字大于0,则cvRNG返回输入,否则返回(uint64_t)-1
return(size_t)(cvRandReal(&rng1)*(double)(UINT32_MAX));//cvRandReal返回0到1之间的随机浮点数
}
私人:
intz;
};
模板
一流的
{
公众:
雅致(新罕布什尔州内、新罕布什尔州内)
:l_u0,c_0,arr_0
{    
b=nb;
l=nb/8+1;
arr_=新的无符号字符[l_];

对于(int i=1;i此代码为哈希集构建位图哈希

// 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
    };
//类的计算哈希
模板
类别X
{
公众:
classX(int z):z(z){}//构造哈希
//方法返回基于种子z的x的哈希代码。
size_t运算符()(t x)常量
{
//从对象x读取前4个字节是一个很好的尝试。
//联盟共享a&b的内存
联合体{T a;大小{u T b;}u;
u、 b=0;//只需清理内存即可
u、 a=x;//由于u.a与u.b共享内存,因此此行使用x的前4个字节初始化u.b。
//如果x是一个带有虚拟方法的实例If类,它将是指向vtable的指针(对于相同cals的所有实例都是相同的)。
//如果x是一个结构,它将是x数据的前4个字节。
//很可能x是一个结构。
//rnd1是cvRandReal函数的基本种子。注意,u.b不是0!
无符号字符rng1=cvRNG(z*(z+1)+u.b);//如果数字大于0,则cvRNG返回输入,否则返回(uint64_t)-1
//如果rng1是一个种子,那么下面的行就是一个散列函数
return(size_t)(cvRandReal(&rng1)*(double)(UINT32_MAX));//cvRandReal返回0到1之间的随机浮点数
}
私人:
int z;//基种子
};
//使用对象T、H的哈希集的位图哈希-哈希函数
模板
一流的
{
公众:
//注意:位图哈希的大小(以位为单位)
//nh:哈希函数的数量。
//这两个数字都假设可以降低散列冲突的概率
雅致(新罕布什尔州内、新罕布什尔州内)
:l_u0,c_0,arr_0
{   
b_nb=nb;//位图哈希的大小(以位为单位)
l_unb=8+1;//位图哈希的大小(字节)
arr_u3;=新的无符号字符[l_3;];//位图数组-哈希数据
//初始化哈希函数。从1开始,因为0种子机不好。

对于(int i=1;iIs不再是代码的作者了?他们通常是这方面的最佳人选:-)这段代码太糟糕了。我想知道这是不是故意的;你知道这段代码是不是通过一个模糊器运行的吗?我花了几分钟的时间才发现
classX
只是一个随机数生成包装器,我已经厌倦了试着破译它。我不想麻烦
classY
,因为我假设它需要5到10分钟it’我只是意识到这是一件同样平凡的事情。不要将此作为编写好代码的指导原则。在回答问题后删除问题中最重要的部分是完全不合适的。我已还原了那个愚蠢的编辑。@Walter这是错误的,我还原了它,我不知道为什么要提交审阅。func2有帮助吗o澄清逻辑。现在在类中有add()和check()操作