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

Associative array. 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 first element of the reversed array.
 
reverse_iterator rend ()
 Returns a reverse iterator to the element. following the last element of the reversed array.
 
size_t size () const
 Number of elements in the container. More...
 
int size_s () const
 Number of elements in the container as signed value. More...
 
size_t length () const
 Same as size(). More...
 
bool isEmpty () const
 Checks if the container has no elements. More...
 
bool isNotEmpty () const
 Checks if the container has elements. 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())
 Remove element with key key from the array and return it.
 
PIMap< Key, T > & operator<< (const PIMap< Key, T > &other)
 Inserts all elements in array other to this array with overwrite.
 
bool operator== (const PIMap< Key, T > &m) const
 Compare operator with array m.
 
bool operator!= (const PIMap< Key, T > &m) const
 Compare operator with array m.
 
bool contains (const Key &key) const
 Tests if element with key key exists in the array.
 
bool containsValue (const T &value) const
 Tests if element with value value exists in the array.
 
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)
 Remove element with key key from the array.
 
PIMap< Key, T > & removeWhere (std::function< bool(const Key &key, const T &value)> test)
 Remove all elements in the array passes the test implemented by the provided function test.
 
PIMap< Key, T > & erase (const Key &key)
 Same as remove().
 
PIMap< Key, T > & clear ()
 Clear array, remove all elements. More...
 
void swap (PIMap< Key, T > &other)
 Swaps array v other with this array. More...
 
PIMap< Key, T > & insert (const Key &key, const T &value)
 Inserts value value with key key in the array. More...
 
PIMap< Key, T > & insert (const PIPair< Key, T > &pair)
 Inserts value pair in the array. 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 an array of values of all elements.
 
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 an array of keys of all elements.
 
void forEach (std::function< void(const Key &key, const T &value)> f) const
 Execute function void f(const Key & key, const T & value) for every element in array.
 
template<typename Key2 , typename T2 >
PIMap< Key2, T2 > map (std::function< PIPair< Key2, T2 >(const Key &key, const T &value)> f) const
 Сreates a new map PIMap<Key2, T2> populated with the results of calling a provided function PIPair<Key2, T2> f(const Key & key, const T & value) on every element in the calling array.
 
template<typename ST >
PIVector< ST > map (std::function< ST(const Key &key, const T &value)> f) const
 Сreates a new array PIVector<ST> populated with the results of calling a provided function ST f(const Key & key, const T & value) on every element in the calling array.
 
PIMap< Key, T > filter (std::function< bool(const Key &key, const T &value)> test) const
 Returns a new array with all elements 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 >

Associative array.

A collection of key/value pairs, from which you retrieve a value using its associated key. There is a finite number of keys in the map, and each key has exactly one value associated with it. value() returns value for key and leave map unchaged in any case. operator [] create entry in map if there is no entry for given key. You can retrieve all keys by method keys() and all values by methos values(). To iterate all entries use class PIMapIterator, or methods makeIterator() and makeReverseIterator(). A key in the Map may only occur once.

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
Macro used for conditional (piDebug) output to PICout(StdOut)
Definition: picout.h:35

Member Function Documentation

◆ size()

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

Number of elements in the container.

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 elements in the container as 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 if the container has no elements.

Returns
true if the container is empty, false otherwise
See also
size(), size_s(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ isNotEmpty()

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

Checks if the container has elements.

Returns
true if the container is not empty, false otherwise
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

Clear array, remove all elements.

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 array v other with this array.

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 with key key in the array.

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 value pair in the array.

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