Пример #1
0
TEST(Hash, testHash) {
    std::vector<int> myvec = {22,41,53,46,30,13,1,67};
    std::vector<std::size_t> hashResult;
    for (std::size_t i = 0; i < myvec.size(); ++i) {
        hashResult.push_back(myHash(myvec[i]));
    }
    assert_eq_vector({0,2,5,6,2,6,3,3}, hashResult);

}
Пример #2
0
QString QgsRenderChecker::imageToHash( QString theImageFile )
{
  QImage myImage;
  myImage.load( theImageFile );
  QByteArray myByteArray;
  QBuffer myBuffer( &myByteArray );
  myImage.save( &myBuffer, "PNG" );
  QString myImageString = QString::fromUtf8( myByteArray.toBase64().data() );
  QCryptographicHash myHash( QCryptographicHash::Md5 );
  myHash.addData( myImageString.toUtf8() );
  return myHash.result().toHex().constData();
}
Пример #3
0
TEST(Hash, testConstruct) {
    std::vector<std::pair<int, bool>> myvec;
    for(std::size_t i = 0; i < 11; ++i) {
        myvec.push_back({0, false});
    }; // initialization
    std::vector<int> mykeys = {22,41,53,46,30,13,1,67};

    double countFindPosition = double(mykeys.size()); // ASL

    for(std::size_t index = 0; index < mykeys.size(); ++index) {
        int key = mykeys[index]; // index is increasing
        std::size_t position = myHash(key);
        std::size_t rePosition = myvec.size();
        if(myvec[position].second == false) {
            myvec[position] = {key, true};
        } else {
            std::size_t i = 1;
            do {
                if(i == 11)
                    break;
                rePosition = conflict(key, 11, i);
                ++i;
            } while(myvec[rePosition].second == true);
            myvec[rePosition] = {key, true};
            countFindPosition += double(i - 1); // ASL
        }
    } // (1) construct

    std::cout << "ASL(success): " << countFindPosition / mykeys.size() << ". should be: " << 17./8 << std::endl;
    EXPECT_EQ(countFindPosition / mykeys.size(), 17./8); // ASL

    ///{{22,true},{67,true},{41,true},{30,true},{0, false},{53,true},{46,true},{0, false},{13,true},{0, false},{1,true}}
    std::vector<int> resultVec;
    for(std::size_t i = 0; i < myvec.size(); ++i) {
        resultVec.push_back(myvec[i].first);
    }
    assert_eq_vector({ 22, 67, 41, 30,  0, 53, 46,  0, 13,  0,  1}, resultVec);
}
Пример #4
0
TEST(Hash, testSearchFailureASL) {
    std::vector<std::pair<int, bool>> myvec;
    for(std::size_t i = 0; i < 11; ++i) {
        myvec.push_back({0, false});
    }; // initialization
    std::vector<int> mykeys = {22,41,53,46,30,13,1,67};

    for(std::size_t index = 0; index < mykeys.size(); ++index) {
        int key = mykeys[index]; // index is increasing
        std::size_t position = myHash(key);
        std::size_t rePosition = myvec.size();
        if(myvec[position].second == false) {
            myvec[position] = {key, true};
        } else {
            std::size_t i = 1;
            do {
                rePosition = conflict(key, 11, i);
                ++i;
            } while(myvec[rePosition].second == true);
            myvec[rePosition] = {key, true};
        }
    } // (1) construct

    // --------------------------------------------------------

    // (2) test search failure ASL

    // for each bucket (aka position)
    std::size_t bucketSize = 11;
    double sumUp = 0.;
    for(std::size_t bucketIndex = 0; bucketIndex < bucketSize; ++bucketIndex) {
        double currentBucketAverageSteps = calculateAverageStepsForBucket(myvec, bucketIndex, bucketSize);
        sumUp += currentBucketAverageSteps;
    }
    printf("Search Failed ASL: %lf\n", sumUp/bucketSize);
}
Пример #5
0
String DataStore::getPath(UINT64 objid) const {
	if (root.empty() || ! File::isDir(root)) {
		throw Error(E_ERROR, "DTS-001", "Empty or invalid root path for DataStore: " +
				root);
	}
	if (depth < 0 || depth > 7) {
		throw Error(E_ERROR, "DTS-002", "Invalid depth for DataStore: " + String(depth));
	}
	
	UINT64 hash = myHash(objid);
	UINT8 *parts = (UINT8 *)&hash;
	char buf[3] = "";
	
	String path = root;
	for (int i = 0; i < 8; ++i) {
		std::sprintf(buf, "%02X", parts[i]);
		if (i <= depth) {
			path += '/';
		}
		path += buf;
	}
	
	return path;
}
Пример #6
0
std::size_t conflict(int key, std::size_t m, std::size_t i) {
    std::size_t position = (myHash(key) + i * myReHash(key)) % m;
    // in this case m = 11, i is increasing from 1 to m - 1
    return position;
}
Пример #7
0
/*
 * main function.
 */
int main()
{
    struct timeval tv;

    srand(time(NULL)^getpid());
    gettimeofday(&tv,NULL);


    // rand seed or fixed seed.
    unsigned int mod = tv.tv_sec^tv.tv_usec^getpid(); 
    printf("%u\n", mod);
    //dictSetHashFunctionSeed(mod);
    dictSetHashFunctionSeed(12391);



    // create <k,v> = <str, str>.
    dictType myType = {
        myHash,                 /* hash function */
        NULL,                   /* key dup */
        NULL,                   /* val dup */
        myCompare,              /* key compare */
        myDestructor,           /* key destructor */
        NULL                    /* val destructor */
    };


    // step 1: create.
    dict* myDict = dictCreate(&myType, NULL);
    assert(myDict != NULL);

    printf("-------------------\n");
    printState(myDict);

    char* key[10] = {"hello0", "hello1", "hello2", "hello3", "hello4", 
                "hello5", "hello6", "hello7", "hello8", "hello9"};

    char* val[10] = {"h0", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9"};


    for(int i=0; i<10; i++)
    {
        unsigned int hash = myHash(key[i]);
        unsigned int idx = hash & 3;
        printf("real key: %u, real idx=%d\n", hash, idx);
    }



    // step 2: add
    printf("----------add first 5-----------------\n");
    for(int i = 0; i<5; i++)
    {
        printf("add %d\n", i);
        int ret = dictAdd(myDict, key[i], val[i]);
        printState(myDict);
        assert(ret==0);
    }

    printf("----------start rehashing..------------\n");
    for(int i=0; i<5; i++)
    {
        dictRehash(myDict, 1);
        printState(myDict);
    }

    printf("----------add  last 5.-----------------\n");
    for(int i = 5; i<10; i++)
    {
        printf("add %d\n", i);
        int ret = dictAdd(myDict, key[i], val[i]);
        printState(myDict);
        assert(ret==0);
    }




    // index.
    printf("------------index---------------\n");
    for(int i = 0; i < 10; i++)
    {
        printf("i=%d\n", i);
        char* v = dictFetchValue(myDict, key[i]);
        int ret = strcmp(v, val[i]); 
        assert(ret == 0);
    }

    char* v = dictFetchValue(myDict, "hello world2");
    assert(v == NULL);
    

    // foreach dict.
    unsigned long cur = 0;
    while(1) 
    {
        cur = dictScan(myDict, cur, dictScanCallback, NULL); 
        if(cur == 0)
        {
            break;
        }
    }

    // release. 
    dictRelease(myDict);

    return 0;
}