diff options
Diffstat (limited to '')
-rw-r--r-- | linden/indra/llcommon/llsimplehash.h | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llsimplehash.h b/linden/indra/llcommon/llsimplehash.h new file mode 100644 index 0000000..259f140 --- /dev/null +++ b/linden/indra/llcommon/llsimplehash.h | |||
@@ -0,0 +1,156 @@ | |||
1 | /** | ||
2 | * @file llsimplehash.h | ||
3 | * | ||
4 | * Copyright (c) 2003-2007, Linden Research, Inc. | ||
5 | * | ||
6 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
7 | * to you under the terms of the GNU General Public License, version 2.0 | ||
8 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
9 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
10 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
11 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
12 | * | ||
13 | * There are special exceptions to the terms and conditions of the GPL as | ||
14 | * it is applied to this Source Code. View the full text of the exception | ||
15 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
16 | * online at http://secondlife.com/developers/opensource/flossexception | ||
17 | * | ||
18 | * By copying, modifying or distributing this software, you acknowledge | ||
19 | * that you have read and understood your obligations described above, | ||
20 | * and agree to abide by those obligations. | ||
21 | * | ||
22 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
23 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
24 | * COMPLETENESS OR PERFORMANCE. | ||
25 | */ | ||
26 | |||
27 | #ifndef LL_LLSIMPLEHASH_H | ||
28 | #define LL_LLSIMPLEHASH_H | ||
29 | |||
30 | #include "llstl.h" | ||
31 | |||
32 | template <typename HASH_KEY_TYPE> | ||
33 | class LLSimpleHashEntry | ||
34 | { | ||
35 | protected: | ||
36 | HASH_KEY_TYPE mHashKey; | ||
37 | LLSimpleHashEntry<HASH_KEY_TYPE>* mNextEntry; | ||
38 | public: | ||
39 | LLSimpleHashEntry(HASH_KEY_TYPE key) : | ||
40 | mHashKey(key), | ||
41 | mNextEntry(0) | ||
42 | { | ||
43 | } | ||
44 | virtual ~LLSimpleHashEntry() | ||
45 | { | ||
46 | } | ||
47 | HASH_KEY_TYPE getHashKey() const | ||
48 | { | ||
49 | return mHashKey; | ||
50 | } | ||
51 | LLSimpleHashEntry<HASH_KEY_TYPE>* getNextEntry() const | ||
52 | { | ||
53 | return mNextEntry; | ||
54 | } | ||
55 | void setNextEntry(LLSimpleHashEntry<HASH_KEY_TYPE>* next) | ||
56 | { | ||
57 | mNextEntry = next; | ||
58 | } | ||
59 | }; | ||
60 | |||
61 | template <typename HASH_KEY_TYPE, int TABLE_SIZE> | ||
62 | class LLSimpleHash | ||
63 | { | ||
64 | public: | ||
65 | LLSimpleHash() | ||
66 | { | ||
67 | llassert(TABLE_SIZE); | ||
68 | llassert((TABLE_SIZE ^ (TABLE_SIZE-1)) == (TABLE_SIZE | (TABLE_SIZE-1))); // power of 2 | ||
69 | memset(mEntryTable, 0, sizeof(mEntryTable)); | ||
70 | } | ||
71 | virtual ~LLSimpleHash() | ||
72 | { | ||
73 | } | ||
74 | |||
75 | virtual int getIndex(HASH_KEY_TYPE key) | ||
76 | { | ||
77 | return key & (TABLE_SIZE-1); | ||
78 | } | ||
79 | |||
80 | bool insert(LLSimpleHashEntry<HASH_KEY_TYPE>* entry) | ||
81 | { | ||
82 | llassert(entry->getNextEntry() == 0); | ||
83 | int index = getIndex(entry->getHashKey()); | ||
84 | entry->setNextEntry(mEntryTable[index]); | ||
85 | mEntryTable[index] = entry; | ||
86 | return true; | ||
87 | } | ||
88 | LLSimpleHashEntry<HASH_KEY_TYPE>* find(HASH_KEY_TYPE key) | ||
89 | { | ||
90 | int index = getIndex(key); | ||
91 | LLSimpleHashEntry<HASH_KEY_TYPE>* res = mEntryTable[index]; | ||
92 | while(res && (res->getHashKey() != key)) | ||
93 | { | ||
94 | res = res->getNextEntry(); | ||
95 | } | ||
96 | return res; | ||
97 | } | ||
98 | bool erase(LLSimpleHashEntry<HASH_KEY_TYPE>* entry) | ||
99 | { | ||
100 | return erase(entry->getHashKey()); | ||
101 | } | ||
102 | bool erase(HASH_KEY_TYPE key) | ||
103 | { | ||
104 | int index = getIndex(key); | ||
105 | LLSimpleHashEntry<HASH_KEY_TYPE>* prev = 0; | ||
106 | LLSimpleHashEntry<HASH_KEY_TYPE>* res = mEntryTable[index]; | ||
107 | while(res && (res->getHashKey() != key)) | ||
108 | { | ||
109 | prev = res; | ||
110 | res = res->getNextEntry(); | ||
111 | } | ||
112 | if (res) | ||
113 | { | ||
114 | LLSimpleHashEntry<HASH_KEY_TYPE>* next = res->getNextEntry(); | ||
115 | if (prev) | ||
116 | { | ||
117 | prev->setNextEntry(next); | ||
118 | } | ||
119 | else | ||
120 | { | ||
121 | mEntryTable[index] = next; | ||
122 | } | ||
123 | return true; | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | return false; | ||
128 | } | ||
129 | } | ||
130 | // Removes and returns an arbitrary ("first") element from the table | ||
131 | // Used for deleting the entire table. | ||
132 | LLSimpleHashEntry<HASH_KEY_TYPE>* pop_element() | ||
133 | { | ||
134 | for (int i=0; i<TABLE_SIZE; i++) | ||
135 | { | ||
136 | LLSimpleHashEntry<HASH_KEY_TYPE>* entry = mEntryTable[i]; | ||
137 | if (entry) | ||
138 | { | ||
139 | mEntryTable[i] = entry->getNextEntry(); | ||
140 | return entry; | ||
141 | } | ||
142 | } | ||
143 | return 0; | ||
144 | } | ||
145 | // debugging | ||
146 | LLSimpleHashEntry<HASH_KEY_TYPE>* get_element_at_index(S32 index) const | ||
147 | { | ||
148 | return mEntryTable[index]; | ||
149 | } | ||
150 | |||
151 | |||
152 | private: | ||
153 | LLSimpleHashEntry<HASH_KEY_TYPE>* mEntryTable[TABLE_SIZE]; | ||
154 | }; | ||
155 | |||
156 | #endif // LL_LLSIMPLEHASH_H | ||