containers.hashset
Hash Set
License
-
Declaration
structHashSet(T, Allocator = Mallocator, alias hashFunction = generateHash!T, bool supportGC = shouldAddGCRange!T, bool storeHash = !isBasicType!T);Hash Set.
Parameters
Tthe element type
Allocatorthe allocator to use. Defaults to
Mallocator.hashFunctionthe hash function to use on the elements
supportGCtrueif the container should support holding references to GC-allocated memory.Examples
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(Allocatorallocator);Use the given
for allocations.allocator -
Declaration
this(size_tbucketCount, Allocatorallocator);Constructs a HashSet with an initial bucket count of
bucketCount.bucketCountmust be a power of two. -
Declaration
voidclear();Removes all items from the set
-
Declaration
boolremove(Tvalue);Removes the given item from the set.
Return Value
falseif thevaluewas not present -
Declaration
inout boolcontains(Tvalue);Return Value
trueifvalueis contained in the set. -
Declaration
inout inout(T)*opBinaryRight(string op)(Tvalue) if (op == "in");Supports a in b syntax
-
Declaration
boolinsert(Tvalue);
boolopOpAssign(string op)(Titem) if (op == "~");
aliasput= insert;
aliasinsertAnywhere= insert;Inserts the given
iteminto the set.Parameters
Tvaluethe
valuetoinsertReturn Value
trueif thevaluewas actually inserted, orfalseif it was already present. -
Declaration
const pure nothrow @nogc @property @safe boolempty();Return Value
trueif the set has no items -
Declaration
const pure nothrow @nogc @property @safe size_tlength();Return Value
the number of items in the set
-
Declaration
nothrow @nogc @trusted autoopSlice(this This)();Forward range interface