holoscan::pose_tree::HashMap

Beta
View as Markdown
template <typename Key, typename Value>
class HashMap

A hash map implementation using open addressing with linear probing.

This class provides a fixed-capacity hash map that uses pre-allocated memory and linear probing for collision resolution. It supports basic operations like insert, get, erase, and has. The hash map is designed for performance-critical scenarios where memory allocation should be minimized.

#include <holoscan/pose_tree/hash_map.hpp>

Template parameters

Key
typename

The key type for the hash map.

Value
typename

The value type for the hash map.


Constructors

HashMap

holoscan::pose_tree::HashMap<Key, Value>::HashMap() = default

Default constructor used to be able to pre-allocate memory.


Methods

initialize

expected_t<void> holoscan::pose_tree::HashMap<Key, Value>::initialize(
int32_t size,
int32_t capacity
)

Reserve memory for the hash map.

Returns: Success or error status.

Parameters

size
int32_t

Maximum number of elements the hash map can store.

capacity
int32_t

Total capacity of the internal array (should be larger than size for efficiency).

has

bool holoscan::pose_tree::HashMap<Key, Value>::has(
const Key &key
) const

Check if a key exists in the hash map.

Returns: True if the key exists, false otherwise.

Parameters

key
const Key &

The key to search for.

get

expected_t<Value> holoscan::pose_tree::HashMap<Key, Value>::get(
const Key &key
) const

Get the value associated with a key.

Returns: Value associated with the key on success, Error::kKeyNotFound if key doesn’t exist.

Parameters

key
const Key &

The key to search for.

try_get

expected_t<Value *> holoscan::pose_tree::HashMap<Key, Value>::try_get(
const Key &key
)

Try to get the value ptr associated with a key.

Returns: Value associated with the key on success, Error::kKeyNotFound if key doesn’t exist.

Parameters

key
const Key &

The key to search for.

insert

expected_t<Value *> holoscan::pose_tree::HashMap<Key, Value>::insert(
const Key &key,
const Value &value
)

Insert a key-value pair into the hash map.

Returns: Success or error status. Error::kHashMapFull if the map is full, Error::kKeyAlreadyExists if the key already exists.

Parameters

key
const Key &

The key to insert.

value
const Value &

The value to associate with the key.

erase

expected_t<void> holoscan::pose_tree::HashMap<Key, Value>::erase(
const Key &key
)

Remove a key-value pair from the hash map.

Returns: Success or error status. Error::kKeyNotFound if the key doesn’t exist.

Parameters

key
const Key &

The key to remove.

size

int32_t holoscan::pose_tree::HashMap<Key, Value>::size() const

Get the current number of elements in the hash map.

Returns: Current size of the hash map.

capacity

int32_t holoscan::pose_tree::HashMap<Key, Value>::capacity() const

Get the capacity of the hash map.

Returns: Max capacity of the hash map.

insert_impl

template <typename ValueType>
expected_t<Value *> holoscan::pose_tree::HashMap<Key, Value>::insert_impl(
const Key &key,
ValueType &&value
)

Internal implementation for inserting key-value pairs.

Returns: Success or error status.

Parameters

key
const Key &

The key to insert.

value
ValueType &&

The value to associate with the key.

fill_holes

void holoscan::pose_tree::HashMap<Key, Value>::fill_holes(
int index
)

Fill holes in the hash table after deletion to maintain linear probing invariants.

This method is called after an element is deleted to ensure that subsequent lookups continue to work correctly with linear probing.

Parameters

index
int

The index where a deletion occurred.

get_index

expected_t<int32_t> holoscan::pose_tree::HashMap<Key, Value>::get_index(
const Key &key
) const

Get the index where the key is.

Returns: The index associated with the key on success, Error::kKeyNotFound if key doesn’t exist.

Parameters

key
const Key &

The key to search for.


Types

Typedefs

NameDefinitionDescription
expected_texpected< T, Error >Expected type used by this class.
unexpected_tunexpected< Error >Unexpected type used by this class.
hash_tuint64_tHash type used internally.

Error

Error codes used by this class.

NameValueDescription
kInvalidArgumentkInvalidArgument is returned when a function is called with argument that does not make sense such as negative size or capacity smaller than size.
kOutOfMemorykOutOfMemory is returned when a memory allocation fails.
kHashMapFullkHashMapFull is returned if the hash map is full.
kKeyNotFoundkKeyNotFound is returned if the key is not found in the hash map.
kKeyAlreadyExistskKeyAlreadyExists is returned if the key already exists in the hash map.

Member variables

NameTypeDescription
entries_std::unique_ptr< Entry[]>Array of entries in the hash map.
size_int32_tCurrent number of elements in the hash map.
max_size_int32_tMaximum number of elements the hash map can store.
capacity_int32_tTotal capacity of the internal array.

Inner classes

Entry

struct holoscan::pose_tree::HashMap::Entry

Internal structure representing an entry in the hash map.

NameTypeDescription
hashhash_tHash value of the key.
keyKeyThe key.
is_occupiedboolWhether this entry is occupied.
valueValueThe value associated with the key.