Motiv
Marvelous OTF2 Traces Interactive Visualizer
Loading...
Searching...
No Matches
Range.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_RANGE_HPP
19#define MOTIV_RANGE_HPP
20
21#include "src/models/communication/CollectiveCommunicationEvent.hpp"
22#include "src/models/communication/Communication.hpp"
23#include "Slot.hpp"
24#include <ranges>
25#include <vector>
26#include <memory>
27
36template<typename T>
37class Range {
38public:
42 using It = typename std::vector<T>::iterator;
43
47 Range() = default;
48
54 Range(It begin, It end) : begin_(begin), end_(end), vec_(nullptr) {};
55
61 Range(const Range &rhs) {
62 if (rhs.vec_) {
63 vec_ = new std::vector<T>(*rhs.vec_);
64 begin_ = std::find(vec_->begin(), vec_->end(), *rhs.begin_);
65 end_ = std::find(vec_->begin(), vec_->end(), *rhs.end_);
66 } else {
67 begin_ = rhs.begin_;
68 end_ = rhs.end_;
69 }
70 };
71
78 explicit Range(std::vector<T> &vec) : vec_(new std::vector<T>(vec)), begin_(vec_->begin()), end_(vec_->end()) {};
79
80public:
91 if (this == &rhs) {
92 return *this;
93 }
94
95
96 if (rhs.vec_) {
97 delete vec_;
98 vec_ = new std::vector<T>(*rhs.vec_);
99 begin_ = std::find(vec_->begin(), vec_->end(), *rhs.begin_);
100 end_ = std::find(vec_->begin(), vec_->end(), *rhs.end_);
101 } else {
102 begin_ = rhs.begin_;
103 end_ = rhs.end_;
104 }
105
106 return *this;
107 };
108
109
114 It begin() const { return begin_; };
115
116
121 It end() const { return end_; };
122
127 [[nodiscard]] bool empty() const { return begin_ == end_; };
128
129 virtual ~Range() {
130 delete vec_;
131 };
132
133private:
134 std::vector<T> *vec_ = nullptr;
135 It begin_;
136 It end_;
137};
138
139#endif //MOTIV_RANGE_HPP
A custom range implementation around std::vector<T>s.
Definition: Range.hpp:37
Range< T > & operator=(const Range< T > &rhs)
Copy assignment constructor.
Definition: Range.hpp:90
bool empty() const
Definition: Range.hpp:127
Range(std::vector< T > &vec)
Definition: Range.hpp:78
Range()=default
Creates an empty range.
Range(It begin, It end)
Creates a Range defined by two iterators.
Definition: Range.hpp:54
typename std::vector< T >::iterator It
Shortcut for the iterator.
Definition: Range.hpp:42
It end() const
Definition: Range.hpp:121
Range(const Range &rhs)
Constructs a new Range object from a given Range object.
Definition: Range.hpp:61
It begin() const
Definition: Range.hpp:114