-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_ids.d
55 lines (40 loc) · 1017 Bytes
/
core_ids.d
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
module d_glat.core_ids;
import d_glat.core_assert;
import std.array : Appender, appender;
import std.functional : partial;
/*
Back-and-forth between string ids and numerical ids.
The Boost License applies, as described in the file ./LICENSE
By Guillaume Lathoud, 2022
glat@glat.info
*/
struct IDS
{
size_t id_of_string( in string s ) pure @safe nothrow
{
if (scope auto p = s in i_of_s_aa)
return *p;
return _register_id_of_string( s );
}
string string_of_id( in size_t id ) pure @safe nothrow
{
return s_of_i_app.data[ id ];
}
void _clear() pure
// Only if you know what you are doing!
{
i_of_s_aa.clear;
s_of_i_app.clear;
}
private:
size_t[string] i_of_s_aa;
Appender!(string[]) s_of_i_app;
size_t _register_id_of_string( in string s ) pure @safe nothrow
{
debug assert( s !in i_of_s_aa );
immutable id = s_of_i_app.data.length;
s_of_i_app.put( s );
i_of_s_aa[ s ] = id;
return id;
}
};