labelRange.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "labelRange.H"
27 #include "token.H"
28 
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 const Foam::labelRange Foam::labelRange::endLabelRange_;
33 const Foam::labelRange::const_iterator Foam::labelRange::endIter_;
35 
36 
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 
40 :
41  start_(0),
42  size_(0)
43 {
44  is >> *this;
45 }
46 
47 
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 
51 (
52  const labelRange& range,
53  const bool touches
54 ) const
55 {
56  label final = touches ? 1 : 0;
57 
58  return
59  (
60  this->size()
61  && range.size()
62  &&
63  (
64  (
65  range.first() >= this->first()
66  && range.first() <= this->last() + final
67  )
68  ||
69  (
70  this->first() >= range.first()
71  && this->first() <= range.last() + final
72  )
73  )
74  );
75 }
76 
77 
79 {
80  // trivial cases first
81  if (!size_)
82  {
83  return *this;
84  }
85  else if (!range.size_)
86  {
87  return range;
88  }
89 
90  const label lower = Foam::min(this->first(), range.first());
91  const label upper = Foam::max(this->last(), range.last());
92  const label sz = upper - lower + 1;
93 
94  return labelRange(lower, sz);
95 }
96 
97 
98 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
99 
101 {
102  if (!size_)
103  {
104  // trivial case
105  operator=(rhs);
106  }
107  else if (rhs.size_)
108  {
109  const label lower = Foam::min(this->first(), rhs.first());
110  const label upper = Foam::max(this->last(), rhs.last());
111 
112  start_ = lower;
113  size_ = upper - lower + 1;
114  }
115 
116  return *this;
117 }
118 
119 
120 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
121 
123 {
124  is.readBegin("labelRange");
125  is >> range.start_ >> range.size_;
126  is.readEnd("labelRange");
127 
128  is.check("operator>>(Istream&, labelRange&)");
129 
130  // disallow invalid sizes
131  if (range.size_ <= 0)
132  {
133  range.clear();
134  }
135 
136  return is;
137 }
138 
139 
141 {
142  // write ASCII only for now
143  os << token::BEGIN_LIST
144  << range.start_ << token::SPACE << range.size_
145  << token::END_LIST;
146 
147 // os << token::BEGIN_BLOCK
148 // << range.start_ << "-" << range.last()
149 // << token::END_BLOCK;
150 
151  os.check("operator<<(Ostream&, const labelRange&)");
152  return os;
153 }
154 
155 
156 // ************************************************************************* //
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
A label range specifier.
Definition: labelRange.H:57
void clear()
Reset to zero size.
Definition: labelRangeI.H:143
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
label size() const
Return the effective size of the range.
Definition: labelRangeI.H:155
static int debug
Definition: labelRange.H:66
scalar range
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & operator>>(Istream &, directionInfo &)
labelRange join(const labelRange &) const
Return a joined range, squashing any gaps in between.
Definition: labelRange.C:78
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:211
labelRange()
Construct an empty range.
Definition: labelRangeI.H:29
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
label last() const
The (inclusive) upper value of the range.
Definition: labelRangeI.H:167
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
labelRange & operator+=(const labelRange &)
Join ranges, squashing any gaps in between.
Definition: labelRange.C:100
An STL const_iterator.
Definition: labelRange.H:144
Ostream & operator<<(Ostream &, const ensightPart &)
bool intersects(const labelRange &, const bool touches=false) const
Return true if the ranges intersect.
Definition: labelRange.C:51
label first() const
The (inclusive) lower value of the range.
Definition: labelRangeI.H:161