Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::AHMapping< Key, ValueType > Class Template Reference

A generic key-value mapping container with inverse operation support. More...

#include <ah-mapping.H>

Collaboration diagram for Aleph::AHMapping< Key, ValueType >:
[legend]

Public Types

using key_type = Key
 Type of the keys.
 
using mapped_type = ValueType
 Type of the mapped values.
 
using value_type = std::pair< const Key, ValueType >
 Type of the key-value pairs.
 
using size_type = size_t
 Type for size-related operations.
 

Public Member Functions

 AHMapping ()=default
 Default constructor.
 
 AHMapping (const AHMapping &other)=default
 Copy constructor.
 
 AHMapping (AHMapping &&other) noexcept=default
 Move constructor.
 
template<typename... Pairs>
 AHMapping (const Key &key, const ValueType &value, Pairs... pairs)
 Constructs a mapping from a list of key-value pairs.
 
AHMappingoperator= (const AHMapping &other)=default
 Copy assignment operator.
 
AHMappingoperator= (AHMapping &&other) noexcept=default
 Move assignment operator.
 
void insert (const Key &key, const ValueType &value)
 Inserts or updates a key-value pair in the mapping.
 
const ValueTypeoperator[] (const Key &key) const
 Accesses the value associated with the given key.
 
ValueTypeoperator[] (const Key &key)
 Accesses the value associated with the given key.
 
AHMapping< ValueType, Key > inverse () const
 Creates a new mapping with keys and values swapped.
 
bool remove (const Key &key) noexcept
 Removes the key-value pair with the given key.
 
bool valid_key (const Key &key) const noexcept
 Checks if a key exists in the mapping.
 
DynList< Key > keys () const
 Gets all keys in the mapping.
 
DynList< ValueTypevalues () const
 Gets all values in the mapping.
 
size_t size () const noexcept
 Gets the number of key-value pairs in the mapping.
 
bool empty () const noexcept
 Checks if the mapping is empty.
 
void clear () noexcept
 Removes all key-value pairs from the mapping.
 
bool contains_value (const ValueType &value) const
 Checks if the mapping contains a specific value.
 
template<typename F >
void for_each (F f) const
 Applies a function to each key-value pair.
 

Private Attributes

DynMapTree< Key, ValueTypetbl
 

Detailed Description

template<typename Key, typename ValueType>
class Aleph::AHMapping< Key, ValueType >

A generic key-value mapping container with inverse operation support.

This class provides a dictionary-like interface with the ability to create an inverse mapping where values become keys and vice versa. It's implemented using a balanced binary search tree (DynMapTree) internally.

Template Parameters
KeyThe type of the keys in the mapping
ValueTypeThe type of the values in the mapping
Note
The ValueType must be copy-constructible and have operator< defined for the inverse() operation to work correctly.

Definition at line 69 of file ah-mapping.H.

Member Typedef Documentation

◆ key_type

template<typename Key , typename ValueType >
using Aleph::AHMapping< Key, ValueType >::key_type = Key

Type of the keys.

Definition at line 74 of file ah-mapping.H.

◆ mapped_type

template<typename Key , typename ValueType >
using Aleph::AHMapping< Key, ValueType >::mapped_type = ValueType

Type of the mapped values.

Definition at line 75 of file ah-mapping.H.

◆ size_type

Type for size-related operations.

Definition at line 77 of file ah-mapping.H.

◆ value_type

template<typename Key , typename ValueType >
using Aleph::AHMapping< Key, ValueType >::value_type = std::pair<const Key, ValueType>

Type of the key-value pairs.

Definition at line 76 of file ah-mapping.H.

Constructor & Destructor Documentation

◆ AHMapping() [1/4]

template<typename Key , typename ValueType >
Aleph::AHMapping< Key, ValueType >::AHMapping ( )
default

Default constructor.

Creates an empty mapping.

◆ AHMapping() [2/4]

template<typename Key , typename ValueType >
Aleph::AHMapping< Key, ValueType >::AHMapping ( const AHMapping< Key, ValueType > &  other)
default

Copy constructor.

Parameters
otherThe mapping to copy from

◆ AHMapping() [3/4]

template<typename Key , typename ValueType >
Aleph::AHMapping< Key, ValueType >::AHMapping ( AHMapping< Key, ValueType > &&  other)
defaultnoexcept

Move constructor.

Parameters
otherThe mapping to move from

◆ AHMapping() [4/4]

template<typename Key , typename ValueType >
template<typename... Pairs>
Aleph::AHMapping< Key, ValueType >::AHMapping ( const Key &  key,
const ValueType value,
Pairs...  pairs 
)
inline

Constructs a mapping from a list of key-value pairs.

Parameters
keyThe first key
valueThe first value
pairsAdditional key-value pairs (must be in pairs)
Note
The number of arguments must be even (key-value pairs)

Definition at line 106 of file ah-mapping.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), and Aleph::AHMapping< Key, ValueType >::tbl.

Member Function Documentation

◆ clear()

template<typename Key , typename ValueType >
void Aleph::AHMapping< Key, ValueType >::clear ( )
inlinenoexcept

Removes all key-value pairs from the mapping.

Definition at line 247 of file ah-mapping.H.

References Aleph::DynSetTree< Key, Tree, Compare >::empty(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by TEST_F().

◆ contains_value()

template<typename Key , typename ValueType >
bool Aleph::AHMapping< Key, ValueType >::contains_value ( const ValueType value) const
inline

Checks if the mapping contains a specific value.

Parameters
valueThe value to search for
Returns
true if the value is found, false otherwise

Definition at line 257 of file ah-mapping.H.

References LocateFunctions< Container, Type >::get_it(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by demo_numeric_mapping(), and TEST_F().

◆ empty()

template<typename Key , typename ValueType >
bool Aleph::AHMapping< Key, ValueType >::empty ( ) const
inlinenoexcept

Checks if the mapping is empty.

Returns
true if the mapping is empty, false otherwise

Definition at line 239 of file ah-mapping.H.

References Aleph::AHMapping< Key, ValueType >::size().

Referenced by TEST_F(), TEST_F(), and TEST_F().

◆ for_each()

template<typename Key , typename ValueType >
template<typename F >
void Aleph::AHMapping< Key, ValueType >::for_each ( F  f) const
inline

Applies a function to each key-value pair.

Template Parameters
FFunction type with signature void(const Key&, const ValueType&)
Parameters
fThe function to apply

Definition at line 271 of file ah-mapping.H.

References LocateFunctions< Container, Type >::get_it(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by demo_numeric_mapping(), and TEST_F().

◆ insert()

template<typename Key , typename ValueType >
void Aleph::AHMapping< Key, ValueType >::insert ( const Key &  key,
const ValueType value 
)
inline

Inserts or updates a key-value pair in the mapping.

Parameters
keyThe key to insert/update
valueThe value to associate with the key

Definition at line 131 of file ah-mapping.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by demo_numeric_mapping(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ inverse()

template<typename Key , typename ValueType >
AHMapping< ValueType, Key > Aleph::AHMapping< Key, ValueType >::inverse ( ) const
inline

Creates a new mapping with keys and values swapped.

Returns
A new mapping where original values are keys and vice versa
Note
If the original mapping has duplicate values, the last inserted key-value pair will overwrite previous ones in the inverse.

Definition at line 166 of file ah-mapping.H.

References LocateFunctions< Container, Type >::get_it(), Aleph::DynList< T >::insert(), Aleph::maps(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ keys()

template<typename Key , typename ValueType >
DynList< Key > Aleph::AHMapping< Key, ValueType >::keys ( ) const
inline

Gets all keys in the mapping.

Returns
A list of all keys

Definition at line 209 of file ah-mapping.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::keys(), and Aleph::AHMapping< Key, ValueType >::tbl.

◆ operator=() [1/2]

template<typename Key , typename ValueType >
AHMapping & Aleph::AHMapping< Key, ValueType >::operator= ( AHMapping< Key, ValueType > &&  other)
defaultnoexcept

Move assignment operator.

Parameters
otherThe mapping to move from
Returns
Reference to this mapping

◆ operator=() [2/2]

template<typename Key , typename ValueType >
AHMapping & Aleph::AHMapping< Key, ValueType >::operator= ( const AHMapping< Key, ValueType > &  other)
default

Copy assignment operator.

Parameters
otherThe mapping to copy from
Returns
Reference to this mapping

◆ operator[]() [1/2]

template<typename Key , typename ValueType >
ValueType & Aleph::AHMapping< Key, ValueType >::operator[] ( const Key &  key)
inline

Accesses the value associated with the given key.

Parameters
keyThe key to look up
Returns
Reference to the associated value (creates if doesn't exist)
Note
If key doesn't exist, inserts a default-constructed value

Definition at line 155 of file ah-mapping.H.

References Aleph::AHMapping< Key, ValueType >::tbl.

◆ operator[]() [2/2]

template<typename Key , typename ValueType >
const ValueType & Aleph::AHMapping< Key, ValueType >::operator[] ( const Key &  key) const
inline

Accesses the value associated with the given key.

Parameters
keyThe key to look up
Returns
Const reference to the associated value
Exceptions
std::domain_errorif the key is not found

Definition at line 142 of file ah-mapping.H.

References ah_domain_error_if, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), and Aleph::AHMapping< Key, ValueType >::tbl.

◆ remove()

template<typename Key , typename ValueType >
bool Aleph::AHMapping< Key, ValueType >::remove ( const Key &  key)
inlinenoexcept

Removes the key-value pair with the given key.

Parameters
keyThe key to remove
Returns
true if the key was found and removed, false otherwise

Definition at line 182 of file ah-mapping.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::remove(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by TEST_F(), and TEST_F().

◆ size()

template<typename Key , typename ValueType >
size_t Aleph::AHMapping< Key, ValueType >::size ( ) const
inlinenoexcept

Gets the number of key-value pairs in the mapping.

Returns
The number of elements

Definition at line 230 of file ah-mapping.H.

References Aleph::DynSetTree< Key, Tree, Compare >::size(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by Aleph::AHMapping< Key, ValueType >::empty(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ valid_key()

template<typename Key , typename ValueType >
bool Aleph::AHMapping< Key, ValueType >::valid_key ( const Key &  key) const
inlinenoexcept

Checks if a key exists in the mapping.

Parameters
keyThe key to check
Returns
true if the key exists, false otherwise

Definition at line 200 of file ah-mapping.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::has(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ values()

template<typename Key , typename ValueType >
DynList< ValueType > Aleph::AHMapping< Key, ValueType >::values ( ) const
inline

Gets all values in the mapping.

Returns
A list of all values

Definition at line 218 of file ah-mapping.H.

References Aleph::DynList< T >::append(), LocateFunctions< Container, Type >::get_it(), and Aleph::AHMapping< Key, ValueType >::tbl.

Referenced by demo_numeric_mapping().

Member Data Documentation

◆ tbl


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