sliceRange.C
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) 2019-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "sliceRange.H"
29 #include "token.H"
30 #include "FixedList.H"
31 #include "List.H"
32 #include "Istream.H"
33 #include "Ostream.H"
34 #include <numeric>
35 
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 
39 :
40  start_(coeffs[0]),
41  size_(std::max(label(0),coeffs[1])), // No negative size
42  stride_(std::max(label(0),coeffs[2])) // No negative stride
43 {}
44 
45 
46 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
47 
49 {
50  List<label> result(size_);
51 
52  if (stride_ > 1)
53  {
54  std::copy(cbegin(), cend(), result.begin());
55  }
56  else if (stride_ == 1)
57  {
58  std::iota(result.begin(), result.end(), start_);
59  }
60  else
61  {
62  // stride = 0 (identical values!)
63  std::fill_n(result.begin(), result.size(), start_);
64  }
65 
66  return result;
67 }
68 
69 
70 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
71 
72 Foam::Istream& Foam::operator>>(Istream& is, sliceRange& range)
73 {
74  label beg, len, stride;
75 
76  is.readBegin("sliceRange");
77  is >> beg >> len >> stride;
78  is.readEnd("sliceRange");
79 
80  range = sliceRange(beg, len, stride);
81 
82  is.check(FUNCTION_NAME);
83  return is;
84 }
85 
86 
87 Foam::Ostream& Foam::operator<<(Ostream& os, const sliceRange& range)
88 {
90  << range.start() << token::SPACE
91  << range.size() << token::SPACE
92  << range.stride() << token::END_LIST;
93 
95  return os;
96 }
97 
98 
99 // ************************************************************************* //
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:134
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
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:315
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
List< label > labels() const
Return list of labels corresponding to the slice.
Definition: sliceRange.C:41
Begin list [isseparator].
Definition: token.H:161
scalar range
Istream & operator>>(Istream &, directionInfo &)
Space [isspace].
Definition: token.H:131
End list [isseparator].
Definition: token.H:162
constexpr sliceRange() noexcept
Default construct an empty slice (0,0,0)
Definition: sliceRangeI.H:23
A set of labels defined by a start, a length and a stride.
Definition: sliceRange.H:54
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
const_iterator cend() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: sliceRangeI.H:329