1 #ifndef _RE2C_UTIL_ORD_HASH_SET_
2 #define _RE2C_UTIL_ORD_HASH_SET_
28 typedef size_t hash_t;
30 std::vector<elem_t*> elems;
31 std::map<hash_t, elem_t*> lookup;
33 static hash_t hash(
const void *data,
size_t size);
34 elem_t *make_elem(elem_t *next,
size_t index,
size_t size,
const void *data);
40 size_t insert(
const void *data,
size_t size);
41 template<
typename data_t>
size_t deref(
size_t i, data_t *&data);
44 ord_hash_set_t::hash_t ord_hash_set_t::hash(
const void *data,
size_t size)
46 const uint8_t *bytes =
static_cast<const uint8_t*
>(data);
48 for (
size_t i = 0; i <
size; ++i)
50 h = h ^ ((h << 5) + (h >> 2) + bytes[i]);
55 ord_hash_set_t::elem_t* ord_hash_set_t::make_elem(
61 elem_t *e =
static_cast<elem_t*
>(malloc(offsetof(elem_t, data) + size));
65 memcpy(e->data, data, size);
76 std::for_each(elems.begin(), elems.end(), free);
86 const hash_t h = hash(data, size);
88 std::map<hash_t, elem_t*>::const_iterator i = lookup.find(h);
89 if (i != lookup.end())
91 for (elem_t *e = i->second; e; e = e->next)
94 && memcmp(e->data, data, size) == 0)
101 const size_t index = elems.size();
102 elems.push_back(lookup[h] = make_elem(lookup[h], index, size, data));
108 elem_t *e = elems[i];
109 data =
reinterpret_cast<data_t*
>(e->data);
110 return e->size /
sizeof(data_t);
115 #endif // _RE2C_UTIL_ORD_HASH_SET_
size_t insert(const void *data, size_t size)
size_t deref(size_t i, data_t *&data)