From afbd097ad8bc7f8433f6b55acbb6d8d75e61bb9f Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Mon, 14 Mar 2016 22:38:42 +0000 Subject: [PATCH] Moved function definitions out of header. --- re2c/Makefile.am | 1 + re2c/src/util/ord_hash_set.cc | 70 +++++++++++++++++++++++++++++++++++ re2c/src/util/ord_hash_set.h | 63 +------------------------------ 3 files changed, 72 insertions(+), 62 deletions(-) create mode 100644 re2c/src/util/ord_hash_set.cc diff --git a/re2c/Makefile.am b/re2c/Makefile.am index 71cfb4bc..1e73e3e3 100644 --- a/re2c/Makefile.am +++ b/re2c/Makefile.am @@ -110,6 +110,7 @@ SRC = \ src/parse/input.cc \ src/parse/scanner.cc \ src/parse/unescape.cc \ + src/util/ord_hash_set.cc \ src/util/s_to_n32_unsafe.cc \ src/util/range.cc re2c_SOURCES = \ diff --git a/re2c/src/util/ord_hash_set.cc b/re2c/src/util/ord_hash_set.cc new file mode 100644 index 00000000..58e30a5d --- /dev/null +++ b/re2c/src/util/ord_hash_set.cc @@ -0,0 +1,70 @@ +#include + +#include "src/util/ord_hash_set.h" + +namespace re2c +{ + +ord_hash_set_t::hash_t ord_hash_set_t::hash(const void *data, size_t size) +{ + const uint8_t *bytes = static_cast(data); + hash_t h = size; // seed + for (size_t i = 0; i < size; ++i) + { + h = h ^ ((h << 5) + (h >> 2) + bytes[i]); + } + return h; +} + +ord_hash_set_t::elem_t* ord_hash_set_t::make_elem( + elem_t *next, + size_t index, + size_t size, + const void *data) +{ + elem_t *e = static_cast(malloc(offsetof(elem_t, data) + size)); + e->next = next; + e->index = index; + e->size = size; + memcpy(e->data, data, size); + return e; +} + +ord_hash_set_t::ord_hash_set_t() + : elems() + , lookup() +{} + +ord_hash_set_t::~ord_hash_set_t() +{ + std::for_each(elems.begin(), elems.end(), free); +} + +size_t ord_hash_set_t::size() const +{ + return elems.size(); +} + +size_t ord_hash_set_t::insert(const void *data, size_t size) +{ + const hash_t h = hash(data, size); + + std::map::const_iterator i = lookup.find(h); + if (i != lookup.end()) + { + for (elem_t *e = i->second; e; e = e->next) + { + if (e->size == size + && memcmp(e->data, data, size) == 0) + { + return e->index; + } + } + } + + const size_t index = elems.size(); + elems.push_back(lookup[h] = make_elem(lookup[h], index, size, data)); + return index; +} + +} // namespace re2c diff --git a/re2c/src/util/ord_hash_set.h b/re2c/src/util/ord_hash_set.h index 9a0e73ea..afa1c69b 100644 --- a/re2c/src/util/ord_hash_set.h +++ b/re2c/src/util/ord_hash_set.h @@ -4,6 +4,7 @@ #include "src/util/c99_stdint.h" #include // malloc, free #include // memcpy +#include // for_each #include #include @@ -41,68 +42,6 @@ public: template size_t deref(size_t i, data_t *&data); }; -ord_hash_set_t::hash_t ord_hash_set_t::hash(const void *data, size_t size) -{ - const uint8_t *bytes = static_cast(data); - hash_t h = size; // seed - for (size_t i = 0; i < size; ++i) - { - h = h ^ ((h << 5) + (h >> 2) + bytes[i]); - } - return h; -} - -ord_hash_set_t::elem_t* ord_hash_set_t::make_elem( - elem_t *next, - size_t index, - size_t size, - const void *data) -{ - elem_t *e = static_cast(malloc(offsetof(elem_t, data) + size)); - e->next = next; - e->index = index; - e->size = size; - memcpy(e->data, data, size); - return e; -} - -ord_hash_set_t::ord_hash_set_t() - : elems() - , lookup() -{} - -ord_hash_set_t::~ord_hash_set_t() -{ - std::for_each(elems.begin(), elems.end(), free); -} - -size_t ord_hash_set_t::size() const -{ - return elems.size(); -} - -size_t ord_hash_set_t::insert(const void *data, size_t size) -{ - const hash_t h = hash(data, size); - - std::map::const_iterator i = lookup.find(h); - if (i != lookup.end()) - { - for (elem_t *e = i->second; e; e = e->next) - { - if (e->size == size - && memcmp(e->data, data, size) == 0) - { - return e->index; - } - } - } - - const size_t index = elems.size(); - elems.push_back(lookup[h] = make_elem(lookup[h], index, size, data)); - return index; -} - template size_t ord_hash_set_t::deref(size_t i, data_t *&data) { elem_t *e = elems[i]; -- 2.40.0