yat
0.11.3pre
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
yat
utility
Segment.h
1
#ifndef theplu_yat_utility_segment
2
#define theplu_yat_utility_segment
3
4
// $Id: Segment.h 2358 2010-12-02 06:58:40Z 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 "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
112
template
<
typename
T,
class
Compare>
113
bool
compare(
const
Segment<T, Compare>
& lhs,
const
Segment<T, Compare>
& rhs)
114
{
115
Compare c;
116
// begin <= end
117
YAT_ASSERT(!c(lhs.
end
(), lhs.
begin
()));
118
YAT_ASSERT(!c(rhs.
end
(), rhs.
begin
()));
119
// take care of case when both sides are zero segments
120
if
(!c(lhs.
begin
(), lhs.
end
()) && !c(rhs.
begin
(), rhs.
end
())) {
121
return
c(lhs.
begin
(), rhs.
begin
());
122
}
123
124
return
! c(rhs.
begin
(), lhs.
end
());
125
}
126
137
template
<
typename
T,
class
Compare>
138
int
compare_3way(
const
Segment<T, Compare>
& lhs,
139
const
Segment<T, Compare>
& rhs)
140
{
141
if
(compare(lhs, rhs))
142
return
-1;
143
if
(compare(rhs, lhs))
144
return
1;
145
return
0;
146
}
147
156
template
<
typename
T,
class
Compare>
157
int
compare_3way(
const
T& element,
158
const
Segment<T, Compare>
& segment)
159
{
160
Compare comp;
161
if
(comp(element, segment.
begin
()))
162
return
-1;
163
if
(comp(element, segment.
end
()))
164
return
0;
165
return
1;
166
}
167
177
template
<
typename
T,
class
Compare>
178
Segment<T, Compare>
intersection(
const
Segment<T, Compare>
& a,
179
const
Segment<T, Compare>
& b)
180
{
181
Compare comp;
182
Segment<T, Compare>
result;
183
184
result.
begin
() =
std::max
(a.
begin
(), b.
begin
(), comp);
185
// the first max is needed in case a and b don't overlap
186
result.
end
() =
std::max
(result.
begin
(),
187
std::min(a.
end
(), b.
end
(), comp),
188
comp);
189
return
result;
190
}
191
195
template
<
typename
T,
class
Compare>
196
struct
SegmentCompare
:
197
public
std::binary_function<Segment<T,Compare>, Segment<T,Compare>, bool>
198
{
202
bool
operator()
(
const
Segment<T, Compare>
& lhs,
203
const
Segment<T, Compare>
& rhs)
const
204
{
return
compare(lhs, rhs); }
205
};
206
207
}}}
208
#endif
Generated on Sat May 24 2014 03:33:05 for yat by
1.8.2