| @@ -1,14 +1,14 @@ |
| 1 | use crate::pack::tree::{Item, Tree}; | 1 | use crate::pack::tree::{Item, Tree}; |
| 2 | | 2 | |
| 3 | /// All the unsafe bits to support parallel iteration with write access | 3 | /// All the unsafe bits to support parallel iteration with write access |
| 4 | impl<D> Tree<D> { | 4 | impl<T> Tree<T> { |
| 5 | #[allow(unsafe_code)] | 5 | #[allow(unsafe_code)] |
| 6 | /// SAFETY: Called from node with is guaranteed to not be aliasing with any other node. | 6 | /// SAFETY: Called from node with is guaranteed to not be aliasing with any other node. |
| 7 | /// Note that we are iterating nodes that we are affecting here, but that only affects the | 7 | /// Note that we are iterating nodes that we are affecting here, but that only affects the |
| 8 | /// 'is_root' field fo the item, not the data field that we are writing here. | 8 | /// 'is_root' field fo the item, not the data field that we are writing here. |
| 9 | /// For all details see `from_node_take_entry()`. | 9 | /// For all details see `from_node_take_entry()`. |
| 10 | unsafe fn from_node_put_data(&self, index: usize, data: D) { | 10 | unsafe fn from_node_put_data(&self, index: usize, data: T) { |
| 11 | let items_mut: &mut Vec<Item<D>> = &mut *(self.items.get()); | 11 | let items_mut: &mut Vec<Item<T>> = &mut *(self.items.get()); |
| 12 | items_mut.get_unchecked_mut(index).data = data; | 12 | items_mut.get_unchecked_mut(index).data = data; |
| 13 | } | 13 | } |
| 14 | | 14 | |
| @@ -22,11 +22,11 @@ impl<D> Tree<D> { · 7 unmodified lines |
| 22 | /// alias multiple nodes in the tree. | 22 | /// alias multiple nodes in the tree. |
| 23 | /// It's safe for multiple threads to hold different chunks, as they are guaranteed to be non-overlapping and unique. | 23 | /// It's safe for multiple threads to hold different chunks, as they are guaranteed to be non-overlapping and unique. |
| 24 | /// If the tree is accessed after iteration, it will panic as no mutation is allowed anymore, nor is | 24 | /// If the tree is accessed after iteration, it will panic as no mutation is allowed anymore, nor is |
| 25 | unsafe fn from_node_take_entry(&self, index: usize) -> (D, Vec<usize>) | 25 | unsafe fn from_node_take_entry(&self, index: usize) -> (T, Vec<usize>) |
| 26 | where | 26 | where |
| 27 | D: Default, | 27 | T: Default, |
| 28 | { | 28 | { |
| 29 | let items_mut: &mut Vec<Item<D>> = &mut *(self.items.get()); | 29 | let items_mut: &mut Vec<Item<T>> = &mut *(self.items.get()); |
| 30 | let item = items_mut.get_unchecked_mut(index); | 30 | let item = items_mut.get_unchecked_mut(index); |
| 31 | let children = std::mem::take(&mut item.children); | 31 | let children = std::mem::take(&mut item.children); |
| 32 | let data = std::mem::take(&mut item.data); | 32 | let data = std::mem::take(&mut item.data); |
| @@ -35,11 +35,11 @@ impl<D> Tree<D> { · 2 unmodified lines |
| 35 | | 35 | |
| 36 | #[allow(unsafe_code)] | 36 | #[allow(unsafe_code)] |
| 37 | /// SAFETY: As `take_entry(…)` - but this one only takes if the data of Node is a root | 37 | /// SAFETY: As `take_entry(…)` - but this one only takes if the data of Node is a root |
| 38 | unsafe fn from_iter_take_entry_if_root(&self, index: usize) -> Option<(D, Vec<usize>)> | 38 | unsafe fn from_iter_take_entry_if_root(&self, index: usize) -> Option<(T, Vec<usize>)> |
| 39 | where | 39 | where |
| 40 | D: Default, | 40 | T: Default, |
| 41 | { | 41 | { |
| 42 | let items_mut: &mut Vec<Item<D>> = &mut *(self.items.get()); | 42 | let items_mut: &mut Vec<Item<T>> = &mut *(self.items.get()); |
| 43 | let item = items_mut.get_unchecked_mut(index); | 43 | let item = items_mut.get_unchecked_mut(index); |
| 44 | if item.is_root { | 44 | if item.is_root { |
| 45 | let children = std::mem::take(&mut item.children); | 45 | let children = std::mem::take(&mut item.children); |
| @@ -52,9 +52,9 @@ impl<D> Tree<D> { · 6 unmodified lines |
| 52 | } | 52 | } |
| 53 | | 53 | |
| 54 | /// Iteration | 54 | /// Iteration |
| 55 | impl<D> Tree<D> { | 55 | impl<T> Tree<T> { |
| 56 | /// Return an iterator over chunks of roots. Roots are not children themselves, they have no parents. | 56 | /// Return an iterator over chunks of roots. Roots are not children themselves, they have no parents. |
| 57 | pub fn iter_root_chunks(&mut self, size: usize) -> Chunks<D> { | 57 | pub fn iter_root_chunks(&mut self, size: usize) -> Chunks<T> { |
| 58 | Chunks { | 58 | Chunks { |
| 59 | tree: self, | 59 | tree: self, |
| 60 | size, | 60 | size, |
| @@ -63,18 +63,18 @@ impl<D> Tree<D> { · 2 unmodified lines |
| 63 | } | 63 | } |
| 64 | } | 64 | } |
| 65 | | 65 | |
| 66 | pub struct Node<'a, D> { | 66 | pub struct Node<'a, T> { |
| 67 | tree: &'a Tree<D>, | 67 | tree: &'a Tree<T>, |
| 68 | index: usize, | 68 | index: usize, |
| 69 | children: Vec<usize>, | 69 | children: Vec<usize>, |
| 70 | pub data: D, | 70 | pub data: T, |
| 71 | } | 71 | } |
| 72 | | 72 | |
| 73 | impl<'a, D> Node<'a, D> | 73 | impl<'a, T> Node<'a, T> |
| 74 | where | 74 | where |
| 75 | D: Default, | 75 | T: Default, |
| 76 | { | 76 | { |
| 77 | pub fn store_changes_then_into_child_iter(self) -> impl Iterator<Item = Node<'a, D>> { | 77 | pub fn store_changes_then_into_child_iter(self) -> impl Iterator<Item = Node<'a, T>> { |
| 78 | #[allow(unsafe_code)] | 78 | #[allow(unsafe_code)] |
| 79 | // SAFETY: The index is valid as it was controlled by `add_child(…)`, then see `take_entry(…)` | 79 | // SAFETY: The index is valid as it was controlled by `add_child(…)`, then see `take_entry(…)` |
| 80 | unsafe { | 80 | unsafe { |
| @@ -95,17 +95,17 @@ where · 14 unmodified lines |
| 95 | } | 95 | } |
| 96 | } | 96 | } |
| 97 | | 97 | |
| 98 | pub struct Chunks<'a, D> { | 98 | pub struct Chunks<'a, T> { |
| 99 | tree: &'a Tree<D>, | 99 | tree: &'a Tree<T>, |
| 100 | size: usize, | 100 | size: usize, |
| 101 | cursor: usize, | 101 | cursor: usize, |
| 102 | } | 102 | } |
| 103 | | 103 | |
| 104 | impl<'a, D> Iterator for Chunks<'a, D> | 104 | impl<'a, T> Iterator for Chunks<'a, T> |
| 105 | where | 105 | where |
| 106 | D: Default, | 106 | T: Default, |
| 107 | { | 107 | { |
| 108 | type Item = Vec<Node<'a, D>>; | 108 | type Item = Vec<Node<'a, T>>; |
| 109 | | 109 | |
| 110 | fn next(&mut self) -> Option<Self::Item> { | 110 | fn next(&mut self) -> Option<Self::Item> { |
| 111 | if self.cursor == self.tree.one_past_last_seen_root { | 111 | if self.cursor == self.tree.one_past_last_seen_root { |