31 template <
typename MapType>
32 std::vector<typename MapType::key_type>
getUniqueKeys(
const MapType& aMap)
34 std::vector<typename MapType::key_type> allKeys;
35 if (aMap.empty()) {
return allKeys; }
37 typedef typename MapType::const_iterator mapIter;
38 for (mapIter it = aMap.begin(); it != aMap.end(); ++it) { allKeys.push_back(it->first); }
39 std::sort(allKeys.begin(), allKeys.end());
40 auto newEnd = std::unique(allKeys.begin(), allKeys.end());
41 allKeys.resize(std::distance(allKeys.begin(), newEnd));
50 template <
typename MapType>
51 unsigned int getUniqueSize(
const MapType& aMap) {
return getUniqueKeys<MapType>(aMap).size(); }
57 template <
typename MapType>
58 std::vector<std::pair<typename MapType::key_type, unsigned int> >
getNValuesPerKey(
const MapType& aMap)
60 typedef typename MapType::key_type keyT;
61 typedef typename MapType::const_iterator mapIter;
63 std::vector<std::pair<keyT, unsigned int> > valuesPerKey;
64 if (aMap.empty())
return valuesPerKey;
66 std::vector<keyT> uniqueKeys = getUniqueKeys<MapType>(aMap);
68 for (keyT key : uniqueKeys) {
69 std::pair<mapIter, mapIter> keyRange = aMap.equal_range(key);
70 valuesPerKey.push_back(std::make_pair(key, std::distance(keyRange.first, keyRange.second)));
78 template <
typename MapType>
79 std::vector<typename MapType::mapped_type>
getValuesToKey(
const MapType& aMap,
typename MapType::key_type aKey)
81 typedef typename MapType::const_iterator mapIter;
83 std::vector<typename MapType::mapped_type> values;
84 if (aMap.empty())
return values;
86 std::pair<mapIter, mapIter> keyRange = aMap.equal_range(aKey);
87 for (mapIter it = keyRange.first; it != keyRange.second; ++it) { values.push_back(it->second); }
100 template <
typename MapType>
101 std::vector<std::tuple<typename MapType::key_type, typename MapType::mapped_type, unsigned int> >
104 typedef typename MapType::key_type keyT;
105 typedef typename MapType::mapped_type mapT;
107 std::vector<std::tuple<keyT, mapT, unsigned int> > keyValuePairs;
108 if (aMap.empty())
return keyValuePairs;
110 std::vector<std::pair<keyT, unsigned int> > nValuesPerKey =
getNValuesPerKey(aMap);
112 for (std::pair<keyT, unsigned int> keyValues : nValuesPerKey) {
113 std::vector<mapT> valuesToKey =
getValuesToKey(aMap, keyValues.first);
115 mapT valueSum = std::accumulate(valuesToKey.begin(), valuesToKey.end(), 0.0);
116 keyValuePairs.push_back(std::make_tuple(keyValues.first, valueSum, keyValues.second));
120 std::sort(keyValuePairs.begin(), keyValuePairs.end(),
121 [](
const std::tuple<keyT, mapT, unsigned int>& lTuple,
const std::tuple<keyT, mapT, unsigned int>& rTuple)
122 { return std::tie(std::get<2>(rTuple), std::get<1>(rTuple)) < std::tie(std::get<2>(lTuple), std::get<1>(lTuple)); }
125 return keyValuePairs;
132 template<
typename MapType>
133 std::vector<typename MapType::mapped_type>
getAllValues(
const MapType& aMap)
135 typedef typename MapType::key_type keyT;
138 typedef typename MapType::mapped_type valueT;
139 std::vector<valueT> allValues;
140 for (
const keyT& key : allKeys) {
142 for (
const valueT& value : keyValues) {
143 allValues.push_back(value);
153 template <
typename MapType>
156 if (aMap.empty())
return std::string(
"passed map is empty!");
158 typedef typename MapType::key_type keyT;
159 typedef typename MapType::mapped_type mapT;
161 std::stringstream mapContent;
162 mapContent <<
"content of map:\n";
164 mapContent <<
"key: " << key <<
" -> value(s):";
165 for (mapT value :
getValuesToKey(aMap, key)) { mapContent <<
" " << value; }
169 return mapContent.str() +
"\n";
std::vector< std::pair< typename MapType::key_type, unsigned int > > getNValuesPerKey(const MapType &aMap)
get the unique keys of a map together with the number of values associated to each key.
std::vector< typename MapType::key_type > getUniqueKeys(const MapType &aMap)
get the unique keys of a map (i.e.
unsigned int getUniqueSize(const MapType &aMap)
get the number of unique keys inside the map NOTE: for non-multimap this is the same as ....
std::vector< typename MapType::mapped_type > getValuesToKey(const MapType &aMap, typename MapType::key_type aKey)
get all values stored in the map for a given key
std::vector< typename MapType::mapped_type > getAllValues(const MapType &aMap)
get all values in the map (i.e.
std::string printMap(const MapType &aMap)
get the contents of the map as string.
std::vector< std::tuple< typename MapType::key_type, typename MapType::mapped_type, unsigned int > > getSortedKeyValueTuples(const MapType &aMap)
get the (key, value, number of values) tuples stored in the map, sorted after the following scheme (d...
Abstract base class for different kinds of events.