/
stringpool.cpp
514 lines (430 loc) · 12.7 KB
/
stringpool.cpp
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
* @file stringpool.cpp
*
* Pool of String (case insensitive). @ingroup data
*
* @author Copyright © 2010-2012 Daniel Swanson <danij@dengine.net>
* @author Copyright © 2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de/StringPool"
#include "de/Reader"
#include "de/Writer"
#include <vector>
#include <list>
#include <set>
#include <algorithm>
/// Macro used for converting internal ids to externally visible Ids.
#define EXPORT_ID(i) (uint(i) + 1)
#define IMPORT_ID(i) (Id((i) - 1))
namespace de {
static String const nullString = "(nullptr)";
typedef uint InternalId;
/**
* Case-insensitive text string (String).
*/
class CaselessString
{
public:
CaselessString(QString text)
: _str(text), _id(0), _userValue(0), _userPointer(0)
{}
CaselessString(CaselessString const& other)
: _str(other._str), _id(other._id), _userValue(other._userValue), _userPointer(0)
{}
void setText(String& text)
{
_str = text;
}
operator String const* () const {
return &_str;
}
operator String const& () const {
return _str;
}
bool operator < (CaselessString const& other) const {
return _str.compare(other, Qt::CaseInsensitive) < 0;
}
bool operator == (CaselessString const& other) const {
return !_str.compare(other, Qt::CaseInsensitive);
}
InternalId id() const {
return _id;
}
void setId(InternalId i) {
_id = i;
}
uint userValue() const {
return _userValue;
}
void setUserValue(uint value) {
_userValue = value;
}
void* userPointer() const {
return _userPointer;
}
void setUserPointer(void* ptr) {
_userPointer = ptr;
}
#if 0
void serialize(Writer* writer) const {
Str_Write(&_str, writer);
Writer_WritePackedUInt32(writer, _id);
Writer_WriteUInt32(writer, _userValue);
}
void deserialize(Reader* reader, ddstring_t* text) {
Str_Read(text, reader);
_str.str = 0; // caller must handle this; we don't take ownership
_id = Reader_ReadPackedUInt32(reader);
_userValue = Reader_ReadUInt32(reader);
}
#endif
private:
String _str;
InternalId _id; ///< The id that refers to this string.
uint _userValue;
void* _userPointer;
};
/**
* Utility class that acts as the value type for the std::set of interned
* strings. Only points to CaselessString instances. The less-than operator
* is needed by std::set to keep the strings ordered.
*/
class CaselessStringRef {
public:
CaselessStringRef(CaselessString const* s = 0) {
_str = s;
}
CaselessStringRef(CaselessStringRef const& other) {
_str = other._str;
}
CaselessString* toStr() const {
return const_cast<CaselessString*>(_str);
}
InternalId id() const {
DENG2_ASSERT(_str);
return _str->id();
}
bool operator < (CaselessStringRef const& other) const {
DENG2_ASSERT(_str);
DENG2_ASSERT(other._str);
return *_str < *other._str;
}
bool operator == (CaselessStringRef const& other) const {
DENG2_ASSERT(_str);
DENG2_ASSERT(other._str);
return *_str == *other._str;
}
private:
CaselessString const* _str;
};
typedef std::set<CaselessStringRef> Interns;
typedef std::vector<CaselessString*> IdMap;
typedef std::list<InternalId> AvailableIds;
struct StringPool::Instance
{
/// Interned strings (owns the CaselessString instances).
Interns interns;
/// InternId => CaselessString*. Only one id can refer to the each CaselessString*.
IdMap idMap;
/// Number of strings in the pool (must always be idMap.size() - available.size()).
size_t count;
/// List of currently unused ids in idMap.
AvailableIds available;
Instance() : count(0)
{}
~Instance()
{
// Free all allocated memory.
clear();
}
void clear()
{
for(uint i = 0; i < idMap.size(); ++i)
{
if(!idMap[i]) continue; // Unused slot.
delete idMap[i];
}
count = 0;
interns.clear();
idMap.clear();
available.clear();
assertCount();
}
void inline assertCount() const
{
DENG2_ASSERT(count == interns.size());
DENG2_ASSERT(count == idMap.size() - available.size());
}
Interns::iterator findIntern(String text)
{
CaselessString const key(text);
return interns.find(CaselessStringRef(&key)); // O(log n)
}
Interns::const_iterator findIntern(String text) const
{
CaselessString const key(text);
return interns.find(CaselessStringRef(&key)); // O(log n)
}
/**
* Before this is called make sure there is no duplicate of @a text in
* the interns set.
*
* @param text Text string to add to the interned strings. A copy is
* made of this.
*/
InternalId copyAndAssignUniqueId(String const& text)
{
CaselessString* str = new CaselessString(text);
// This is a new string that is added to the pool.
interns.insert(str); // O(log n)
return assignUniqueId(str);
}
InternalId assignUniqueId(CaselessString* str) // O(1)
{
InternalId idx;
// Any available ids in the shortlist?
if(!available.empty()) // O(1)
{
idx = available.front();
available.pop_front();
idMap[idx] = str;
}
else
{
// Expand the idMap.
idx = idMap.size();
idMap.push_back(str); // O(1) (amortized)
}
str->setId(idx);
// We have one more logical string in the pool.
count++;
assertCount();
return idx;
}
void releaseAndDestroy(InternalId id, Interns::iterator* iterToErase = 0)
{
DENG2_ASSERT(id < idMap.size());
CaselessString* interned = idMap[id];
DENG2_ASSERT(interned != 0);
idMap[id] = 0;
available.push_back(id);
// Delete the string itself, no one refers to it any more.
delete interned;
// If the caller already located the interned string, let's use it
// to erase the string in O(1) time. Otherwise it's up to the
// caller to make sure it gets removed from the interns.
if(iterToErase) interns.erase(*iterToErase); // O(1) (amortized)
// One less string.
count--;
assertCount();
}
};
StringPool::StringPool()
{
d = new Instance();
}
StringPool::StringPool(String* strings, uint count)
{
d = new Instance();
for(uint i = 0; strings && i < count; ++i)
{
intern(strings[i]);
}
}
StringPool::~StringPool()
{
delete d;
}
void StringPool::clear()
{
d->clear();
}
bool StringPool::empty() const
{
d->assertCount();
return !d->count;
}
uint StringPool::size() const
{
d->assertCount();
return uint(d->count);
}
StringPool::Id StringPool::intern(String str)
{
Interns::iterator found = d->findIntern(str); // O(log n)
if(found != d->interns.end())
{
// Already got this one.
return EXPORT_ID(found->id());
}
return EXPORT_ID(d->copyAndAssignUniqueId(str)); // O(log n)
}
String const& StringPool::internAndRetrieve(String str)
{
InternalId id = IMPORT_ID(intern(str));
return *d->idMap[id];
}
StringPool& StringPool::setUserValue(Id id, uint value)
{
if(id == 0) return *this;
InternalId const internalId = IMPORT_ID(id);
DENG2_ASSERT(internalId < d->idMap.size());
DENG2_ASSERT(d->idMap[internalId] != 0);
d->idMap[internalId]->setUserValue(value); // O(1)
return *this;
}
uint StringPool::userValue(Id id) const
{
if(id == 0) return 0;
InternalId const internalId = IMPORT_ID(id);
DENG2_ASSERT(internalId < d->idMap.size());
DENG2_ASSERT(d->idMap[internalId] != 0);
return d->idMap[internalId]->userValue(); // O(1)
}
StringPool& StringPool::setUserPointer(Id id, void* ptr)
{
if(id == 0) return *this;
InternalId const internalId = IMPORT_ID(id);
DENG2_ASSERT(internalId < d->idMap.size());
DENG2_ASSERT(d->idMap[internalId] != 0);
d->idMap[internalId]->setUserPointer(ptr); // O(1)
return *this;
}
void* StringPool::userPointer(Id id) const
{
if(id == 0) return NULL;
InternalId const internalId = IMPORT_ID(id);
DENG2_ASSERT(internalId < d->idMap.size());
DENG2_ASSERT(d->idMap[internalId] != 0);
return d->idMap[internalId]->userPointer(); // O(1)
}
StringPool::Id StringPool::isInterned(String str) const
{
Interns::const_iterator found = d->findIntern(str); // O(log n)
if(found != d->interns.end())
{
return EXPORT_ID(found->id());
}
// Not found.
return 0;
}
String const& StringPool::string(Id id) const
{
if(id == 0) return nullString; /// @todo Should error?
InternalId const internalId = IMPORT_ID(id);
DENG2_ASSERT(internalId < d->idMap.size());
return *d->idMap[internalId];
}
bool StringPool::remove(String str)
{
Interns::iterator found = d->findIntern(str); // O(log n)
if(found != d->interns.end())
{
d->releaseAndDestroy(found->id(), &found); // O(1) (amortized)
return true;
}
return false;
}
bool StringPool::removeById(Id id)
{
if(id == 0) return false;
InternalId const internalId = IMPORT_ID(id);
if(id >= d->idMap.size()) return false;
CaselessString* str = d->idMap[internalId];
if(!str) return false;
d->interns.erase(str); // O(log n)
d->releaseAndDestroy(str->id());
return true;
}
int StringPool::iterate(int (*callback)(Id, void*), void* data) const
{
if(!callback) return 0;
for(uint i = 0; i < d->idMap.size(); ++i)
{
if(!d->idMap[i]) continue;
int result = callback(EXPORT_ID(i), data);
if(result) return result;
}
return 0;
}
#if 0
void StringPool::write(Writer* writer) const
{
// Number of strings altogether (includes unused ids).
Writer_WriteUInt32(writer, d->idMap.size());
// Write the interns.
Writer_WriteUInt32(writer, d->interns.size());
for(Interns::const_iterator i = d->interns.begin(); i != d->interns.end(); ++i)
{
i->toStr()->serialize(writer);
}
}
void StringPool::read(Reader* reader)
{
clear();
uint numStrings = Reader_ReadUInt32(reader);
d->idMap.resize(numStrings, 0);
uint numInterns = Reader_ReadUInt32(reader);
while(numInterns--)
{
ddstring_t text;
Str_InitStd(&text);
CaselessString* str = new CaselessString;
str->deserialize(reader, &text);
// Create a copy of the string whose ownership StringPool controls.
str->setText(strdup(Str_Text(&text)));
d->interns.insert(str);
Str_Free(&text);
// Update the id map.
d->idMap[str->id()] = str;
d->count++;
}
// Update the available ids.
for(uint i = 0; i < d->idMap.size(); ++i)
{
if(!d->idMap[i]) d->available.push_back(i);
}
d->assertCount();
}
#endif
#if _DEBUG
typedef struct {
int padding; ///< Number of characters to left-pad output.
uint count; ///< Running total of the number of strings printed.
StringPool const* pool; ///< StringPool instance being printed.
} printinternedstring_params_t;
static int printInternedString(StringPool::Id internId, void* params)
{
printinternedstring_params_t* p = (printinternedstring_params_t*)params;
QByteArray stringUtf8 = p->pool->string(internId).toUtf8();
fprintf(stderr, "%*u %5u %s\n", p->padding, p->count++, internId, stringUtf8.constData());
return 0; // Continue iteration.
}
void StringPool::print() const
{
int numDigits = 5;
printinternedstring_params_t p;
p.padding = 2 + numDigits;
p.pool = this;
p.count = 0;
fprintf(stderr, "StringPool [%p]\n idx id string\n", (void*)this);
iterate(printInternedString, &p);
fprintf(stderr, " There is %u %s in the pool.\n", size(),
size() == 1? "string":"strings");
}
#endif
} // namespace de