-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtype.h
341 lines (300 loc) · 7.21 KB
/
dtype.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
/* This file is part of Anvil. Anvil is copyright 2007-2010 The Regents
* of the University of California. It is distributed under the terms of
* version 2 of the GNU GPL. See the file LICENSE for details. */
#ifndef __DTYPE_H
#define __DTYPE_H
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
/* this enum is available to C code */
enum dtype_ctype
{
/* we want this 0 so that a zeroed out dtype will be
* correctly initialized, and the integer zero even */
DT_UINT32 = 0,
DT_DOUBLE,
DT_STRING,
DT_BLOB
};
/* abortable transaction handle */
typedef uint32_t abortable_tx;
#define NO_ABORTABLE_TX ((abortable_tx) 0)
/* make it easier to add this parameter to many functions */
#define ATX_DEF abortable_tx atx
#ifdef __cplusplus
#include <ext/hash_map>
#include "blob.h"
#include "istr.h"
#include "magic_test.h"
#include "blob_comparator.h"
/* for prototypes, required or optional parameter */
#define ATX_REQ ATX_DEF
#define ATX_OPT ATX_REQ = NO_ABORTABLE_TX
/* all data stored in Anvil is wrapped by this type */
class dtype
{
public:
enum ctype
{
UINT32 = DT_UINT32,
DOUBLE = DT_DOUBLE,
STRING = DT_STRING,
BLOB = DT_BLOB
};
ctype type;
union
{
uint32_t u32;
double dbl;
};
/* alas, we can't put these in the union */
istr str;
blob blb;
inline dtype(uint32_t x) : type(UINT32), u32(x) {}
inline dtype(double x) : type(DOUBLE), dbl(x) {}
inline dtype(const istr & x) : type(STRING), u32(0), str(x) {}
/* have to provide this even though usually istr is transparent */
inline dtype(const char * x) : type(STRING), u32(0), str(x) {}
inline dtype(const char * x, size_t length) : type(STRING), u32(0), str(x, length) {}
inline dtype(const blob & x) : type(BLOB), u32(0), blb(x) {}
inline dtype(const blob & b, ctype t)
: type(t)
{
assert(b.exists());
switch(t)
{
case UINT32:
assert(b.size() == sizeof(uint32_t));
u32 = b.index<uint32_t>(0);
return;
case DOUBLE:
assert(b.size() == sizeof(double));
dbl = b.index<double>(0);
return;
case STRING:
str = b;
return;
case BLOB:
blb = b;
return;
}
abort();
}
inline blob flatten() const
{
switch(type)
{
case UINT32:
return blob(sizeof(uint32_t), &u32);
case DOUBLE:
return blob(sizeof(double), &dbl);
case STRING:
return blob(strlen(str), str);
case BLOB:
return blb;
}
abort();
}
static inline const char * name(ctype type)
{
switch(type)
{
case UINT32:
return "uint32";
case DOUBLE:
return "double";
case STRING:
return "string";
case BLOB:
return "blob";
}
return "unknown";
}
inline int compare(const dtype & x, const blob_comparator * blob_cmp = NULL) const
{
assert(type == x.type);
switch(type)
{
case UINT32:
return (u32 < x.u32) ? -1 : u32 != x.u32;
case DOUBLE:
return (dbl < x.dbl) ? -1 : dbl != x.dbl;
case STRING:
if(str == x.str)
return 0;
if(!str || !x.str)
return str ? 1 : -1;
return strcmp(str, x.str);
case BLOB:
return blob_cmp ? blob_cmp->compare(blb, x.blb) : blb.compare(x.blb);
}
abort();
}
/* avoid constructing a second dtype if it is not necessary */
inline int compare(uint32_t x) const
{
assert(type == UINT32);
return (u32 < x) ? -1 : u32 != x;
}
inline int compare(double x) const
{
assert(type == DOUBLE);
return (dbl < x) ? -1 : dbl != x;
}
inline int compare(const char * x) const
{
assert(type == STRING);
if(str == x)
return 0;
if(!str || !x)
return str ? 1 : -1;
return strcmp(str, x);
}
inline int compare(const istr & x) const
{
assert(type == STRING);
if(str == x)
return 0;
if(!str || !x)
return str ? 1 : -1;
return strcmp(str, x);
}
inline int compare(const blob & x, const blob_comparator * blob_cmp = NULL) const
{
assert(type == BLOB);
return blob_cmp ? blob_cmp->compare(blb, x) : blb.compare(x);
}
};
/* this class can be used to wrap a blob comparator pointer so that it can be
* used for STL methods like std::sort() when you have dtypes and not blobs */
class dtype_comparator_object
{
public:
inline bool operator()(const dtype & a, const dtype & b) const
{
return a.compare(b, blob_cmp) < 0;
}
inline dtype_comparator_object(const blob_comparator * comparator = NULL) : blob_cmp(comparator) {}
private:
const blob_comparator * blob_cmp;
};
/* if it is necessary to change the comparator during the use of an STL
* container like std::set, then dtype_comparator_refobject will help you out */
class dtype_comparator_refobject
{
public:
inline bool operator()(const dtype & a, const dtype & b) const
{
return a.compare(b, blob_cmp) < 0;
}
inline dtype_comparator_refobject(const blob_comparator * const & comparator) : blob_cmp(comparator) {}
private:
const blob_comparator * const & blob_cmp;
};
template<class T>
struct dtype_hash_helper
{
};
template<>
struct dtype_hash_helper<unsigned int>
{
inline size_t operator()(unsigned int x) const
{
return x;
}
};
template<>
struct dtype_hash_helper<unsigned long>
{
inline size_t operator()(unsigned long x) const
{
return x;
}
};
template<>
struct dtype_hash_helper<void *>
{
size_t operator()(const void * x) const
{
/* this will be one of the above two cases */
return dtype_hash_helper<uintptr_t>()((uintptr_t) x);
}
};
template<>
struct dtype_hash_helper<double>
{
inline size_t operator()(double x) const
{
union {
uint64_t i;
double d;
} u = {i: 0};
u.d = x;
/* we count on the compiler to optimize this */
if(sizeof(size_t) == sizeof(uint64_t))
return u.i;
return (size_t) ((u.i >> 32) ^ u.i);
}
};
/* good for hash_map; has an equality operator and a hash operator */
class dtype_hashing_comparator
{
public:
inline bool operator()(const dtype & a, const dtype & b) const
{
return !a.compare(b, blob_cmp);
}
inline size_t operator()(const dtype & dt) const
{
switch(dt.type)
{
case dtype::UINT32:
return dt.u32;
case dtype::DOUBLE:
/* 0 and -0 both hash to zero */
if(dt.dbl == 0.0)
return 0;
return dtype_hash_helper<double>()(dt.dbl);
case dtype::STRING:
return __gnu_cxx::hash<const char *>()((const char *) dt.str);
case dtype::BLOB:
{
if(blob_cmp)
return blob_cmp->hash(dt.blb);
/* uses FNV hash taken from stl::tr1::hash */
size_t r = 2166136261u;
size_t length = dt.blb.size();
for(size_t i = 0; i < length; i++)
{
r ^= dt.blb[i];
r *= 16777619;
}
return r;
}
}
abort();
}
inline dtype_hashing_comparator(const blob_comparator * const & comparator) : blob_cmp(comparator) {}
private:
const blob_comparator * const & blob_cmp;
};
typedef magic_test<dtype> dtype_test;
/* not actually a dtype_test, but usable the same way with a template */
/* XXX: get rid of this - some dtables may have constant-time behavior finding known keys, e.g. array_dtable */
class dtype_static_test
{
public:
inline dtype_static_test(const dtype & key, const blob_comparator * const & cmp) : secret(key), blob_cmp(cmp) {}
inline int operator()(const dtype & key) const
{
return key.compare(secret, blob_cmp);
}
private:
const dtype secret;
const blob_comparator * const & blob_cmp;
};
#endif /* __cplusplus */
#endif /* __DTYPE_H */