yat  0.21pre
Segment.h
1 #ifndef theplu_yat_utility_segment
2 #define theplu_yat_utility_segment
3 
4 // $Id: Segment.h 4089 2021-09-07 00:56:40Z peter $
5 
6 /*
7  Copyright (C) 2010, 2015, 2016, 2017 Peter Johansson
8  Copyright (C) 2019 Jari Häkkinen
9  Copyright (C) 2021 Peter Johansson
10 
11  This file is part of the yat library, http://dev.thep.lu.se/yat
12 
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 3 of the
16  License, or (at your option) any later version.
17 
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with yat. If not, see <http://www.gnu.org/licenses/>.
25 */
26 
27 #include "yat_assert.h"
28 
29 #include <algorithm>
30 #include <functional>
31 
32 namespace theplu {
33 namespace yat {
34 namespace utility {
35 
48  template<typename T, class Compare = std::less<T> >
49  class Segment
50  {
51  public:
55  typedef T value_type;
56 
60  Segment(void) {}
61 
67  Segment(const T& begin, const T& end)
68  : begin_(begin), end_(end) {}
69 
77  Segment(T&& begin, T&& end)
78  : begin_(std::move(begin)), end_(std::move(end)) {}
79 
83  T& begin(void) { return begin_; }
84 
88  const T& begin(void) const { return begin_; }
89 
93  T& end(void) { return end_; }
94 
98  const T& end(void) const { return end_; }
99 
100  private:
101  T begin_;
102  T end_;
103 
104  // using compiler generated copying
105  //Segment(const Segment&);
106  //Segment& operator=(const Segment&);
107  };
108 
109 
115  template<typename T>
116  Segment<T> make_segment(T first, T last)
117  {
118  return Segment<T>(first, last);
119  }
120 
121 
136  template<typename T, class Compare>
138  const Segment<T, Compare>& rhs)
139  {
140  Compare comp;
141  return comp(lhs.begin(), rhs.begin()) || comp(rhs.begin(), lhs.begin())
142  || comp(lhs.end(), rhs.end()) || comp(rhs.end(), lhs.end());
143  }
144 
145 
155  template<typename T, class Compare>
157  const Segment<T, Compare>& rhs)
158  {
159  return !(lhs!=rhs);
160  }
161 
162 
178  template<typename T, class Compare>
179  bool compare(const Segment<T, Compare>& lhs, const Segment<T, Compare>& rhs)
180  {
181  Compare c;
182  // begin <= end
183  YAT_ASSERT(!c(lhs.end(), lhs.begin()));
184  YAT_ASSERT(!c(rhs.end(), rhs.begin()));
185  // take care of case when both sides are zero segments
186  if (!c(lhs.begin(), lhs.end()) && !c(rhs.begin(), rhs.end())) {
187  return c(lhs.begin(), rhs.begin());
188  }
189 
190  return ! c(rhs.begin(), lhs.end());
191  }
192 
203  template<typename T, class Compare>
205  const Segment<T, Compare>& rhs)
206  {
207  if (compare(lhs, rhs))
208  return -1;
209  if (compare(rhs, lhs))
210  return 1;
211  return 0;
212  }
213 
222  template<typename T, class Compare>
223  int compare_3way(const T& element,
224  const Segment<T, Compare>& segment)
225  {
226  Compare comp;
227  if (comp(element, segment.begin()))
228  return -1;
229  if (comp(element, segment.end()))
230  return 0;
231  return 1;
232  }
233 
241  template<typename T, class Compare>
242  int compare_3way(const Segment<T, Compare>& segment,
243  const T& element)
244  {
245  return -compare_3way(element, segment);
246  }
247 
257  template<typename T, class Compare>
259  const Segment<T, Compare>& b)
260  {
261  Compare comp;
262  Segment<T, Compare> result;
263 
264  result.begin() = std::max(a.begin(), b.begin(), comp);
265  // the first max is needed in case a and b don't overlap
266  result.end() = std::max(result.begin(),
267  std::min(a.end(), b.end(), comp),
268  comp);
269  return result;
270  }
271 
272 
286  template<typename T, class Compare>
288  const Segment<T, Compare>& b)
289  {
290  Compare comp;
291  return Segment<T, Compare>(std::min(a.begin(), b.begin(), comp),
292  std::max(a.end(), b.end(), comp));
293  }
294 
295 
304  template<typename T, class Compare>
307  {
308  Compare comp;
309  return !(comp(lhs.begin(), rhs.begin()) || comp(rhs.end(), lhs.end()));
310  }
311 
312 
321  template<typename T, class Compare>
324  {
325  return !compare(lhs, rhs) && !compare(rhs, lhs);
326  }
327 
328 
332  template<typename T, class Compare>
333  struct SegmentCompare :
334  public std::binary_function<Segment<T,Compare>, Segment<T,Compare>, bool>
335  {
340  const Segment<T, Compare>& rhs) const
341  { return compare(lhs, rhs); }
342  };
343 
344 }}}
345 #endif
bool includes(const yat::utility::Segment< T, Compare > &lhs, const yat::utility::Segment< T, Compare > &rhs)
Definition: Segment.h:305
The Department of Theoretical Physics namespace as we define it.
T & end(void)
Definition: Segment.h:93
T value_type
Definition: Segment.h:55
Definition: stl_utility.h:64
Segment< T, Compare > set_union(const Segment< T, Compare > &a, const Segment< T, Compare > &b)
union of two segments
Definition: Segment.h:287
Segment< T > make_segment(T first, T last)
Definition: Segment.h:116
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:339
Segment(void)
default constructor
Definition: Segment.h:60
functor using compare
Definition: Segment.h:333
int compare_3way(const T &element, const Segment< T, Compare > &segment)
Definition: Segment.h:223
Segment(T &&begin, T &&end)
Constructor.
Definition: Segment.h:77
a class for a Segment or Interval
Definition: Segment.h:49
bool operator==(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Definition: Segment.h:156
const T & end(void) const
Definition: Segment.h:98
Segment< T, Compare > intersection(const Segment< T, Compare > &a, const Segment< T, Compare > &b)
Definition: Segment.h:258
int compare_3way(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Definition: Segment.h:204
const T & begin(void) const
Definition: Segment.h:88
bool operator!=(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Inequality operator.
Definition: Segment.h:137
bool compare(const Segment< T, Compare > &lhs, const Segment< T, Compare > &rhs)
Definition: Segment.h:179
Segment(const T &begin, const T &end)
Constructor.
Definition: Segment.h:67
T & begin(void)
Definition: Segment.h:83
bool overlap(const yat::utility::Segment< T, Compare > &lhs, const yat::utility::Segment< T, Compare > &rhs)
Definition: Segment.h:322
int compare_3way(const Segment< T, Compare > &segment, const T &element)
Definition: Segment.h:242

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14