1
/// An enum used to indicate an edge or a self loop.
2
pub enum Edge<T> {
3
    Regular(T, T),
4

            
5
    /// For a self loop we only provide a mutable reference to the single state.
6
    Selfloop(T),
7
}
8

            
9
/// Index two locations (from, to) of an edge, returns mutable references to it.
10
460
pub fn index_edge<T>(slice: &mut [T], a: usize, b: usize) -> Edge<&mut T> {
11
460
    if a == b {
12
        assert!(a <= slice.len());
13
        Edge::Selfloop(slice.get_mut(a).unwrap())
14
    } else {
15
460
        assert!(a <= slice.len() && b < slice.len());
16

            
17
        // safe because a, b are in bounds and distinct
18
        unsafe {
19
460
            let ar = &mut *(slice.get_unchecked_mut(a) as *mut _);
20
460
            let br = &mut *(slice.get_unchecked_mut(b) as *mut _);
21
460
            Edge::Regular(ar, br)
22
        }
23
    }
24
460
}