-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.cpp
156 lines (124 loc) · 3.7 KB
/
Cache.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
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
#include "Cache.hpp"
#include "GameStuff.hpp"
#include <Library/Library.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::ostringstream;
namespace Cache {
namespace {
//from: http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
const unsigned long long HashPrime = 1610612741ULL;
void hash_range(unsigned int &out, const unsigned char *begin, unsigned int size) {
unsigned long long hash_temp = out;
for (unsigned int s = 0; s < size; ++s) {
hash_temp = (hash_temp * 256 + (unsigned long long)begin[s]) % HashPrime;
}
out = hash_temp;
}
template< typename TYPE >
void hash(unsigned int &out, TYPE const &val);
template< typename TYPE >
void hash(unsigned int &out, TYPE *foo) {
hash(out, *foo);
}
template< typename TYPE >
void hash(unsigned int &out, vector< TYPE > const &vec) {
for (unsigned int e = 0; e < vec.size(); ++e) {
hash(out, vec[e]);
}
}
#define QUICK_HASH( X ) \
template< > \
void hash(unsigned int &out, vector< X > const &vec) { \
hash_range(out, (unsigned char *)&(vec[0]), vec.size() * sizeof( X )); \
}
QUICK_HASH( float )
QUICK_HASH( unsigned int )
QUICK_HASH( int )
//QUICK_HASH( Trace::TracePoint )
//QUICK_HASH( Game2d::Control )
QUICK_HASH( Game2d::Fragment )
#define BASIC_HASH( X ) \
template< > \
void hash(unsigned int &out, X const &x) { \
hash_range(out, (unsigned char *)&(x), sizeof( X )); \
}
BASIC_HASH( float )
BASIC_HASH( int )
BASIC_HASH( unsigned int )
BASIC_HASH( Game2d::Control )
/*template< >
void hash(unsigned int &out, Trace const &foo) {
hash(out, foo.trace);
}*/
string get_filename(string name, unsigned int depends) {
ostringstream ret;
ret << "cache/" << name;
unsigned int hash_val = 0;
if (depends & DEPENDS_INPUT_FRAGMENTS) {
depends ^= DEPENDS_INPUT_FRAGMENTS;
hash(hash_val, Game2d::fragments);
cout << "After fragments: " << hash_val << endl; //DEBUG
}
if (depends & DEPENDS_INPUT_BINS) {
depends ^= DEPENDS_INPUT_BINS;
hash(hash_val, Game2d::bins);
cout << "After bins: " << hash_val << endl; //DEBUG
}
/*if (depends & DEPENDS_INPUT_TRACES) {
hash(hash_val, Input::traces);
}*/
if (depends & DEPENDS_INPUT_DELTA) {
depends ^= DEPENDS_INPUT_DELTA;
hash(hash_val, Game2d::Delta);
}
if (depends & DEPENDS_INPUT_DISCOUNT) {
depends ^= DEPENDS_INPUT_DISCOUNT;
hash(hash_val, Game2d::Discount);
}
if (depends & DEPENDS_INPUT_CONTROL_QUALITY_WEIGHT) {
depends ^= DEPENDS_INPUT_CONTROL_QUALITY_WEIGHT;
hash(hash_val, Game2d::ControlQualityWeight);
}
if (depends & DEPENDS_LIBRARY) {
depends ^= DEPENDS_LIBRARY;
hash(hash_val, Library::signature);
}
assert(depends == 0);
ret << "." << hash_val << ".cache";
return ret.str();
}
const unsigned int TokenSize = 6;
char Token[TokenSize] = {'C', 'a', '$', 'h', 'e', 'd'};
}
CacheFile::CacheFile( string name, unsigned int depends ) {
filename = get_filename( name, depends );
}
bool CacheFile::load( vector< pair< void *, unsigned int > > const &data ) {
ifstream file(filename.c_str());
if (!file) return false;
for (unsigned int w = 0; w < data.size(); ++w) {
if (!file.read((char *)data[w].first, data[w].second)) return false;
}
char token_buffer[TokenSize];
if (!file.read(token_buffer, TokenSize)) return false;
for (unsigned int i = 0; i < TokenSize; ++i) {
if (token_buffer[i] != Token[i]) {
return false;
}
}
return true;
}
void CacheFile::save( vector< pair< void *, unsigned int > > const &data ) {
ofstream file(filename.c_str());
for (unsigned int w = 0; w < data.size(); ++w) {
file.write((char *)data[w].first, data[w].second);
}
file.write(Token, TokenSize);
}
}