-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusingCustomKeys.dox
86 lines (62 loc) · 2.25 KB
/
usingCustomKeys.dox
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
Using Custom Keys {#customKeys}
============================
This page is meant to guide you through the usage of a custom key with a cache.
## Key Requirements
* Must have both a move constructor and a move assignment operator.
* Must implement equality operator (`bool operator==(const Self&) const;`).
* Must be hashable by the hasher passed as template parameter to the cache (by default, `absl::Hash<Key>`).
The key does _not_ need to be copyable.
## Usage Example
### With `absl::Hash` (default hasher)
```cpp
#include <string>
#include <cachemere.h>
struct ComplexKey {
std::string first;
std::string second;
bool operator==(const ComplexKey& other) const {
return
}
/// Override for the type to be hashable by `absl::Hash`.
template<typename H> friend H AbslHashValue(H h, const ComplexKey& s)
{
return H::combine(std::move(h), s.first, s.second);
}
/// For use with cachemere::measurement::CapacityDynamicallyAllocated.
size_t capacity() const {
return first.capacity() + second.capacity();
}
};
usig ValueT = int;
using Cache = cachemere::presets::memory::LRUCache<ComplexKey,
ValueT,
cachemere::measurement::SizeOf,
cachemere::measurement::CapacityDynamicallyAllocated>;
```
### With a custom hasher
```cpp
#include <string>
#include <cachemere.h>
struct ComplexKey {
std::string first;
std::string second;
bool operator==(const ComplexKey& other) const {
return
}
/// For use with cachemere::measurement::CapacityDynamicallyAllocated.
size_t capacity() const {
return first.capacity() + second.capacity();
}
};
struct VeryBadHash {
size_t operator(const ComplexKey&) {
return 0;
}
};
usig ValueT = int;
using Cache = cachemere::presets::memory::LRUCache<ComplexKey,
ValueT,
cachemere::measurement::SizeOf,
cachemere::measurement::CapacityDynamicallyAllocated,
VeryBadHash>;
```