StringMap

Ordered string-value associtaive array with extremely fast lookup.

Constructors

this
this(typeof(null) aa)

Initialize the associtave 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

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.

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.

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 associtave 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.

opEquals
bool opEquals(StringMap rhs)
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.

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.

Templates

toAA
template toAA()

Converts the associtave array to a common Dlang associative array.

Parameters

T

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

U

an unsigned type that can hold an index of keys. U.max must be less then the maximum possible number of struct members.

Examples

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);

Meta