1 /*
2  * The contents of this file are subject to the Mozilla Public License
3  * Version 1.0 (the "License"); you may not use this file except in
4  * compliance with the License. You may obtain a copy of the License at
5  * http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9  * License for the specific language governing rights and limitations
10  * under the License.
11  *
12  * The Initial Developer of this code is David Baum.
13  * Portions created by David Baum are Copyright (C) 1998 David Baum.
14  * All Rights Reserved.
15  *
16  * Portions created by John Hansen are Copyright (C) 2005 John Hansen.
17  * All Rights Reserved.
18  *
19  */
20 
21 #ifndef __PHashTable_h
22 #define __PHashTable_h
23 
24 #ifndef __PListS_h
25 #include "PListS.h"
26 #endif
27 
28 #ifndef __PTypes_h
29 #include "PTypes.h"
30 #endif
31 
32 
33 class PHashable : public PLinkS<PHashable> {
34 public:
35 	virtual	~PHashable();
36 
37 	bool		MatchKey(const char *key);
GetKey()38 	const char*	GetKey() const { return fKey; }
39 
40 protected:
41 	char*	fKey;
42 };
43 
44 
45 class P_HashTable {
46 public:
47 				P_HashTable(int size);
48 				~P_HashTable();
49 
50 	bool		Remove(PHashable *item);
51 	void		Add(PHashable *item);
52 
53 	void		DeleteAll();
54 
55 	int			Hash(const char *key);
56 
GetBucketCount()57 	int			GetBucketCount() const	{ return fSize; }
58 
59 protected:
_GetBucket(int i)60 	PHashable*	_GetBucket(int i)	{ return fBuckets[i].GetHead(); }
61 	PHashable*	_Find(const char *key);
62 
63 private:
64 	int					fSize;
65 	PListSS<PHashable>*	fBuckets;
66 };
67 
68 
69 template <class T> class PHashTable : public P_HashTable
70 {
71 public:
PHashTable(int size)72 				PHashTable(int size) : P_HashTable(size) {}
73 
Find(const char * key)74 	T*			Find(const char *key)	{ return (T*) _Find(key); }
GetBucket(int i)75 	T*			GetBucket(int i)		{ return (T*) _GetBucket(i); }
76 };
77 
78 
79 #endif
80