-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_hash.cc
46 lines (38 loc) · 1.03 KB
/
string_hash.cc
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
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "time_counter.h"
class StringHash {
public:
bool Exists(const std::string& item_hash) {
return hashes_.find(item_hash) != std::string::npos;
}
void Add(const std::string& item_hash) {
hashes_ = hashes_ + item_hash;
}
private:
std::string hashes_;
};
std::string SomeItemHash(unsigned item) {
std::stringstream stream;
stream << std::setfill('0') << std::setw(32) << std::hex << item;
return stream.str();
}
int main() {
StringHash hash_store;
for (unsigned i = 0; i < 100; ++i)
hash_store.Add(SomeItemHash(i));
std::vector<std::string> incoming_hashes;
for (unsigned i = 0; i < 30; ++i)
incoming_hashes.push_back(SomeItemHash(i + 1232134));
TimeCounter counter;
for (const auto &incoming_hash : incoming_hashes) {
if (!hash_store.Exists(incoming_hash))
hash_store.Add(incoming_hash);
}
std::cout << counter.Elapsed().count()
<< " ms" << std::endl;
}