|
| 1 | +#ifndef Corrade_Containers_StringStlHash_h |
| 2 | +#define Corrade_Containers_StringStlHash_h |
| 3 | +/* |
| 4 | + This file is part of Corrade. |
| 5 | +
|
| 6 | + Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, |
| 7 | + 2017, 2018, 2019, 2020, 2021, 2022 |
| 8 | + Vladimír Vondruš <[email protected]> |
| 9 | +
|
| 10 | + Permission is hereby granted, free of charge, to any person obtaining a |
| 11 | + copy of this software and associated documentation files (the "Software"), |
| 12 | + to deal in the Software without restriction, including without limitation |
| 13 | + the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 | + and/or sell copies of the Software, and to permit persons to whom the |
| 15 | + Software is furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | + The above copyright notice and this permission notice shall be included |
| 18 | + in all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 26 | + DEALINGS IN THE SOFTWARE. |
| 27 | +*/ |
| 28 | + |
| 29 | +/** @file |
| 30 | +@brief STL @ref std::hash compatibility for @ref Corrade::Containers::String and @ref Corrade::Containers::BasicStringView "StringView" |
| 31 | +@m_since_latest |
| 32 | +
|
| 33 | +Including this header adds a @ref std::hash specialization for |
| 34 | +@ref Corrade::Containers::String / @ref Corrade::Containers::BasicStringView "StringView", |
| 35 | +making them usable in @ref std::unordered_map and @ref std::unordered_set. See |
| 36 | +@ref Containers-String-stl "String STL compatibility" and |
| 37 | +@ref Containers-BasicStringView-stl "StringView STL compatibility" for more |
| 38 | +information. |
| 39 | +*/ |
| 40 | + |
| 41 | +/* Alone, <functional> is relatively big (9kLOC on GCC 11 -std=c++11), but when |
| 42 | + combined with <unordered_map> (12k) it results in just 13k, so it's not like |
| 43 | + we'd gain anything by trying to find a forward declaration. On libc++ 11 |
| 44 | + it's 22k for <functional> (!!) and 36k (!!!) for <unordered_map> either |
| 45 | + alone or together with <functional>. So even though the numbers are QUITE |
| 46 | + HORRENDOUS, a forward declaration wouldn't help there either. On GCC 11 and |
| 47 | + -std=c++20 the size goes up to 30k as well. Haha. |
| 48 | +
|
| 49 | + Compared to these, I don't feel like bothering to optimize my side of the |
| 50 | + include chain, even though the MurmurHash2 include could potentially get |
| 51 | + deinlined. */ |
| 52 | +#include <functional> |
| 53 | + |
| 54 | +#include "Corrade/Containers/String.h" |
| 55 | +#include "Corrade/Containers/StringView.h" |
| 56 | +#include "Corrade/Utility/MurmurHash2.h" |
| 57 | + |
| 58 | +/* Listing these namespaces doesn't add anything to the docs, so don't */ |
| 59 | +#ifndef DOXYGEN_GENERATING_OUTPUT |
| 60 | +namespace std { |
| 61 | + |
| 62 | +template<> struct hash<Corrade::Containers::StringView> { |
| 63 | + std::size_t operator()(Corrade::Containers::StringView key) const { |
| 64 | + const Corrade::Utility::MurmurHash2 hash; |
| 65 | + const Corrade::Utility::HashDigest<sizeof(std::size_t)> digest = hash(key.data(), key.size()); |
| 66 | + return *reinterpret_cast<const std::size_t*>(digest.byteArray()); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +template<> struct hash<Corrade::Containers::MutableStringView>: hash<Corrade::Containers::StringView> {}; |
| 71 | + |
| 72 | +template<> struct hash<Corrade::Containers::String> { |
| 73 | + std::size_t operator()(const Corrade::Containers::String& key) const { |
| 74 | + const Corrade::Utility::MurmurHash2 hash; |
| 75 | + const Corrade::Utility::HashDigest<sizeof(std::size_t)> digest = hash(key.data(), key.size()); |
| 76 | + return *reinterpret_cast<const std::size_t*>(digest.byteArray()); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +} |
| 81 | +#endif |
| 82 | + |
| 83 | +#endif |
0 commit comments