The OpenD Programming Language

StringMap

Ordered string-value associative array with extremely fast lookup.

Constructors

this
this(typeof(null) aa)

Initialize the associative array with default value.

this
this(T[string] aa)

Constructs an associative array using keys and values from the builtin associative array Complexity: O(n log(n))

this
this(string[] keys, T[] values)

Constructs an associative array using keys and values.

Members

Aliases

byKey
alias byKey = keys
byValue
alias byValue = values

Returns a dynamic array, the elements of which are the values in the associative array. Doesn't allocate a new copy.

serdeKeysProxy
alias serdeKeysProxy = Unqual!T

Functions

assumeSafeAppend
inout(typeof(this)) assumeSafeAppend()

Assume that it is safe to append to this associative array. Appends made to this associative array after calling this function may append in place, even if the array was a slice of a larger array to begin with. Use this only when it is certain there are no elements in use beyond the associative array in the memory block. If there are, those elements will be overwritten by appending to this associative array.

byKeyValue
auto byKeyValue()

Return a range over all elements (key-values pairs) currently stored in the associative array.

capacity
size_t capacity()

(Property) Gets the current capacity of an associative array. The capacity is the size that the underlaynig slices can grow to before the underlying arrays may be reallocated or extended.

clear
void clear()

Removes all remaining keys and values from an associative array.

dup
auto dup()
empty
bool empty()
findPos
ptrdiff_t findPos(const(char)[] key)

Finds position of the key in the associative array .

get
inout(T) get(const(char)[] key, inout(T) defaultValue)

Looks up key; if it exists returns corresponding value else evaluates and returns defaultValue.

keys
const(string)[] keys()

Returns a dynamic array, the elements of which are the keys in the associative array. Doesn't allocate a new copy.

length
size_t length()
opAssign
ref opAssign(typeof(null) )

Reset the associative array

opBinaryRight
inout(T)* opBinaryRight(const(char)[] key)

Complexity: O(log(s)), where s is the number of the keys with the same length as the input key.

opCmp
int opCmp(typeof(this) rhs)

opCmp Implementation. Doesn't depend on order

opEquals
bool opEquals(StringMap!V rhs)
bool opEquals(const(V)[const(K)] rhs)

== implementation. Doesn't depend on order

opIndex
auto opIndex()
opIndex
inout(T) opIndex(const(char)[] key)

Complexity: O(log(s)), where s is the number of the keys with the same length as the input key.

opIndexAssign
T opIndexAssign(R value, string key)
T opIndexAssign(T value, string key)

Complexity: O(log(s)) (exist) or O(n) (not exist), where s is the count of the strings with the same length as they key.

remove
bool remove(const(char)[] key)

remove(key) does nothing if the given key does not exist and returns false. If the given key does exist, it removes it from the AA and returns true.

require
T require(string key, T value)

Looks up key; if it exists returns corresponding value else evaluates value, adds it to the associative array and returns it.

reserve
size_t reserve(size_t newcapacity)

Reserves capacity for an associative array. The capacity is the size that the underlaying slices can grow to before the underlying arrays may be reallocated or extended.

sort
ref sort()

Sorts table according to the keys

toHash
size_t toHash()

hashOf Implementation. Doesn't depend on order

toString
string toString()
void toString(W w)
values
inout(T)[] values()

Returns a dynamic array, the elements of which are the values in the associative array. Doesn't allocate a new copy.

Structs

KeyValue
struct KeyValue

Templates

toAA
template toAA()

Converts to a builtin associative array.

Parameters

T

mutable value type, can be instance of $(AlgebraicREF Algebraic) for example.

Examples

class C
{
    this(int x) { this.x = x; }
    int x;
    bool opEquals(scope const C rhs) const scope @safe pure nothrow @nogc
    {
        return x == rhs.x;
    }

    override size_t toHash() @safe const scope pure nothrow @nogc
    {
        return x;
    }
}
StringMap!(const C) table;
const v0 = new C(42);
const v1 = new C(43);
table["0"] = v0;
table["1"] = v1;
assert(table.keys == ["0", "1"]);
assert(table.values == [v0, v1]); // TODO: qualify unittest as `pure` when this is inferred `pure`
static assert(is(typeof(table.values) == const(C)[]));
StringMap!int table;
table["L"] = 3;
table["A"] = 2;
table["val"] = 1;
assert(table.keys == ["L", "A", "val"]);
assert(table.values == [3, 2, 1]);
assert(table["A"] == 2);
table.values[2] += 10;
assert(table["A"] == 2);
assert(table["L"] == 3);
assert(table["val"] == 11);
assert(table.keys == ["L", "A", "val"]);
assert(table.values == [3, 2, 11]);
table.remove("A");
assert(table.keys == ["L", "val"]);
assert(table.values == [3, 11]);
assert(table["L"] == 3);
assert(table["val"] == 11);

assert(table == table);

// sorting
table["A"] = 2;
table.sort;
assert(table.keys == ["A", "L", "val"]);
assert(table.values == [2, 3, 11]);
assert(table["A"] == 2);
assert(table["L"] == 3);
assert(table["val"] == 11);
static void testEquals(X, Y)()
{
    X x;
    Y y;
    assert(x == y);

    x["L"] = 3;
    assert(x != y);
    x["A"] = 2;
    assert(x != y);
    x["val"] = 1;
    assert(x != y);

    y["val"] = 1;
    assert(x != y);
    y["L"] = 3;
    assert(x != y);
    y["A"] = 2;
    assert(x == y);

    x = X.init;
    assert(x != y);

    y = Y.init;
    assert(x == y);
}

testEquals!(StringMap!int, StringMap!uint)();
testEquals!(StringMap!int, uint[string])();
testEquals!(uint[string], StringMap!int)();

Meta