HashSet

Hash Set.

Constructors

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
bool opBinaryRight(T value)

Supports a in b syntax

range
Range range()

Forward range interface

remove
bool remove(T value)

Removes the given item from the set.

Parameters

T

the element type

hashFunction

the hash function to use on the elements

Examples

import std.array : array;
import std.algorithm : canFind;
import std.uuid : randomUUID;
auto s = HashSet!string(16);
assert (!s.contains("nonsense"));
s.put("test");
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.range.array;
assert (strings.canFind("a"));
assert (strings.canFind("b"));
assert (strings.canFind("c"));
assert (strings.canFind("d"));
assert (strings.canFind("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);

Meta