summaryrefslogtreecommitdiff
path: root/Build/source/libs/graphite/src/generic/HashMap.h
blob: dc2de9d0af731f14df2797e352734908f1200a17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 2001 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: HashMap.h
Responsibility: Steve McConnel
Last reviewed: Not yet.

Description:
	This provides a set of template collection classes to replace std::map.  Their primary
	reason to exist is to allow explicit checking for internal memory allocation failures.
----------------------------------------------------------------------------------------------*/
#pragma once
#ifndef HASHMAP_H_INCLUDED
#define HASHMAP_H_INCLUDED
//:End Ignore

/*----------------------------------------------------------------------------------------------
	Functor class for computing a hash value from an arbitrary object.

	Hungarian: hsho
----------------------------------------------------------------------------------------------*/
class HashObj
{
public:
	int operator () (void * pKey, int cbKey);
};

/*----------------------------------------------------------------------------------------------
	Functor class for comparing two arbitrary objects (of the same class) for equality.

	Hungarian: eqlo
----------------------------------------------------------------------------------------------*/
class EqlObj
{
public:
	bool operator () (void * pKey1, void * pKey2, int cbKey);
};

/*----------------------------------------------------------------------------------------------
	Hash map template collection class whose keys are objects of an arbitrary class.

	Hungarian: hm[K][T]
----------------------------------------------------------------------------------------------*/
template<class K, class T, class H = HashObj, class Eq = EqlObj> class HashMap
{
public:
	//:> Member classes

	/*------------------------------------------------------------------------------------------
		This is the basic data structure for storing one key-value pair in a hash map.  In
		order to handle hash collisions, this structure is a member of a linked list.
		Hungarian: hsnd
	------------------------------------------------------------------------------------------*/
	class HashNode
	{
	public:
		//:> Constructors/destructors/etc.

		HashNode(void)
			: m_key(K()), m_value(T()), m_nHash(0), m_ihsndNext(0)
		{
		}
		HashNode(K & key, T & value, int nHash, int ihsndNext = -1)
			: m_key(key), m_value(value), m_nHash(nHash), m_ihsndNext(ihsndNext)
		{
		}
		~HashNode()
		{
		}

		//:> Member variable access

		void PutKey(K & key)
		{
			m_key = key;
		}
		K & GetKey()
		{
			return m_key;
		}
		void PutValue(T & value)
		{
			m_value = value;
		}
		T & GetValue()
		{
			return m_value;
		}
		void PutHash(int nHash)
		{
			m_nHash = nHash;
		}
		int GetHash()
		{
			return m_nHash;
		}
		void PutNext(int ihsndNext)
		{
			m_ihsndNext = ihsndNext;
		}
		int GetNext()
		{
			return m_ihsndNext;
		}

		/*--------------------------------------------------------------------------------------
			Check whether the given HashNode is being used.
		--------------------------------------------------------------------------------------*/
		bool InUse()
		{
			return m_ihsndNext >= -1;
		}

	protected:
		//:> Member variables

		K m_key;
		T m_value;
		int	m_nHash;
		int	m_ihsndNext;	// -1 means end of list, -(ihsnd + 3) for free list members
	};

	/*------------------------------------------------------------------------------------------
		This provides an iterator for stepping through all HashNodes stored in the hash map.
		This is useful primarily for saving the contents of a hash map to a file.

		Hungarian: ithm[K][T]
	------------------------------------------------------------------------------------------*/
	class iterator
	{
	public:
		// Constructors/destructors/etc.

		iterator() : m_phmParent(NULL), m_ihsnd(0)
		{
		}
		iterator(HashMap<K,T,H,Eq> * phm, int ihsnd) : m_phmParent(phm), m_ihsnd(ihsnd)
		{
		}
		iterator(const iterator & v) : m_phmParent(v.m_phmParent), m_ihsnd(v.m_ihsnd)
		{
		}
		~iterator()
		{
		}

		// Other public methods

		iterator & operator = (const iterator & ithm)
		{
			m_phmParent = ithm.m_phmParent;
			m_ihsnd = ithm.m_ihsnd;
			return *this;
		}
		T & operator * (void)
		{
			Assert(m_phmParent);
			Assert(m_phmParent->m_prghsnd);
			Assert(m_ihsnd < m_phmParent->m_ihsndLim);
			return m_phmParent->m_prghsnd[m_ihsnd].GetValue();
		}
		HashNode * operator -> (void)
		{
			Assert(m_phmParent);
			Assert(m_phmParent->m_prghsnd);
			Assert(m_ihsnd < m_phmParent->m_ihsndLim);
			return &m_phmParent->m_prghsnd[m_ihsnd];
		}
		iterator & operator ++ (void)
		{
			Assert(m_phmParent);
			++m_ihsnd;
			// make sure that this new HashNode is actually in use
			while (m_ihsnd < m_phmParent->m_ihsndLim)
			{
				if (m_phmParent->m_prghsnd[m_ihsnd].InUse())
					return *this;
				// skip to the next one and check it
				++m_ihsnd;
			}
			if (m_ihsnd > m_phmParent->m_ihsndLim)
				m_ihsnd = m_phmParent->m_ihsndLim;
			return *this;
		}
		bool operator == (const iterator & ithm)
		{
			return (m_phmParent == ithm.m_phmParent) && (m_ihsnd == ithm.m_ihsnd);
		}
		bool operator != (const iterator & ithm)
		{
			return (m_phmParent != ithm.m_phmParent) || (m_ihsnd != ithm.m_ihsnd);
		}
		T & GetValue(void)
		{
			Assert(m_phmParent);
			Assert(m_phmParent->m_prghsnd);
			Assert(m_ihsnd < m_phmParent->m_ihsndLim);
			Assert(m_phmParent->m_prghsnd[m_ihsnd].InUse());
			return m_phmParent->m_prghsnd[m_ihsnd].GetValue();
		}
		K & GetKey(void)
		{
			Assert(m_phmParent);
			Assert(m_phmParent->m_prghsnd);
			Assert(m_ihsnd < m_phmParent->m_ihsndLim);
			Assert(m_phmParent->m_prghsnd[m_ihsnd].InUse());
			return m_phmParent->m_prghsnd[m_ihsnd].GetKey();
		}
		int GetHash()
		{
			Assert(m_phmParent);
			Assert(m_phmParent->m_prghsnd);
			Assert(m_ihsnd < m_phmParent->m_ihsndLim);
			Assert(m_phmParent->m_prghsnd[m_ihsnd].InUse());
			return m_phmParent->m_prghsnd[m_ihsnd].GetHash();
		}
		int GetIndex()
		{
			Assert(m_phmParent);
			Assert(m_phmParent->m_prghsnd);
			Assert(m_ihsnd < m_phmParent->m_ihsndLim);
			Assert(m_phmParent->m_prghsnd[m_ihsnd].InUse());
			return m_ihsnd;
		}

	protected:
		//:> Member variables

		HashMap<K,T,H,Eq> * m_phmParent;
		int m_ihsnd;
	};
	friend class iterator;

	//:> Constructors/destructors/etc.

	HashMap();
	~HashMap();
	HashMap(HashMap<K,T,H,Eq> & hm);

	//:> Other public methods

	iterator Begin();
	iterator End();
	void Insert(K & key, T & value, bool fOverwrite = false, int * pihsndOut = NULL);
	bool Retrieve(K & key, T * pvalueRet);
	bool Delete(K & key);
	void Clear();
	void CopyTo(HashMap<K,T,H,Eq> & hmKT);
	void CopyTo(HashMap<K,T,H,Eq> * phmKT);

	bool GetIndex(K & key, int * pihsndRet);
	bool IndexKey(int ihsnd, K * pkeyRet);
	bool IndexValue(int ihsnd, T * pvalueRet);

	int Size();

	/*------------------------------------------------------------------------------------------
		The assignment operator allows an entire hashmap to be assigned as the value of another
		hashmap.  It throws an error if it runs out of memory.

		@return a reference to this hashmap.  (That is how the assignment operator is defined!)

		@param hm is a reference to the other hashmap.
	------------------------------------------------------------------------------------------*/
	HashMap<K,T,H,Eq> & operator = (HashMap<K,T,H,Eq> & hm)
	{
		hm.CopyTo(this);
		return *this;
	}

	//:Ignore
#ifdef DEBUG
	int _BucketCount();
	int _EmptyBuckets();
	int _BucketsUsed();
	int _FullestBucket();
	bool AssertValid()
	{
		AssertPtrN(m_prgihsndBuckets);
		Assert(m_prgihsndBuckets || !m_cBuckets);
		Assert(!m_prgihsndBuckets || m_cBuckets);
		AssertArray(m_prgihsndBuckets, m_cBuckets);
		AssertPtrN(m_prghsnd);
		Assert(m_prghsnd || !m_ihsndMax);
		Assert(!m_prghsnd || m_ihsndMax);
		AssertArray(m_prghsnd, m_ihsndMax);
		Assert(0 <= m_ihsndLim && m_ihsndLim <= m_ihsndMax);
		Assert(-1 <= FreeListIdx(m_ihsndFirstFree));
		Assert(FreeListIdx(m_ihsndFirstFree) < m_ihsndLim);
		return true;
	}
#endif
	//:End Ignore

protected:
	//:> Member variables

	int * m_prgihsndBuckets;
	int m_cBuckets;
	HashNode * m_prghsnd;
	int m_ihsndLim;
	int m_ihsndMax;
	int m_ihsndFirstFree;		// stores -(ihsnd + 3)

	//:> Protected methods
	//:Ignore

	/*------------------------------------------------------------------------------------------
		Map between real index and "free list" index.  Note that this mapping is bidirectional.
	------------------------------------------------------------------------------------------*/
	int FreeListIdx(int ihsnd)
	{
		return -(ihsnd + 3);
	}
	//:End Ignore
};


// Local Variables:
// mode:C++
// c-file-style:"cellar"
// tab-width:4
// End:

#endif /*HASHMAP_H_INCLUDED*/