IntRanges.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) 2020-2023 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 "token.H"
29 #include "List.H"
30 #include "Istream.H"
31 #include "Ostream.H"
32 #include <numeric>
33 
34 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 
39 template<class T>
40 inline static Istream& input(Istream& is, IntRange<T>& range)
41 {
42  is.readBegin("IntRange");
43  is >> range.start() >> range.size();
44  is.readEnd("IntRange");
45 
46  is.check(FUNCTION_NAME);
47  return is;
48 }
49 
50 template<class T>
51 inline static Ostream& output(Ostream& os, const IntRange<T>& range)
52 {
54  << range.start() << token::SPACE << range.size()
55  << token::END_LIST;
56 
58  return os;
59 }
60 
61 } // End namespace Foam
62 
63 
64 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
65 
66 Foam::List<Foam::label> Foam::identity(const IntRange<int32_t>& range)
67 {
68  List<label> result;
69 
70  if (range.size() > 0)
71  {
72  result.resize(range.size());
73  std::iota(result.begin(), result.end(), range.start());
74  }
75 
76  return result;
77 }
78 
79 
80 #if defined(WM_LABEL_SIZE) && (WM_LABEL_SIZE >= 64)
81 Foam::List<Foam::label> Foam::identity(const IntRange<int64_t>& range)
82 {
83  List<label> result;
84 
85  if (range.size() > 0)
86  {
87  result.resize(range.size());
88  std::iota(result.begin(), result.end(), range.start());
89  }
90 
91  return result;
92 }
93 #endif
94 
95 
96 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
97 
99 {
100  return input(is, range);
101 }
102 
105 {
106  return input(is, range);
107 }
108 
110 Foam::Ostream& Foam::operator<<(Ostream& os, const IntRange<int32_t>& range)
111 {
112  return output(os, range);
113 }
114 
115 
116 Foam::Ostream& Foam::operator<<(Ostream& os, const IntRange<int64_t>& range)
117 {
118  return output(os, range);
119 }
120 
121 
122 // ************************************************************************* //
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
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
An interval of (signed) integers defined by a start and a size.
Definition: IntRange.H:59
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Begin list [isseparator].
Definition: token.H:161
scalar range
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
Definition: labelLists.C:44
Istream & operator>>(Istream &, directionInfo &)
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
Space [isspace].
Definition: token.H:131
End list [isseparator].
Definition: token.H:162
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
bool readEnd(const char *funcName)
End read of data chunk, ends with &#39;)&#39;.
Definition: Istream.C:152
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
Namespace for OpenFOAM.