File: string_table.d

package info (click to toggle)
mir-core 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 560 kB
  • sloc: makefile: 9; sh: 7
file content (446 lines) | stat: -rw-r--r-- 13,095 bytes parent folder | download | duplicates (2)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/++
Mir String Table designed for fast deserialization routines.

License: $(HTTP www.apache.org/licenses/LICENSE-2.0, Apache-2.0)
Authors: Ilia Ki 
Macros:
+/
module mir.string_table;

/++
Fast string table used to get key's id.
The keys should be first sorted by length and then lexicographically.
Params:
    U = an unsigned type that can hold an index of sorted keys. `U.max` must be less then length of the table.
    C = character type
+/
struct MirStringTable(U, C = char)
    if (__traits(isUnsigned, U) && (is(C == char) || is(C == wchar) || is(C == dchar)))
{
    /++
    Keys sorted by length and then lexicographically.
    +/
    const(immutable(C)[])[] sortedKeys;

    private U[] table;

    /++
    The keys should be first sorted by length and then lexicographically.

    The constructor uses GC.
    It can be used in `@nogc` code when if constructed in compile time.
    +/
    this()(const(immutable(C)[])[] sortedKeys)
        @trusted pure nothrow
    {
        pragma(inline, false);
        this.sortedKeys = sortedKeys;
        assert(sortedKeys.length <= U.max);
        const largest = sortedKeys.length ? sortedKeys[$ - 1].length : 0;
        table = new U[largest + 2];
        size_t ski;
        foreach (length; 0 .. largest + 1)
        {
            while(ski < sortedKeys.length && sortedKeys[ski].length == length)
                ski++;
            table[length + 1] = cast(U)ski;
        }
    }

    /++
    Params:
        key = string to find index for
        index = (ref) index to fill with key's position.
    Returns:
        true if keys index has been found
    +/
    bool get()(scope const C[] key, ref uint index)
         const @trusted pure nothrow @nogc
    {
        import mir.utility: _expect;
        if (_expect(key.length + 1 < table.length, true))
        {

            // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
            // 0 1 2 3 4 5 6   8 9 10    12          16

            auto low = table[key.length] + 0u;
            auto high = table[key.length + 1] + 0u;
            auto items = sortedKeys.ptr;
            if (low < high)
            {
                version (none)
                {
                    if (key.length == 0)
                    {
                        index = 0;
                        return true;
                    }
                }
                L: do {
                    auto mid = (low + high) / 2;

                    version (all)
                    {
                        import core.stdc.string: memcmp;
                        int r = void;

                        if (__ctfe)
                            r = __cmp(key, items[mid]);
                        else
                        version (BigEndian)
                            r = memcmp(key.ptr, items[mid].ptr, key.length);
                        else
                        static if (C.sizeof == 1)
                            r = memcmp(key.ptr, items[mid].ptr, key.length);
                        else
                            r = __cmp(key, items[mid]);

                        if (r == 0)
                        {
                            index = mid;
                            return true;
                        }
                        if (r > 0)
                            low = mid + 1;
                        else
                            high = mid;
                    }
                    else
                    {
                        size_t i;
                        auto value = items[mid];
                        do {
                            if (key[i] < value[i])
                            {
                                high = mid;
                                continue L;
                            }
                            else
                            if (key[i] > value[i])
                            {
                                low = mid + 1;
                                continue L;
                            }
                        } while (++i < key.length);
                        index = mid;
                        return true;
                    }
                }
                while(low < high);
            }
        }
        return false;
    }

    ///
    uint opIndex()(scope const C[] key)
         const @trusted pure nothrow @nogc
    {
        import mir.utility: _expect;
        uint ret = 0;
        if (get(key, ret)._expect(true))
            return ret;
        assert(0);
    }
}

/// ditto
struct MirStringTable(size_t length, size_t maxKeyLength, bool caseInsensetive = false, C = char)
    if (is(C == char) || is(C == wchar) || is(C == dchar))
{
    ///
    const(immutable(C)[])[length] sortedKeys;

    private alias U = minimalIndexType!length;
    private U[maxKeyLength + 2] table;

    /++
    The keys should be first sorted by length and then lexicographically.

    The constructor uses GC.
    It can be used in `@nogc` code when if constructed in compile time.
    +/
    this(immutable(C)[][length] sortedKeys)
        @trusted pure nothrow
    {
        pragma(inline, false);
        this.sortedKeys = sortedKeys;
        size_t ski;
        foreach (length; 0 .. maxKeyLength + 1)
        {
            while(ski < sortedKeys.length && sortedKeys[ski].length == length)
                ski++;
            table[length + 1] = cast(U)ski;
        }
    }

    /++
    Params:
        key = string to find index for
        index = (ref) index to fill with key's position.
    Returns:
        true if keys index has been found
    +/
    bool get()(scope const(C)[] key, ref uint index)
         const @trusted pure nothrow @nogc
    {
        import mir.utility: _expect;
        if (_expect(key.length <= maxKeyLength, true))
        {
            static if (caseInsensetive)
            {
                C[maxKeyLength] buffer = void;
                foreach(i, C c; key)
                    buffer[i] = c.fastToUpper;
                key = buffer[0 .. key.length];
            }
            auto low = table[key.length] + 0u;
            auto high = table[key.length + 1] + 0u;
            auto items = sortedKeys.ptr;
            if (low < high)
            {
                static if (!(maxKeyLength >= 16))
                {
                    if (key.length == 0)
                    {
                        index = 0;
                        return true;
                    }
                }
                L: do {
                    auto mid = (low + high) / 2;

                    static if (maxKeyLength >= 16)
                    {
                        import core.stdc.string: memcmp;
                        int r = void;

                        if (__ctfe)
                            r = __cmp(key, items[mid]);
                        else
                        version (BigEndian)
                            r = memcmp(key.ptr, items[mid].ptr, key.length);
                        else
                        static if (C.sizeof == 1)
                            r = memcmp(key.ptr, items[mid].ptr, key.length);
                        else
                            r = __cmp(key, items[mid]);

                        if (r == 0)
                        {
                            index = mid;
                            return true;
                        }
                        if (r > 0)
                            low = mid + 1;
                        else
                            high = mid;
                    }
                    else
                    {
                        size_t i;
                        auto value = items[mid];
                        do {
                            if (key[i] < value[i])
                            {
                                high = mid;
                                continue L;
                            }
                            else
                            if (key[i] > value[i])
                            {
                                low = mid + 1;
                                continue L;
                            }
                        } while (++i < key.length);
                        index = mid;
                        return true;
                    }
                }
                while(low < high);
            }
        }
        return false;
    }

    ///
    uint opIndex()(scope const C[] key)
         const @trusted pure nothrow @nogc
    {
        import mir.utility: _expect;
        uint ret = 0;
        if (get(key, ret)._expect(true))
            return ret;
        assert(0);
    }
}

///
@safe pure nothrow @nogc
version(mir_core_test) unittest
{
    static immutable sortedKeys = ["", "a", "b", "aab", "abb", "aaaaa"];
    static immutable table = MirStringTable!ubyte(sortedKeys); // CTFE
    static assert (table[""] == 0);
    static assert (table["a"] == 1);
    static assert (table["b"] == 2);
    static assert (table["abb"] == 4);
    assert (table["aaaaa"] == 5);
}


///
@safe pure nothrow
version(mir_core_test) unittest
{
    import mir.utility: simpleSort;
    auto keys = ["aaaaa", "abb", "", "b", "a", "aab"];
    // sorts keys by length and then lexicographically.
    keys.simpleSort!smallerStringFirst;
    assert(keys == ["", "a", "b", "aab", "abb", "aaaaa"]);
}

@safe pure nothrow
version(mir_core_test) unittest
{
    import mir.utility: simpleSort;
    auto keys = ["aaaaa"w, "abb"w, ""w, "b"w, "a"w, "aab"w];
    // sorts keys by length and then lexicographically.
    keys.simpleSort!smallerStringFirst;
    assert(keys == [""w, "a"w, "b"w, "aab"w, "abb"w, "aaaaa"w]);
}

package template minimalIndexType(size_t length)
{
    static if (length <= ubyte.max)
        alias minimalIndexType = ubyte;
    else
    static if (length <= ushort.max)
        alias minimalIndexType = ushort;
    else
    static if (length <= uint.max)
        alias minimalIndexType = uint;
    else
        alias minimalIndexType = ulong;
}

package template minimalSignedIndexType(size_t length)
{
    static if (length <= byte.max)
        alias minimalSignedIndexType = byte;
    else
    static if (length <= short.max)
        alias minimalSignedIndexType = short;
    else
    static if (length <= int.max)
        alias minimalSignedIndexType = int;
    else
        alias minimalSignedIndexType = long;
}

/++
Compares strings by length and then lexicographically.
+/
sizediff_t smallerStringFirstCmp(T)(T[] a, T[] b)
{
    if (sizediff_t d = a.length - b.length)
    {
        return d;
    }

    import std.traits: Unqual;
    static if (is(Unqual!T == ubyte) || is(Unqual!T == char))
    {
        import core.stdc.string: memcmp;
        if (__ctfe)
            return __cmp(a, b);
        else
            return (() @trusted => memcmp(a.ptr, b.ptr, a.length))();
    }
    else
    {
        return __cmp(a, b);
    }
}

///
@safe pure nothrow @nogc
version(mir_core_test) unittest
{
    assert(smallerStringFirstCmp("aa", "bb") < 0);
    assert(smallerStringFirstCmp("aa", "aa") == 0);
    assert(smallerStringFirstCmp("aaa", "aa") > 0);

    static assert(smallerStringFirstCmp("aa", "bb") < 0);
    static assert(smallerStringFirstCmp("aa", "aa") == 0);
    static assert(smallerStringFirstCmp("aaa", "aa") > 0);
}

/++
Compares strings by length and then lexicographically.
+/
template smallerStringFirst(alias direction = "<")
    if (direction == "<" || direction == ">")
{
    ///
    bool smallerStringFirst(T)(T[] a, T[] b)
    {
        auto r = smallerStringFirstCmp(a, b);
        static if (direction == "<")
            return r < 0;
        else
            return r > 0;
    }
}

///
@safe pure nothrow @nogc
version(mir_core_test) unittest
{
    assert(smallerStringFirst("aa", "bb") == true);
    assert(smallerStringFirst("aa", "aa") == false);
    assert(smallerStringFirst("aaa", "aa") == false);
}

package auto fastToUpper(C)(const C a)
{   // std.ascii may not be inlined
    return 'a' <= a && a <= 'z' ? cast(C)(a ^ 0x20) : a;
}

package @safe pure nothrow @nogc
C[] fastToUpperInPlace(C)(scope return C[] a)
{
    foreach(ref C e; a)
        e = e.fastToUpper;
    return a;
}

package immutable(C)[][] prepareStringTableKeys(bool caseInsensetive = false, C)(immutable(C)[][] keys)
{
    static if (caseInsensetive)
    {
        foreach (ref key; keys)
        {
            auto upper = cast(immutable) key.dup.fastToUpperInPlace;
            if (upper != key)
                key = upper;
        }
    }
    import mir.utility: simpleSort;
    return keys.simpleSort!smallerStringFirst;
}

package template createTable(C)
    if (is(C == char) || is(C == wchar) || is(C == dchar))
{
    auto createTable(immutable(C)[][] keys, bool caseInsensetive = false)()
    {
        static immutable C[][] sortedKeys = prepareStringTableKeys!caseInsensetive(keys);
        alias Table = MirStringTable!(sortedKeys.length, sortedKeys.length ? sortedKeys[$ - 1].length : 0, caseInsensetive, C);
        static if (sortedKeys.length)
            return Table(sortedKeys[0 .. sortedKeys.length]);
        else
            return Table.init;
    }
}