spade/delaunay_core/handles/iterators/
fixed_iterators.rs

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
use core::marker::PhantomData;

use super::super::handle_defs::{DelaunayElementType, DynamicHandleImpl, FixedHandleImpl};
use super::super::InnerOuterMarker;

use crate::delaunay_core::Dcel;

pub struct FixedHandleIterator<Type, InnerOuter> {
    range: core::ops::Range<usize>,
    ty: PhantomData<Type>,
    inner_outer: PhantomData<InnerOuter>,
}

impl<Type: Default, InnerOuter: InnerOuterMarker> FixedHandleIterator<Type, InnerOuter> {
    pub(crate) fn new(to: usize) -> Self {
        Self {
            range: 0..to,
            ty: Default::default(),
            inner_outer: Default::default(),
        }
    }
}

impl<Type: Default, InnerOuter: InnerOuterMarker> Iterator
    for FixedHandleIterator<Type, InnerOuter>
{
    type Item = FixedHandleImpl<Type, InnerOuter>;

    fn next(&mut self) -> Option<Self::Item> {
        self.range.next().map(FixedHandleImpl::new)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.range.size_hint()
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.range.nth(n).map(FixedHandleImpl::new)
    }
}

impl<Type: Default, InnerOuter: InnerOuterMarker> ExactSizeIterator
    for FixedHandleIterator<Type, InnerOuter>
{
}

impl<Type: Default, InnerOuter: InnerOuterMarker> DoubleEndedIterator
    for FixedHandleIterator<Type, InnerOuter>
{
    fn next_back(&mut self) -> Option<Self::Item> {
        self.range.next_back().map(FixedHandleImpl::new)
    }

    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        self.range.nth_back(n).map(FixedHandleImpl::new)
    }
}

pub struct DynamicHandleIterator<'a, V, DE, UE, F, Type, InnerOuter> {
    fixed_iterator: FixedHandleIterator<Type, InnerOuter>,
    dcel: &'a Dcel<V, DE, UE, F>,
    inner_outer: PhantomData<InnerOuter>,
}

impl<'a, V, DE, UE, F, Type, InnerOuter> DynamicHandleIterator<'a, V, DE, UE, F, Type, InnerOuter>
where
    Type: DelaunayElementType + Default,
    InnerOuter: InnerOuterMarker,
{
    pub(crate) fn new(dcel: &'a Dcel<V, DE, UE, F>) -> Self {
        DynamicHandleIterator {
            fixed_iterator: FixedHandleIterator::new(Type::num_elements(dcel)),
            dcel,
            inner_outer: PhantomData,
        }
    }
}

impl<'a, V, DE, UE, F, Type, InnerOuter> Iterator
    for DynamicHandleIterator<'a, V, DE, UE, F, Type, InnerOuter>
where
    Type: DelaunayElementType,
    InnerOuter: InnerOuterMarker,
{
    type Item = DynamicHandleImpl<'a, V, DE, UE, F, Type, InnerOuter>;

    fn next(&mut self) -> Option<Self::Item> {
        self.fixed_iterator
            .next()
            .map(|handle| DynamicHandleImpl::new(self.dcel, handle.adjust_inner_outer()))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.fixed_iterator.size_hint()
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.fixed_iterator
            .nth(n)
            .map(|handle| DynamicHandleImpl::new(self.dcel, handle.adjust_inner_outer()))
    }
}

impl<V, DE, UE, F, Type, InnerOuter> DoubleEndedIterator
    for DynamicHandleIterator<'_, V, DE, UE, F, Type, InnerOuter>
where
    Type: DelaunayElementType,
    InnerOuter: InnerOuterMarker,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        self.fixed_iterator
            .next_back()
            .map(|handle| DynamicHandleImpl::new(self.dcel, handle.adjust_inner_outer()))
    }

    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        self.fixed_iterator
            .nth_back(n)
            .map(|handle| DynamicHandleImpl::new(self.dcel, handle.adjust_inner_outer()))
    }
}

impl<V, DE, UE, F, Type, InnerOuter> ExactSizeIterator
    for DynamicHandleIterator<'_, V, DE, UE, F, Type, InnerOuter>
where
    Type: DelaunayElementType,
    InnerOuter: InnerOuterMarker,
{
}