HashSet

Hash Set.

Constructors

this
this()

Undocumented in source.

this
this(Allocator allocator)

Use the given allocator for allocations.

this
this(size_t bucketCount, Allocator allocator)

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

this
this(size_t bucketCount)

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

Destructor

~this
~this()

Undocumented in source.

Postblit

this(this)
this(this)

Undocumented in source.

Members

Aliases

opSlice
alias opSlice = range

Forward range interface

put
alias put = insert

Inserts the given item into the set.

Functions

clear
void clear()

Removes all items from the set

contains
bool contains(T value)
empty
bool empty()
insert
bool insert(T value)

Inserts the given item into the set.

length
size_t length()
opBinaryRight
inout(T)* opBinaryRight(T value)

Supports a in b syntax

range
auto range()

Forward range interface

remove
bool remove(T value)

Removes the given item from the set.

Examples

1 import std.array : array;
2 import std.algorithm : canFind;
3 import std.uuid : randomUUID;
4 
5 auto s = HashSet!string(16);
6 assert(!s.contains("nonsense"));
7 assert(s.put("test"));
8 assert(s.contains("test"));
9 assert(!s.put("test"));
10 assert(s.contains("test"));
11 assert(s.length == 1);
12 assert(!s.contains("nothere"));
13 s.put("a");
14 s.put("b");
15 s.put("c");
16 s.put("d");
17 string[] strings = s.range.array;
18 assert(strings.canFind("a"));
19 assert(strings.canFind("b"));
20 assert(strings.canFind("c"));
21 assert(strings.canFind("d"));
22 assert(strings.canFind("test"));
23 assert(*("a" in s) == "a");
24 assert(*("b" in s) == "b");
25 assert(*("c" in s) == "c");
26 assert(*("d" in s) == "d");
27 assert(*("test" in s) == "test");
28 assert(strings.length == 5);
29 assert(s.remove("test"));
30 assert(s.length == 4);
31 s.clear();
32 assert(s.length == 0);
33 assert(s.empty);
34 s.put("abcde");
35 assert(s.length == 1);
36 foreach (i; 0 .. 10_000)
37 {
38 	s.put(randomUUID().toString);
39 }
40 assert(s.length == 10_001);
41 
42 // Make sure that there's no range violation slicing an empty set
43 HashSet!int e;
44 foreach (i; e[])
45 	assert(i > 0);
46 
47 enum MAGICAL_NUMBER = 600_000;
48 
49 HashSet!int f;
50 foreach (i; 0 .. MAGICAL_NUMBER)
51 	assert(f.insert(i));
52 import std.range:walkLength;
53 assert(f.length == f[].walkLength);
54 foreach (i; 0 .. MAGICAL_NUMBER)
55 	assert(i in f);
56 foreach (i; 0 .. MAGICAL_NUMBER)
57 	assert(f.remove(i));
58 foreach (i; 0 .. MAGICAL_NUMBER)
59 	assert(!f.remove(i));
60 
61 HashSet!int g;
62 foreach (i; 0 .. MAGICAL_NUMBER)
63 	assert(g.insert(i));
64 
65 static struct AStruct
66 {
67 	int a;
68 	int b;
69 }
70 
71 HashSet!(AStruct*, Mallocator, a => a.a) fred;
72 fred.insert(new AStruct(10, 10));
73 auto h = new AStruct(10, 10);
74 assert(h in fred);

Meta