CMagic  0.5.0
Portable C library of utilities and data structures
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
cmagic::map< Key, Value > Class Template Reference

Classes

class  iterator
 

Public Types

using key_type = Key
 Type of map keys. More...
 
using mapped_type = Value
 Type of map values. More...
 
using value_type = std::pair< key_type, mapped_type >
 Type of map elements. More...
 
using size_type = size_t
 Type used to measure element size. More...
 

Public Member Functions

 map ()
 Constructs an empty map with standard memory allocation. More...
 
 operator bool () const
 Checks if the map is properly initialized. More...
 
iterator begin () const
 Return iterator to beginning. More...
 
iterator end () const
 Return iterator to end. More...
 
void clear ()
 Removes all elements from the map, leaving the container with a size of 0. More...
 
std::pair< iterator, bool > insert (const value_type &val)
 Inserts a new element to the map if its key is not equivalent to any element already contained in the map. More...
 
std::pair< iterator, bool > insert (value_type &&val)
 Inserts a new element to the map if its key is not equivalent to any element already contained in the map. More...
 
void erase (const key_type &key)
 Removes a single element from the map. More...
 
size_type size () const
 Returns the number of elements in the map. More...
 
bool empty () const
 Returns whether the map is empty (i.e. whether its size is 0). More...
 
iterator find (const key_type &key) const
 Searches the container for an element with a key equivalent to key and returns an iterator to it if found, otherwise it returns map::end. More...
 

Static Public Member Functions

static map custom_allocation_map ()
 Constructs an empty map using custom CMagic memory allocation from memory.h. More...
 

Member Typedef Documentation

◆ key_type

template<typename Key , typename Value >
using cmagic::map< Key, Value >::key_type = Key

Type of map keys.

◆ mapped_type

template<typename Key , typename Value >
using cmagic::map< Key, Value >::mapped_type = Value

Type of map values.

◆ size_type

template<typename Key , typename Value >
using cmagic::map< Key, Value >::size_type = size_t

Type used to measure element size.

◆ value_type

template<typename Key , typename Value >
using cmagic::map< Key, Value >::value_type = std::pair<key_type, mapped_type>

Type of map elements.

Constructor & Destructor Documentation

◆ map()

template<typename Key , typename Value >
cmagic::map< Key, Value >::map ( )
inline

Constructs an empty map with standard memory allocation.

Returns
a new empty map

Member Function Documentation

◆ begin()

template<typename Key , typename Value >
iterator cmagic::map< Key, Value >::begin ( ) const
inline

Return iterator to beginning.

Returns an iterator pointing to the first element in the map. If the container is empty, the returned iterator value shall not be dereferenced.

Returns
an iterator to the beginning of the container

◆ clear()

template<typename Key , typename Value >
void cmagic::map< Key, Value >::clear ( )
inline

Removes all elements from the map, leaving the container with a size of 0.

◆ custom_allocation_map()

template<typename Key , typename Value >
static map cmagic::map< Key, Value >::custom_allocation_map ( )
inlinestatic

Constructs an empty map using custom CMagic memory allocation from memory.h.

Returns
a new empty map

◆ empty()

template<typename Key , typename Value >
bool cmagic::map< Key, Value >::empty ( ) const
inline

Returns whether the map is empty (i.e. whether its size is 0).

This function does not modify the container in any way. To clear the content of a map, see map::clear.

Returns
true if the container size is 0, false otherwise

◆ end()

template<typename Key , typename Value >
iterator cmagic::map< Key, Value >::end ( ) const
inline

Return iterator to end.

It does not point to any element, and thus shall not be dereferenced.

Returns
an iterator to the element past the end of the sequence

◆ erase()

template<typename Key , typename Value >
void cmagic::map< Key, Value >::erase ( const key_type key)
inline

Removes a single element from the map.

Parameters
keykey of the value to be removed from the map. Function does nothing if the key doesn't exist in the map.

◆ find()

template<typename Key , typename Value >
iterator cmagic::map< Key, Value >::find ( const key_type key) const
inline

Searches the container for an element with a key equivalent to key and returns an iterator to it if found, otherwise it returns map::end.

Parameters
keykey to be searched for
Returns
an iterator to the element, if key is found, or map::end otherwise

◆ insert() [1/2]

template<typename Key , typename Value >
std::pair<iterator, bool> cmagic::map< Key, Value >::insert ( const value_type val)
inline

Inserts a new element to the map if its key is not equivalent to any element already contained in the map.

Because keys in a map are unique, the insertion operation checks whether an inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element.

Parameters
valvalue to be copied (or moved) to the map
Returns
a pair, with its member pair::first map to an iterator pointing to either the newly inserted element or to the equivalent element already in the map or end if allocation of the new element has failed. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed (or could not be inserted due to allocation failure).

◆ insert() [2/2]

template<typename Key , typename Value >
std::pair<iterator, bool> cmagic::map< Key, Value >::insert ( value_type &&  val)
inline

Inserts a new element to the map if its key is not equivalent to any element already contained in the map.

Because keys in a map are unique, the insertion operation checks whether an inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element.

Parameters
valvalue to be copied (or moved) to the map
Returns
a pair, with its member pair::first map to an iterator pointing to either the newly inserted element or to the equivalent element already in the map or end if allocation of the new element has failed. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed (or could not be inserted due to allocation failure).

◆ operator bool()

template<typename Key , typename Value >
cmagic::map< Key, Value >::operator bool ( ) const
inline

Checks if the map is properly initialized.

Example usage:

if (map) {
map.insert({ "key", 123 });
} else {
std::cerr << "Map allocation failed!\n";
}
Returns
true if map is initialized, false if map allocation has failed and no operation should be made on it

◆ size()

template<typename Key, typename Value>
size_type cmagic::map< Key, Value >::size ( ) const
inline

Returns the number of elements in the map.

Returns
number of elements in the map

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