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 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 List<label> makeIdentity(const IntRange<T>& range)
41 {
42  if (range.size() < 1)
43  {
44  return List<label>();
45  }
46 
47  List<label> result(range.size());
48  std::iota(result.begin(), result.end(), range.start());
49 
50  return result;
51 }
52 
53 template<class T>
54 inline static Istream& input(Istream& is, IntRange<T>& range)
55 {
56  is.readBegin("IntRange");
57  is >> range.start() >> range.size();
58  is.readEnd("IntRange");
59 
60  is.check(FUNCTION_NAME);
61  return is;
62 }
63 
64 template<class T>
65 inline static Ostream& output(Ostream& os, const IntRange<T>& range)
66 {
68  << range.start() << token::SPACE << range.size()
69  << token::END_LIST;
70 
72  return os;
73 }
74 
75 } // End namespace Foam
76 
77 
78 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
79 
80 Foam::List<Foam::label> Foam::identity(const IntRange<int32_t>& range)
81 {
82  return makeIdentity(range);
83 }
84 
85 
86 #if defined(WM_LABEL_SIZE) && (WM_LABEL_SIZE >= 64)
87 Foam::List<Foam::label> Foam::identity(const IntRange<int64_t>& range)
88 {
90 }
91 #endif
92 
93 
94 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
95 
97 {
98  return input(is, range);
99 }
100 
103 {
104  return input(is, range);
105 }
106 
108 Foam::Ostream& Foam::operator<<(Ostream& os, const IntRange<int32_t>& range)
109 {
110  return output(os, range);
111 }
112 
113 
114 Foam::Ostream& Foam::operator<<(Ostream& os, const IntRange<int64_t>& range)
115 {
116  return output(os, range);
117 }
118 
119 
120 // ************************************************************************* //
static List< label > makeIdentity(const IntRange< T > &range)
Definition: IntRanges.C:33
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:104
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:158
scalar range
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:31
Istream & operator>>(Istream &, directionInfo &)
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:47
Space [isspace].
Definition: token.H:128
End list [isseparator].
Definition: token.H:159
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:58
Namespace for OpenFOAM.