当前位置: 首页>>代码示例>>C++>>正文


C++ Hash::AddData方法代码示例

本文整理汇总了C++中Hash::AddData方法的典型用法代码示例。如果您正苦于以下问题:C++ Hash::AddData方法的具体用法?C++ Hash::AddData怎么用?C++ Hash::AddData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Hash的用法示例。


在下文中一共展示了Hash::AddData方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: test_performance

void HashTest::test_performance()
{
    const int ITERATION_COUNT = 1000000;
    {
        Hash hf;
        clock_t t = clock();
        for(int i = 0; i < ITERATION_COUNT; ++i)
        {
            Id<16> id(true);
            hf.AddData(id.ConstData(), id.Size);
            GUINT32 hash = hf.Final();
            GUTIL_UNUSED(hash);
        }
        clock_t e = clock();
        qDebug("Number of ticks to do %d hashes was: %d", ITERATION_COUNT, (int)(e - t));
    }
    
    {
        std::hash<string> hf;
        clock_t t = clock();
        for(int i = 0; i < ITERATION_COUNT; ++i)
        {
            Id<16> id(true);
            string s((char const *)id.ConstData(), id.Size);
            GUINT32 hash = hf(s);
            GUTIL_UNUSED(hash);
        }
        clock_t e = clock();
        qDebug("Number of ticks to do %d standard hashes was: %d", ITERATION_COUNT, (int)(e - t));
    }
}
开发者ID:karagog,项目名称:gutil,代码行数:31,代码来源:tst_hash.cpp

示例2: test_collisions

void HashTest::test_collisions()
{
    // Do a collision check. It should be practically impossible to get a collision
    double my_avg = 0;
    const int ITERATION_COUNT = 25;
    for(int i = 0; i < ITERATION_COUNT; ++i)
    {
        //qDebug("Iteration %d", i);
        set<GUINT32> hashes;
        GUINT32 cnt = 0;
        bool collision = false;
        Hash hf;
        forever{
            cnt++;
            Id<16> id(true);
            hf.AddData(id.ConstData(), id.Size);
            GUINT32 hash = hf.Final();
            collision = hashes.find(hash) != hashes.end();
            if(!collision)
                hashes.insert(hash);
            else
                break;
        }
        
        //qDebug(QString("Did %1 hashes before a collision").arg(cnt).toAscii());
        //QVERIFY(cnt > 10000);
        my_avg += cnt;
    }
    my_avg /= ITERATION_COUNT;
    qDebug("My average number of hashes before collision was: %f", my_avg);
    QVERIFY(my_avg > 50000);
    
    // Compare our results with the standard hash
    double their_avg = 0;
    for(int i = 0; i < ITERATION_COUNT; ++i)
    {
        set<GUINT32> hashes;
        GUINT32 cnt = 0;
        bool collision = false;
        std::hash<string> sh;
        forever{
            cnt++;
            Id<16> id(true);
            string s((char const *)id.ConstData(), id.Size);
            GUINT32 hash = sh(s);
            collision = hashes.find(hash) != hashes.end();
            if(!collision)
                hashes.insert(hash);
            else
                break;
        }
        their_avg += cnt;
        //qDebug(QString("Standard hash did %1 hashes before a collision").arg(cnt).toAscii());
    }
    their_avg /= ITERATION_COUNT;
    qDebug("The average number of standard hashes before collision was: %f", their_avg);
}
开发者ID:karagog,项目名称:gutil,代码行数:57,代码来源:tst_hash.cpp

示例3: test_basics

void HashTest::test_basics()
{
    Hash h;
    qDebug(QString("The hash object is %1 bytes large").arg(sizeof(h)).toAscii());
    QVERIFY(4 == h.Size);
    QVERIFY(Hash::Size == h.Size);
    
    // The null hash is 0
    QVERIFY(h.Final() == 0);
    QVERIFY(Hash::ComputeHash(NULL, 0) == 0);
    
    {
        // Make sure hashes of all single bytes are unique
        set<GUINT32> hashes;
        for(int i = 0; i < 0x100; ++i){
            byte tmp = i;
            GUINT32 hash = Hash::ComputeHash(&tmp, 1);
            QVERIFY(hashes.find(hash) == hashes.end());
            hashes.insert(hash);
        }
        
        // Make sure hashing the same input gives us the same output
        for(int i = 0; i < 0x100; ++i){
            byte tmp = i;
            GUINT32 hash = Hash::ComputeHash(&tmp, 1);
            QVERIFY(hashes.find(hash) != hashes.end());
        }
    }
    
    // Hashes one way are not the same as hashes the other way
    {
        set<GUINT32> hashes;
        for(byte c = 'a'; c <= 'z'; ++c){
            for(byte c2 = c + 1; c2 <= 'z'; ++c2){
                h.AddData(&c, 1);
                h.AddData(&c2, 1);
                GUINT32 hash1 = h.Final();
                
                h.AddData(&c2, 1);
                h.AddData(&c, 1);
                GUINT32 hash2 = h.Final();
                
                QVERIFY(hash1 != hash2);
                QVERIFY(hashes.find(hash1) == hashes.end());
                QVERIFY(hashes.find(hash2) == hashes.end());
                hashes.insert(hash1);
                hashes.insert(hash2);
            }
        }
    }
    
    // Hashes of repeat characters don't repeat hashes
    {
        set<GUINT32> hashes;
        for(int i = 1; i < 10000; i++){
            String s('a', i);
            GUINT32 hash = Hash::ComputeHash((byte const *)s.ConstData(), i);
            
            QVERIFY2(hashes.find(hash) == hashes.end(), QString("%1: %2").arg(i).arg(hash, 8, 16).toAscii());
            hashes.insert(hash);
        }
    }
}
开发者ID:karagog,项目名称:gutil,代码行数:63,代码来源:tst_hash.cpp


注:本文中的Hash::AddData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。