Belle II Software  release-05-02-19
MapHelperFunctions.h
1 /**************************************************************************
2 * BASF2 (Belle Analysis Framework 2) *
3 * Copyright(C) 2014 - Belle II Collaboration *
4 * *
5 * Author: The Belle II Collaboration *
6 * Contributors: Thomas Madlener *
7 * *
8 * This software is provided "as is" without any warranty. *
9 **************************************************************************/
10 
11 # pragma once
12 
13 #include <string>
14 #include <iterator>
15 #include <sstream>
16 #include <algorithm>
17 #include <vector>
18 #include <utility>
19 #include <tuple>
20 #include <numeric>
21 
22 namespace Belle2 {
33  template <typename MapType>
34  std::vector<typename MapType::key_type> getUniqueKeys(const MapType& aMap)
35  {
36  std::vector<typename MapType::key_type> allKeys; // collect all keys of the map -> then sort & unique (+resize)
37  if (aMap.empty()) { return allKeys; }
38 
39  typedef typename MapType::const_iterator mapIter;
40  for (mapIter it = aMap.begin(); it != aMap.end(); ++it) { allKeys.push_back(it->first); }
41  std::sort(allKeys.begin(), allKeys.end());
42  auto newEnd = std::unique(allKeys.begin(), allKeys.end());
43  allKeys.resize(std::distance(allKeys.begin(), newEnd));
44 
45  return allKeys;
46  }
47 
52  template <typename MapType>
53  unsigned int getUniqueSize(const MapType& aMap) { return getUniqueKeys<MapType>(aMap).size(); }
54 
59  template <typename MapType>
60  std::vector<std::pair<typename MapType::key_type, unsigned int> > getNValuesPerKey(const MapType& aMap)
61  {
62  typedef typename MapType::key_type keyT;
63  typedef typename MapType::const_iterator mapIter;
64 
65  std::vector<std::pair<keyT, unsigned int> > valuesPerKey;
66  if (aMap.empty()) return valuesPerKey; // return empty vector if map is empty
67 
68  std::vector<keyT> uniqueKeys = getUniqueKeys<MapType>(aMap);
69 
70  for (keyT key : uniqueKeys) {
71  std::pair<mapIter, mapIter> keyRange = aMap.equal_range(key);
72  valuesPerKey.push_back(std::make_pair(key, std::distance(keyRange.first, keyRange.second)));
73  }
74  return valuesPerKey;
75  }
76 
80  template <typename MapType>
81  std::vector<typename MapType::mapped_type> getValuesToKey(const MapType& aMap, typename MapType::key_type aKey)
82  {
83  typedef typename MapType::const_iterator mapIter;
84 
85  std::vector<typename MapType::mapped_type> values;
86  if (aMap.empty()) return values;
87 
88  std::pair<mapIter, mapIter> keyRange = aMap.equal_range(aKey);
89  for (mapIter it = keyRange.first; it != keyRange.second; ++it) { values.push_back(it->second); }
90 
91  return values;
92  }
93 
102  template <typename MapType>
103  std::vector<std::tuple<typename MapType::key_type, typename MapType::mapped_type, unsigned int> >
104  getSortedKeyValueTuples(const MapType& aMap)
105  {
106  typedef typename MapType::key_type keyT;
107  typedef typename MapType::mapped_type mapT;
108 
109  std::vector<std::tuple<keyT, mapT, unsigned int> > keyValuePairs;
110  if (aMap.empty()) return keyValuePairs; // return empty vector if nothing is stored in map
111 
112  std::vector<std::pair<keyT, unsigned int> > nValuesPerKey = getNValuesPerKey(aMap);
113 
114  for (std::pair<keyT, unsigned int> keyValues : nValuesPerKey) {
115  std::vector<mapT> valuesToKey = getValuesToKey(aMap, keyValues.first);
116 
117  mapT valueSum = std::accumulate(valuesToKey.begin(), valuesToKey.end(), 0.0);
118  keyValuePairs.push_back(std::make_tuple(keyValues.first, valueSum, keyValues.second));
119  }
120 
121  // sort using a lambda function (using std::tie as std::tuple has a defined operator < that ensures strict weak ordering)
122  std::sort(keyValuePairs.begin(), keyValuePairs.end(),
123  [](const std::tuple<keyT, mapT, unsigned int>& lTuple, const std::tuple<keyT, mapT, unsigned int>& rTuple)
124  { return std::tie(std::get<2>(rTuple), std::get<1>(rTuple)) < std::tie(std::get<2>(lTuple), std::get<1>(lTuple)); }
125  );
126 
127  return keyValuePairs;
128  }
129 
134  template<typename MapType>
135  std::vector<typename MapType::mapped_type> getAllValues(const MapType& aMap)
136  {
137  typedef typename MapType::key_type keyT;
138  std::vector<keyT> allKeys = getUniqueKeys(aMap);
139 
140  typedef typename MapType::mapped_type valueT;
141  std::vector<valueT> allValues;
142  for (const keyT& key : allKeys) {
143  std::vector<valueT> keyValues = getValuesToKey(aMap, key);
144  for (const valueT& value : keyValues) {
145  allValues.push_back(value);
146  }
147  }
148 
149  return allValues;
150  }
151 
155  template <typename MapType>
156  std::string printMap(const MapType& aMap)
157  {
158  if (aMap.empty()) return std::string("passed map is empty!");
159 
160  typedef typename MapType::key_type keyT;
161  typedef typename MapType::mapped_type mapT;
162 
163  std::stringstream mapContent;
164  mapContent << "content of map:\n";
165  for (keyT key : getUniqueKeys(aMap)) {
166  mapContent << "key: " << key << " -> value(s):";
167  for (mapT value : getValuesToKey(aMap, key)) { mapContent << " " << value; }
168  mapContent << "\n";
169  }
170 
171  return mapContent.str() + "\n"; // terminate with endline
172  }
173 
175 }
Belle2::getUniqueSize
unsigned int getUniqueSize(const MapType &aMap)
get the number of unique keys inside the map NOTE: for non-multimap this is the same as ....
Definition: MapHelperFunctions.h:61
Belle2::getSortedKeyValueTuples
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...
Definition: MapHelperFunctions.h:112
Belle2::getUniqueKeys
std::vector< typename MapType::key_type > getUniqueKeys(const MapType &aMap)
get the unique keys of a map (i.e.
Definition: MapHelperFunctions.h:42
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::getAllValues
std::vector< typename MapType::mapped_type > getAllValues(const MapType &aMap)
get all values in the map (i.e.
Definition: MapHelperFunctions.h:143
Belle2::getValuesToKey
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
Definition: MapHelperFunctions.h:89
Belle2::getNValuesPerKey
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.
Definition: MapHelperFunctions.h:68
Belle2::printMap
std::string printMap(const MapType &aMap)
get the contents of the map as string.
Definition: MapHelperFunctions.h:164