-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadditivedictionaryhash.cpp
More file actions
63 lines (47 loc) · 1.5 KB
/
additivedictionaryhash.cpp
File metadata and controls
63 lines (47 loc) · 1.5 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
#include "AdditiveDictionaryHash.h"
#include <fstream>
AdditiveDictionaryHash::AdditiveDictionaryHash(): hashTable<ADWord>::hashTable(100000)
{
;
}
bool AdditiveDictionaryHash::compare(std::string A, std::string B){
return A.length() > B.length();
}
unsigned hash_function(ADWord *X){
return X->getAmount();
}
bool equ(ADWord *A, ADWord *B){
if (A == 0 || B == 0){
return false;
}else{
return (A->getValue().compare(B->getValue()) == 0);
}
}
bool AdditiveDictionaryHash::addValue(std::string value, unsigned amount){
ADWord *in = hashTable::find(new ADWord(value, amount), hash_function, equ);
if (in){
in->setAmount(in->getAmount() + amount);
}else{
hashTable::add(new ADWord(value, amount), hash_function);
}
return true;
}
bool AdditiveDictionaryHash::toDoFromFile(std::string file_name, unsigned amount, bool (AdditiveDictionaryHash::*todo)(std::string, unsigned int)){
std::ifstream file;
file.open(file_name);
std::string word;
unsigned count = 0;
while (file>>word && ++count < amount) {
(this->*todo)(word, 1);
}
file.close();
return true;
}
bool AdditiveDictionaryHash::findValue(std::string value, unsigned amount){
ADWord *in = hashTable<ADWord>::find(new ADWord(value, amount), hash_function, equ);
return in != 0;
}
bool AdditiveDictionaryHash::delValue(std::string value, unsigned amount){
ADWord V(value, amount);
return hashTable<ADWord>::del(&V, hash_function, equ);
}