labelRangesI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 inline Foam::labelRanges::labelRanges(const label initialCapacity)
32 :
33  ranges_(initialCapacity)
34 {}
35 
36 
38 :
39  ranges_(list)
40 {}
41 
42 
44 :
45  ranges_(std::move(list))
46 {}
47 
48 
49 template<int AnySizeMin>
51 (
53 )
54 :
55  ranges_(std::move(list))
56 {}
57 
58 
59 // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
60 
63 (
64  const UList<labelRange>* list,
65  const label idx,
66  const label subIdx
67 ) noexcept
68 :
69  list_(list),
70  index_(idx),
71  subIndex_(subIdx)
72 {}
73 
74 
75 inline Foam::label Foam::labelRanges::const_iterator::
76 operator*() const
77 {
78  return (*list_)[index_][subIndex_];
79 }
80 
81 
84 operator++()
85 {
86  // TBD: trap (index_ >= list_->size()) and make a no-op?
87  if (++subIndex_ >= (*list_)[index_].size())
88  {
89  // Move to the next range
90  ++index_;
91  subIndex_ = 0;
92  }
93 
94  return *this;
95 }
96 
97 
100 operator++(int)
101 {
102  const_iterator old(*this);
103  this->operator++();
104  return old;
105 }
106 
107 
108 inline constexpr bool
109 Foam::labelRanges::const_iterator::
110 operator==
111 (
112  const const_iterator& iter
113 ) const noexcept
114 {
115  return
116  (
117  index_ == iter.index_
118  && subIndex_ == iter.subIndex_
119  );
120 }
121 
122 
123 inline constexpr bool
124 Foam::labelRanges::const_iterator::
125 operator!=
126 (
127  const const_iterator& iter
128 ) const noexcept
129 {
130  return !(*this == iter);
131 }
132 
133 
134 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135 
136 inline Foam::label Foam::labelRanges::totalSize() const noexcept
137 {
138  label total = 0;
139  for (const labelRange& range : ranges_)
140  {
141  if (range.size() > 0) // Ignore negative size (paranoid)
142  {
143  total += range.size();
144  }
145  }
146  return total;
147 }
148 
149 
150 inline bool Foam::labelRanges::contains(const label value) const noexcept
151 {
152  for (const labelRange& range : ranges_)
153  {
154  if (range.contains(value))
155  {
156  return true;
157  }
158  }
159 
160  return false;
161 }
162 
163 
164 template<class... Args>
166 {
167  return ranges_.emplace_back(args...);
168 }
169 
170 
171 inline void Foam::labelRanges::sort()
172 {
173  Foam::sort(ranges_);
174 }
175 
176 
177 // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
178 
181 {
182  return const_iterator(&ranges_);
183 }
184 
185 
188 {
189  return const_iterator(&ranges_, ranges_.size());
190 }
191 
192 
195 {
196  return const_iterator(&ranges_);
197 }
198 
199 
202 {
203  return const_iterator(&ranges_, ranges_.size());
204 }
205 
206 
208 Foam::labelRanges::cbegin(const label i) const
209 {
210  if (i <= 0) return this->cbegin();
211 
212  label idx = 0;
213  label subIdx = i;
214 
215  for (const labelRange& range : ranges_)
216  {
217  if (subIdx < range.size())
218  {
219  return const_iterator(&ranges_, idx, subIdx);
220  }
221  else
222  {
223  ++idx;
224  if (range.size() > 0) // Ignore negative size (paranoid)
225  {
226  subIdx -= range.size();
227  }
228  }
229  }
230 
231  return this->cend();
232 }
233 
234 
236 Foam::labelRanges::begin(const label i) const
237 {
238  return this->cbegin(i);
239 }
240 
241 
242 // ************************************************************************* //
labelRanges()=default
Default construct.
const_iterator begin() const noexcept
A const_iterator set to the beginning of the list.
Definition: labelRangesI.H:187
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
bool contains(const label value) const noexcept
True if the value is contained within any of the sub-ranges.
Definition: labelRangesI.H:143
constexpr const_iterator(const UList< labelRange > *list, const label idx=0, const label subIdx=0) noexcept
Construct from range list at given index (and sub-index)
Definition: labelRangesI.H:56
scalar range
label operator*() const
Return the current label.
Definition: labelRangesI.H:69
const const_iterator cend() const noexcept
A const_iterator set to beyond the end of the list.
Definition: labelRangesI.H:180
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
void sort()
Inplace sort of the range elements.
Definition: labelRangesI.H:164
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
const const_iterator end() const noexcept
A const_iterator set to beyond the end of the list.
Definition: labelRangesI.H:194
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:223
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
const direction noexcept
Definition: Scalar.H:258
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:190
labelRange & emplace_back(Args &&... args)
Construct a range element at the end of the list, return reference to the new element.
label totalSize() const noexcept
The linear size (sum of all the element sizes)
Definition: labelRangesI.H:129
Foam::argList args(argc, argv)
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the list.
Definition: labelRangesI.H:173
Forward input iterator with const access.
Definition: labelRanges.H:221