-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_set.cpp
49 lines (40 loc) · 1.37 KB
/
string_set.cpp
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
// $Id: string_set.cpp,v 1.5 2017-10-10 16:02:40-07 - - $
// Chenxing Ji
// cji13
// Nelson Yeap
// nyeap
#include <string>
#include <unordered_set>
using namespace std;
#include "string_set.h"
unordered_set<string> string_set::set;
string_set::string_set() {
set.max_load_factor (0.5);
}
string_set set;
const string* string_set::intern (const char* string) {
auto handle = set.insert (string);
return &*handle.first;
}
void string_set::dump (FILE* out) {
static unordered_set<string>::hasher hash_fn
= string_set::set.hash_function();
size_t max_bucket_size = 0;
for (size_t bucket = 0; bucket < set.bucket_count(); ++bucket) {
bool need_index = true;
size_t curr_size = set.bucket_size (bucket);
if (max_bucket_size < curr_size) max_bucket_size = curr_size;
for (auto itor = set.cbegin (bucket);
itor != set.cend (bucket); ++itor) {
if (need_index) fprintf (out, "hash[%4zu]: ", bucket);
else fprintf (out, " %4s ", "");
need_index = false;
const string* str = &*itor;
fprintf (out, "%22zu %p->\"%s\"\n", hash_fn(*str),
str, str->c_str());
}
}
fprintf (out, "load_factor = %.3f\n", set.load_factor());
fprintf (out, "bucket_count = %zu\n", set.bucket_count());
fprintf (out, "max_bucket_size = %zu\n", max_bucket_size);
}