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

Unified Diff: compiled/StringMap.h

Issue 29676720: Noissue - Extract the stringHash to allow using in std::unordered_map<> (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Jan. 22, 2018, 4:31 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 | « no previous file | no next file » | 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
@@ -17,16 +17,35 @@
#pragma once
#include <cstddef>
#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;
+ }
+}
+
+struct StringHash
+{
+ 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 Key key_type;
typedef const String& key_type_cref;
@@ -57,21 +76,17 @@
void erase()
{
first.erase();
}
static size_type hash(key_type_cref 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 Key, typename Value>
struct StringMapEntry : StringSetEntry<Key>
{
typedef StringSetEntry<Key> super;
typedef Value value_type;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld