ATLAS Offline Software
Loading...
Searching...
No Matches
MultiRange.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include <iostream>
7#include <algorithm> //remove_if
8#include <ranges>
9#include <numeric>
10
11MultiRange::MultiRange (const Range& r, const Range& s) {
12 m_ranges.push_back (r);
13 m_ranges.push_back (s);
14}
15
16//-----------------------------------------------
18 m_ranges.clear ();
19}
20
21//-----------------------------------------------
22void MultiRange::add (const Range& range) {
23 // Add new range ONLY if an equivalent does NOT exist
24 if (std::ranges::find(m_ranges, range) == m_ranges.end()) {
25 m_ranges.push_back(range);
26 }
27}
28
29//-----------------------------------------------
30void MultiRange::add (Range&& range) {
31 // Add new range ONLY if an equivalent does NOT exist
32 if (std::ranges::find(m_ranges, range) == m_ranges.end()) {
33 m_ranges.emplace_back (std::move(range));
34 }
35}
36
37//-----------------------------------------------
39 m_ranges.emplace_back (id);
40}
41
42//-----------------------------------------------
44 // Remove all ranges for which id matches
45 [[maybe_unused]] auto erased = std::erase_if (m_ranges,
46 [&id] (const Range& r) { return r.match(id); });
47}
48
49
50
51//-----------------------------------------------
53 return (m_ranges.back());
54}
55
56//-----------------------------------------------
58 bool result = std::ranges::any_of(m_ranges, [&id](const Range & r){return r.match(id);});
59 return result ? 1:0;
60}
61
62//-----------------------------------------------
64 static const Range null_range;
65 if (index >= m_ranges.size ()) return (null_range);
66 return (m_ranges[index]);
67}
68
69//-----------------------------------------------
71 return (m_ranges.size ());
72}
73
75 return (m_ranges.begin ());
76}
77
79 return (m_ranges.end ());
80}
81
83 size_type init = 0;
84 return std::accumulate(m_ranges.begin(), m_ranges.end(), init,
85 [](size_type a, const Range& b){return a + b.cardinality();});
86}
87
89 // Loop over ranges in MultiRange and calculate hash for each
90 // range
91 size_type init = 0;
92 return std::accumulate(m_ranges.begin(), m_ranges.end(), init,
93 [&id](size_type a, const Range& b){return a + b.cardinalityUpTo(id);});
94}
95
96
97//-----------------------------------------------
99 range_vector::size_type i;
100 range_vector::size_type j;
101 for (i = 0; i < m_ranges.size (); ++i) {
102 const Range& r = m_ranges[i];
103 for (j = i + 1; j < m_ranges.size (); ++j) {
104 const Range& s = m_ranges[j];
105 if (r.overlaps_with (s)) return (true);
106 }
107 }
108 return (false);
109}
110
111//-----------------------------------------------
116
117//-----------------------------------------------
122
123//-----------------------------------------------
125 static const identifier_factory factory(*this);
126 return (factory);
127}
128
129//-----------------------------------------------
131 static const const_identifier_factory factory;
132 return (factory);
133}
134
135//-----------------------------------------------
137 :
138 m_range_it(multirange.m_ranges.begin()),
139 m_range_end(multirange.m_ranges.end()){
140 if (m_range_it == m_range_end)return; // no ranges
144 if (m_range_it != m_range_end) {
145 m_id_fac_it = ConstRangeIterator(*m_range_it).begin();
146 m_id_fac_end = ConstRangeIterator(*m_range_it).end();
148 // Set id
149 m_id = *m_id_fac_it;
150 }
151 }
152}
153
154//-----------------------------------------------
156 if (m_id.fields () == 0) return;
157 m_id.clear();
158 if (m_range_it != m_range_end) {
159 if (m_id_fac_it != m_id_fac_end) {
160 ++m_id_fac_it;
161 }
162 if (m_id_fac_it == m_id_fac_end) {
163 ++m_range_it;
164 if (m_range_it != m_range_end) {
165 m_id_fac_it = ConstRangeIterator(*m_range_it).begin();
166 m_id_fac_end = ConstRangeIterator(*m_range_it).end();
167 }
168 }
169 if (m_id_fac_it != m_id_fac_end) {
170 m_id = *m_id_fac_it;
171 }
172 }
173}
174
175//-----------------------------------------------
179
180//-----------------------------------------------
182 return (m_id == other.m_id);
183}
184
185//-----------------------------------------------
187 :
188 m_range_it(multirange.m_ranges.begin()),
189 m_range_end(multirange.m_ranges.end()){
190
191 if (m_range_it == m_range_end) return; // no ranges
195 if (m_range_it != m_range_end) {
196 m_id_fac_it = ConstRangeIterator(*m_range_it).begin();
197 m_id_fac_end = ConstRangeIterator(*m_range_it).end();
199 // Set id
200 m_id = *m_id_fac_it;
201 }
202 }
203}
204
205//-----------------------------------------------
207 if (m_id.fields () == 0) return;
208 m_id.clear();
209 if (m_range_it != m_range_end) {
210 if (m_id_fac_it != m_id_fac_end) {
211 ++m_id_fac_it;
212 }
213 if (m_id_fac_it == m_id_fac_end) {
214 ++m_range_it;
215 if (m_range_it != m_range_end) {
216 m_id_fac_it = ConstRangeIterator(*m_range_it).begin();
217 m_id_fac_end = ConstRangeIterator(*m_range_it).end();
218 }
219 }
220 if (m_id_fac_it != m_id_fac_end) {
221 m_id = *m_id_fac_it;
222 }
223 }
224}
225
226//-----------------------------------------------
230
231//-----------------------------------------------
233 return (m_id == other.m_id);
234}
235
236//-----------------------------------------------
237void MultiRange::show () const {
238 show (std::cout);
239}
240
241void MultiRange::show (std::ostream& s) const {
242 range_vector::size_type i;
243 for (i = 0; i < m_ranges.size (); ++i) {
244 if (i > 0) s << std::endl;
245 const Range& r = m_ranges[i];
246 r.show (s);
247 }
248}
249
250//-----------------------------------------------
251MultiRange::operator std::string () const {
252 std::string result;
253 range_vector::size_type i{};
254 for ( ; i < m_ranges.size (); ++i){
255 if (i > 0) result += " | ";
256 const Range& r = m_ranges[i];
257 result += (std::string) r;
258 }
259 return (result);
260}
static Double_t a
ConstRangeIterator end() const
ConstRangeIterator begin() const
range_vector::const_iterator m_range_end
Definition MultiRange.h:69
const ExpandedIdentifier & operator*() const
bool operator==(const const_identifier_factory &other) const
range_vector::const_iterator m_range_it
Definition MultiRange.h:68
This factory is able to generate all possible identifiers, from a fully bounded Range.
Definition MultiRange.h:30
range_vector::const_iterator m_range_it
Definition MultiRange.h:46
bool operator==(const identifier_factory &other) const
range_vector::const_iterator m_range_end
Definition MultiRange.h:47
ConstRangeIterator m_id_fac_it
Definition MultiRange.h:44
ConstRangeIterator m_id_fac_end
Definition MultiRange.h:45
const ExpandedIdentifier & operator*() const
ExpandedIdentifier m_id
Definition MultiRange.h:43
void clear()
size_type cardinalityUpTo(const ExpandedIdentifier &id) const
bool has_overlap() const
Check if there are overlaps between any couple of Ranges.
range_vector::const_iterator const_iterator
Definition MultiRange.h:22
identifier_factory factory_end()
const Range & operator[](size_type index) const
Accessors.
void remove_range(const ExpandedIdentifier &id)
Remove a Range made from a single ExpandedIdentifier.
range_vector m_ranges
Definition MultiRange.h:131
size_type cardinality() const
Computes a possible cardinality from all ranges.
const_iterator begin() const
Range & back()
Get the last entered Range.
ExpandedIdentifier::size_type size_type
Definition MultiRange.h:21
identifier_factory factory_begin()
const_iterator end() const
void add(const Range &range)
int match(const ExpandedIdentifier &id) const
Match an identifier.
void show() const
MultiRange()=default
size_type size() const
A Range describes the possible ranges for the field values of an ExpandedIdentifier.
int r
Definition globals.cxx:22
Definition index.py:1
std::size_t erase_if(T_container &container, T_Func pred)