Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HashKey.h
1 /*
2  * $Id$
3  * Copyright (c) 2006 - IRIT-UPS <casse@irit.fr>
4  *
5  * HashKey class interface.
6  */
7 #ifndef ELM_UTIL_HASH_KEY_H
8 #define ELM_UTIL_HASH_KEY_H
9 
10 #include <elm/types.h>
11 #include <elm/string.h>
12 #include <elm/util/Option.h>
13 
14 namespace elm {
15 
16 namespace t { typedef t::intptr hash; }
17 
18 // Useful hash functions
19 t::hash hash_string(const char *chars, int length);
20 t::hash hash_cstring(const char *chars);
21 t::hash hash_jenkins(const void *block, int size);
22 bool hash_equals(const void *p1, const void *p2, int size);
23 
24 // HashKey class
25 template <class T> class HashKey {
26 public:
27  static t::hash hash(const T& key) { return hash_jenkins(&key, sizeof(T)); };
28  static inline bool equals(const T& key1, const T& key2) { return &key1 == &key2 || hash_equals(&key1, &key2, sizeof(T)); }
29 };
30 
31 template <class T> class HashKey<const T&> {
32 public:
33  static t::hash hash(const T& key) { return hash_jenkins(&key, sizeof(T)); };
34  static inline bool equals(const T& key1, const T& key2) { return &key1 == &key2 || hash_equals(&key1, &key2, sizeof(T)); }
35 };
36 
37 template <class T> class HashKey<T&> {
38 public:
39  static t::hash hash(const T& key) { return hash_jenkins(&key, sizeof(T)); };
40  static inline bool equals(const T& key1, const T& key2) { return &key1 == &key2 || hash_equals(&key1, &key2, sizeof(T)); }
41 };
42 
43 
44 // Predefined hash keys
45 template <> class HashKey<int> {
46 public:
47  static inline t::hash hash(int key) { return (unsigned long)key; }
48  static inline bool equals(int key1, int key2) { return key1 == key2; }
49 };
50 
51 template <> class HashKey<void *> {
52 public:
53  static inline t::hash hash(void *key) { return t::hash(key); }
54  static inline bool equals(void *key1, void *key2) { return key1 == key2; }
55 };
56 
57 template <> class HashKey<const void *> {
58 public:
59  static inline t::hash hash(const void *key) { return t::hash(key); }
60  static inline bool equals(const void *key1, const void *key2)
61  { return key1 == key2; }
62 };
63 
64 template <> class HashKey<CString> {
65 public:
66  static t::hash hash(CString key)
67  { return hash_cstring(&key); }
68  static inline bool equals(CString key1, CString key2)
69  { return key1 == key2; }
70 };
71 
72 template <> class HashKey<String> {
73 public:
74  static t::hash hash(const String& key)
75  { return hash_string(key.chars(), key.length()); };
76  static inline bool equals(const String& key1, const String& key2)
77  { return key1 == key2; };
78 };
79 
80 }; // elm
81 
82 #endif // ELM_UTIL_HASH_KEY_H
Definition: CString.h:17
static bool equals(const T &key1, const T &key2)
Definition: HashKey.h:40
static t::hash hash(int key)
Definition: HashKey.h:47
static bool equals(const T &key1, const T &key2)
Definition: HashKey.h:34
uint32 size
Definition: int.h:41
static bool equals(const T &key1, const T &key2)
Definition: HashKey.h:28
Definition: HashKey.h:25
static t::hash hash(CString key)
Definition: HashKey.h:66
bool hash_equals(const void *p1, const void *p2, int size)
Definition: util_HashKey.cpp:100
const char * chars(void) const
Definition: String.h:74
static bool equals(const String &key1, const String &key2)
Definition: HashKey.h:76
static t::hash hash(const T &key)
Definition: HashKey.h:39
static t::hash hash(const T &key)
Definition: HashKey.h:27
static bool equals(void *key1, void *key2)
Definition: HashKey.h:54
int length(void) const
Definition: String.h:73
Definition: String.h:31
static t::hash hash(const T &key)
Definition: HashKey.h:33
t::intptr hash
Definition: HashKey.h:16
t::hash hash_jenkins(const void *block, int size)
Definition: util_HashKey.cpp:44
static t::hash hash(const String &key)
Definition: HashKey.h:74
t::hash hash_string(const char *chars, int length)
Definition: util_HashKey.cpp:63
static t::hash hash(void *key)
Definition: HashKey.h:53
static bool equals(CString key1, CString key2)
Definition: HashKey.h:68
uint32 intptr
Definition: int.h:44
static bool equals(int key1, int key2)
Definition: HashKey.h:48
static bool equals(const void *key1, const void *key2)
Definition: HashKey.h:60
static t::hash hash(const void *key)
Definition: HashKey.h:59
t::hash hash_cstring(const char *chars)
Definition: util_HashKey.cpp:80