Skip to content

ourovoros-io/sway-hashmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Sway Memory HashMap

Implementation of a hash map container whose content lives in memory.

Functions

Insert

Inserts a key-value pair into the map.

Arguments

  • key: [K] - The key to which the value is paired.
  • value: [V] - The value to be stored.

Examples

use sway_hashmap::HashMap;

fn foo(ref mut map: HashMap<u64, bool>) {
    let key = 5_u64;
    let value = true;
    map.insert(key, value);
    let retrieved_value = map.get(key).unwrap();
    assert(value == retrieved_value);
}

Get

Retrieves the entry stored at key, regardless of whether a value is actually stored at that location or not.

Arguments

  • key: [K] - The key to which the value is paired.

Returns

  • [Option<V>] - The value stored at key if any.

Examples

use sway_hashmap::HashMap;

fn foo(ref mut map: HashMap<u64, bool>) {
    let key = 5_u64;
    let value = true;
    map.insert(key, value);
    let retrieved_value = map.get(key).unwrap();
    assert(value == retrieved_value);
}

Remove

Clears a value previously stored using a key

Arguments

  • key: [K] - The key to which the value is paired.

Returns

  • [bool] - Indicates whether there was a value previously stored at key.

Examples

use sway_hashmap::HashMap;

fn foo(ref mut map: HashMap<u64, bool>) {
    let key = 5_u64;
    let value = true;
    map.insert(key, value);
    let removed = map.remove(key);
    assert(removed);
    assert(map.get(key).is_none());
}

TryInsert

Inserts a key-value pair into the map if a value does not already exist for the key.

Arguments

  • key: [K] - The key to which the value is paired.
  • value: [V] - The value to be stored.

Returns

  • [Result<V, HashMapError<V>>] - Result::Ok(value) if the value was inserted, or Result::Err(HashMapError::OccupiedError(pre_existing_value)) if a value already existed for the key.

Examples

use sway_hashmap::{HashMap, HashMapError};

fn foo(ref mut map: HashMap<u64, bool>) {
    let key = 5_u64;
    let value = true;
    map.insert(key, value);

    let new_value = false;
    let result = map.try_insert(key, new_value);
    assert(result == Result::Err(HashMapError::OccupiedError(value))); // The old value is returned.

    let retrieved_value = map.get(key).unwrap();
    assert(value == retrieved_value); // New value was not inserted, as a value already existed.

    let key2 = 10_u64;
    let returned_value = map.try_insert(key2, new_value);
    assert(returned_value == Result::Ok(new_value)); // New value is returned.
}

About

Implementation of a hash map container whose content lives in memory.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages