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
use crate::{CoordinateType, Point};
use std::iter::FromIterator;
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MultiPoint<T>(pub Vec<Point<T>>)
where
T: CoordinateType;
impl<T: CoordinateType, IP: Into<Point<T>>> From<IP> for MultiPoint<T> {
fn from(x: IP) -> MultiPoint<T> {
MultiPoint(vec![x.into()])
}
}
impl<T: CoordinateType, IP: Into<Point<T>>> From<Vec<IP>> for MultiPoint<T> {
fn from(v: Vec<IP>) -> MultiPoint<T> {
MultiPoint(v.into_iter().map(|p| p.into()).collect())
}
}
impl<T: CoordinateType, IP: Into<Point<T>>> FromIterator<IP> for MultiPoint<T> {
fn from_iter<I: IntoIterator<Item = IP>>(iter: I) -> Self {
MultiPoint(iter.into_iter().map(|p| p.into()).collect())
}
}
impl<T: CoordinateType> IntoIterator for MultiPoint<T> {
type Item = Point<T>;
type IntoIter = ::std::vec::IntoIter<Point<T>>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a, T: CoordinateType> IntoIterator for &'a MultiPoint<T> {
type Item = &'a Point<T>;
type IntoIter = ::std::slice::Iter<'a, Point<T>>;
fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter()
}
}
impl<'a, T: CoordinateType> IntoIterator for &'a mut MultiPoint<T> {
type Item = &'a mut Point<T>;
type IntoIter = ::std::slice::IterMut<'a, Point<T>>;
fn into_iter(self) -> Self::IntoIter {
(&mut self.0).iter_mut()
}
}
impl<T: CoordinateType> MultiPoint<T> {
pub fn iter(&self) -> impl Iterator<Item = &Point<T>> {
self.0.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point<T>> {
self.0.iter_mut()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::point;
#[test]
fn test_iter() {
let multi = MultiPoint(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);
let mut first = true;
for p in &multi {
if first {
assert_eq!(p, &point![x: 0, y: 0]);
first = false;
} else {
assert_eq!(p, &point![x: 10, y: 10]);
}
}
first = true;
for p in &multi {
if first {
assert_eq!(p, &point![x: 0, y: 0]);
first = false;
} else {
assert_eq!(p, &point![x: 10, y: 10]);
}
}
}
#[test]
fn test_iter_mut() {
let mut multi = MultiPoint(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);
for point in &mut multi {
point.0.x += 1;
point.0.y += 1;
}
for point in multi.iter_mut() {
point.0.x += 1;
point.0.y += 1;
}
let mut first = true;
for p in &multi {
if first {
assert_eq!(p, &point![x: 2, y: 2]);
first = false;
} else {
assert_eq!(p, &point![x: 12, y: 12]);
}
}
}
}