-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
116 lines (87 loc) · 2.37 KB
/
main.cpp
File metadata and controls
116 lines (87 loc) · 2.37 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
struct arrayelement
{
char protein[30];
int count = 0;
};
arrayelement pHash[40];
arrayelement proteins[40];
char lookup[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int g[26] = {20, 16, 12, 25, 26, 10, 12, 13, 5, 15, 28, 19, 14, 20, 10, 15, 3, 27, 7, 15, 11, 21, 8, 27, 18, 18};
//int g[26] = {14, 29, 30, 4, 21, 17, 19, 5, 14, 26, 0, 2, 24, 26, 11, 24, 3, 6, 2, 8, 18, 2, 0, 1, 5, 27};
int parseIndex(char x)
{
for(int i = 0; i < 26; i++)
{
if(x == lookup[i])
{
return g[i];
}
}
return 0;
}
void perfectHash(char value[80])
{
int key = (parseIndex(value[0]) + parseIndex(value[1]) +
parseIndex(value[strlen(value - 1)]) + strlen(value)) % 40;
key = abs(key);
if(pHash[key].count == 0)
{
strcpy(pHash[key].protein, value);
pHash[key].count++;
}
else if(strcmp(pHash[key].protein, value) == 0)
{
pHash[key].count++;
return;
}
else
{
cout << "collision at " << pHash[key].protein << " and " << value << endl;
}
}
void hashValue(char value[80])
{
///Hashed key = (first letter + 2 * last letter) % 40
int key = (parseIndex(value[0]) + 2 * parseIndex(value[sizeof(value)/sizeof(*value) - 1])) % 40;
if(proteins[key].count == 0)
{
strcpy(proteins[key].protein, value);
proteins[key].count = 1;
return;
}
while(proteins[key].count != 0)
{
if(strcmp(proteins[key].protein, value) == 0)
{
proteins[key].count++;
return;
}
key++;
if(key >= 40)
{
key = 0;
}
}
strcpy(proteins[key].protein, value);
proteins[key].count++;
}
int main()
{
char buffer2[80];
ifstream Hash("file3.txt");
if(!Hash) {cerr << "Input file could not be opened\n"; exit(1);}
while(Hash >> buffer2)
{
perfectHash(buffer2);
}
Hash.close();
}