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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use crate::{CoordNum, Geometry};
#[cfg(any(feature = "approx", test))]
use approx::{AbsDiffEq, RelativeEq};
use std::iter::FromIterator;
use std::ops::{Index, IndexMut};
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
pub struct GeometryCollection<T>(pub Vec<Geometry<T>>)
where
T: CoordNum;
impl<T: CoordNum> Default for GeometryCollection<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: CoordNum> GeometryCollection<T> {
pub fn new() -> GeometryCollection<T> {
GeometryCollection(Vec::new())
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<T: CoordNum, IG: Into<Geometry<T>>> From<IG> for GeometryCollection<T> {
fn from(x: IG) -> Self {
GeometryCollection(vec![x.into()])
}
}
impl<T: CoordNum, IG: Into<Geometry<T>>> FromIterator<IG> for GeometryCollection<T> {
fn from_iter<I: IntoIterator<Item = IG>>(iter: I) -> Self {
GeometryCollection(iter.into_iter().map(|g| g.into()).collect())
}
}
impl<T: CoordNum> Index<usize> for GeometryCollection<T> {
type Output = Geometry<T>;
fn index(&self, index: usize) -> &Geometry<T> {
self.0.index(index)
}
}
impl<T: CoordNum> IndexMut<usize> for GeometryCollection<T> {
fn index_mut(&mut self, index: usize) -> &mut Geometry<T> {
self.0.index_mut(index)
}
}
#[derive(Debug)]
pub struct IntoIteratorHelper<T: CoordNum> {
iter: ::std::vec::IntoIter<Geometry<T>>,
}
impl<T: CoordNum> IntoIterator for GeometryCollection<T> {
type Item = Geometry<T>;
type IntoIter = IntoIteratorHelper<T>;
fn into_iter(self) -> Self::IntoIter {
IntoIteratorHelper {
iter: self.0.into_iter(),
}
}
}
impl<T: CoordNum> Iterator for IntoIteratorHelper<T> {
type Item = Geometry<T>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
#[derive(Debug)]
pub struct IterHelper<'a, T: CoordNum> {
iter: ::std::slice::Iter<'a, Geometry<T>>,
}
impl<'a, T: CoordNum> IntoIterator for &'a GeometryCollection<T> {
type Item = &'a Geometry<T>;
type IntoIter = IterHelper<'a, T>;
fn into_iter(self) -> Self::IntoIter {
IterHelper {
iter: self.0.iter(),
}
}
}
impl<'a, T: CoordNum> Iterator for IterHelper<'a, T> {
type Item = &'a Geometry<T>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
#[derive(Debug)]
pub struct IterMutHelper<'a, T: CoordNum> {
iter: ::std::slice::IterMut<'a, Geometry<T>>,
}
impl<'a, T: CoordNum> IntoIterator for &'a mut GeometryCollection<T> {
type Item = &'a mut Geometry<T>;
type IntoIter = IterMutHelper<'a, T>;
fn into_iter(self) -> Self::IntoIter {
IterMutHelper {
iter: self.0.iter_mut(),
}
}
}
impl<'a, T: CoordNum> Iterator for IterMutHelper<'a, T> {
type Item = &'a mut Geometry<T>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
impl<'a, T: CoordNum> GeometryCollection<T> {
pub fn iter(&'a self) -> IterHelper<'a, T> {
self.into_iter()
}
pub fn iter_mut(&'a mut self) -> IterMutHelper<'a, T> {
self.into_iter()
}
}
#[cfg(any(feature = "approx", test))]
impl<T> RelativeEq for GeometryCollection<T>
where
T: AbsDiffEq<Epsilon = T> + CoordNum + RelativeEq,
{
#[inline]
fn default_max_relative() -> Self::Epsilon {
T::default_max_relative()
}
#[inline]
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
if self.0.len() != other.0.len() {
return false;
}
let mut mp_zipper = self.iter().zip(other.iter());
mp_zipper.all(|(lhs, rhs)| lhs.relative_eq(&rhs, epsilon, max_relative))
}
}
#[cfg(any(feature = "approx", test))]
impl<T> AbsDiffEq for GeometryCollection<T>
where
T: AbsDiffEq<Epsilon = T> + CoordNum,
T::Epsilon: Copy,
{
type Epsilon = T;
#[inline]
fn default_epsilon() -> Self::Epsilon {
T::default_epsilon()
}
#[inline]
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
if self.0.len() != other.0.len() {
return false;
}
let mut mp_zipper = self.into_iter().zip(other.into_iter());
mp_zipper.all(|(lhs, rhs)| lhs.abs_diff_eq(&rhs, epsilon))
}
}