Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.string_table

Mir String Table designed for fast deserialization routines.
License:
Authors:
Ilia Ki
struct MirStringTable(U, C = char) if (__traits(isUnsigned, U) && (is(C == char) || is(C == wchar) || is(C == dchar)));

struct MirStringTable(size_t length, size_t maxKeyLength, bool caseInsensetive = false, C = char) if (is(C == char) || is(C == wchar) || is(C == dchar));
Fast string table used to get key's id. The keys should be first sorted by length and then lexicographically.
Parameters:
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
Examples:
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);
Examples:
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"]);
const(immutable(C)[])[] sortedKeys;
Keys sorted by length and then lexicographically.
pure nothrow @trusted this()(const(immutable(C)[])[] sortedKeys);
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.
const pure nothrow @nogc @trusted bool get()(scope const C[] key, ref uint index);
Parameters:
C[] key string to find index for
uint index (ref) index to fill with key's position.
Returns:
true if keys index has been found
const pure nothrow @nogc @trusted uint opIndex()(scope const C[] key);
sizediff_t smallerStringFirstCmp(T)(T[] a, T[] b);
Compares strings by length and then lexicographically.
Examples:
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);
template smallerStringFirst(alias direction = "<") if (direction == "<" || direction == ">")
Compares strings by length and then lexicographically.
Examples:
assert(smallerStringFirst("aa", "bb") == true);
assert(smallerStringFirst("aa", "aa") == false);
assert(smallerStringFirst("aaa", "aa") == false);
bool smallerStringFirst(T)(T[] a, T[] b);