std::collections::hashset (v0.2.2)
Hash set of keys.
Latest
Overview
std::collections::hashset stores a set of unique keys with insert, contains, and remove.
insert returns true when the key was new; dropHashSet releases the storage.
Source: thrustc/std/v0.2.2/collections/hashset.thrust
Public Signatures
Exported declarations for this module snapshot.
const HASHSET_INITIAL_CAPACITY: usize @public = 8;
struct HashSetEntry @public {
struct HashSet [K] @public {
fn newHashSet [K] () HashSet[K] @public;
fn withCapacity [K] (capacity: usize) HashSet[K] @public;
fn dropHashSet [K] (set: ptr[HashSet[K]]) void @public @deallocator;
fn getLength [K] (set: ptr[HashSet[K]]) usize @public;
fn getCapacity [K] (set: ptr[HashSet[K]]) usize @public;
fn isEmpty [K] (set: ptr[HashSet[K]]) bool @public;
fn contains [K] (set: ptr[HashSet[K]], key: ptr) bool @public;
fn insert [K] (set: ptr[HashSet[K]], key: ptr) bool @public;
fn remove [K] (set: ptr[HashSet[K]], key: ptr) bool @public;
fn clear [K] (set: ptr[HashSet[K]]) void @public;
fn reserveCapacity [K] (set: ptr[HashSet[K]], requestedCapacity: usize) void @public;
Behavior and Use
insert returns true when the key was new; contains reports presence; remove deletes a key.
Call dropHashSet when the set is no longer needed to release the backing storage.
Examples
import std::collections::hashset;
import std::io;
// Keep track of which feature flags are enabled.
fn main() s32 @public {
var enabled := hashset::newHashSet[s32]();
var flagA: s32 = 7;
hashset::insert[s32](ref enabled, ref flagA);
if hashset::contains[s32](ref enabled, ref flagA) {
io::print("flag A is enabled\n");
}
hashset::dropHashSet[s32](ref enabled);
return 0;
}
import std::collections::hashset;
import std::io;
// Revoke a guest tag from the allowed set and report what remains.
fn main() s32 @public {
var tags := hashset::newHashSet[s32]();
var admin: s32 = 1;
var guest: s32 = 2;
hashset::insert[s32](ref tags, ref admin);
hashset::insert[s32](ref tags, ref guest);
hashset::remove[s32](ref tags, ref guest);
io::print("remaining tags: %d\n", hashset::getLength[s32](ref tags));
hashset::dropHashSet[s32](ref tags);
return 0;
}
import std::collections::hashset;
import std::io;
// Pre-size a set of known VIP ids, then check membership.
fn main() s32 @public {
var vips := hashset::withCapacity[s32](8);
var id: s32 = 5;
hashset::insert[s32](ref vips, ref id);
if hashset::contains[s32](ref vips, ref id) {
io::print("welcome, VIP\n");
}
hashset::dropHashSet[s32](ref vips);
return 0;
}
Notes
- contains reports presence without removing the key.
- insert returns true only for newly added keys.