Ninja
Main Page
Namespaces
Classes
Files
File List
File Members
hash_map.h
Go to the documentation of this file.
1
// Copyright 2011 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef NINJA_MAP_H_
16
#define NINJA_MAP_H_
17
18
#include "
string_piece.h
"
19
20
// MurmurHash2, by Austin Appleby
21
static
inline
22
unsigned
int
MurmurHash2
(
const
void
* key,
size_t
len) {
23
static
const
unsigned
int
seed = 0xDECAFBAD;
24
const
unsigned
int
m = 0x5bd1e995;
25
const
int
r = 24;
26
unsigned
int
h = seed ^ len;
27
const
unsigned
char
* data = (
const
unsigned
char
*)key;
28
while
(len >= 4) {
29
unsigned
int
k = *(
unsigned
int
*)data;
30
k *= m;
31
k ^= k >> r;
32
k *= m;
33
h *= m;
34
h ^= k;
35
data += 4;
36
len -= 4;
37
}
38
switch
(len) {
39
case
3: h ^= data[2] << 16;
40
case
2: h ^= data[1] << 8;
41
case
1: h ^= data[0];
42
h *= m;
43
};
44
h ^= h >> 13;
45
h *= m;
46
h ^= h >> 15;
47
return
h;
48
}
49
50
#ifdef _MSC_VER
51
#include <hash_map>
52
53
using
stdext::hash_map;
54
using
stdext::hash_compare;
55
56
struct
StringPieceCmp :
public
hash_compare<StringPiece> {
57
size_t
operator()(
const
StringPiece
& key)
const
{
58
return
MurmurHash2
(key.
str_
, key.
len_
);
59
}
60
bool
operator()(
const
StringPiece
& a,
const
StringPiece
& b)
const
{
61
int
cmp = strncmp(a.
str_
, b.
str_
, min(a.
len_
, b.
len_
));
62
if
(cmp < 0) {
63
return
true
;
64
}
else
if
(cmp > 0) {
65
return
false
;
66
}
else
{
67
return
a.
len_
< b.
len_
;
68
}
69
}
70
};
71
72
#else
73
74
#include <ext/hash_map>
75
76
using
__gnu_cxx::hash_map;
77
78
namespace
__gnu_cxx {
79
template
<>
80
struct
hash<std::string> {
81
size_t
operator()
(
const
std::string& s)
const
{
82
return
hash<const char*>()(s.c_str());
83
}
84
};
85
86
template
<>
87
struct
hash<
StringPiece
> {
88
size_t
operator()
(
StringPiece
key)
const
{
89
return
MurmurHash2
(key.
str_
, key.
len_
);
90
}
91
};
92
93
}
94
#endif
95
96
/// A template for hash_maps keyed by a StringPiece whose string is
97
/// owned externally (typically by the values). Use like:
98
/// ExternalStringHash<Foo*>::Type foos; to make foos into a hash
99
/// mapping StringPiece => Foo*.
100
template
<
typename
V>
101
struct
ExternalStringHashMap
{
102
#ifdef _MSC_VER
103
typedef
hash_map<StringPiece, V, StringPieceCmp>
Type
;
104
#else
105
typedef
hash_map<StringPiece, V>
Type
;
106
#endif
107
};
108
109
#endif // NINJA_MAP_H_
StringPiece::str_
const char * str_
Definition:
string_piece.h:49
StringPiece
StringPiece represents a slice of a string whose memory is managed externally.
Definition:
string_piece.h:27
ExternalStringHashMap
A template for hash_maps keyed by a StringPiece whose string is owned externally (typically by the va...
Definition:
hash_map.h:101
__gnu_cxx::hash< std::string >::operator()
size_t operator()(const std::string &s) const
Definition:
hash_map.h:81
__gnu_cxx::hash< StringPiece >::operator()
size_t operator()(StringPiece key) const
Definition:
hash_map.h:88
MurmurHash2
static unsigned int MurmurHash2(const void *key, size_t len)
Definition:
hash_map.h:22
ExternalStringHashMap::Type
hash_map< StringPiece, V > Type
Definition:
hash_map.h:105
StringPiece::len_
size_t len_
Definition:
string_piece.h:50
string_piece.h
Generated on Sat May 31 2014 08:04:22 for Ninja by
1.8.7