1 
2 /* Copyright (C) 1999-2019 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 #include "rmem.h"   // for d_size_t
13 
14 struct StringEntry;
15 
16 // StringValue is a variable-length structure. It has neither proper c'tors nor a
17 // factory method because the only thing which should be creating these is StringTable.
18 struct StringValue
19 {
20     void *ptrvalue;
21     size_t length;
lstringStringValue22     char *lstring() { return (char *)(this + 1); }
23 
lenStringValue24     size_t len() const { return length; }
toDcharsStringValue25     const char *toDchars() const { return (const char *)(this + 1); }
26 
27     StringValue();  // not constructible
28 };
29 
30 struct StringTable
31 {
32 private:
33     StringEntry *table;
34     size_t tabledim;
35 
36     uint8_t **pools;
37     size_t npools;
38     size_t nfill;
39 
40     size_t count;
41 
42 public:
43     void _init(d_size_t size = 0);
44     void reset(d_size_t size = 0);
45     ~StringTable();
46 
47     StringValue *lookup(const char *s, d_size_t len);
48     StringValue *insert(const char *s, size_t len, void *ptrvalue);
49     StringValue *update(const char *s, d_size_t len);
50     int apply(int (*fp)(StringValue *));
51 
52 private:
53     uint32_t allocValue(const char *p, size_t length, void *ptrvalue);
54     StringValue *getValue(uint32_t validx);
55     size_t findSlot(hash_t hash, const char *s, size_t len);
56     void grow();
57 };
58