1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use geo_types::{CoordNum, Coordinate};
pub fn partition_slice<T, P>(data: &mut [T], predicate: P) -> (&mut [T], &mut [T])
where
P: Fn(&T) -> bool,
{
let len = data.len();
if len == 0 {
return (&mut [], &mut []);
}
let (mut l, mut r) = (0, len - 1);
loop {
while l < len && predicate(&data[l]) {
l += 1;
}
while r > 0 && !predicate(&data[r]) {
r -= 1;
}
if l >= r {
return data.split_at_mut(l);
}
data.swap(l, r);
}
}
pub enum EitherIter<T, I1, I2>
where
I1: Iterator<Item = T>,
I2: Iterator<Item = T>,
{
A(I1),
B(I2),
}
impl<T, I1, I2> Iterator for EitherIter<T, I1, I2>
where
I1: Iterator<Item = T>,
I2: Iterator<Item = T>,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match self {
EitherIter::A(iter) => iter.next(),
EitherIter::B(iter) => iter.next(),
}
}
}
pub fn partial_max<T: PartialOrd>(a: T, b: T) -> T {
if a > b {
a
} else {
b
}
}
pub fn partial_min<T: PartialOrd>(a: T, b: T) -> T {
if a < b {
a
} else {
b
}
}
pub use crate::algorithm::coordinate_position::{coord_pos_relative_to_ring, CoordPos};
use std::cmp::Ordering;
#[inline]
pub fn lex_cmp<T: CoordNum>(p: &Coordinate<T>, q: &Coordinate<T>) -> Ordering {
p.x.partial_cmp(&q.x)
.unwrap()
.then(p.y.partial_cmp(&q.y).unwrap())
}
pub fn least_index<T: CoordNum>(pts: &[Coordinate<T>]) -> usize {
pts.iter()
.enumerate()
.min_by(|(_, p), (_, q)| lex_cmp(p, q))
.unwrap()
.0
}
pub fn least_and_greatest_index<T: CoordNum>(pts: &[Coordinate<T>]) -> (usize, usize) {
assert_ne!(pts.len(), 0);
let (min, max) = pts
.iter()
.enumerate()
.fold((None, None), |(min, max), (idx, p)| {
(
if let Some((midx, min)) = min {
if lex_cmp(p, min) == Ordering::Less {
Some((idx, p))
} else {
Some((midx, min))
}
} else {
Some((idx, p))
},
if let Some((midx, max)) = max {
if lex_cmp(p, max) == Ordering::Greater {
Some((idx, p))
} else {
Some((midx, max))
}
} else {
Some((idx, p))
},
)
});
(min.unwrap().0, max.unwrap().0)
}
#[cfg(test)]
mod test {
use super::{partial_max, partial_min};
#[test]
fn test_partial_max() {
assert_eq!(5, partial_max(5, 4));
assert_eq!(5, partial_max(5, 5));
}
#[test]
fn test_partial_min() {
assert_eq!(4, partial_min(5, 4));
assert_eq!(4, partial_min(4, 4));
}
}