source: trunk/yat/utility/Segment.h @ 2321

Last change on this file since 2321 was 2321, checked in by Peter, 13 years ago

adding missing include

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1#ifndef theplu_yat_utility_segment
2#define theplu_yat_utility_segment
3
4// $Id: Segment.h 2321 2010-09-02 22:40:08Z peter $
5
6/*
7  Copyright (C) 2010 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 <algorithm>
26#include <functional>
27
28namespace theplu {
29namespace yat {
30namespace utility {
31
32  /**
33     \brief a class for a Segment or Interval
34
35     A class for a Segment [begin, end).
36
37     \since new in yat 0.7
38   */
39  template<typename T, class Compare = std::less<T> >
40  class Segment
41  {
42  public:
43    /**
44       type the Segment holds
45     */
46    typedef T value_type;
47
48    /**
49       \brief default constructor
50     */
51    Segment(void) {}
52
53    /**
54       \brief Constructor
55
56       Creates a segment [begin, end)
57     */
58    Segment(const T& begin, const T& end)
59      : begin_(begin), end_(end) {}
60
61    /**
62       \return reference to first element in Segment
63     */
64    T& begin(void) { return begin_; }
65
66    /**
67       \return const reference to first element in Segment
68     */
69    const T& begin(void) const { return begin_; }
70
71    /**
72       \return reference to first element greater than Segment
73     */
74    T& end(void) { return end_; }
75
76    /**
77       \return const reference to first element greater than Segment
78     */
79    const T& end(void) const { return end_; }
80   
81  private:
82    T begin_;
83    T end_;
84   
85    // using compiler generated copying
86    //Segment(const Segment&);
87    //Segment& operator=(const Segment&);
88  };
89
90  /**
91     \return true if all elements in \a lhs is less than all elements
92     in \a rhs, i.e., returns false if rhs.begin() is less than
93     lhs.end()
94
95     \relates Segment
96
97     \since new in yat 0.7
98   */
99  template<typename T, class Compare>
100  bool compare(const Segment<T, Compare>& lhs, const Segment<T, Compare>& rhs)
101  {
102    Compare c;
103    return ! c(rhs.begin(), lhs.end());
104  }
105
106  /**
107     \return -1 if all elements in lhs is less than all elements in
108     rhs; +1 if all elements in rhs is less than all elements in lhs;
109     and 0 if lhs and rhs overlap.
110
111     \relates Segment
112
113     \since new in yat 0.7
114   */
115  template<typename T, class Compare>
116  int compare_3way(const Segment<T, Compare>& lhs, 
117                    const Segment<T, Compare>& rhs)
118  {
119    if (compare(lhs, rhs))
120      return -1;
121    if (compare(rhs, lhs))
122      return 1;
123    return 0;
124  }
125
126  /**
127     \return -1 if lhs is less than all elements in rhs; +1 if all
128     elements in rhs is less than lhs; and 0 if lhs and rhs overlap
129
130     \relates Segment
131
132     \since new in yat 0.7
133   */
134  template<typename T, class Compare>
135  int compare_3way(const T& lhs, 
136                   const Segment<T, Compare>& rhs)
137  {
138    Compare comp;
139    if (comp(lhs, rhs.begin()))
140      return -1;
141    if (comp(lhs, rhs.end()))
142      return 0;
143    return 1;
144  }
145
146  /**
147     \return intersection of \a a and \a b. If \a a and \b do not
148     overlap an empty Section is returned (begin==end), but the exact
149     value of begin is undefined.
150
151     \relates Segment
152
153     \since new in yat 0.7
154   */
155  template<typename T, class Compare>
156  Segment<T, Compare> intersection(const Segment<T, Compare>& a, 
157                                   const Segment<T, Compare>& b)
158  {
159    Compare comp;
160    Segment<T, Compare> result;
161
162    result.begin() = std::max(a.begin(), b.begin(), comp);
163    result.end() = std::max(result.begin(), 
164                            std::min(a.end(), b.end(), comp),
165                            comp);
166    return result;
167  }
168
169}}}
170#endif
Note: See TracBrowser for help on using the repository browser.