libmoost
|
multi_map is a container that associates keys to list of values. More...
#include <multi_map.hpp>
Classes | |
class | const_iterator |
class | const_range |
class | iterator |
class | range |
Public Types | |
typedef TKey | first_type |
typedef TVal | second_type |
typedef policies::map_policy_selector < TKey, multimap_value_type, TLocMap >::policy_type | loc_map_policy_type |
typedef std::vector< TVal > ::iterator | range_iterator |
typedef std::vector< TVal > ::const_iterator | const_range_iterator |
Public Member Functions | |
iterator | begin () |
const_iterator | begin () const |
iterator | end () |
const_iterator | end () const |
multi_map (const loc_map_policy_type &locHandlerPolicy=loc_map_policy_type()) | |
template<int WhichKey, typename Iterator > | |
void | create_map (Iterator first, Iterator last, size_t suggestedSize=128) |
template<typename Iterator , typename GetKeyPolicy , typename GetValuesPolicy > | |
void | create_map (Iterator first, Iterator last, const GetKeyPolicy &getKey, const GetValuesPolicy &getValues, size_t suggestedSize=128) |
template<int WhichKey, typename TFirst , typename TSecond > | |
void | create_map (std::vector< TFirst, TSecond > &i2i, bool doSort=true) |
template<int WhichKey, typename TFirst , typename TSecond > | |
void | create_map_compressed (std::vector< TFirst, TSecond > &i2i, bool doSort=true) |
template<int WhichKey, typename Iterator > | |
void | create_map_compressed (Iterator first, Iterator last, size_t suggestedSize=128) |
range | operator[] (const TKey &key) |
const_range | operator[] (const TKey &key) const |
void | clear () |
Will clear the whole table. | |
size_t | size () const |
Returns the number of keys. | |
bool | empty () const |
Protected Attributes | |
TLocMap | m_locations |
loc_map_policy_type | m_locHandlerPolicy |
std::vector< TVal > | m_data |
Private Types | |
typedef multi_map< TKey, TVal, TLocMap > | self_type |
multi_map is a container that associates keys to list of values.
It is similar in concept of having an hashmap: hm[key] -> {val, val, val, ..} but it's way more efficient.
Example usage:
moost::container::multi_map<int, float> test; vector< pair<int, float> > data; data.push_back( make_pair(10, 2.0f) ); data.push_back( make_pair(10, 3.0f) ); data.push_back( make_pair(10, 1.0f) ); data.push_back( make_pair(12, 1.0f) ); test.create_map<1>(data); // using the first of the pair as key, it will sort itself moost::container::multi_map<int, float>::range rt; rt = test[10]; moost::container::multi_map<int, float>::range_iterator mapIt; for ( mapIt = rt.begin(); mapIt != rt.end(); ++mapIt ) cout << *mapIt << endl;
Is it also possible to define a different container for the locatin map, i.e. when the keys are within a well know range a vector might be a better solution:
typedef multi_map< int, float, vector<multimap_value_t> > multi_map_vec; multi_map_vec::loc_map_policy_type policy(10) // max key index is 9 multi_map_vec data(policy);
In the case of the default location map (dense_hash_map) the empty key can be easily set by using the location map policy, i.e. to set it to -1 we just do
typedef moost::container::multi_map<int, float> map_type; map_type test(map_type::loc_map_policy_type(-1));
Definition at line 94 of file multi_map.hpp.
typedef std::vector<TVal>::const_iterator moost::container::multi_map< TKey, TVal, TLocMap >::const_range_iterator |
Definition at line 110 of file multi_map.hpp.
typedef TKey moost::container::multi_map< TKey, TVal, TLocMap >::first_type |
Definition at line 102 of file multi_map.hpp.
typedef policies::map_policy_selector< TKey, multimap_value_type, TLocMap>::policy_type moost::container::multi_map< TKey, TVal, TLocMap >::loc_map_policy_type |
Reimplemented in moost::container::neigh_multi_map< TLocMap >, and moost::container::simple_multi_map< TKey, TVal, TLocMap >.
Definition at line 107 of file multi_map.hpp.
typedef std::vector<TVal>::iterator moost::container::multi_map< TKey, TVal, TLocMap >::range_iterator |
Definition at line 109 of file multi_map.hpp.
typedef TVal moost::container::multi_map< TKey, TVal, TLocMap >::second_type |
Definition at line 103 of file multi_map.hpp.
typedef multi_map<TKey, TVal, TLocMap> moost::container::multi_map< TKey, TVal, TLocMap >::self_type [private] |
Definition at line 98 of file multi_map.hpp.
moost::container::multi_map< TKey, TVal, TLocMap >::multi_map | ( | const loc_map_policy_type & | locHandlerPolicy = loc_map_policy_type() | ) | [inline] |
Definition at line 310 of file multi_map.hpp.
iterator moost::container::multi_map< TKey, TVal, TLocMap >::begin | ( | ) | [inline] |
const_iterator moost::container::multi_map< TKey, TVal, TLocMap >::begin | ( | ) | const [inline] |
Definition at line 303 of file multi_map.hpp.
void moost::container::multi_map< TKey, TVal, TLocMap >::clear | ( | ) |
Will clear the whole table.
Definition at line 595 of file multi_map.hpp.
void moost::container::multi_map< TKey, TVal, TLocMap >::create_map | ( | Iterator | first, |
Iterator | last, | ||
size_t | suggestedSize = 128 |
||
) |
Create the map from a vector of pairs, where the key is either the first or the second element of the pair, and it is specified by the template argument (either 1 or 2 are valid).
i2i | the item2item vector of pairs |
doSort | if true it will sort by the keys. This is a required step and takes a bit of time because it is using a stable sort. If your data is already sorted by the key, you can avoid this. |
Definition at line 406 of file multi_map.hpp.
void moost::container::multi_map< TKey, TVal, TLocMap >::create_map | ( | Iterator | first, |
Iterator | last, | ||
const GetKeyPolicy & | getKey, | ||
const GetValuesPolicy & | getValues, | ||
size_t | suggestedSize = 128 |
||
) |
Create the map from a couple of iterators. It allows to specify getKey and getValue policies to access special type of structures.
Definition at line 446 of file multi_map.hpp.
void moost::container::multi_map< TKey, TVal, TLocMap >::create_map | ( | std::vector< TFirst, TSecond > & | i2i, |
bool | doSort = true |
||
) |
Create the map from a vector of pairs, where the key is either the first or the second element of the pair, and it is specified by the template argument (either 1 or 2 are valid).
i2i | the item2item vector of pairs |
doSort | if true it will sort by the keys. This is a required step and takes a bit of time because it is using a stable sort. If your data is already sorted by the key, you can avoid this. |
Definition at line 387 of file multi_map.hpp.
void moost::container::multi_map< TKey, TVal, TLocMap >::create_map_compressed | ( | std::vector< TFirst, TSecond > & | i2i, |
bool | doSort = true |
||
) |
Definition at line 480 of file multi_map.hpp.
void moost::container::multi_map< TKey, TVal, TLocMap >::create_map_compressed | ( | Iterator | first, |
Iterator | last, | ||
size_t | suggestedSize = 128 |
||
) |
Equivalent to create_map, but will map the keys with a _single_ value to the same memory location thus saving memory. Use it only if you have a lot of entries with a single value and (if it's a data structure) the operator < is implemented.
Definition at line 499 of file multi_map.hpp.
bool moost::container::multi_map< TKey, TVal, TLocMap >::empty | ( | ) | const [inline] |
Definition at line 370 of file multi_map.hpp.
iterator moost::container::multi_map< TKey, TVal, TLocMap >::end | ( | ) | [inline] |
const_iterator moost::container::multi_map< TKey, TVal, TLocMap >::end | ( | ) | const [inline] |
Definition at line 306 of file multi_map.hpp.
multi_map< TKey, TVal, TLocMap >::range moost::container::multi_map< TKey, TVal, TLocMap >::operator[] | ( | const TKey & | key | ) |
Definition at line 558 of file multi_map.hpp.
multi_map< TKey, TVal, TLocMap >::const_range moost::container::multi_map< TKey, TVal, TLocMap >::operator[] | ( | const TKey & | key | ) | const |
Definition at line 577 of file multi_map.hpp.
size_t moost::container::multi_map< TKey, TVal, TLocMap >::size | ( | ) | const [inline] |
Returns the number of keys.
Definition at line 367 of file multi_map.hpp.
std::vector<TVal> moost::container::multi_map< TKey, TVal, TLocMap >::m_data [protected] |
Definition at line 378 of file multi_map.hpp.
TLocMap moost::container::multi_map< TKey, TVal, TLocMap >::m_locations [protected] |
Definition at line 375 of file multi_map.hpp.
loc_map_policy_type moost::container::multi_map< TKey, TVal, TLocMap >::m_locHandlerPolicy [protected] |
Definition at line 376 of file multi_map.hpp.