From bbfef3151dab8f42c890d9a04f8f1b96c426ee36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sat, 21 Jan 2017 13:20:07 +0100 Subject: [PATCH] Use hash of string as identifier --- include/cppast/cpp_entity_index.hpp | 45 +++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/include/cppast/cpp_entity_index.hpp b/include/cppast/cpp_entity_index.hpp index 0cd3311..59cb361 100644 --- a/include/cppast/cpp_entity_index.hpp +++ b/include/cppast/cpp_entity_index.hpp @@ -5,7 +5,7 @@ #ifndef CPPAST_CPP_ENTITY_INDEX_HPP_INCLUDED #define CPPAST_CPP_ENTITY_INDEX_HPP_INCLUDED -#include +#include #include #include @@ -19,22 +19,40 @@ namespace cppast { class cpp_entity; + /// \exclude + namespace detail + { + constexpr std::size_t fnv_basis = 14695981039346656037ull; + constexpr std::size_t fnv_prime = 1099511628211ull; + + // FNV-1a 64 bit hash + constexpr std::size_t id_hash(const char* str, std::size_t hash = fnv_basis) + { + return *str ? id_hash(str + 1, (hash ^ *str) * fnv_prime) : hash; + } + } // namespace detail + /// A [ts::strong_typedef]() representing the unique id of a [cppast::cpp_entity](). /// - /// It is fully comparable. - struct cpp_entity_id : type_safe::strong_typedef, - type_safe::strong_typedef_op::equality_comparison, - type_safe::strong_typedef_op::relational_comparison + /// It is comparable for equality. + struct cpp_entity_id : type_safe::strong_typedef, + type_safe::strong_typedef_op::equality_comparison { - using strong_typedef::strong_typedef; + explicit cpp_entity_id(const std::string& str) : cpp_entity_id(str.c_str()) + { + } + + explicit cpp_entity_id(const char* str) : strong_typedef(detail::id_hash(str)) + { + } }; inline namespace literals { /// \returns A new [cppast::cpp_entity_id]() created from the given string. - inline cpp_entity_id operator""_id(const char* str, std::size_t size) + inline cpp_entity_id operator""_id(const char* str, std::size_t) { - return cpp_entity_id(std::string(str, size)); + return cpp_entity_id(str); } } @@ -68,8 +86,17 @@ namespace cppast } private: + struct hash + { + std::size_t operator()(const cpp_entity_id& id) const noexcept + { + return static_cast(id); + } + }; + mutable std::mutex mutex_; - mutable std::map> map_; + mutable std::unordered_map, hash> + map_; }; } // namespace cppast