annotate dmd/StringTable.d @ 146:af7e5ebef6ad

redundant extern(C)
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Tue, 14 Sep 2010 23:34:50 +0100
parents e28b18c23469
children af724d3510d7
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
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 class StringTable
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 void** table;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 uint count;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 uint tabledim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 this(uint size = 37)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 {
4
d706d958e4e8 Step 2 of restoring GC functionality.
korDen
parents: 2
diff changeset
21 table = cast(void**)GC.calloc(size * (void*).sizeof);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 memset(table, 0, size * (void*).sizeof);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 tabledim = size;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 count = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 ~this()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 /// TODO: is it *really* needed?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 // Zero out dangling pointers to help garbage collector.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 // Should zero out StringEntry's too.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 ///for (uint i = 0; i < count; i++) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 /// table[i] = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 ///}
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
36 ///free(table);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 //table = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 StringValue* lookup(immutable(dchar_t)[] s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 StringEntry* se = *cast(StringEntry**)search(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 if (se !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 return &se.value;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 StringValue* insert(immutable(dchar_t)[] s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 StringEntry** pse = cast(StringEntry**)search(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 StringEntry* se = *pse;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 if (se !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 return null; // error: already in table
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 se = new StringEntry(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 *pse = se;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 return &se.value;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 StringValue* update(immutable(dchar_t)[] s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 StringEntry **pse;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 StringEntry *se;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 pse = cast(StringEntry**)search(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 se = *pse;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 if (se is null) // not in table: so create new entry
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 se = new StringEntry(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 *pse = se;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 return &se.value;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 private:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 void** search(immutable(dchar_t)[] s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 int cmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 //printf("StringTable::search(%p,%d)\n",s,len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 hash_t hash = Dchar.calcHash(s.ptr, s.length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 uint u = hash % tabledim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 StringEntry** se = cast(StringEntry**)&table[u];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 //printf("\thash = %d, u = %d\n",hash,u);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 while (*se)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 cmp = (*se).hash - hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 if (cmp == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 cmp = (*se).value.lstring.len() - s.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 if (cmp == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 cmp = Dchar.memcmp(s.ptr, (*se).value.lstring.toDchars().ptr, s.length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 if (cmp == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 if (cmp < 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 se = &(*se).left;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 se = &(*se).right;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 //printf("\treturn %p, %p\n",se, (*se));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 return cast(void**)se;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }