-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashTable.cpp
More file actions
86 lines (77 loc) · 1.69 KB
/
HashTable.cpp
File metadata and controls
86 lines (77 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "HashTable.h"
#include "snp.h"
#include "math.h"
/* HashTable constructor
*/
HashTable::HashTable (int s) {
size = s;
hashTable = new list<snp>[size];
}
/* contains
* Takes RSID string as input and checks whether or not
* the hash table contains an SNP with that RSID by searching
*/
bool HashTable::contains(const string &s) {
return !(find(s)==nullptr);
}
/* put
* add snp to table by computing hash and adding in the correct place
*/
void HashTable::put(snp* node) {
int hash = computeHash(node->getRS());
snp* word= find(node->getRS());
if(word == nullptr) {
hashTable[hash].push_back(*node);
}
}
/* find
* Takes RSID string and finds the corresponding
* SNP (if it exists) in the table
*/
snp* HashTable::find(const string&s) {
int hash = computeHash(s);
for(auto& node : hashTable[hash]) {
if(node.getRS() == s) {
return &node;
}
}
return nullptr;
}
/* getRandom
* find a random SNP
*/
snp* HashTable::getRandom(void) {
int rHash = rand()%size;
list<snp> bucket = hashTable[rHash];
while(bucket.size() == 0) {
rHash = rand()%size;
bucket = hashTable[rHash];
}
int rPos = rand()%bucket.size();
for(auto& node : hashTable[rHash]) {
if(rPos == 0) {
return &node;
}
rPos--;
}
return nullptr;
}
/* computeHash
* compute the hash of a given RSID string
* by iterating over it and XORing it in different ways
*/
int HashTable::computeHash(const string& s) {
int hash = 0;
for(unsigned i = 0; i < s.size(); i+=2) {
hash = (hash << 2) ^ s.at(i);
hash = hash % size;
}
for(unsigned i = 0; i < s.size(); i+=2) {
hash = (hash << 5) ^ (s.at(i) << 3);
if(i+1 < s.size()) {
hash = hash ^ (s.at(i+1));
}
hash = hash % size;
}
return hash % size;
}