Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: compiled/StringMap.h

Issue 29587914: Issue 5142 - Convert Element Hiding to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Added OwnedStringMap to address ownership of keys Created Nov. 25, 2017, 3:22 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « compiled/String.h ('k') | compiled/bindings/main.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiled/StringMap.h
===================================================================
--- a/compiled/StringMap.h
+++ b/compiled/StringMap.h
@@ -13,30 +13,53 @@
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstddef>
+#include <type_traits>
#include "Map.h"
#include "String.h"
+namespace {
+ size_t stringHash(const String& key)
+ {
+ // FNV-1a hash function
+ size_t result = 2166136261;
+ for (size_t i = 0; i < key.length(); i++)
+ result = (result ^ key[i]) * 16777619;
+ return result;
+ }
+}
+
+class StringHash
+{
+public:
+ size_t operator()(const String& key) const
+ {
+ return stringHash(key);
+ }
+};
+
namespace StringMap_internal
{
+ template<typename Key,
+ class = typename std::enable_if<std::is_base_of<String, Key>::value>::type>
struct StringSetEntry
{
typedef String key_type;
typedef size_t size_type;
- DependentString first;
+ Key first;
- StringSetEntry(const key_type& key = DependentString())
+ StringSetEntry(const key_type& key = Key())
{
if (!key.is_invalid())
first.reset(key);
}
bool equals(const key_type& other) const
{
return first.equals(other);
@@ -54,42 +77,40 @@
void erase()
{
first.erase();
}
static size_type hash(const key_type& key)
{
- // FNV-1a hash function
- size_type result = 2166136261;
- for (String::size_type i = 0; i < key.length(); i++)
- result = (result ^ key[i]) * 16777619;
- return result;
+ return stringHash(key);
}
};
- template<typename Value>
- struct StringMapEntry : StringSetEntry
+ template<typename Key, typename Value>
+ struct StringMapEntry : StringSetEntry<Key>
{
- typedef StringSetEntry super;
+ typedef StringSetEntry<Key> super;
typedef Value value_type;
Value second;
- StringMapEntry(const key_type& key = DependentString(),
+ StringMapEntry(const typename super::key_type& key = Key(),
value_type value = value_type())
: super(key), second(value)
{
}
void erase()
{
super::erase();
second = value_type();
}
};
}
-using StringSet = Set<StringMap_internal::StringSetEntry>;
+using StringSet = Set<StringMap_internal::StringSetEntry<DependentString>>;
template<typename Value>
-using StringMap = Map<StringMap_internal::StringMapEntry<Value>>;
+using StringMap = Map<StringMap_internal::StringMapEntry<DependentString, Value>>;
+template<typename Value>
+using OwnedStringMap = Map<StringMap_internal::StringMapEntry<OwnedString, Value>>;
« no previous file with comments | « compiled/String.h ('k') | compiled/bindings/main.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld