1 
2 /* Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
3  * http://www.digitalmars.com
4  * Distributed under the Boost Software License, Version 1.0.
5  * http://www.boost.org/LICENSE_1_0.txt
6  * https://github.com/dlang/dmd/blob/master/src/dmd/root/stringtable.h
7  */
8 
9 #pragma once
10 
11 #include "root.h"
12 
13 struct StringEntry;
14 
15 // StringValue is a variable-length structure. It has neither proper c'tors nor a
16 // factory method because the only thing which should be creating these is StringTable.
17 struct StringValue
18 {
19     void *ptrvalue;
20     size_t length;
lstringStringValue21     char *lstring() { return (char *)(this + 1); }
22 
lenStringValue23     size_t len() const { return length; }
toDcharsStringValue24     const char *toDchars() const { return (const char *)(this + 1); }
25 
26     StringValue();  // not constructible
27 };
28 
29 struct StringTable
30 {
31 private:
32     StringEntry *table;
33     size_t tabledim;
34 
35     uint8_t **pools;
36     size_t npools;
37     size_t nfill;
38 
39     size_t count;
40 
41 public:
42     void _init(size_t size = 0);
43     void reset(size_t size = 0);
44     ~StringTable();
45 
46     StringValue *lookup(const char *s, size_t len);
47     StringValue *insert(const char *s, size_t len, void *ptrvalue);
48     StringValue *update(const char *s, size_t len);
49     int apply(int (*fp)(StringValue *));
50 
51 private:
52     uint32_t allocValue(const char *p, size_t length, void *ptrvalue);
53     StringValue *getValue(uint32_t validx);
54     size_t findSlot(hash_t hash, const char *s, size_t len);
55     void grow();
56 };
57