HermesCommon  3.0
range.cpp
Go to the documentation of this file.
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://www.hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "range.h"
23 #include <algorithm>
24 namespace Hermes
25 {
26  Range::Range() : empty_range(true) {}
27 
28  Range::Range(const int& lower_bound, const int& upper_bound) : lower_bound(lower_bound), upper_bound(upper_bound), empty_range(lower_bound > upper_bound)
29  {
30  }
31 
32  bool Range::empty() const
33  {
34  return empty_range;
35  }
36 
37  const int& Range::lower() const
38  {
39  return lower_bound;
40  }
41 
42  const int& Range::upper() const
43  {
44  return upper_bound;
45  }
46 
47  bool Range::is_in_closed(const Range& range) const
48  {
49  return (this->empty() ? false : (range.lower_bound >= lower_bound && range.upper_bound <= upper_bound));
50  }
51 
52  bool Range::is_in_closed(const int& value) const
53  {
54  return (this->empty() ? false : (value >= lower_bound && value <= upper_bound));
55  }
56 
57  bool Range::is_in_open(const int& value) const
58  {
59  return (this->empty() ? false : (value > lower_bound && value < upper_bound));
60  }
61 
62  void Range::enlarge_to_include(const int& value)
63  {
64  if (empty_range) {
65  lower_bound = upper_bound = value;
66  empty_range = false;
67  }
68  else {
69  if (lower_bound > value)
70  lower_bound = value;
71  if (upper_bound < value)
72  upper_bound = value;
73  }
74  }
75 
76  Range Range::make_envelope(const Range& a, const Range& b)
77  {
78  if (a.empty())
79  return b;
80  else if (b.empty())
81  return a;
82  else
83  return Range(std::min(a.lower(), b.lower()), std::max(a.upper(), b.upper()));
84  }
85 }
General namespace for the Hermes library.
Range of values.
Definition: range.h:26
File containing the class Range.