FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/include/daScript/simulate/runtime_table.h at master · moneytech/daScript · GitHub
moneytech
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
include
/
daScript
/
simulate
/
runtime_table.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
205 lines (186 loc) · 7.72 KB
Breadcrumbs
daScript
/
include
/
daScript
/
simulate
/
runtime_table.h
Copy path
File metadata and controls
205 lines (186 loc) · 7.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
pragma
once
#
include
"
daScript/misc/arraytype.h
"
#
include
"
daScript/simulate/hash.h
"
namespace
das
{
//
TODO:
//
- return correct insert index of original value? is this at all possible?
//
- throw runtime error in the context, when grow inside locked table (recover well)
extern
const
char
* rts_null;
template
<
typename
KeyType>
struct
KeyCompare
{
__forceinline
bool
operator
() (
const
KeyType & a,
const
KeyType & b ) {
return
a == b;
}
};
template
<>
struct
KeyCompare
<
char
*> {
__forceinline
bool
operator
() (
const
char
* a,
const
char
* b ) {
if
( a==b )
return
true
;
if
( !a || !b )
return
false
;
return
strcmp
(a,b)==
0
;
}
};
template
<>
struct
KeyCompare
<
const
char
*> {
__forceinline
bool
operator
() (
const
char
* a,
const
char
* b ) {
if
( a==b )
return
true
;
if
( !a || !b )
return
false
;
return
strcmp
(a,b)==
0
;
}
};
template
<
typename
KeyType>
class
TableHash
{
Context * context =
nullptr
;
uint32_t
valueTypeSize =
0
;
enum
{
minCapacity =
8
,
minLookups =
4
};
public:
TableHash
() =
delete
;
TableHash
(
const
TableHash & ) =
delete
;
TableHash
( Context * ctx,
uint32_t
vs ) : context(ctx), valueTypeSize(vs) {}
__forceinline
uint32_t
indexFromHash
(
uint32_t
hash,
uint32_t
shift )
const
{
return
hash >> shift;
//
i don't know why this is faster, but it is
}
__forceinline
uint32_t
computeShift
(
uint32_t
capacity) {
return
__builtin_clz
(capacity-
1
);
}
__forceinline
uint32_t
computeMaxLookups
(
uint32_t
capacity) {
uint32_t
desired =
32
-
__builtin_clz
(capacity-
1
);
return
das::max
(
uint32_t
(minLookups), desired *
6
);
}
__forceinline
int
find
(
const
Table & tab, KeyType key,
uint32_t
hash )
const
{
uint32_t
mask = tab.
capacity
-
1
;
uint32_t
index =
indexFromHash
(hash, tab.
shift
);
uint32_t
lastI = (index+tab.
maxLookups
) & mask;
auto
pKeys = (
const
KeyType *) tab.
keys
;
auto
pHashes = tab.
hashes
;
while
( index != lastI ) {
auto
kh = pHashes[index];
if
( kh==
HASH_EMPTY32
) {
return
-
1
;
}
else
if
( kh==hash && KeyCompare<KeyType>()(pKeys[index],key) ) {
return
(
int
) index;
}
index = (index +
1
) & mask;
}
return
-
1
;
}
__forceinline
int
insertNew
( Table & tab,
uint32_t
hash )
const
{
//
TODO: take key under account and be less agressive?
uint32_t
mask = tab.
capacity
-
1
;
uint32_t
index =
indexFromHash
(hash, tab.
shift
);
uint32_t
lastI = (index+tab.
maxLookups
) & mask;
auto
pHashes = tab.
hashes
;
while
( index != lastI ) {
auto
kh = pHashes[index];
if
( kh==
HASH_EMPTY32
) {
return
(
int
) index;
}
index = (index +
1
) & mask;
}
return
-
1
;
}
__forceinline
int
reserve
( Table & tab, KeyType key,
uint32_t
hash ) {
for
( ;; ) {
uint32_t
mask = tab.
capacity
-
1
;
uint32_t
index =
indexFromHash
(hash, tab.
shift
);
uint32_t
lastI = (index+tab.
maxLookups
) & mask;
uint32_t
insertI = -
1u
;
auto
pKeys = (KeyType *) tab.
keys
;
auto
pHashes = tab.
hashes
;
while
( index != lastI ) {
auto
kh = pHashes[index];
if
(kh ==
HASH_EMPTY32
) {
if
( tab.
isLocked
() ) context->
throw_error
(
"
can't insert into locked table
"
);
if
( insertI != -
1u
) index = insertI;
pHashes[index] = hash;
pKeys[index] = key;
tab.
size
++;
return
(
int
)index;
}
else
if
(kh ==
HASH_KILLED32
) {
if
( insertI == -
1u
) insertI = index;
}
else
if
(kh == hash && KeyCompare<KeyType>()(pKeys[index], key)) {
return
(
int
)index;
}
index = (index +
1
) & mask;
}
if
( !
grow
(tab) ) {
return
-
1
;
}
}
}
__forceinline
int
erase
( Table & tab, KeyType key,
uint32_t
hash ) {
uint32_t
mask = tab.
capacity
-
1
;
uint32_t
index =
indexFromHash
(hash, tab.
shift
);
uint32_t
lastI = (index+tab.
maxLookups
) & mask;
auto
pKeys = (
const
KeyType *) tab.
keys
;
auto
pHashes = tab.
hashes
;
while
( index != lastI ) {
auto
kh = pHashes[index];
if
( kh==
HASH_EMPTY32
) {
return
-
1
;
}
else
if
( kh==hash && KeyCompare<KeyType>()(pKeys[index],key) ) {
tab.
size
--;
pHashes[index] =
HASH_KILLED32
;
memset
(tab.
data
+ index*valueTypeSize,
0
, valueTypeSize);
return
(
int
) index;
}
index = (index +
1
) & mask;
}
return
-
1
;
}
bool
grow
( Table & tab ) {
uint32_t
newCapacity =
das::max
(
uint32_t
(minCapacity), tab.
capacity
*
2
);
repeatIt:;
Table newTab;
uint32_t
memSize = newCapacity * (valueTypeSize +
sizeof
(KeyType) +
sizeof
(
uint32_t
));
newTab.
data
= (
char
*) context->
heap
->
allocate
(memSize);
context->
heap
->
mark_comment
(newTab.
data
,
"
table
"
);
if
( !newTab.
data
) {
context->
throw_error
(
"
can't grow table, out of heap
"
);
return
false
;
}
newTab.
keys
= newTab.
data
+ newCapacity * valueTypeSize;
newTab.
hashes
= (
uint32_t
*)(newTab.
keys
+ newCapacity *
sizeof
(KeyType));
newTab.
size
= tab.
size
;
newTab.
capacity
= newCapacity;
newTab.
lock
= tab.
lock
;
newTab.
flags
= tab.
flags
;
newTab.
maxLookups
=
computeMaxLookups
(newCapacity);
newTab.
shift
=
computeShift
(newCapacity);
memset
(newTab.
data
,
0
, newCapacity*valueTypeSize);
auto
pHashes = newTab.
hashes
;
memset
(pHashes,
0
, newCapacity *
sizeof
(
uint32_t
));
if
( tab.
size
) {
auto
pKeys = (KeyType *) newTab.
keys
;
auto
pOldValues = tab.
data
;
auto
pValues = newTab.
data
;
auto
pOldKeys = (
const
KeyType *) tab.
keys
;
auto
pOldHashes = tab.
hashes
;
for
(
uint32_t
i=
0
; i!=tab.
capacity
; ++i ) {
auto
hash = pOldHashes[i];
if
( hash>
HASH_KILLED32
) {
int
index =
insertNew
(newTab, hash);
if
( index==-
1
) {
newCapacity *=
2
;
goto
repeatIt;
}
else
{
pHashes[index] = hash;
pKeys[index] = pOldKeys[i];
memcpy
( pValues + index*valueTypeSize, pOldValues + i*valueTypeSize, valueTypeSize );
}
}
}
}
if
(tab.
capacity
) {
uint32_t
oldSize = tab.
capacity
* (valueTypeSize +
sizeof
(KeyType) +
sizeof
(
uint32_t
));
context->
heap
->
free
(tab.
data
, oldSize);
}
swap
( newTab, tab );
return
true
;
}
};
}
Back
|
FazBrowse Home
|
New Git URL