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

Generated on Thu Dec 12 2019 03:12:08 for yat by  doxygen 1.8.11