AlgoPlus v0.1.0
Loading...
Searching...
No Matches
hash_table< KeyType, ValueType > Class Template Reference

A simple implementation of a hash table. More...

#include <hash_table.h>

Classes

class  Iterator
 Iterator class. More...
 

Public Types

using BucketType = std::unordered_map<size_t, std::list<std::pair<KeyType, ValueType>>>
 

Public Member Functions

 hash_table (std::vector< std::pair< KeyType, ValueType > > v={})
 Construct a new hash table object.
 
 hash_table (const hash_table &h)
 Copy constructor of the hash_table.
 
hash_tableoperator= (const hash_table &h)
 operator = for the hash_table class
 
 ~hash_table ()
 Destroy the hash table object.
 
void insert (const KeyType &key, const ValueType &value)
 Inserts a key-value pair into the hash table.
 
std::optional< ValueType > retrieve (const KeyType &key)
 Retrieves the value associated with the given key.
 
void remove (const KeyType &key)
 Removes the key-value pair associated with the given key from the hash table.
 
Iterator begin ()
 
Iterator end ()
 

Friends

std::ostream & operator<< (std::ostream &out, hash_table< KeyType, ValueType > &h)
 << operator for hash_table class
 

Detailed Description

template<typename KeyType, typename ValueType>
class hash_table< KeyType, ValueType >

A simple implementation of a hash table.

Template Parameters
KeyTypeType of the keys in the hash table.
ValueTypeType of the values in the hash table.

This is a templated class for a hash table, a data structure that provides fast data retrieval and storage operations based on keys. The template parameters are the type of the keys and the type of the values stored in the hash table. Keys cannot be duplicate and an insertion of an existing key leads to an update of the corresponding value.

The following are the class methods

Note
Use only types that can be hashed as the KeyType.

Constructor & Destructor Documentation

◆ hash_table() [1/2]

template<typename KeyType , typename ValueType >
hash_table< KeyType, ValueType >::hash_table ( std::vector< std::pair< KeyType, ValueType > > v = {})
inline

Construct a new hash table object.

Parameters
vthe initializer vector

◆ hash_table() [2/2]

template<typename KeyType , typename ValueType >
hash_table< KeyType, ValueType >::hash_table ( const hash_table< KeyType, ValueType > & h)
inline

Copy constructor of the hash_table.

Parameters
hthe hash table we want to copy

Member Function Documentation

◆ insert()

template<typename KeyType , typename ValueType >
void hash_table< KeyType, ValueType >::insert ( const KeyType & key,
const ValueType & value )
inline

Inserts a key-value pair into the hash table.

This function inserts a key-value pair into the hash table. If a pair with the same key already exists, it updates the value.

Parameters
keyThe key to insert.
valueThe value to insert.

◆ operator=()

template<typename KeyType , typename ValueType >
hash_table & hash_table< KeyType, ValueType >::operator= ( const hash_table< KeyType, ValueType > & h)
inline

operator = for the hash_table class

Parameters
hthe hash table we want to copy
Returns
hash_table&

◆ remove()

template<typename KeyType , typename ValueType >
void hash_table< KeyType, ValueType >::remove ( const KeyType & key)
inline

Removes the key-value pair associated with the given key from the hash table.

This function removes the key-value pair associated with the given key from the hash table.

Parameters
keyThe key to remove.

◆ retrieve()

template<typename KeyType , typename ValueType >
std::optional< ValueType > hash_table< KeyType, ValueType >::retrieve ( const KeyType & key)
inline

Retrieves the value associated with the given key.

Parameters
keyThe key to retrieve the value for.
Returns
The value associated with the given key, if it exists. Otherwise, returns std::nullopt.

Friends And Related Symbol Documentation

◆ operator<<

template<typename KeyType , typename ValueType >
std::ostream & operator<< ( std::ostream & out,
hash_table< KeyType, ValueType > & h )
friend

<< operator for hash_table class

Returns
std::ostream&

The documentation for this class was generated from the following file: