containers.hashmap

Hash Map

Authors

Brian Schott

  • Declaration

    struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K, bool supportGC = shouldAddGCRange!K || shouldAddGCRange!V);

    Associative array / hash map.

    Parameters

    K

    the key type

    V

    the value type

    Allocator

    the allocator type to use. Defaults to Mallocator

    hashFunction

    the hash function to use on the keys

    supportGC

    true if the container should support holding references to GC-allocated memory.

    Examples

    1. import std.uuid : randomUUID; import std.range.primitives : walkLength; auto hm = HashMap!(string, int)(16); assert (hm.length == 0); assert (!hm.remove("abc")); hm["answer"] = 42; assert (hm.length == 1); assert ("answer" in hm); hm.remove("answer"); assert (hm.length == 0); hm["one"] = 1; hm["one"] = 1; assert (hm.length == 1); assert (hm["one"] == 1); hm["one"] = 2; assert(hm["one"] == 2); foreach (i; 0 .. 1000) { hm[randomUUID().toString] = i; } assert (hm.length == 1001); assert (hm.keys().length == hm.length); assert (hm.values().length == hm.length); () @nogc { assert (hm.byKey().walkLength == hm.length); assert (hm.byValue().walkLength == hm.length); assert (hm[].walkLength == hm.length); assert (hm.byKeyValue().walkLength == hm.length); }(); foreach (v; hm) {} auto hm2 = HashMap!(char, char)(4); hm2['a'] = 'a'; HashMap!(int, int) hm3; assert (hm3.get(100, 20) == 20); hm3[100] = 1; assert (hm3.get(100, 20) == 1); auto pValue = 100 in hm3; assert(*pValue == 1);

    • Declaration

      pure nothrow @nogc @safe this(Allocator allocator);

      Use the given allocator for allocations.

    • Declaration

      this(size_t bucketCount, Allocator allocator);

      Constructs an HashMap with an initial bucket count of bucketCount. bucketCount must be a power of two.

    • Declaration

      void clear();

      Removes all items from the map

    • Declaration

      ref auto opIndex(this This)(K key);

      Supports aa[key] syntax.

    • Declaration

      inout pure @nogc @safe bool containsKey(this This)(K key);

      Return Value

      true if there is an entry in this map for the given key, false otherwise.

    • get

      Declaration

      auto get(this This)(K key, lazy V defaultValue);

      Gets the value for the given key, or returns defaultValue if the given key is not present.

      Parameters

      K key

      the key to look up

      value

      the default value

      Return Value

      the value indexed by key, if present, or defaultValue otherwise.

    • Declaration

      auto getOrAdd(this This)(K key, lazy V defaultValue);

      If the given key does not exist in the HashMap, adds it with the value defaultValue.

      Parameters

      K key

      the key to look up

      value

      the default value

      Return Value

      a pointer to the value stored in the HashMap with the given key. The pointer is guaranteed to be valid only until the next HashMap modification.

    • Declaration

      void opIndexAssign(V value, const K key);

      Supports aa[key] = value; syntax.

    • Declaration

      inout nothrow @trusted inout(V)* opBinaryRight(string op)(const K key) if (op == "in");

      Supports key in aa syntax.

      Return Value

      pointer to the value corresponding to the given key, or null if the key is not present in the HashMap.

    • Declaration

      bool remove(K key);

      Removes the value associated with the given key

      Return Value

      true if a value was actually removed.

    • Declaration

      const pure nothrow @nogc @property @safe size_t length();

      Return Value

      the number of key/value pairs in this container.

    • Declaration

      const pure nothrow @nogc @property @safe bool empty();

      Return Value

      true if there are no items in this container.

    • Declaration

      inout @trusted auto byKey(this This)();

      Return Value

      a range of the keys in this map.

    • Declaration

      const @property K[] keys();

      Return Value

      a GC-allocated array filled with the keys contained in this map.

    • Declaration

      inout @trusted auto byValue(this This)();
      alias opSlice = byValue;

      Return Value

      a range of the values in this map.

    • Declaration

      const @property auto values(this This)();

      Return Value

      a GC-allocated array containing the values contained in this map.

    • Declaration

      inout @trusted auto byKeyValue(this This)();

      Return Value

      a range of the kev/value pairs in this map. The element type of this range is a struct with key and value fields.