PIP 5.6.1
Platform-Independent Primitives
Public Member Functions | List of all members
PIMap< Key, T > Class Template Reference

Map of unique keys and associated values. More...

#include <pimap.h>

Public Member Functions

 PIMap ()
 Constructs an empty map.
 
 PIMap (const PIMap< Key, T > &other)
 Copy constructor.
 
 PIMap (PIMap< Key, T > &&other)
 Move constructor.
 
 PIMap (std::initializer_list< std::pair< Key, T > > init_list)
 Contructs map from C++11 initializer list. More...
 
PIMap< Key, T > & operator= (const PIMap< Key, T > &other)
 Assign operator.
 
PIMap< Key, T > & operator= (PIMap< Key, T > &&other)
 Assign move operator.
 
iterator begin ()
 Iterator to the first element.
 
iterator end ()
 Iterator to the element following the last element.
 
reverse_iterator rbegin ()
 Returns a reverse iterator to the last map entry.
 
reverse_iterator rend ()
 Returns a reverse iterator to the position before the first map entry.
 
size_t size () const
 Number of entries in the map. More...
 
int size_s () const
 Number of entries in the map as a signed value. More...
 
size_t length () const
 Same as size(). More...
 
bool isEmpty () const
 Checks whether the map is empty. More...
 
bool isNotEmpty () const
 Checks whether the map contains entries. More...
 
T & operator[] (const Key &key)
 Full access to element key key. More...
 
const T & at (const Key &key) const
 Read only access to element by key. More...
 
take (const Key &key, const T &default_=T())
 Removes entry with key key and returns its value.
 
PIMap< Key, T > & operator<< (const PIMap< Key, T > &other)
 Inserts all entries from other, overwriting existing keys.
 
bool operator== (const PIMap< Key, T > &m) const
 Compares this map with m.
 
bool operator!= (const PIMap< Key, T > &m) const
 Compares this map with m.
 
bool contains (const Key &key) const
 Checks whether the map contains key key.
 
bool containsValue (const T &value) const
 Checks whether the map contains value value.
 
PIMap< Key, T > & reserve (size_t new_size)
 Attempts to allocate memory for at least new_size elements.
 
PIMap< Key, T > & remove (const Key &key)
 Removes entry with key key.
 
PIMap< Key, T > & removeWhere (std::function< bool(const Key &key, const T &value)> test)
 Removes all entries passes the test implemented by the provided function test.
 
PIMap< Key, T > & erase (const Key &key)
 Same as remove().
 
PIMap< Key, T > & clear ()
 Clears the map. More...
 
void swap (PIMap< Key, T > &other)
 Swaps this map with other. More...
 
PIMap< Key, T > & insert (const Key &key, const T &value)
 Inserts value value for key key. More...
 
PIMap< Key, T > & insert (const PIPair< Key, T > &pair)
 Inserts entry pair. More...
 
value (const Key &key, const T &default_=T()) const
 Returns the value of the element by the key key or default_ if there is no such element.
 
PIVector< T > values () const
 Returns values of all map entries.
 
Key key (const T &value, const Key &default_=Key()) const
 Returns the key of the first element whose value matches value or default_ if there is no such element.
 
PIVector< Key > keys () const
 Returns keys of all map entries.
 
void forEach (std::function< void(const Key &key, const T &value)> f) const
 Calls f for every map entry.
 
template<typename Key2 , typename T2 >
PIMap< Key2, T2 > map (std::function< PIPair< Key2, T2 >(const Key &key, const T &value)> f) const
 Creates a new PIMap from results of applying f to each map entry.
 
template<typename ST >
PIVector< ST > map (std::function< ST(const Key &key, const T &value)> f) const
 Creates a new PIVector from results of applying f to each map entry.
 
PIMap< Key, T > filter (std::function< bool(const Key &key, const T &value)> test) const
 Returns a new map with all entries that pass the test implemented by the provided function bool test(const Key & key, const T & value).
 

Detailed Description

template<typename Key, typename T>
class PIMap< Key, T >

Map of unique keys and associated values.

Stores key/value pairs and keeps keys unique. value() returns the value for a key or the provided default, while operator[] creates a default value for a missing key. Use keys() and values() to retrieve map contents, and makeIterator() or makeReverseIterator() to traverse entries.

Constructor & Destructor Documentation

◆ PIMap()

template<typename Key , typename T >
PIMap< Key, T >::PIMap ( std::initializer_list< std::pair< Key, T > >  init_list)
inline

Contructs map from C++11 initializer list.

PIMap <int, PIString> m{{1, "a"}, {2, "b"}};
piCout << m; // {1: a, 2: b}
#define piCout
Definition: picout.h:36

Member Function Documentation

◆ size()

template<typename Key , typename T >
size_t PIMap< Key, T >::size ( ) const
inline

Number of entries in the map.

See also
size_s(), capacity(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ size_s()

template<typename Key , typename T >
int PIMap< Key, T >::size_s ( ) const
inline

Number of entries in the map as a signed value.

See also
size(), capacity(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ length()

template<typename Key , typename T >
size_t PIMap< Key, T >::length ( ) const
inline

Same as size().

See also
size(), size_s(), capacity(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ isEmpty()

template<typename Key , typename T >
bool PIMap< Key, T >::isEmpty ( ) const
inline

Checks whether the map is empty.

See also
size(), size_s(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ isNotEmpty()

template<typename Key , typename T >
bool PIMap< Key, T >::isNotEmpty ( ) const
inline

Checks whether the map contains entries.

See also
size(), size_s(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ operator[]()

template<typename Key , typename T >
T & PIMap< Key, T >::operator[] ( const Key &  key)
inline

Full access to element key key.

If the map contains no item with key key, the function inserts a default-constructed value into the map with key key, and returns a reference to it.

m["огурец"] = 500;
piCout << m; // {огурец: 500}
m["лук"] = 25;
piCout << m; // {огурец: 500, лук: 25}
m["огурец"] = 350;
piCout << m; // {огурец: 350, лук: 25}
See also
insert(), value(), key()

◆ at()

template<typename Key , typename T >
const T & PIMap< Key, T >::at ( const Key &  key) const
inline

Read only access to element by key.

Note
Element with key key must exists, otherwise it will lead to undefined program behavior and memory errors.
See also
operator[](), value(), key()

◆ clear()

template<typename Key , typename T >
PIMap< Key, T > & PIMap< Key, T >::clear ( )
inline

Clears the map.

Note
Reserved memory will not be released.
See also
resize()

◆ swap()

template<typename Key , typename T >
void PIMap< Key, T >::swap ( PIMap< Key, T > &  other)
inline

Swaps this map with other.

This operation is very fast and never fails.

◆ insert() [1/2]

template<typename Key , typename T >
PIMap< Key, T > & PIMap< Key, T >::insert ( const Key &  key,
const T &  value 
)
inline

Inserts value value for key key.

If an element with the key key already exists, it will be overwritten with the value value.

◆ insert() [2/2]

template<typename Key , typename T >
PIMap< Key, T > & PIMap< Key, T >::insert ( const PIPair< Key, T > &  pair)
inline

Inserts entry pair.

The first element of the pair is the key, and the second is the value.