yat  0.18.2pre
Segment.h
1 #ifndef theplu_yat_utility_segment
2 #define theplu_yat_utility_segment
3 
4 // $Id: Segment.h 3855 2020-01-02 01:11:34Z peter $
5 
6 /*
7  Copyright (C) 2010, 2015, 2016, 2017 Peter Johansson
8  Copyright (C) 2019 Jari Häkkinen
9 
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11 
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16 
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #include "yat_assert.h"
27 
28 #include <algorithm>
29 #include <functional>
30 
31 namespace theplu {
32 namespace yat {
33 namespace utility {
34 
47  template<typename T, class Compare = std::less<T> >
48  class Segment
49  {
50  public:
54  typedef T value_type;
55 
59  Segment(void) {}
60 
66  Segment(const T& begin, const T& end)
67  : begin_(begin), end_(end) {}
68 
72  T& begin(void) { return begin_; }
73 
77  const T& begin(void) const { return begin_; }
78 
82  T& end(void) { return end_; }
83 
87  const T& end(void) const { return end_; }
88 
89  private:
90  T begin_;
91  T end_;
92 
93  // using compiler generated copying
94  //Segment(const Segment&);
95  //Segment& operator=(const Segment&);
96  };
97 
98 
104  template<typename T>
105  Segment<T> make_segment(T first, T last)
106  {
107  return Segment<T>(first, last);
108  }
109 
110 
125  template<typename T, class Compare>
127  const Segment<T, Compare>& rhs)
128  {
129  Compare comp;
130  return comp(lhs.begin(), rhs.begin()) || comp(rhs.begin(), lhs.begin())
131  || comp(lhs.end(), rhs.end()) || comp(rhs.end(), lhs.end());
132  }
133 
134 
144  template<typename T, class Compare>
146  const Segment<T, Compare>& rhs)
147  {
148  return !(lhs!=rhs);
149  }
150 
151 
167  template<typename T, class Compare>
168  bool compare(const Segment<T, Compare>& lhs, const Segment<T, Compare>& rhs)
169  {
170  Compare c;
171  // begin <= end
172  YAT_ASSERT(!c(lhs.end(), lhs.begin()));
173  YAT_ASSERT(!c(rhs.end(), rhs.begin()));
174  // take care of case when both sides are zero segments
175  if (!c(lhs.begin(), lhs.end()) && !c(rhs.begin(), rhs.end())) {
176  return c(lhs.begin(), rhs.begin());
177  }
178 
179  return ! c(rhs.begin(), lhs.end());
180  }
181 
192  template<typename T, class Compare>
194  const Segment<T, Compare>& rhs)
195  {
196  if (compare(lhs, rhs))
197  return -1;
198  if (compare(rhs, lhs))
199  return 1;
200  return 0;
201  }
202 
211  template<typename T, class Compare>
212  int compare_3way(const T& element,
213  const Segment<T, Compare>& segment)
214  {
215  Compare comp;
216  if (comp(element, segment.begin()))
217  return -1;
218  if (comp(element, segment.end()))
219  return 0;
220  return 1;
221  }
222 
230  template<typename T, class Compare>
231  int compare_3way(const Segment<T, Compare>& segment,
232  const T& element)
233  {
234  return -compare_3way(element, segment);
235  }
236 
246  template<typename T, class Compare>
248  const Segment<T, Compare>& b)
249  {
250  Compare comp;
251  Segment<T, Compare> result;
252 
253  result.begin() = std::max(a.begin(), b.begin(), comp);
254  // the first max is needed in case a and b don't overlap
255  result.end() = std::max(result.begin(),
256  std::min(a.end(), b.end(), comp),
257  comp);
258  return result;
259  }
260 
261 
275  template<typename T, class Compare>
277  const Segment<T, Compare>& b)
278  {
279  Compare comp;
280  return Segment<T, Compare>(std::min(a.begin(), b.begin(), comp),
281  std::max(a.end(), b.end(), comp));
282  }
283 
284 
293  template<typename T, class Compare>
296  {
297  Compare comp;
298  return !(comp(lhs.begin(), rhs.begin()) || comp(rhs.end(), lhs.end()));
299  }
300 
301 
310  template<typename T, class Compare>
313  {
314  return !compare(lhs, rhs) && !compare(rhs, lhs);
315  }
316 
317 
321  template<typename T, class Compare>
322  struct SegmentCompare :
323  public std::binary_function<Segment<T,Compare>, Segment<T,Compare>, bool>
324  {
329  const Segment<T, Compare>& rhs) const
330  { return compare(lhs, rhs); }
331  };
332 
333 }}}
334 #endif
bool includes(const yat::utility::Segment< T, Compare > &lhs, const yat::utility::Segment< T, Compare > &rhs)
Definition: Segment.h:294
The Department of Theoretical Physics namespace as we define it.
T & end(void)
Definition: Segment.h:82
T value_type
Definition: Segment.h:54
Segment< T, Compare > set_union(const Segment< T, Compare > &a, const Segment< T, Compare > &b)
union of two segments
Definition: Segment.h:276
Segment< T > make_segment(T first, T last)
Definition: Segment.h:105
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:699
bool operator()(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs) const
Definition: Segment.h:328
Segment(void)
default constructor
Definition: Segment.h:59
functor using compare
Definition: Segment.h:322
int compare_3way(const T &element, const Segment< T, Compare > &segment)
Definition: Segment.h:212
a class for a Segment or Interval
Definition: Segment.h:48
bool operator==(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Definition: Segment.h:145
const T & end(void) const
Definition: Segment.h:87
Segment< T, Compare > intersection(const Segment< T, Compare > &a, const Segment< T, Compare > &b)
Definition: Segment.h:247
int compare_3way(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Definition: Segment.h:193
const T & begin(void) const
Definition: Segment.h:77
bool operator!=(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Inequality operator.
Definition: Segment.h:126
bool compare(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Definition: Segment.h:168
Segment(const T &begin, const T &end)
Constructor.
Definition: Segment.h:66
T & begin(void)
Definition: Segment.h:72
bool overlap(const yat::utility::Segment< T, Compare > &lhs, const yat::utility::Segment< T, Compare > &rhs)
Definition: Segment.h:311
int compare_3way(const Segment< T, Compare > &segment, const T &element)
Definition: Segment.h:231

Generated on Tue Sep 7 2021 17:32:33 for yat by  doxygen 1.8.14