Motiv
Marvelous OTF2 Traces Interactive Visualizer
Loading...
Searching...
No Matches
utils.hpp
1/*
2 * Marvelous OTF2 Traces Interactive Visualizer (MOTIV)
3 * Copyright (C) 2023 Florian Gallrein, Björn Gehrke
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 * (at your option) 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#ifndef MOTIV_UTILS_HPP
19#define MOTIV_UTILS_HPP
20
21#include <QLayout>
22#include <QWidget>
23#include <QDebug>
24#include "src/models/communication/CommunicationKind.hpp"
25#include "src/models/Range.hpp"
26
34void resetLayout(QLayout *apLayout);
35
36std::string communicationKindName(CommunicationKind kind);
37
38std::string collectiveCommunicationOperationName(otf2::collective_type type);
39
40template<typename T, typename K, typename C = std::less<K>>
41std::map<K, Range<T>, C> groupBy(Range<T> range, std::function<K(const T)> keySelector, std::function<bool(const T, const T)> compare) {
42
43 std::map<K, Range<T>, C> group;
44
45 if(range.empty()) {
46 return group;
47 }
48
49 C keyComparator;
50
51 std::sort(range.begin(), range.end(), compare);
52
53 auto start = range.begin();
54 auto it = range.begin() + 1;
55 while(it != range.end()) {
56 auto key = keySelector(*it);
57 auto startKey = keySelector(*start);
58 if((keyComparator(key, startKey) || keyComparator(startKey, key))) {
59 auto r = std::vector<T>(start, it);
60 group[startKey] = Range<T>(r);
61 start = it;
62 }
63
64 it++;
65 }
66 auto startKey = keySelector(*start);
67 auto r = std::vector<T>(start, it);
68 group[startKey] = Range<T>(r);
69
70 return group;
71}
72
73template<typename T, typename K, typename C = std::less<K>>
74std::map<K, Range<T>, C> groupBy(Range<T> range, std::function<K(const T)> keySelector) {
75 return groupBy(range, keySelector, [](const T lhs, const T rhs) {return lhs < rhs;});
76}
77
78
79#endif //MOTIV_UTILS_HPP
A custom range implementation around std::vector<T>s.
Definition: Range.hpp:37
bool empty() const
Definition: Range.hpp:127
It end() const
Definition: Range.hpp:121
It begin() const
Definition: Range.hpp:114