Matrixhash implementation and testing (2)

Source: Internet
Author: User

The hash function is used to generate a 32-bit hash from 64bit, which is relatively efficient, but the time is not tested. In addition, the space overhead is relatively large.

/*************************************** **********************************
> File name: hash_table.cpp
> Author: wjy
> Mail: [email protected]
> Created time: Second 7/8 09:54:52 2014
**************************************** ********************************/

# Include <iostream>
# Include <time. h>
# Include <string. h>
# Include <ctime>
# Include <stdlib. h>
# Include <stdio. h>
Using namespace STD;

Unsigned long matrix [] = {
694164548,378 records, 3807463199,268 4797435, 2359943013,223 1240996, 2135863124,121 1164704, 2302089482,410 5647604, 3076642034,721 61852, 59560020,611 878035, 2814490697,391 5797072, 1219075273,397 8906545, 193953705,413 2630722, 2627050652,198 9142569, 2745032496,735 records, 3485798578,163 7027010, 2467907528,311 records, 1201254602,373 7777921, 796676896,234 9326933, 2449810837,391 0433321, 3550767862,164 4342526, 1438602584,217 9832898, 3868189175,181 4084994, 615833333,203 1832363, 3130167043,420 0349959, 1930419194,378 7258680, 3369981751,332 9843958, 3446569769,385 records, 163055231,100 records, 422846498,101 1763997, 533633029,255 7610330, 4221463260,950 17657, 88369066,322 7540105, 3815919250,223 4633741, 1819183943,556 442040
};
Void changetoarray (unsigned long input, int * array, int Len ){
Len = 64;
For (INT I = 0; I <Len; I ++ ){
If (input> I) & 0x1) = 1 ){
Array [I] = 1;
} Else {
Array [I] = 0;
}
}
}
Int matrix [32] [64];
Void changetomatrix (){
For (INT I = 0; I <32; I ++)
For (Int J = 0; j <64; j ++ ){
If (Matrix [I]> J) & 0x1) = 1 ){
Matrix [I] [J] = 1;
} Else {
Matrix [I] [J] = 0;
}
}
}
Unsigned int matrixhash (unsigned long input ){
Int TMP [64];
Changetoarray (input, TMP, 64 );
// For (INT I = 0; I <64; I ++)
// Cout <TMP [I];
Unsigned int res = 0;
For (INT I = 0; I <64; I ++ ){
If (TMP [I] = 1 ){
Res = res ^ matrix [I];
}
}
Return res;
}

/* End of AP hash function */
Const int num = 1000003; // 10000019
Struct hashnode {
Unsigned long key;
Unsigned int value;
Hashnode * next;
Bool flag;
Hashnode (){
Flag = false;
Next = NULL;
Key = 0;
Value = 0;
}
Hashnode (unsigned long key, int value): Key (key), value (value ){
Next = NULL;
Flag = false;
}
};
Hashnode hnode [num];
/* Unsigned int hashvalue (char * Str ){
Register unsigned int h;
Register unsigned char * P;
For (H = 0, P = (unsigned char *) STR; * P; P ++ ){
H = (H <5)-H + (* P); // H = 31 * H + * P;
}
Return h;
}*/
Bool iscontainskey (unsigned long key ){
Char TMP [65];
// ITOA (Key, TMP, 10 );
// Sprintf (TMP, "% d", key );
// Int Len = strlen (TMP );
Unsigned int hashcode = matrixhash (key) % num;
If (hnode [hashcode]. Flag = false ){
Return false;
} Else {
Hashnode * P = & hnode [hashcode];
While (P! = NULL ){
If (p-> key = Key ){
Return true;
}
P = p-> next;
}
}
Return false;
}
Void put (unsigned long key, int value ){
// Char TMP [65];
// ITOA (Key, TMP, 10 );
// Sprintf (TMP, "% d", key );
// Int Len = strlen (TMP );

Unsigned int hashcode = matrixhash (key) % num;
If (hnode [hashcode]. Flag = false ){
Hnode [hashcode]. Flag = true;
Hnode [hashcode]. Key = key;
Hnode [hashcode]. value = value;
} Else if (! Iscontainskey (key )){
Hashnode * P = new hashnode (Key, value );
P-> flag = true;
P-> next = hnode [hashcode]. Next;
Hnode [hashcode]. Next = P;
} Else {
Hashnode * P = & hnode [hashcode];
While (P! = NULL ){
If (p-> key = Key ){
P-> value = value;
Break;
}
P = p-> next;
}
}
}
Int get (unsigned long key ){
// Char TMP [65];
// ITOA (Key, TMP, 10 );
// Sprintf (TMP, "% LlU", key );
// Int Len = strlen (TMP );
Unsigned int hashcode = matrixhash (key) % num;
If (iscontainskey (key) = false ){
// Alert ("the key not exists ");
Return-1;
} Else {
Hashnode * P = & hnode [hashcode];
While (P! = NULL ){
If (p-> key = Key ){
Return p-> value;
}
P = p-> next;
}
}
Return-1;
}
Unsigned long strtoull (char * Key, int Len ){
Unsigned long res = 0;
For (INT I = len-1; I> = 0; I --){

Res = res * 10 + (Key [I]-'0 ');
}
Return res;
}
Int main (){
File * fp = fopen ("data.txt", "R ");
If (FP = NULL ){
Cout <"end" <Endl;
Exit (-1 );
}
Char key [65];
While (! Feof (FP )){
Fscanf (FP, "% s", key );
Int Len = strlen (key );
Put (strtoull (Key, Len), 1 );
}
Fclose (FP );
Fp = fopen ("matrixhash", "W ");
File * fp2 = fopen ("count_matrixhash", "W ");
Int sum = 0;
For (INT I = 0; I <num; I ++ ){
Int COUNT = 0;
Hashnode * P = & hnode [I];
While (P! = NULL & P-> flag! = False ){
If (p-> flag! = False ){
Count ++;
If (p-> key! = 0)
Fprintf (FP, "% LlU-> % d \ n", p-> key, p-> value );
}
P = p-> next;
}
If (count> 0) sum + = 1;
If (count> = 1 ){
// Fprintf (fp2, "% d th node has % d collision \ n", I, count );
Fprintf (fp2, "% d \ n", count );
}
}
Cout <"matrixhash" <"\ t" <count <"\ t" <num <Endl;
Fclose (fp2 );
Fclose (FP );
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.