aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/llstringtable.h
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/llstringtable.h
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llcommon/llstringtable.h')
-rw-r--r--linden/indra/llcommon/llstringtable.h227
1 files changed, 227 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llstringtable.h b/linden/indra/llcommon/llstringtable.h
new file mode 100644
index 0000000..d53c0e1
--- /dev/null
+++ b/linden/indra/llcommon/llstringtable.h
@@ -0,0 +1,227 @@
1/**
2 * @file llstringtable.h
3 * @brief The LLStringTable class provides a _fast_ method for finding
4 * unique copies of strings.
5 *
6 * Copyright (c) 2001-2007, Linden Research, Inc.
7 *
8 * The source code in this file ("Source Code") is provided by Linden Lab
9 * to you under the terms of the GNU General Public License, version 2.0
10 * ("GPL"), unless you have obtained a separate licensing agreement
11 * ("Other License"), formally executed by you and Linden Lab. Terms of
12 * the GPL can be found in doc/GPL-license.txt in this distribution, or
13 * online at http://secondlife.com/developers/opensource/gplv2
14 *
15 * There are special exceptions to the terms and conditions of the GPL as
16 * it is applied to this Source Code. View the full text of the exception
17 * in the file doc/FLOSS-exception.txt in this software distribution, or
18 * online at http://secondlife.com/developers/opensource/flossexception
19 *
20 * By copying, modifying or distributing this software, you acknowledge
21 * that you have read and understood your obligations described above,
22 * and agree to abide by those obligations.
23 *
24 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
25 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
26 * COMPLETENESS OR PERFORMANCE.
27 */
28
29#ifndef LL_STRING_TABLE_H
30#define LL_STRING_TABLE_H
31
32#include "llstl.h"
33#include <list>
34#include <set>
35
36#if LL_WINDOWS
37# if (_MSC_VER >= 1300)
38# define STRING_TABLE_HASH_MAP 1
39# pragma warning(disable : 4996)
40# endif
41#else
42//# define STRING_TABLE_HASH_MAP 1
43#endif
44
45#if STRING_TABLE_HASH_MAP
46#include <hash_map>
47#endif
48
49// string_table.h
50// LLStringTable class header file
51// Provides a _fast_ method for finding unique copies of strings
52//
53// Copyright 2001-2002, Linden Research, Inc.
54
55const U32 MAX_STRINGS_LENGTH = 256;
56
57class LLStringTableEntry
58{
59public:
60 LLStringTableEntry(const char *str)
61 : mString(NULL), mCount(1)
62 {
63 // Copy string
64 U32 length = (U32)strlen(str) + 1; /*Flawfinder: ignore*/
65 length = llmin(length, MAX_STRINGS_LENGTH);
66 mString = new char[length];
67 strncpy(mString, str, length); /*Flawfinder: ignore*/
68 mString[length - 1] = 0;
69 }
70 ~LLStringTableEntry()
71 {
72 delete [] mString;
73 mCount = 0;
74 }
75 void incCount() { mCount++; }
76 BOOL decCount() { return --mCount; }
77
78 char *mString;
79 S32 mCount;
80};
81
82class LLStringTable
83{
84public:
85 LLStringTable(int tablesize);
86 ~LLStringTable();
87
88 char *checkString(const char *str);
89 char *checkString(const std::string& str);
90 LLStringTableEntry *checkStringEntry(const char *str);
91 LLStringTableEntry *checkStringEntry(const std::string& str);
92
93 char *addString(const char *str);
94 char *addString(const std::string& str);
95 LLStringTableEntry *addStringEntry(const char *str);
96 LLStringTableEntry *addStringEntry(const std::string& str);
97 void removeString(const char *str);
98
99 S32 mMaxEntries;
100 S32 mUniqueEntries;
101
102#if STRING_TABLE_HASH_MAP
103 typedef std::hash_multimap<U32, LLStringTableEntry *> string_hash_t;
104 string_hash_t mStringHash;
105#else
106 typedef std::list<LLStringTableEntry *> string_list_t;
107 typedef string_list_t * string_list_ptr_t;
108 string_list_ptr_t *mStringList;
109#endif
110};
111
112extern LLStringTable gStringTable;
113
114//============================================================================
115
116// This class is designed to be used locally,
117// e.g. as a member of an LLXmlTree
118// Strings can be inserted only, then quickly looked up
119
120typedef const std::string* LLStdStringHandle;
121
122class LLStdStringTable
123{
124public:
125 LLStdStringTable(S32 tablesize = 0)
126 {
127 if (tablesize == 0)
128 {
129 tablesize = 256; // default
130 }
131 // Make sure tablesize is power of 2
132 for (S32 i = 31; i>0; i--)
133 {
134 if (tablesize & (1<<i))
135 {
136 if (tablesize >= (3<<(i-1)))
137 tablesize = (1<<(i+1));
138 else
139 tablesize = (1<<i);
140 break;
141 }
142 }
143 mTableSize = tablesize;
144 mStringList = new string_set_t[tablesize];
145 }
146 ~LLStdStringTable()
147 {
148 cleanup();
149 delete[] mStringList;
150 }
151 void cleanup()
152 {
153 // remove strings
154 for (S32 i = 0; i<mTableSize; i++)
155 {
156 string_set_t& stringset = mStringList[i];
157 for (string_set_t::iterator iter = stringset.begin(); iter != stringset.end(); iter++)
158 {
159 delete *iter;
160 }
161 stringset.clear();
162 }
163 }
164
165 LLStdStringHandle lookup(const std::string& s)
166 {
167 U32 hashval = makehash(s);
168 return lookup(hashval, s);
169 }
170
171 LLStdStringHandle checkString(const std::string& s)
172 {
173 U32 hashval = makehash(s);
174 return lookup(hashval, s);
175 }
176
177 LLStdStringHandle insert(const std::string& s)
178 {
179 U32 hashval = makehash(s);
180 LLStdStringHandle result = lookup(hashval, s);
181 if (result == NULL)
182 {
183 result = new std::string(s);
184 mStringList[hashval].insert(result);
185 }
186 return result;
187 }
188 LLStdStringHandle addString(const std::string& s)
189 {
190 return insert(s);
191 }
192
193private:
194 U32 makehash(const std::string& s)
195 {
196 S32 len = (S32)s.size();
197 const char* c = s.c_str();
198 U32 hashval = 0;
199 for (S32 i=0; i<len; i++)
200 {
201 hashval = ((hashval<<5) + hashval) + *c++;
202 }
203 return hashval & (mTableSize-1);
204 }
205 LLStdStringHandle lookup(U32 hashval, const std::string& s)
206 {
207 string_set_t& stringset = mStringList[hashval];
208 LLStdStringHandle handle = &s;
209 string_set_t::iterator iter = stringset.find(handle); // compares actual strings
210 if (iter != stringset.end())
211 {
212 return *iter;
213 }
214 else
215 {
216 return NULL;
217 }
218 }
219
220private:
221 S32 mTableSize;
222 typedef std::set<LLStdStringHandle, compare_pointer_contents<std::string> > string_set_t;
223 string_set_t* mStringList; // [mTableSize]
224};
225
226
227#endif