-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheatmap.h
348 lines (290 loc) · 8.71 KB
/
heatmap.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#pragma once
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include "common.h"
const std::string save_file = "heatmap_file.txt"; /// Файл, куда сохраняется статистика
const int tap_count_to_save = 3000; /// Сколько должно пройти нажатий, чтобы текущая статистика сбросилась в файл на диск
const int tap_count_to_update_date = 100; /// Сколько должно пройти нажатий, чтобы обновилась дата, вдруг день уже не сегодняшний, или вдруг человек не выключает компьютер
class Vector3DIterator
{
public:
Vector3DIterator(int colN, int rowN, int layerN, int colI = 0, int rowI = 0, int layerI = 0) : colI(colI), rowI(rowI), layerI(layerI), colN(colN), rowN(rowN), layerN(layerN) {}
Tap operator*(void) const {
return Tap{colI, rowI, layerI};
}
Vector3DIterator& operator++(void) {
colI++;
if (colI >= colN) {
colI = 0;
rowI++;
if (rowI >= rowN) {
rowI = 0;
layerI++;
}
}
return *this;
}
bool operator==(const Vector3DIterator& b) const {
return
colI == b.colI &&
rowI == b.rowI &&
layerI == b.layerI;
}
bool operator!=(const Vector3DIterator& b) const {
return !(operator==(b));
}
private:
int colI, rowI, layerI;
int colN, rowN, layerN;
};
template<typename T>
class Vector3D
{
public:
Vector3D() : l(0, Rows(0, Cols(0, T()))) {}
Vector3D(int layersN, int rowsN, int colsN, const T& obj) : l(layersN, Rows(rowsN, Cols(colsN, obj))) {}
Vector3DIterator begin(void) const {
return Vector3DIterator(colsCount(), rowsCount(), layersCount());
}
Vector3DIterator end(void) const {
return Vector3DIterator(colsCount(), rowsCount(), layersCount(), 0, 0, layersCount());
}
T& operator[](const Tap& tap) {
if (tap.layer >= layersCount() || tap.row >= rowsCount() || tap.col >= colsCount()) {
resize(std::max(tap.layer+1, layersCount()),
std::max(tap.row+1, rowsCount()),
std::max(tap.col+1, colsCount()));
}
return l[tap.layer][tap.row][tap.col];
}
const T& operator[](const Tap& tap) const {
if (tap.layer >= layersCount() || tap.row >= rowsCount() || tap.col >= colsCount()) {
/*std::cerr << "Error when trying access to tap: " << tap << std::endl;
throw std::exception();*/
return T();
}
return l[tap.layer][tap.row][tap.col];
}
int layersCount(void) const {
return l.size();
}
int rowsCount(void) const {
if (layersCount() > 0)
return l[0].size();
else
return 0;
}
int colsCount(void) const {
if (rowsCount() > 0)
return l[0][0].size();
else
return 0;
}
void save(std::ostream& out) const {
out << layersCount() << " " << rowsCount() << " " << colsCount() << std::endl;
for (int i = 0; i < layersCount(); ++i) {
for (int j = 0; j < rowsCount(); ++j) {
for (int k = 0; k < colsCount(); ++k) {
if constexpr (has_save<T>::value) {
l[i][j][k].save(out);
out << std::endl;
} else {
out << l[i][j][k] << " ";
}
}
if constexpr (!has_save<T>::value) {
out << std::endl;
}
}
if constexpr (!has_save<T>::value) {
out << std::endl;
}
}
}
void load(std::istream& in) {
int layersN, rowsN, colsN;
in >> layersN >> rowsN >> colsN;
if constexpr (has_load<T>::value) {
std::string line;
std::getline(in, line);
}
l.clear();
l.resize(layersN, Rows(rowsN, Cols(colsN, T())));
for (int i = 0; i < layersN; ++i) {
for (int j = 0; j < rowsN; ++j) {
for (int k = 0; k < colsN; ++k) {
if constexpr (has_load<T>::value) {
l[i][j][k].load(in);
} else {
in >> l[i][j][k];
}
}
}
}
}
private:
// Шаблонная магия, которая проверяет, есть ли у класса метод save. Ох уж эти шаблоны, их срочно надо сделать по-нормальному, чтобы такой херни не было. Большое спасибо https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-template-to-check-for-a-functions-existence
template <typename D>
class has_save
{
typedef char one;
typedef long two;
template <typename C> static one test( decltype(&C::save) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<D>(0)) == sizeof(char) };
};
// Аналогичная шаблонная магия, но только на метод load
template <typename D>
class has_load
{
typedef char one;
typedef long two;
template <typename C> static one test( decltype(&C::load) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<D>(0)) == sizeof(char) };
};
typedef std::vector<T> Cols;
typedef std::vector<Cols> Rows;
typedef std::vector<Rows> Layers;
void resize(int layersN, int rowsN, int colsN) {
Layers result(layersN, Rows(rowsN, Cols(colsN, T())));
for (int i = 0; i < l.size(); ++i) {
for (int j = 0; j < l[i].size(); ++j) {
for (int k = 0; k < l[i][j].size(); ++k) {
result[i][j][k] = l[i][j][k];
}
}
}
swap(result, l);
}
Layers l;
};
class Heatmap
{
public:
virtual void onTap(Tap tap) = 0;
virtual void save(std::ostream& out) const = 0;
virtual void load(std::istream& in) = 0;
};
/** Просто статистика числа нажатий по каждой клавише. */
class OneTapHeatmap : public Heatmap
{
public:
OneTapHeatmap() : OneTapHeatmap(0, 0, 0) {}
OneTapHeatmap(int layersN, int rowsN, int colsN);
void onTap(Tap tap);
void save(std::ostream& out) const;
void load(std::istream& in);
Vector3DIterator begin(void) const { return v.begin(); }
Vector3DIterator end(void) const { return v.end(); }
int layersCount(void) const;
int rowsCount(void) const;
int colsCount(void) const;
int getTapCount(Tap tap) const;
private:
int counter;
Vector3D<int32_t> v;
};
/** Статистика двух нажатий, когда одно идет за другим. Как часто та или иная комбинация встречается. */
class TwoTapHeatmap : public Heatmap
{
public:
TwoTapHeatmap(int layersN, int rowsN, int colsN);
void onTap(Tap tap);
void save(std::ostream& out) const;
void load(std::istream& in);
Vector3DIterator begin(void) const { return v.begin(); }
Vector3DIterator end(void) const { return v.end(); }
const OneTapHeatmap& operator[](Tap tap) const { return v[tap]; }
int layersCount(void) const;
int rowsCount(void) const;
int colsCount(void) const;
int getTapCount(Tap first, Tap second) const; // У второго слой считается таким же как и у первого
private:
Vector3D<OneTapHeatmap> v;
Tap lastTap;
};
/** Количество нажатий за определенный день(просто всех клавиш). */
class DayHeatmap : public Heatmap
{
public:
DayHeatmap();
void onTap(Tap tap);
void save(std::ostream& out) const;
void load(std::istream& in);
int getToday(void) const;
int getYear(int day) const;
int getMonth(int day) const;
int getDay(int day) const;
std::map<int, int32_t> getStatistics() const;
private:
void updateToday();
int counter;
int today;
std::map<int, int32_t> statistics;
};
class AllStatistics
{
public:
AllStatistics(const std::string& file);
void load(void);
void save(void) const;
OneTapHeatmap onetap;
TwoTapHeatmap twotap;
DayHeatmap daytap;
std::string file;
};
//-----------------------------------------------------------------------------
class Fingers
{
public:
void save(std::ostream& out) const;
void load(std::istream& in);
int getFinger(Tap tap) const;
int getRow(Tap tap) const;
std::string getHandName(int finger);
std::string getFingerName(int finger);
std::string getRowName(int row);
void set(Tap tap, int finger, int row);
private:
class LoadablePair : public std::pair<int, int>
{
public:
LoadablePair() : std::pair<int, int>() {}
LoadablePair(const std::pair<int, int>& p) : std::pair<int, int>(p) {}
void save(std::ostream& out) const {
out << this->first << " " << this->second;
}
void load(std::istream& in) {
in >> this->first >> this->second;
}
};
Vector3D<LoadablePair> v;
};
class Names
{
public:
void save(std::ostream& out) const;
void load(std::istream& in);
std::string getName(Tap tap);
void setName(Tap tap, const std::string& name);
private:
class LoadableString : public std::string
{
public:
LoadableString() : std::string() {}
LoadableString(const std::string& str) : std::string(str) {}
void save(std::ostream& out) const {
out << *this;
}
void load(std::istream& in) {
std::getline(in, *this);
}
};
Vector3D<LoadableString> v;
};