std::collections::hashmap (v0.2.2)
Hash map from keys to values.
Latest
Overview
std::collections::hashmap maps keys to values with insert, get, and remove.
get returns a pointer to the value or nullptr when the key is absent. insert returns true when the key was new. dropHashMap releases the storage.
Source: thrustc/std/v0.2.2/collections/hashmap.thrust
Public Signatures
Exported declarations for this module snapshot.
const HASHMAP_INITIAL_CAPACITY: usize @public = 8;
struct HashMapEntry [V] @public {
struct HashMap [K, V] @public {
fn newHashMap [K, V] () HashMap[K, V] @public;
fn withCapacity [K, V] (capacity: usize) HashMap[K, V] @public;
fn dropHashMap [K, V] (map: ptr[HashMap[K, V]]) void @public @deallocator;
fn getLength [K, V] (map: ptr[HashMap[K, V]]) usize @public;
fn getCapacity [K, V] (map: ptr[HashMap[K, V]]) usize @public;
fn isEmpty [K, V] (map: ptr[HashMap[K, V]]) bool @public;
fn containsKey [K, V] (map: ptr[HashMap[K, V]], key: ptr) bool @public;
fn get [K, V] (map: ptr[HashMap[K, V]], key: ptr) ptr[V] @public;
fn insert [K, V] (map: ptr[HashMap[K, V]], key: ptr, value: V) bool @public;
fn remove [K, V] (map: ptr[HashMap[K, V]], key: ptr) bool @public;
fn clear [K, V] (map: ptr[HashMap[K, V]]) void @public;
fn reserveCapacity [K, V] (map: ptr[HashMap[K, V]], requestedCapacity: usize) void @public;
Behavior and Use
get returns a pointer to the value or nullptr when the key is absent, so callers can test before dereferencing.
insert returns true when the key was new; dropHashMap releases the storage when the map is no longer needed.
Examples
import std::collections::hashmap;
import std::io;
// A small scoreboard keyed by player id.
fn main() s32 @public {
var scores := hashmap::newHashMap[s32, s32]();
var player1: s32 = 1;
var player2: s32 = 2;
hashmap::insert[s32, s32](ref scores, ref player1, 100);
hashmap::insert[s32, s32](ref scores, ref player2, 200);
var found: ptr[s32] = hashmap::get[s32, s32](ref scores, ref player1);
if found != nullptr {
io::print("player 1 score: %d\n", (deref found));
}
hashmap::dropHashMap[s32, s32](ref scores);
return 0;
}
import std::collections::hashmap;
import std::io;
// Track how many times each page id was visited, then tidy up.
fn main() s32 @public {
var visits := hashmap::newHashMap[s32, s32]();
var home: s32 = 1;
var about: s32 = 2;
hashmap::insert[s32, s32](ref visits, ref home, 10);
hashmap::insert[s32, s32](ref visits, ref about, 20);
if hashmap::containsKey[s32, s32](ref visits, ref home) {
io::print("home was visited\n");
}
hashmap::remove[s32, s32](ref visits, ref home);
hashmap::clear[s32, s32](ref visits);
hashmap::dropHashMap[s32, s32](ref visits);
return 0;
}
import std::collections::hashmap;
import std::io;
// A registry of commands indexed by their name.
fn main() s32 @public {
var commands := hashmap::newHashMap[CString, s32]();
var run := "run";
var build := "build";
hashmap::insert[CString, s32](ref commands, run as ptr, 42);
hashmap::insert[CString, s32](ref commands, build as ptr, 42);
var found: ptr[s32] = hashmap::get[CString, s32](ref commands, run as ptr);
if found != nullptr {
io::print("command found\n");
}
hashmap::dropHashMap[CString, s32](ref commands);
return 0;
}
import std::tstring;
import std::collections::hashmap;
import std::io;
// A translation table keyed by message id, using owned strings as keys.
fn main() s32 @public {
var translations := hashmap::newHashMap[tstring::TString, s32]();
var hello := tstring::fromCString("hello");
var lookup := tstring::fromCString("hello");
hashmap::insert[tstring::TString, s32](ref translations, (ref hello) as ptr, 11);
hashmap::insert[tstring::TString, s32](ref translations, (ref lookup) as ptr, 33);
var found: ptr[s32] = hashmap::get[tstring::TString, s32](ref translations, (ref lookup) as ptr);
if found != nullptr {
io::print("translation present\n");
}
hashmap::dropHashMap[tstring::TString, s32](ref translations);
tstring::dropTString(ref hello);
tstring::dropTString(ref lookup);
return 0;
}
Notes
- get returns a borrowed pointer; do not use it after the map is destroyed or rehashed.
- Check the returned pointer for nullptr before dereferencing.