yat  0.14.5pre
Segment.h
1 #ifndef theplu_yat_utility_segment
2 #define theplu_yat_utility_segment
3 
4 // $Id: Segment.h 3550 2017-01-03 05:41:02Z peter $
5 
6 /*
7  Copyright (C) 2010, 2015, 2016 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 
112  template<typename T, class Compare>
114  const Segment<T, Compare>& rhs)
115  {
116  Compare comp;
117  return comp(lhs.begin(), rhs.begin()) || comp(rhs.begin(), lhs.begin())
118  || comp(lhs.end(), rhs.end()) || comp(rhs.end(), lhs.end());
119  }
120 
121 
131  template<typename T, class Compare>
133  const Segment<T, Compare>& rhs)
134  {
135  return !(lhs!=rhs);
136  }
137 
138 
154  template<typename T, class Compare>
155  bool compare(const Segment<T, Compare>& lhs, const Segment<T, Compare>& rhs)
156  {
157  Compare c;
158  // begin <= end
159  YAT_ASSERT(!c(lhs.end(), lhs.begin()));
160  YAT_ASSERT(!c(rhs.end(), rhs.begin()));
161  // take care of case when both sides are zero segments
162  if (!c(lhs.begin(), lhs.end()) && !c(rhs.begin(), rhs.end())) {
163  return c(lhs.begin(), rhs.begin());
164  }
165 
166  return ! c(rhs.begin(), lhs.end());
167  }
168 
179  template<typename T, class Compare>
181  const Segment<T, Compare>& rhs)
182  {
183  if (compare(lhs, rhs))
184  return -1;
185  if (compare(rhs, lhs))
186  return 1;
187  return 0;
188  }
189 
198  template<typename T, class Compare>
199  int compare_3way(const T& element,
200  const Segment<T, Compare>& segment)
201  {
202  Compare comp;
203  if (comp(element, segment.begin()))
204  return -1;
205  if (comp(element, segment.end()))
206  return 0;
207  return 1;
208  }
209 
217  template<typename T, class Compare>
218  int compare_3way(const Segment<T, Compare>& segment,
219  const T& element)
220  {
221  return -compare_3way(element, segment);
222  }
223 
233  template<typename T, class Compare>
235  const Segment<T, Compare>& b)
236  {
237  Compare comp;
238  Segment<T, Compare> result;
239 
240  result.begin() = std::max(a.begin(), b.begin(), comp);
241  // the first max is needed in case a and b don't overlap
242  result.end() = std::max(result.begin(),
243  std::min(a.end(), b.end(), comp),
244  comp);
245  return result;
246  }
247 
251  template<typename T, class Compare>
252  struct SegmentCompare :
253  public std::binary_function<Segment<T,Compare>, Segment<T,Compare>, bool>
254  {
259  const Segment<T, Compare>& rhs) const
260  { return compare(lhs, rhs); }
261  };
262 
263 }}}
264 #endif
const T & begin(void) const
Definition: Segment.h:76
T & end(void)
Definition: Segment.h:81
T value_type
Definition: Segment.h:53
const T & end(void) const
Definition: Segment.h:86
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:697
Segment(void)
default constructor
Definition: Segment.h:58
functor using compare
Definition: Segment.h:252
int compare_3way(const T &element, const Segment< T, Compare > &segment)
Definition: Segment.h:199
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:132
Segment< T, Compare > intersection(const Segment< T, Compare > &a, const Segment< T, Compare > &b)
Definition: Segment.h:234
int compare_3way(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Definition: Segment.h:180
bool operator()(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs) const
Definition: Segment.h:258
bool operator!=(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Inequality operator.
Definition: Segment.h:113
bool compare(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Definition: Segment.h:155
Segment(const T &begin, const T &end)
Constructor.
Definition: Segment.h:65
T & begin(void)
Definition: Segment.h:71
int compare_3way(const Segment< T, Compare > &segment, const T &element)
Definition: Segment.h:218

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5