Added hash and comparisons for std::string.
This commit is contained in:
parent
e4f420e693
commit
33278ed545
10
lib/lib.cpp
10
lib/lib.cpp
@ -72,3 +72,13 @@ alpha_to_int (char c)
|
||||
return - (c - 'A' + 1);
|
||||
}
|
||||
}
|
||||
|
||||
hash_t
|
||||
hash (const std::string &s)
|
||||
{
|
||||
size_t n = s.length ();
|
||||
hash_t h = hash (n);
|
||||
for (unsigned i = 0; i < n; i ++)
|
||||
h = hash_combine (h, hash (s[i]));
|
||||
return h;
|
||||
}
|
||||
|
17
lib/lib.h
17
lib/lib.h
@ -47,6 +47,23 @@ inline hash_t hash (unsigned long x) { return (hash_t)x; }
|
||||
|
||||
inline hash_t hash (uint64 x) { return (hash_t)((x >> 32) ^ x); }
|
||||
|
||||
hash_t hash (const std::string &s);
|
||||
|
||||
inline bool operator == (const std::string &s, const std::string &t)
|
||||
{
|
||||
return strcmp (s.c_str (), t.c_str ()) == 0;
|
||||
}
|
||||
|
||||
inline bool operator < (const std::string &s, const std::string &t)
|
||||
{
|
||||
return strcmp (s.c_str (), t.c_str ()) < 0;
|
||||
}
|
||||
|
||||
inline bool operator <= (const std::string &s, const std::string &t)
|
||||
{
|
||||
return strcmp (s.c_str (), t.c_str ()) <= 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline hash_t hash (const T &x)
|
||||
{
|
||||
|
20
main.cpp
20
main.cpp
@ -143,7 +143,25 @@ main ()
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
map<std::string, int> m;
|
||||
m.push ("foo", 3);
|
||||
m.push ("barz", 4);
|
||||
m.push ("pazazz", 39);
|
||||
|
||||
assert (m % "foo");
|
||||
assert (m("foo") == 3);
|
||||
assert (! (m % "fop"));
|
||||
|
||||
hashmap<std::string, int> m2;
|
||||
m2.push ("foo", 3);
|
||||
m2.push ("barz", 4);
|
||||
m2.push ("pazazz", 39);
|
||||
|
||||
assert (m2 % "foo");
|
||||
assert (m2("foo") == 3);
|
||||
assert (! (m2 % "fop"));
|
||||
|
||||
#if 0
|
||||
test_ring<Z2> (2);
|
||||
test_ring<Z> (0);
|
||||
test_ring<Q> (0);
|
||||
|
Loading…
Reference in New Issue
Block a user