1use std::{
2 hash::Hash,
3 ops::{Deref, DerefMut},
4};
5
6use crate::{
7 path::Path,
8 prelude::iter::depth::{Iter, TreeIterTarget},
9 tree::{
10 position::{AttachedPosition, DetachedPosition},
11 Position,
12 },
13};
14
15use super::{detached::DetachedEntry, Entry, NodeIDX, Tree, NODEBOUND, TREEBOUND};
16
17pub type TreeNode<R, N, B> = Node<R, N, B, NODEBOUND>;
18
19pub struct Node<R, N, B, const BOUND: bool>
20where
21 R: Deref<Target = Tree<N, B>>,
22{
23 pub(super) tree: R,
24 pub(super) position: AttachedPosition<B, NodeIDX>,
25}
26
27impl<R, N, B, const BOUND: bool> Deref for Node<R, N, B, BOUND>
28where
29 R: Deref<Target = Tree<N, B>>,
30{
31 type Target = N;
32
33 fn deref(&self) -> &Self::Target {
34 self.value()
35 }
36}
37
38impl<R, N, B, const BOUND: bool> DerefMut for Node<R, N, B, BOUND>
39where
40 R: DerefMut<Target = Tree<N, B>>,
41{
42 fn deref_mut(&mut self) -> &mut Self::Target {
43 self.value_mut()
44 }
45}
46
47impl<R, N, B, const BOUND: bool> From<(R, Path<B>, Path<NodeIDX>)> for Node<R, N, B, BOUND>
48where
49 R: Deref<Target = Tree<N, B>>,
50{
51 fn from((tree, path, idxs): (R, Path<B>, Path<NodeIDX>)) -> Self {
52 Self {
53 tree,
54 position: AttachedPosition::from(path, idxs),
55 }
56 }
57}
58
59impl<R, N, B, const BOUND: bool> Node<R, N, B, BOUND>
60where
61 R: Deref<Target = Tree<N, B>>,
62{
63 pub fn from(tree: R, position: AttachedPosition<B, NodeIDX>) -> Self {
64 Self { tree, position }
65 }
66
67 pub(super) fn into_detached_entry(
68 self,
69 position: DetachedPosition<B, NodeIDX>,
70 ) -> DetachedEntry<R, N, B, BOUND> {
71 DetachedEntry {
72 tree: self.tree,
73 position,
74 }
75 }
76
77 pub fn into_entry(self) -> Entry<R, N, B, BOUND> {
78 self.into()
79 }
80
81 pub fn branch(&self) -> Option<&B> {
82 self.position.at_branch()
83 }
84
85 pub fn children(&self) -> Vec<&B> {
86 self.tree.nodes[*self.position.at()]
87 .children
88 .keys()
89 .collect() }
91
92 pub fn path(&self) -> &Path<B> {
93 self.position.path()
94 }
95
96 pub fn value(&self) -> &N {
97 &self.tree.nodes[*self.position.at()].value
98 }
99}
100
101impl<R, N, B, const BOUND: bool> Node<R, N, B, BOUND>
102where
103 R: DerefMut<Target = Tree<N, B>>,
104{
105 pub fn insert(&mut self, value: N) -> N {
106 std::mem::replace(&mut self.tree.nodes[*self.position.at()].value, value)
107 }
108
109 pub fn value_mut(&mut self) -> &mut N {
110 &mut self.tree.nodes[*self.position.at()].value
111 }
112}
113
114impl<'a, N, B> Node<&'a Tree<N, B>, N, B, TREEBOUND> {
115 pub fn iter<T: TreeIterTarget<'a, N, B>>(&self) -> Iter<'a, N, B, T> {
116 Iter::new(self.tree)
117 }
118}
119impl<R, N, B> Node<R, N, B, TREEBOUND>
122where
123 R: Deref<Target = Tree<N, B>>,
124{
125 pub fn move_up(&mut self, up: usize) -> Option<usize> {
126 self.position.move_up(up)
127 }
128}
129
130impl<R, N, B> Node<R, N, B, TREEBOUND>
131where
132 R: Deref<Target = Tree<N, B>>,
133 B: Eq + Hash,
134{
135 pub fn move_down(mut self, mut path: Path<B>) -> Entry<R, N, B, TREEBOUND> {
136 let mut position = Position::Attached(self.position);
137 let mut p_idx = *position.attached_mut().unwrap().at();
138 while position.is_attached() {
139 if path.is_empty() {
140 break;
141 }
142 let branch = path.pop_first().unwrap();
143 match self.tree.nodes[p_idx].children.get(&branch).copied() {
144 Some(idx) => {
145 position.attached_mut().unwrap().move_down(branch, idx);
146 p_idx = idx;
147 }
148 None => {
149 let detached = match position {
150 Position::Attached(attached) => attached.move_down_detach(branch),
151 Position::Detached(detached) => detached,
152 };
153 position = Position::Detached(detached);
154 }
155 }
156 }
157 if let Position::Detached(mut detached_position) = position {
158 for branch in path {
159 detached_position.move_down(branch);
160 }
161 DetachedEntry::from(self.tree, detached_position).into()
162 } else {
163 self.position = position.unwrap_attached();
164 self.into()
165 }
166 }
167
168 pub fn move_down_branch(mut self, branch: B) -> Entry<R, N, B, TREEBOUND> {
169 if let Some(idx) = self.tree.nodes[*self.position.at()]
170 .children
171 .get(&branch)
172 .copied()
173 {
174 self.position.move_down(branch, idx);
175 self.into()
176 } else {
177 DetachedEntry::from(self.tree, self.position.move_down_detach(branch)).into()
178 }
179 }
180}
181impl<R, N, B> Node<R, N, B, TREEBOUND>
182where
183 R: DerefMut<Target = Tree<N, B>>,
184 B: Eq + Hash + Clone,
185{
186 pub fn remove_subtree(mut self) -> DetachedEntry<R, N, B, TREEBOUND> {
187 if let Some((_, &p_idx)) = self.position.parent() {
188 self.tree
189 .remove_subtree_at(p_idx, self.position.at_branch().unwrap().clone());
190 } else {
191 self.tree.clear();
193 }
194 DetachedEntry::from(self.tree, self.position.remove_node())
195 }
196
197 pub fn remove_child_subtrees(&mut self, children: Vec<B>) {
198 for c in children {
199 self.tree.remove_subtree_at(*self.position.at(), c);
200 }
201 }
202}
203
204