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
use crate::NodeId;
use crate::Position;
use crate::TreeSequence;

use crate::bindings;

#[repr(transparent)]
struct LLEdgeDifferenceIterator(bindings::tsk_diff_iter_t);

impl Drop for LLEdgeDifferenceIterator {
    fn drop(&mut self) {
        unsafe { bindings::tsk_diff_iter_free(&mut self.0) };
    }
}

impl LLEdgeDifferenceIterator {
    pub fn new_from_treeseq(
        treeseq: &TreeSequence,
        flags: bindings::tsk_flags_t,
    ) -> Result<Self, crate::TskitError> {
        let mut inner = std::mem::MaybeUninit::<bindings::tsk_diff_iter_t>::uninit();
        let code =
            unsafe { bindings::tsk_diff_iter_init(inner.as_mut_ptr(), treeseq.as_ptr(), flags) };
        handle_tsk_return_value!(code, Self(unsafe { inner.assume_init() }))
    }
}

/// Marker type for edge insertion.
pub struct Insertion {}

/// Marker type for edge removal.
pub struct Removal {}

mod private {
    pub trait EdgeDifferenceIteration {}

    impl EdgeDifferenceIteration for super::Insertion {}
    impl EdgeDifferenceIteration for super::Removal {}
}

struct LLEdgeList<T: private::EdgeDifferenceIteration> {
    inner: bindings::tsk_edge_list_t,
    marker: std::marker::PhantomData<T>,
}

macro_rules! build_lledgelist {
    ($name: ident, $generic: ty) => {
        type $name = LLEdgeList<$generic>;

        impl Default for $name {
            fn default() -> Self {
                Self {
                    inner: bindings::tsk_edge_list_t {
                        head: std::ptr::null_mut(),
                        tail: std::ptr::null_mut(),
                    },
                    marker: std::marker::PhantomData::<$generic> {},
                }
            }
        }
    };
}

build_lledgelist!(LLEdgeInsertionList, Insertion);
build_lledgelist!(LLEdgeRemovalList, Removal);

/// Concrete type implementing [`Iterator`] over [`EdgeInsertion`] or [`EdgeRemoval`].
/// Created by [`EdgeDifferencesIterator::edge_insertions`] or
/// [`EdgeDifferencesIterator::edge_removals`], respectively.
pub struct EdgeDifferences<'a, T: private::EdgeDifferenceIteration> {
    inner: &'a LLEdgeList<T>,
    current: *mut bindings::tsk_edge_list_node_t,
}

impl<'a, T: private::EdgeDifferenceIteration> EdgeDifferences<'a, T> {
    fn new(inner: &'a LLEdgeList<T>) -> Self {
        Self {
            inner,
            current: std::ptr::null_mut(),
        }
    }
}

/// An edge difference. Edge insertions and removals are differentiated by
/// marker types [`Insertion`] and [`Removal`], respectively.
#[derive(Debug, Copy, Clone)]
pub struct EdgeDifference<T: private::EdgeDifferenceIteration> {
    left: Position,
    right: Position,
    parent: NodeId,
    child: NodeId,
    marker: std::marker::PhantomData<T>,
}

impl<T: private::EdgeDifferenceIteration> EdgeDifference<T> {
    fn new<P: Into<Position>, N: Into<NodeId>>(left: P, right: P, parent: N, child: N) -> Self {
        Self {
            left: left.into(),
            right: right.into(),
            parent: parent.into(),
            child: child.into(),
            marker: std::marker::PhantomData::<T> {},
        }
    }

    pub fn left(&self) -> Position {
        self.left
    }
    pub fn right(&self) -> Position {
        self.right
    }
    pub fn parent(&self) -> NodeId {
        self.parent
    }
    pub fn child(&self) -> NodeId {
        self.child
    }
}

impl<T> std::fmt::Display for EdgeDifference<T>
where
    T: private::EdgeDifferenceIteration,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "left: {}, right: {}, parent: {}, child: {}",
            self.left(),
            self.right(),
            self.parent(),
            self.child()
        )
    }
}

/// Type alias for [`EdgeDifference<Insertion>`]
pub type EdgeInsertion = EdgeDifference<Insertion>;
/// Type alias for [`EdgeDifference<Removal>`]
pub type EdgeRemoval = EdgeDifference<Removal>;

impl<'a, T> Iterator for EdgeDifferences<'a, T>
where
    T: private::EdgeDifferenceIteration,
{
    type Item = EdgeDifference<T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current.is_null() {
            self.current = self.inner.inner.head;
        } else {
            self.current = unsafe { *self.current }.next;
        }
        if self.current.is_null() {
            None
        } else {
            let left = unsafe { (*self.current).edge.left };
            let right = unsafe { (*self.current).edge.right };
            let parent = unsafe { (*self.current).edge.parent };
            let child = unsafe { (*self.current).edge.child };
            Some(Self::Item::new(left, right, parent, child))
        }
    }
}

/// Manages iteration over trees to obtain
/// edge differences.
pub struct EdgeDifferencesIterator {
    inner: LLEdgeDifferenceIterator,
    insertion: LLEdgeInsertionList,
    removal: LLEdgeRemovalList,
    left: f64,
    right: f64,
    advanced: i32,
}

impl EdgeDifferencesIterator {
    // NOTE: will return None if tskit-c cannot
    // allocate memory for internal structures.
    pub(crate) fn new_from_treeseq(
        treeseq: &TreeSequence,
        flags: bindings::tsk_flags_t,
    ) -> Result<Self, crate::TskitError> {
        LLEdgeDifferenceIterator::new_from_treeseq(treeseq, flags).map(|inner| Self {
            inner,
            insertion: LLEdgeInsertionList::default(),
            removal: LLEdgeRemovalList::default(),
            left: f64::default(),
            right: f64::default(),
            advanced: 0,
        })
    }

    fn advance_tree(&mut self) {
        // SAFETY: our tree sequence is guaranteed
        // to be valid and own its tables.
        self.advanced = unsafe {
            bindings::tsk_diff_iter_next(
                &mut self.inner.0,
                &mut self.left,
                &mut self.right,
                &mut self.removal.inner,
                &mut self.insertion.inner,
            )
        };
    }

    pub fn left(&self) -> Position {
        self.left.into()
    }

    pub fn right(&self) -> Position {
        self.right.into()
    }

    pub fn interval(&self) -> (Position, Position) {
        (self.left(), self.right())
    }

    pub fn edge_removals(&self) -> impl Iterator<Item = EdgeRemoval> + '_ {
        EdgeDifferences::<Removal>::new(&self.removal)
    }

    pub fn edge_insertions(&self) -> impl Iterator<Item = EdgeInsertion> + '_ {
        EdgeDifferences::<Insertion>::new(&self.insertion)
    }
}

impl streaming_iterator::StreamingIterator for EdgeDifferencesIterator {
    type Item = EdgeDifferencesIterator;

    fn advance(&mut self) {
        self.advance_tree()
    }

    fn get(&self) -> Option<&Self::Item> {
        if self.advanced > 0 {
            Some(self)
        } else {
            None
        }
    }
}