1/*
2 * Copyright 2010, Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3 * Copyright 2004-2007, Ingo Weinhold <ingo_weinhold@gmx.de>. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
6#ifndef HASH_KEYS_H
7#define HASH_KEYS_H
8
9#include <String.h>
10
11
12namespace BPrivate {
13
14#if 0
15// TODO: Move here from HashMap.h and adapt all clients.
16// HashKey32
17template<typename Value>
18struct HashKey32 {
19	HashKey32() {}
20	HashKey32(const Value& value) : value(value) {}
21
22	uint32 GetHashCode() const
23	{
24		return (uint32)value;
25	}
26
27	HashKey32<Value> operator=(const HashKey32<Value>& other)
28	{
29		value = other.value;
30		return *this;
31	}
32
33	bool operator==(const HashKey32<Value>& other) const
34	{
35		return (value == other.value);
36	}
37
38	bool operator!=(const HashKey32<Value>& other) const
39	{
40		return (value != other.value);
41	}
42
43	Value	value;
44};
45
46
47// HashKey64
48template<typename Value>
49struct HashKey64 {
50	HashKey64() {}
51	HashKey64(const Value& value) : value(value) {}
52
53	uint32 GetHashCode() const
54	{
55		uint64 v = (uint64)value;
56		return (uint32)(v >> 32) ^ (uint32)v;
57	}
58
59	HashKey64<Value> operator=(const HashKey64<Value>& other)
60	{
61		value = other.value;
62		return *this;
63	}
64
65	bool operator==(const HashKey64<Value>& other) const
66	{
67		return (value == other.value);
68	}
69
70	bool operator!=(const HashKey64<Value>& other) const
71	{
72		return (value != other.value);
73	}
74
75	Value	value;
76};
77#endif
78
79
80struct HashKeyString {
81	HashKeyString() {}
82	HashKeyString(const BString& value) : value(value) {}
83	HashKeyString(const char* string) : value(string) {}
84
85	uint32 GetHashCode() const
86	{
87		// from the Dragon Book: a slightly modified hashpjw()
88		uint32 hash = 0;
89		const char* string = value.String();
90		if (string != NULL) {
91			for (; *string; string++) {
92				uint32 g = hash & 0xf0000000;
93				if (g != 0)
94					hash ^= g >> 24;
95				hash = (hash << 4) + *string;
96			}
97		}
98		return hash;
99	}
100
101	HashKeyString operator=(const HashKeyString& other)
102	{
103		value = other.value;
104		return *this;
105	}
106
107	bool operator==(const HashKeyString& other) const
108	{
109		return (value == other.value);
110	}
111
112	bool operator!=(const HashKeyString& other) const
113	{
114		return (value != other.value);
115	}
116
117	BString	value;
118};
119
120}	// namespace BPrivate
121
122using BPrivate::HashKeyString;
123
124#endif	// HASH_KEYS_H
125