-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache-cpp.cpp
More file actions
148 lines (129 loc) · 3.52 KB
/
cache-cpp.cpp
File metadata and controls
148 lines (129 loc) · 3.52 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* This is free and unencumbered software released into the public domain.
* Refer to LICENSE.txt in this directory. */
/* Randomized stress test of a sqrt(x) caching mechanism. */
#include <algorithm>
#include <cmath>
#include <deque>
#include <exception>
#include <iostream>
#include <map>
#include <random>
#include <sstream>
#include <vector>
/*
* Returns a random integer in the range [0, max]
*/
static int
random_int(int max)
{
/* Just use a deterministic PRNG for demo purposes */
static std::mt19937 generator;
static std::uniform_int_distribution<int> distribution(0, max);
return distribution(generator);
}
/*
* Cache containing a double ended queue of 100 <number, square root value> pairs
*/
class CacheSqroot
{
public:
CacheSqroot()
{
}
int operator()(int number);
private:
const size_t cache_size = 100;
std::deque<std::pair<unsigned int, unsigned int>> data;
};
/*
* Searches for number inside the data cache; if found, it returns its cached square root.
* Otherwise, it stores its square root at the end of the cache queue, doing so also for
* numbers immediately lower and higher than the argument
*/
int
CacheSqroot::operator()(int number)
{
auto f = std::find_if(data.begin(), data.end(),
[&number](const std::pair<unsigned char, unsigned char> &p) {
return p.first == number;
});
if (f != data.end())
{
/* Cache hit! */
return f->second;
}
/* Cache miss. Find correct result and populate a few cache entries. */
int sqroot;
for (int number_adj = number - 1; number_adj < number + 1; ++number_adj)
{
unsigned char sqroot_adj = static_cast<int>(sqrt(number_adj));
data.emplace_back(number_adj, sqroot_adj);
while (data.size() > cache_size)
data.pop_front();
if (number_adj == number)
{
/* This is our return value. */
sqroot = sqroot_adj;
}
}
return sqroot;
}
/*
* Class used for evaluating the accuracy of the cache content, by comparing the cached
* square root of a number, with its calculated (correct) one
*/
class CacheTester
{
public:
CacheTester(size_t cachesize __attribute__((unused))) {}
void operator()(int number);
public:
class CacheFailure : public std::exception
{
public:
CacheFailure(int number, int cached, int correct)
{
std::stringstream ss;
ss << "number=" << number
<< " sqroot_cache=" << cached
<< " sqroot_correct=" << correct;
details = ss.str();
}
const char *what() const noexcept
{
return details.c_str();
}
private:
std::string details;
};
private:
CacheSqroot sqrooter;
};
void
CacheTester::operator()(int number)
{
/* Check cache_calculate() with the given number. */
int sqroot_cache = sqrooter(number);
int sqroot_correct = static_cast<int>(sqrt(number));
if (sqroot_cache != sqroot_correct)
{
/* cache.calculate() returned incorrect value. */
throw CacheFailure(number, sqroot_cache, sqroot_correct);
}
}
int
main()
{
CacheTester tester(100);
/* Repeatedly check CacheSqroot calculate(). */
for (int i = 0;; ++i)
{
if (i % 100 == 0)
{
std::cout << "i=" << i << "\n";
}
/* Check cache_calculate() with a random number. */
int number = random_int(255);
tester(number);
}
}