containers.hashset

Hash Set

Authors

Brian Schott

  • Declaration

    struct HashSet(T, Allocator = Mallocator, alias hashFunction = generateHash!T, bool supportGC = shouldAddGCRange!T, bool storeHash = !isBasicType!T);

    Hash Set.

    Parameters

    T

    the element type

    Allocator

    the allocator to use. Defaults to Mallocator.

    hashFunction

    the hash function to use on the elements

    supportGC

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

    Examples

    1. import std.algorithm : canFind; import std.array : array; import std.range : walkLength; import std.uuid : randomUUID; auto s = HashSet!string(16); assert(!s.contains("nonsense")); assert(s.put("test")); assert(s.contains("test")); assert(!s.put("test")); assert(s.contains("test")); assert(s.length == 1); assert(!s.contains("nothere")); s.put("a"); s.put("b"); s.put("c"); s.put("d"); string[] strings = s[].array; assert(strings.canFind("a")); assert(strings.canFind("b")); assert(strings.canFind("c")); assert(strings.canFind("d")); assert(strings.canFind("test")); assert(*("a" in s) == "a"); assert(*("b" in s) == "b"); assert(*("c" in s) == "c"); assert(*("d" in s) == "d"); assert(*("test" in s) == "test"); assert(strings.length == 5); assert(s.remove("test")); assert(s.length == 4); s.clear(); assert(s.length == 0); assert(s.empty); s.put("abcde"); assert(s.length == 1); foreach (i; 0 .. 10_000) { s.put(randomUUID().toString); } assert(s.length == 10_001); // Make sure that there's no range violation slicing an empty set HashSet!int e; foreach (i; e[]) assert(i > 0); enum MAGICAL_NUMBER = 600_000; HashSet!int f; foreach (i; 0 .. MAGICAL_NUMBER) assert(f.insert(i)); assert(f.length == f[].walkLength); foreach (i; 0 .. MAGICAL_NUMBER) assert(i in f); foreach (i; 0 .. MAGICAL_NUMBER) assert(f.remove(i)); foreach (i; 0 .. MAGICAL_NUMBER) assert(!f.remove(i)); HashSet!int g; foreach (i; 0 .. MAGICAL_NUMBER) assert(g.insert(i)); static struct AStruct { int a; int b; } HashSet!(AStruct*, Mallocator, a => a.a) fred; fred.insert(new AStruct(10, 10)); auto h = new AStruct(10, 10); assert(h in fred);

    • Declaration

      this(Allocator allocator);

      Use the given allocator for allocations.

    • Declaration

      this(size_t bucketCount, Allocator allocator);

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

    • Declaration

      void clear();

      Removes all items from the set

    • Declaration

      bool remove(T value);

      Removes the given item from the set.

      Return Value

      false if the value was not present

    • Declaration

      inout bool contains(T value);

      Return Value

      true if value is contained in the set.

    • Declaration

      inout inout(T)* opBinaryRight(string op)(T value) if (op == "in");

      Supports a in b syntax

    • Declaration

      bool insert(T value);
      bool opOpAssign(string op)(T item) if (op == "~");
      alias put = insert;
      alias insertAnywhere = insert;

      Inserts the given item into the set.

      Parameters

      T value

      the value to insert

      Return Value

      true if the value was actually inserted, or false if it was already present.

    • Declaration

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

      Return Value

      true if the set has no items

    • Declaration

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

      Return Value

      the number of items in the set

    • Declaration

      nothrow @nogc @trusted auto opSlice(this This)();

      Forward range interface