Deterministic Gaussian Sampling
Loading...
Searching...
No Matches
floating_bucket_cache.h
1#ifndef FLOATING_BUCKET_CACHE_H
2#define FLOATING_BUCKET_CACHE_H
3
4#include <cassert>
5#include <cstdint>
6#include <iostream>
7#include <unordered_map>
8
9#define FLOATING_BUCKET_CACHE_SIZE 1.25
10
12 public:
13 floatingBucketCacheManager(size_t size) {
14 cache.reserve((size_t)((double)size * FLOATING_BUCKET_CACHE_SIZE));
15 };
16
17 inline bool get(double floatingKey, double* value) const {
18 const uint64_t hashKey = hashFloatingKey(floatingKey);
19 auto it = cache.find(hashKey);
20 if (it != cache.end()) {
21 *value = it->second;
22 return true;
23 }
24 return false;
25 }
26
27 inline void set(double floatingKey, double value) {
28 const uint64_t hashKey = hashFloatingKey(floatingKey);
29 cache[hashKey] = value;
30 }
31
32 inline void clear() { cache.clear(); }
33
34 private:
35 static constexpr double epsilon = 1e-15;
36
37 std::unordered_map<uint64_t, double> cache;
38
39 inline uint64_t hashFloatingKey(double floatingKey) const {
40 return static_cast<uint64_t>(floatingKey / epsilon);
41 }
42};
43
45 public:
47 cache.reserve((size_t)((double)size * FLOATING_BUCKET_CACHE_SIZE));
48 }
49
50 inline bool get(double floatingKey, size_t intKey, double* value) const {
51 const uint64_t hashKey = hashFloatingKey(floatingKey, intKey);
52 auto it = cache.find(hashKey);
53 if (it != cache.end()) {
54 *value = it->second;
55 return true;
56 }
57 return false;
58 }
59
60 inline void set(double floatingKey, size_t intKey, double value) {
61 const uint64_t hashKey = hashFloatingKey(floatingKey, intKey);
62 cache[hashKey] = value;
63 }
64
65 inline void clear() { cache.clear(); }
66
67 private:
68 static constexpr double epsilon = 1e-9;
69 static constexpr uint64_t floatingBits = 40;
70 static constexpr uint64_t intBits = 24;
71
72 static constexpr uint64_t floatingMask = (uint64_t(1) << floatingBits) - 1;
73 static constexpr uint64_t intMask = (uint64_t(1) << intBits) - 1;
74
75 std::unordered_map<uint64_t, double> cache;
76
77 inline uint64_t hashFloatingKey(double floatingKey, size_t intKey) const {
78 assert(intKey <= intMask);
79 return (static_cast<uint64_t>(floatingKey / epsilon) & floatingMask) |
80 ((static_cast<uint64_t>(intKey) & intMask) << floatingBits);
81 }
82};
83
84#endif // FLOATING_BUCKET_CACHE_H
Definition floating_bucket_cache.h:44
Definition floating_bucket_cache.h:11