cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Collections.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Utils_Collections_h
20#define CepGen_Utils_Collections_h
21
22#include <algorithm>
23#include <map>
24#include <set>
25#include <unordered_map>
26#include <unordered_set>
27#include <vector>
28
29namespace cepgen {
30 namespace utils {
32 template <typename K, typename T>
33 inline std::vector<K> keys(const std::map<K, T>& coll) {
34 std::vector<K> keys;
35 std::transform(coll.begin(), coll.end(), std::back_inserter(keys), [](const auto& val) { return val.first; });
36 return keys;
37 }
39 template <typename K, typename T>
40 inline std::vector<K> keys(const std::unordered_map<K, T>& coll) {
41 std::vector<K> keys;
42 std::transform(coll.begin(), coll.end(), std::back_inserter(keys), [](const auto& val) { return val.first; });
43 return keys;
44 }
46 template <typename T>
47 inline bool contains(const std::vector<T>& coll, const T& item) {
48 return std::find(coll.begin(), coll.end(), item) != coll.end();
49 }
51 template <typename T>
52 inline bool contains(const std::set<T>& coll, const T& item) {
53 return std::find(coll.begin(), coll.end(), item) != coll.end();
54 }
56 template <typename K, typename T>
57 inline bool contains(const std::unordered_map<K, T>& coll, const T& item) {
58 return std::find_if(coll.begin(), coll.end(), [&item](const auto& kv) { return kv.second == item; }) !=
59 coll.end();
60 }
62 template <typename T>
63 inline void normalise(std::vector<T>& coll) {
64 std::unordered_set<T> set;
65 for (const auto& it : coll)
66 set.insert(it);
67 coll.assign(set.begin(), set.end());
68 std::sort(coll.begin(), coll.end());
69 }
71 template <typename T>
72 inline bool uniform(const std::vector<T>& coll) {
73 return coll.size() > 1 ? coll == std::vector<T>(coll.size(), coll.at(0)) : true;
74 }
75 } // namespace utils
76} // namespace cepgen
77
78#endif
void normalise(std::vector< T > &coll)
Remove duplicates and sort a collection.
Definition Collections.h:63
std::vector< K > keys(const std::map< K, T > &coll)
Retrieve all keys from a map.
Definition Collections.h:33
bool uniform(const std::vector< T > &coll)
Check if all elements of a collection are uniform.
Definition Collections.h:72
bool contains(const std::vector< T > &coll, const T &item)
Check if a vector contains an item.
Definition Collections.h:47
Common namespace for this Monte Carlo generator.