e97a589f3cc8eb7c8b8a9eac48c522f34a51fd03
2 \brief STON Hash Tables
3 \details Aligned general purpose hash functions and memory definitions
4 whose columns are provided, and whose rows, and sizes, are derived.
6 ht_size = header.ht_columns << header.ht_2pow;
7 ht_rows = 0x1 << header.ht_2pow;
9 All generic hashtables in henge must have a power-of-two number of
10 rows. An ht_columns value that is also a power-of-two will result in
11 a power-of-two sized memory imprint for the structure, making it easy
14 Elements in the columns may be of any arbitrary size.
16 typedef uint32_t my_ht_type;
17 ht_bytes = ht_size * sizeof(my_ht_type);
19 implementation covers only 32-bit unit sizes.
23 ----------------------------------------------------------------------------*/
26 /* Define STON_NOSTATIC to expose included function symbols */
28 #define STON_FUNC_STATIC static
30 #define STON_FUNC_STATIC
31 #endif //STON_NOSTATIC
32 /* If GNUC is detected, uses attributes to stop inlining */
34 #define STON_FUNC_NOINLINE __attribute__ ((noinline))
36 #define STON_FUNC_NOINLINE
38 /* Define STON_NOINLINE to prevent inline compiler hints */
40 #define STON_FUNC_INLINE inline
42 #define STON_FUNC_INLINE
43 #endif //STON_NOINLINE
44 /* Define STON_FUNC to override the default STON Function attributes */
46 #define STON_FUNC STON_FUNC_STATIC STON_FUNC_INLINE
54 ston_ht
ston_ht32_fread(FILE*,long,void*(*)(size_t));
57 #endif //STON_HT_FREAD
59 #include <string.h> //mem*
60 /* STON Hashtable Structure
61 Hashtables are stored as dynamically sized two dimensional arrays
63 typedef struct ston_ht_header_t
64 { uint16_t ht_columns
;
65 uint8_t ht_2pow
, ht_flags
;
69 uint32_t ston_up2pow(uint32_t);
71 uint8_t ston_trailing0(uint32_t);
73 ston_ht
ston_ht32_create(uint16_t,uint8_t,uint8_t,void*(*)(size_t));
75 uint32_t* ston_ht32_row(ston_ht
,uint32_t);
77 uint32_t ston_ht32_insert(ston_ht
,uint32_t,uint16_t,uint32_t);
79 size_t ston_ht32_insertx(ston_ht
,uint32_t,uint32_t*,size_t,size_t);
81 #define ston_ht32_new(_COL,_N,_F,_FN) (ston_ht32_create(_COL,ston_trailing0(ston_up2pow(_N << 1)),_F,_FN))
82 #define ston_ht32_entry(_HT,_KEY,_COL) (ston_ht32_row(_HT,_KEY) + _COL)
83 #define ston_ht_size(_HT) ((_HT)->ht_columns << (_HT)->ht_2pow)
84 #define ston_ht_rows(_HT) (0x1 << (_HT)->ht_2pow)
85 #define ston_ht_cols(_HT) ((_HT)->ht_columns)
86 #define ston_ht_start(_HT) ((uint8_t*)((_HT) + 1))
87 #define ston_ht_keyrow(_HT,_KEY) ((_KEY) & (ston_ht_rows(ht) - 1))
88 #define ston_ht32_start(_HT) ((uint32_t*)ston_ht_start(_HT))
89 #define ston_ht32_end(_HT) (ston_ht32_start(_HT) + ston_ht_size(_HT))
90 #define ston_ht32_size(_HT) (ston_ht_size(_HT) * sizeof(uint32_t))
92 /** @see http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */
96 { val
= (val
<< 1) - 1;
105 /** @see https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightParallel */
107 uint8_t ston_trailing0
112 if (v
& 0x0000FFFF) c
-= 16;
113 if (v
& 0x00FF00FF) c
-= 8;
114 if (v
& 0x0F0F0F0F) c
-= 4;
115 if (v
& 0x33333333) c
-= 2;
116 if (v
& 0x55555555) c
-= 1;
120 /* Creates a new hash table, provided a memory allocation function that takes a
121 single size_t bytes, a column count, and a row count which determines the
124 use ston_ht32_new to specify the exact or estimated number of unique keys
125 held in the table. With ston_ht32_new, the provided ht_rows is doubled, and
126 rounded up to the nearest power of two to create a hash table with minimal
130 ston_ht ston_ht32_create
131 ( uint16_t ht_columns
,
134 void* (*alloc_fn
)(size_t)
136 { size_t ht_bytes
= (ht_columns
<< ht_2pow
) * sizeof(uint32_t);
137 ston_ht ht
= (ston_ht
) alloc_fn(sizeof(ston_ht_h
) + ht_bytes
);
139 { ht
->ht_columns
= ht_columns
;
140 ht
->ht_2pow
= ht_2pow
;
141 ht
->ht_flags
= ht_flags
;
142 memset(ht
+ 1, 0, ht_bytes
);
148 /* Reads a 32-bit hash table out of the provided file at the provide fpos, into
149 a buffer allocated by alloc_fn. Memory is allocated to the stack until the
150 entire structure is verified, and all file operations are finished.
151 Returns NULL with properly set errno on failure.
153 ston_ht ston_ht32_fread
156 void* (*alloc_fn
)(size_t)
158 { struct ston_ht_header_t header
;
159 ston_ht stack_ht
, ht
;
161 size_t table_size
, alloc_size
;
163 if ((fpos_start
= ftell(file
)) == -1)
165 if (fread(&header
, sizeof(header
), 1, file
) != 1)
167 table_size
= ston_ht32_size(&header
);
168 alloc_size
= sizeof(header
) + table_size
;
169 stack_ht
= (ston_ht
) alloca(alloc_size
);
170 memcpy(stack_ht
, &header
, sizeof(header
));
171 if (fread(stack_ht
+ sizeof(header
), table_size
, 1, file
) != 1)
173 if (fseek(file
, fpos_start
, SEEK_SET
) != 0)
175 ht
= (ston_ht
) alloc_fn(alloc_size
);
177 memcpy(ht
, stack_ht
, alloc_size
);
180 /* Try to seek the file back to origin without clobbering errno */
182 fseek(file
, fpos_start
, SEEK_SET
);
188 /* Returns a pointer to the row of data in the hashtable containing the provided
189 key, inserts if not found. Returns NULL on overflow.
192 uint32_t* ston_ht32_row
193 ( struct ston_ht_header_t
* ht
,
197 uint32_t* row_start
= ston_ht32_start(ht
);
198 uint32_t* row_end
= ston_ht32_end(ht
);
199 uint16_t ht_cols
= ston_ht_cols(ht
);
200 size_t row_number
= ston_ht_keyrow(ht
,key
);
202 row
= row_start
+ (row_number
* ht_cols
);
212 if (row
+ ht_cols
< row_end
)
223 /* Inserts a value into a hashtable at the specified column, returning the
226 uint32_t ston_ht32_insert
227 ( struct ston_ht_header_t
* ht
,
232 { uint32_t* value_location
, old_value
;
233 value_location
= ston_ht32_entry(ht
,key
,column
);
234 old_value
= *value_location
;
235 *value_location
= value
;
239 /* Inserts a row of units into a hashtable, starting with the specified column.
240 Returns the number of elements that were written. This function will not
241 overflow internal buffers, but will return a short count (lower than the
242 provided 'units') when truncation of source data occurs. */
246 ( struct ston_ht_header_t
* ht
,
252 { uint32_t* data_row
= ston_ht32_row(ht
,key
);
253 uint32_t* data_limit
= data_row
+ ston_ht_cols(ht
);
254 uint32_t* data_trg
= data_row
+ start_column
;
255 if (data_row
== NULL
)
257 while (units
-- && data_trg
< data_limit
)
258 *data_trg
++ = *data_src
++;
259 return (size_t)(data_trg
- data_row
);
263 #ifndef STON_DHT_SIZE
264 #define STON_DHT_SIZE 4096
267 /* STON Dynamic Hashtable Structure
268 A dynamic form of the generic hashtable implementation above which uses
271 typedef struct ston_dht_header_t
277 typedef struct ston_dht_t
279 void* pages
[sizeof(void*) * 8];
280 void* (*ht_alloc
)(size_t);
281 void (*ht_free
)(void*);
285 ston_dht
ston_dht_create(uint16_t,uint8_t,uint8_t,void*(*)(size_t),void(*)(void*));
287 uint32_t* ston_dht32_row(ston_dht
,uint32_t);
289 uint32_t ston_dht32_insert(ston_dht
,uint32_t,uint16_t,uint32_t);
291 size_t ston_dht32_insertx(ston_dht
,uint32_t,uint32_t*,uint16_t,size_t);
293 ston_dht
ston_dht_free(ston_dht
);
295 #define ston_dht_units(_HT,_DEPTH) ((_HT)->header.columns << _DEPTH)
296 #define ston_dht_bytes(_HT,_DEPTH) (ston_dht_units(_HT,_DEPTH) * (_HT)->header.unit_bytes)
297 #define ston_dht_new(_COL,_ALOC,_FRE) (ston_dht_create(_COL,3,sizeof(int),_ALOC,_FRE))
298 #define ston_dht_sized(_COL,_N,_ALOC,_FRE) (ston_dht_create(_COL,ston_trailing0(ston_up2pow(_N),sizeof(int),_ALOC,_FRE)))
299 #define ston_dht32_entry(_HT,_KEY,_COL) (ston_dht32_row(_HT,_KEY) + _COL)
300 #define ston_dht32_new(_COL,_ALOC,_FRE) (ston_dht_create(_COL,0,sizeof(uint32_t),_ALOC,_FRE))
301 #define ston_dht32_sized(_COL,_N,_ALOC,_FRE) (ston_dht_create(_COL,ston_trailing0(ston_up2pow(_N)),sizeof(uint32_t),_ALOC,_FRE))
304 /* Creates a new bucketted hash table, provided a memory allocation function
305 that takes a single size_t bytes, a memory free function, a column count, and
306 a row count which determines the size of the buckets.
309 ston_dht ston_dht_create
313 void* (*ht_alloc
)(size_t),
314 void (*ht_free
)(void*)
316 { ston_dht ht
= (ston_dht
) ht_alloc(sizeof(struct ston_dht_t
));
318 { ht
->header
.columns
= columns
;
319 ht
->header
.start_depth
= start_depth
;
320 ht
->header
.unit_bytes
= unit_bytes
;
321 memset(ht
->pages
, 0, sizeof(void*) * sizeof(void*) * 8);
322 ht
->pages
[start_depth
] = ht_alloc(ston_dht_bytes(ht
, start_depth
));
323 ht
->ht_alloc
= ht_alloc
;
324 ht
->ht_free
= ht_free
;
325 if (ht
->pages
[start_depth
] == NULL
&& ht_free
!= NULL
)
328 memset(ht
->pages
[start_depth
], 0, ston_dht_bytes(ht
, start_depth
));
333 /* Returns a pointer to the row of data in the hashtable containing the provided
334 key, inserts if not found. Returns NULL on overflow.
337 uint32_t* ston_dht32_row
338 ( struct ston_dht_t
* ht
,
341 { uint16_t columns
= ht
->header
.columns
;
342 uint8_t depth
= ht
->header
.start_depth
;
343 uint32_t mask
= ((0x1 << depth
) - 1) >> 1;
348 if (ht
->pages
[depth
] == NULL
)
349 { ht
->pages
[depth
] = ht
->ht_alloc(ston_dht_bytes(ht
, depth
));
350 if (ht
->pages
[depth
] == NULL
)
352 memset(ht
->pages
[depth
], 0, ston_dht_bytes(ht
, depth
));
354 page
= ht
->pages
[depth
];
355 row
= (uint32_t*)page
+ ((key
& mask
) * columns
);
357 if (row_key
== key
|| row_key
== 0)
362 mask
= (mask
<< 1) | 0x1;
366 /* Inserts a value into a hashtable at the specified column, returning the
369 uint32_t ston_dht32_insert
370 ( struct ston_dht_t
* ht
,
375 { uint32_t* value_location
, old_value
;
376 value_location
= ston_dht32_entry(ht
,key
,column
);
377 old_value
= *value_location
;
378 *value_location
= value
;
382 /* Insert multiple values, returning the number of bytes written */
386 ( struct ston_dht_t
* ht
,
389 uint16_t start_column
,
392 { uint32_t* data_row
= ston_dht32_row(ht
,key
);
393 uint32_t* data_limit
= data_row
+ ht
->header
.columns
;
394 uint32_t* data_trg
= data_row
+ start_column
;
395 if (data_row
== NULL
)
397 while (units
-- && data_trg
< data_limit
)
398 *data_trg
++ = *data_src
++;
399 return (size_t)(data_trg
- data_row
);
402 /* Free the dynamic hash table */
404 struct ston_dht_t
* ston_dht_free
405 ( struct ston_dht_t
* ht
)
406 { void (*ht_free
)(void*) = ht
->ht_free
;
407 uint8_t depth
= ht
->header
.start_depth
;
408 void** pages
= ht
->pages
;
410 { while (pages
[depth
] != NULL
)
411 ht_free(pages
[depth
++]);