annotate dmd/StringTable.d @ 192:eb38fdcb3e62 default tip

updated to compile with dmd2.062
author korDen
date Sat, 02 Mar 2013 01:25:52 -0800
parents 0622fff7810a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.StringTable;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 4
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.StringValue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.StringEntry;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Dchar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import core.stdc.stdlib;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import core.stdc.string;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10
4
d706d958e4e8 Step 2 of restoring GC functionality.
korDen
parents: 2
diff changeset
11 import core.memory;
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
12
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
13 import std.stdio;
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
14
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
15 struct StringTable
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
17 Object[string] table;
180
0622fff7810a Fixed a few memory allocation related issues
korDen
parents: 178
diff changeset
18 /*
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
19 ~this()
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
20 {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
21 foreach (k, v; table) {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
22 delete v;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
23 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
24 }
180
0622fff7810a Fixed a few memory allocation related issues
korDen
parents: 178
diff changeset
25 */
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
26 Object* lookup(string s)
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
27 {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
28 return s in table;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
29 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
30
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
31 Object* insert(string s)
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
32 {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
33 auto value = s in table;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
34 if (value !is null) {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
35 return null;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
36 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
37
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
38 table[s] = null;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
39
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
40 return s in table;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
41 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
42
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
43 Object* update(string s)
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
44 {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
45 auto value = s in table;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
46 if (value !is null) {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
47 return value;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
48 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
49
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
50 table[s] = null;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
51
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
52 return s in table;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
53 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
54
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
55 /*
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
56 StringValue* lookup(string s)
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
57 {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
58 if (auto p = s in table) {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
59 return *p;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
60 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
61
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
62 return null;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
63 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
64
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
65 StringValue* insert(string s)
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
66 {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
67 if (auto p = s in table) {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
68 return null;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
69 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
70
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
71 auto value = new StringValue();
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
72 value.lstring.string_ = s;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
73 table[s] = value;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
74
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
75 return value;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
76 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
77
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
78 StringValue* update(string s)
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
79 {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
80 if (auto p = s in table) {
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
81 return *p;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
82 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
83
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
84 auto value = new StringValue();
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
85 value.lstring.string_ = s;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
86 table[s] = value;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
87
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
88 return value;
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
89 }
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
90 */
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
91
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
92 /*
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 void** table;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 uint count;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 uint tabledim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 this(uint size = 37)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
99 register();
4
d706d958e4e8 Step 2 of restoring GC functionality.
korDen
parents: 2
diff changeset
100 table = cast(void**)GC.calloc(size * (void*).sizeof);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 memset(table, 0, size * (void*).sizeof);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 tabledim = size;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 count = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 }
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
105
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 ~this()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 /// TODO: is it *really* needed?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 // Zero out dangling pointers to help garbage collector.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 // Should zero out StringEntry's too.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 ///for (uint i = 0; i < count; i++) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 /// table[i] = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 ///}
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
115 ///free(table);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 //table = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 StringValue* lookup(immutable(dchar_t)[] s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 {
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
121 StringEntry* se = *search(s);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 if (se !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 return &se.value;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 }
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
127
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 StringValue* insert(immutable(dchar_t)[] s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 {
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
130 StringEntry** pse = search(s);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 StringEntry* se = *pse;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 if (se !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 return null; // error: already in table
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 se = new StringEntry(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 *pse = se;
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
138 ++count;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 return &se.value;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
143
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
144 void insertCopy(StringEntry* proto)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
145 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
146 StringEntry** pse = search(proto.value.lstring.string_);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
147 StringEntry* se = *pse;
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
148 if (se is null)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
149 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
150 se = new StringEntry(proto);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
151 *pse = se;
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
152 ++count;
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
153 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
154 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
155
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 StringValue* update(immutable(dchar_t)[] s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 {
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
158 StringEntry** pse = search(s);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
159 StringEntry* se = *pse;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 if (se is null) // not in table: so create new entry
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 se = new StringEntry(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 *pse = se;
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
164 ++count;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 return &se.value;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
169 void copyTo(StringTable stringTable)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
170 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
171 for (int u = 0; u < tabledim; ++u) {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
172 StringEntry** se = cast(StringEntry**)&table[u];
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
173 copyNode(*se, stringTable);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
174 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
175 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
176
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 private:
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
178 void copyNode(StringEntry* node, StringTable stringTable)
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
179 {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
180 if (node is null) {
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
181 return;
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
182 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
183
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
184 copyNode(node.left, stringTable);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
185 stringTable.insertCopy(node);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
186 copyNode(node.right, stringTable);
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
187 }
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
188
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
189 StringEntry** search(immutable(dchar_t)[] s)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 int cmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 //printf("StringTable::search(%p,%d)\n",s,len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 hash_t hash = Dchar.calcHash(s.ptr, s.length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 uint u = hash % tabledim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 StringEntry** se = cast(StringEntry**)&table[u];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 //printf("\thash = %d, u = %d\n",hash,u);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 while (*se)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 cmp = (*se).hash - hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 if (cmp == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 cmp = (*se).value.lstring.len() - s.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 if (cmp == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 cmp = Dchar.memcmp(s.ptr, (*se).value.lstring.toDchars().ptr, s.length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 if (cmp == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 if (cmp < 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 se = &(*se).left;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 se = &(*se).right;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 //printf("\treturn %p, %p\n",se, (*se));
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
218 return se;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 }
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
220 */
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
221 }