diff --git a/include/mapnik/unicode.hpp b/include/mapnik/unicode.hpp index 092efb2e5..4522c3b8e 100644 --- a/include/mapnik/unicode.hpp +++ b/include/mapnik/unicode.hpp @@ -50,9 +50,12 @@ private: } namespace U_ICU_NAMESPACE { -inline std::size_t hash_value(mapnik::value_unicode_string const& val) { + +inline std::size_t hash_value(mapnik::value_unicode_string const& val) +{ return val.hashCode(); } + } #endif // MAPNIK_UNICODE_HPP diff --git a/include/mapnik/value.hpp b/include/mapnik/value.hpp index 84470173e..647d0a295 100644 --- a/include/mapnik/value.hpp +++ b/include/mapnik/value.hpp @@ -25,6 +25,7 @@ // mapnik #include +#include #include #include #include @@ -980,6 +981,12 @@ inline bool value::is_null() const return util::apply_visitor(mapnik::detail::is_null_visitor(), base_); } +// hash function +inline std::size_t hash_value(value const& val) +{ + return hash_value(val.base()); +} + } // namespace mapnik #endif // MAPNIK_VALUE_HPP diff --git a/include/mapnik/value_hash.hpp b/include/mapnik/value_hash.hpp new file mode 100644 index 000000000..1137d9686 --- /dev/null +++ b/include/mapnik/value_hash.hpp @@ -0,0 +1,74 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2014 Artem Pavlenko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ + +#ifndef MAPNIK_VALUE_HASH_HPP +#define MAPNIK_VALUE_HASH_HPP + +// mapnik +#include +#include +#include +// stl +#include + +namespace mapnik { namespace detail { + +template +inline void hash_combine(std::size_t & seed, T const& v) +{ + std::hash hasher; + seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); +} + +struct value_hasher: public util::static_visitor +{ + std::size_t operator() (value_null val) const + { + return hash_value(val); + } + + std::size_t operator() (value_unicode_string const& val) const + { + return hash_value(val); + } + + template + std::size_t operator()(T const& val) const + { + std::hash hasher; + return hasher(val); + } +}; + +} // namespace detail + +template +std::size_t hash_value(T const& val) +{ + std::size_t seed = util::apply_visitor(detail::value_hasher(), val); + detail::hash_combine(seed, val.get_type_index()); + return seed; +} + +} // namespace mapnik + +#endif // MAPNIK_VALUE_HASH_HPP diff --git a/include/mapnik/value_types.hpp b/include/mapnik/value_types.hpp index 9b65b4c7a..58628ce0b 100644 --- a/include/mapnik/value_types.hpp +++ b/include/mapnik/value_types.hpp @@ -112,7 +112,8 @@ inline std::size_t hash_value(value_null const&) } template -inline std::basic_ostream& operator<<(std::basic_ostream& out, value_null const& v) { +inline std::basic_ostream& operator<<(std::basic_ostream& out, value_null const& v) +{ return out; }