The OpenD Programming Language

HashSet

Hash Set.

Constructors

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

A destructor is present on this object, but not explicitly documented in the source.

Postblit

A postblit is present on this object, but not explicitly documented in the source.

Members

Aliases

insertAnywhere
alias insertAnywhere = insert
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

opOpAssign
bool opOpAssign(T item)

Inserts the given item into the set.

opSlice
auto opSlice()

Forward range interface

remove
bool remove(T value)

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

Meta