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
use shipyard::*;
use std::collections::VecDeque;
use super::*;

pub struct ChildrenIter<C> {
    pub child_storage: C,
    pub cursor: (EntityId, usize),
}

impl<'a, C> Iterator for ChildrenIter<C>
where
    C: Get<Out = &'a Child> + Copy,
{
    type Item = EntityId;

    fn next(&mut self) -> Option<Self::Item> {
        let (entity, num_children) = &mut self.cursor;
        if *num_children > 0 {
            *num_children -= 1;
            let ret = *entity;
            self.cursor.0 = self.child_storage.try_get(ret).unwrap().next;
            Some(ret)
        } else {
            None
        }
    }
}

pub struct AncestorIter<C> {
    pub child_storage: C,
    pub cursor: EntityId,
}

impl<'a, C> Iterator for AncestorIter<C>
where
    C: Get<Out = &'a Child> + Copy,
{
    type Item = EntityId;

    fn next(&mut self) -> Option<Self::Item> {
        self.child_storage.try_get(self.cursor).ok().map(|child| {
            self.cursor = child.parent;
            child.parent
        })
    }
}

pub struct DescendantsDepthFirstIter<P, C> {
    pub parent_storage: P,
    pub child_storage: C,
    pub cursors: Vec<(EntityId, usize)>,
}

impl<'a, P, C> Iterator for DescendantsDepthFirstIter<P, C>
where
    P: Get<Out = &'a Parent> + Copy,
    C: Get<Out = &'a Child> + Copy,
{
    type Item = EntityId;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(cursor) = self.cursors.last_mut() {
            let (entity, num_children) = cursor;
            if *num_children > 0 {
                *num_children -= 1;

                let ret = *entity;

                *entity = self.child_storage.try_get(ret).unwrap().next;

                if let Ok(parent) = self.parent_storage.try_get(ret) {
                    self.cursors.push((parent.first_child, parent.num_children));
                }
                Some(ret)
            } else {
                self.cursors.pop();
                self.next()
            }
        } else {
            None
        }
    }
}

pub struct DescendantsBreadthFirstIter<P, C> {
    pub parent_storage: P,
    pub child_storage: C,
    pub cursors: VecDeque<(EntityId, usize)>,
}

impl<'a, P, C> Iterator for DescendantsBreadthFirstIter<P, C>
where
    P: Get<Out = &'a Parent> + Copy,
    C: Get<Out = &'a Child> + Copy,
{
    type Item = EntityId;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(cursor) = self.cursors.front_mut() {
            let (entity, num_children) = cursor;
            if *num_children > 0 {
                *num_children -= 1;

                let ret = *entity;

                *entity = self.child_storage.try_get(ret).unwrap().next;

                if let Ok(parent) = self.parent_storage.try_get(ret) {
                    self.cursors.push_back((parent.first_child, parent.num_children));
                }
                Some(ret)
            } else {
                self.cursors.pop_front();
                self.next()
            }
        } else {
            None
        }
    }
}

pub trait HierarchyIter<'a, P, C> 
{
    fn ancestors(&self, id: EntityId) -> AncestorIter<C>;
    fn children(&self, id: EntityId) -> ChildrenIter<C>;
    fn descendants_depth_first(&self, id: EntityId) -> DescendantsDepthFirstIter<P, C>;
    fn descendants_breadth_first(&self, id: EntityId) -> DescendantsBreadthFirstIter<P, C>;
}

impl<'a, P, C> HierarchyIter<'a, P, C> for (P, C)
where
    P: Get<Out = &'a Parent> + Copy,
    C: Get<Out = &'a Child> + Copy,
    //<P as IntoIter>::IntoIter: Shiperator + CurrentId<Id = EntityId>,
    //<C as IntoIter>::IntoIter: Shiperator + CurrentId<Id = EntityId>,
{
    fn ancestors(&self, id: EntityId) -> AncestorIter<C> {
        let (_, child_storage) = *self;
        AncestorIter {
            child_storage,
            cursor: id,
        }
    }

    fn children(&self, id: EntityId) -> ChildrenIter<C> {
        let (parent_storage, child_storage) = *self;
        ChildrenIter {
            child_storage,
            cursor: parent_storage
                .try_get(id)
                .map_or((id, 0), |parent| (parent.first_child, parent.num_children)),
        }
    }

    fn descendants_depth_first(&self, id: EntityId) -> DescendantsDepthFirstIter<P, C> {
        let (parent_storage, child_storage) = *self;
        DescendantsDepthFirstIter {
            parent_storage,
            child_storage,
            cursors: parent_storage.try_get(id).map_or_else(|_| Vec::new(), |parent| {
                vec![(parent.first_child, parent.num_children)]
            }),
        }
    }
    fn descendants_breadth_first(&self, id: EntityId) -> DescendantsBreadthFirstIter<P, C> {
        let (parent_storage, child_storage) = *self;
        DescendantsBreadthFirstIter {
            parent_storage,
            child_storage,
            cursors: parent_storage
                .try_get(id)
                .map_or_else(|_| VecDeque::new(), |parent| {
                    let mut queue = VecDeque::new();
                    queue.push_front((parent.first_child, parent.num_children));
                    queue
                }),
        }
    }
}