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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
//! Walk on `Tree`/`Node` or `Forest`. use super::{Tree,Forest,Node}; use rust::{Vec,null}; /// Distinguish between visiting a leaf node and (begin/end of) visiting a branched node. #[derive( Copy, Clone, Debug, Eq, PartialEq )] pub enum Visit<'a, T:'a> { Begin( &'a Node<T> ), End ( &'a Node<T> ), Leaf ( &'a Node<T> ), } impl<'a, T:'a> Visit<'a,T> { /// Returns the node under visit, regardless of whether it is a leaf node or (begin/end of) visiting a branched node. #[inline] pub fn node( &self ) -> &Node<T> { match *self { Visit::Begin( node ) => node, Visit::End ( node ) => node, Visit::Leaf ( node ) => node, } } } /// Mapping to Option<Visit> enum VisitType { None, Begin, End, Leaf } /// Cursor on `Node` and its siblings. struct Nodes<T> { node : *const Node<T>, sentinel : *const Node<T>, } impl<T> Nodes<T> { /// Only the given node will be visited. #[inline] fn this( node: *const Node<T> ) -> Self { Nodes{ node, sentinel: unsafe{ (*node).next as *const Node<T> }}} /// The given node and all its siblings will be visited. #[inline] fn sibs( node: *const Node<T> ) -> Self { Nodes{ node, sentinel: node }} } /// Control of the `Walk`'s stack. enum Direction { Up, // Current node and all its siblings and all their descendents have been visited, so go back to their parent. Down, // Try to visit the first child of the current node. Right, // Try to visit the next sibling of the current node. } /// Walk on `Node`. struct Walk<T> { path : Vec<Nodes<T>>, // stack for keep the current node and all its ancestors. direction : Direction, visit_type : VisitType, // maps to Option<Visit>, needed by get(). origin : *const Node<T>, // for rewind. } impl<T> Walk<T> { #[inline] fn reset( &mut self ) { self.path.clear(); self.direction = Direction::Down; self.visit_type = VisitType::None; } #[inline] fn init_visit( &mut self ) { self.visit_type = if let Some( nodes ) = self.path.last() { unsafe { if (*nodes.node).is_leaf() { VisitType::Leaf } else { VisitType::Begin } } } else { VisitType::None }; } #[inline] fn on_node( &mut self, node: *const Node<T> ) { self.reset(); self.path.push( Nodes::this( node )); self.init_visit(); self.origin = node; } #[inline] fn on_forest( &mut self, head: *const Node<T> ) { self.reset(); self.path.push( Nodes::sibs( head )); self.init_visit(); self.origin = head; } #[inline] fn revisit( &mut self ) { if !self.origin.is_null() { match self.visit_type { VisitType::None => self.path.push( Nodes::sibs( self.origin )), _ => (), } self.direction = Direction::Down; self.init_visit(); } } /// Returns the current node in the traversal, or `None` if the traversal is completed. #[inline] fn get( &self ) -> Option<Visit<T>> { if let Some( nodes ) = self.path.last() { unsafe { match self.visit_type { VisitType::Begin => Some( Visit::Begin( &*nodes.node )), VisitType::End => Some( Visit::End ( &*nodes.node )), VisitType::Leaf => Some( Visit::Leaf ( &*nodes.node )), VisitType::None => None, }} } else { None } } /// Advance the cursor in the traversal. #[inline] fn forward( &mut self ) { loop { match self.direction { Direction::Up => { self.path.pop(); if self.path.last().is_some() { self.direction = Direction::Right; self.visit_type = VisitType::End; } else { self.direction = Direction::Down; self.visit_type = VisitType::None; } break; }, Direction::Down => { let new_nodes; if let Some( nodes ) = self.path.last_mut() { let node = unsafe{ &*nodes.node }; if node.is_leaf() { self.direction = Direction::Right; continue; } else { let head = unsafe{ node.head() }; new_nodes = Some( Nodes::sibs( head as *const Node<T> )); self.visit_type = if unsafe{ (*head).is_leaf() } { VisitType::Leaf } else { VisitType::Begin }; } } else { break; } new_nodes.map( |nodes| self.path.push( nodes )); break; } Direction::Right => { if let Some( nodes ) = self.path.last_mut() { nodes.node = unsafe{ (*nodes.node).next as *const Node<T> }; if nodes.node == nodes.sentinel { self.direction = Direction::Up; continue; } else { if unsafe{ (*nodes.node).is_leaf() } { self.visit_type = VisitType::Leaf; } else { self.visit_type = VisitType::Begin; self.direction = Direction::Down; } break; } } } } } } /// Advance the cursor and return the newly visited node. /// /// NOTICE: the FIRST node in the traversal can NOT be accessed via next() call. #[inline] fn next( &mut self ) -> Option<Visit<T>> { self.forward(); self.get() } /// Set the cursor to the current node's parent and returns it, or `None` if it has no parent. #[inline] fn to_parent( &mut self ) -> Option<Visit<T>> { if self.path.last().is_some() { self.path.pop(); if self.path.last().is_some() { self.direction = Direction::Right; self.visit_type = VisitType::End; return self.get(); } } self.direction = Direction::Down; self.visit_type = VisitType::None; None } /// Returns the parent of current node, or `None` if it has no parent. #[inline] fn get_parent( &self ) -> Option<&Node<T>> { if self.path.len() >= 2 { self.path.get( self.path.len()-2 ).map( |parent| unsafe{ &*parent.node }) } else { None } } /// Set the cursor to the current node's next `n`-th sibling and returns it, or `None` if such sibling does not exist. /// Returns the current node if n == 0. #[inline] fn to_sib( &mut self, n: usize ) -> Option<Visit<T>> { if let Some( nodes ) = self.path.last_mut() { for _ in 0..n { nodes.node = unsafe{ (*nodes.node).next as *const Node<T> }; if nodes.node == nodes.sentinel { self.direction = Direction::Up; return None; } } if unsafe{ (*nodes.node).is_leaf() } { self.visit_type = VisitType::Leaf; } else { self.visit_type = VisitType::Begin; self.direction = Direction::Down; } } else { return None; } return self.get(); } /// Set the cursor to the current node's `n`-th child and returns it, or `None` if it has no child. /// Notice that `n == 0` indicating the first child. #[inline] fn to_child( &mut self, n: usize ) -> Option<Visit<T>> { let new_nodes; if let Some( nodes ) = self.path.last_mut() { let node = unsafe{ &*nodes.node }; if node.is_leaf() { self.direction = Direction::Right; return None; } else { let head = unsafe{ node.head() }; new_nodes = Some( Nodes::sibs( head as *const Node<T> )); self.visit_type = if unsafe{ (*head).is_leaf() } { VisitType::Leaf } else { VisitType::Begin }; } } else { return None; } new_nodes.map( |nodes| self.path.push( nodes )); self.to_sib( n ) } } impl<T> Default for Walk<T> { #[inline] fn default() -> Self { Walk{ path: Vec::default(), direction: Direction::Down, visit_type: VisitType::None, origin: null() } } } /// Tree traversal pub struct TreeWalk<T> { tree : Tree<T>, walk : Walk<T>, } impl<T> TreeWalk<T> { /// Returns the current node in the tree traversal, or `None` if the traversal is completed. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,TreeWalk}; /// let tree = tr(0) / tr(1)/tr(2)/tr(3); /// let walk = TreeWalk::from( tree ); /// assert_eq!( walk.get(), Some( Visit::Begin( ( tr(0)/tr(1)/tr(2)/tr(3) ).root() ))); /// ``` #[inline] pub fn get( &self ) -> Option<Visit<T>> { self.walk.get() } /// Depth first search on `TreeWalk`. /// Preorder or postorder at will. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,TreeWalk}; /// let tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ); /// let mut walk = TreeWalk::from( tree ); /// assert_eq!( walk.get(), Some( Visit::Begin( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(2).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(3).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( (tr(1)/tr(2)/tr(3)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(4)/tr(5)/tr(6)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(5).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(6).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( (tr(4)/tr(5)/tr(6)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), None ); /// walk.forward(); /// assert_eq!( walk.get(), None ); /// ``` #[inline] pub fn forward( &mut self ) { self.walk.forward(); } /// Advance the cursor and return the newly visited node. /// /// NOTICE: the FIRST node in the traversal can NOT be accessed via next() call. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,TreeWalk}; /// let tree = tr(0) / tr(1)/tr(2)/tr(3); /// let mut walk = TreeWalk::from( tree ); /// assert_eq!( walk.next(), Some( Visit::Leaf( tr(1).root() ))); /// assert_eq!( walk.next(), Some( Visit::Leaf( tr(2).root() ))); /// assert_eq!( walk.next(), Some( Visit::Leaf( tr(3).root() ))); /// assert_eq!( walk.next(), Some( Visit::End( ( tr(0)/tr(1)/tr(2)/tr(3) ).root() ))); /// assert_eq!( walk.next(), None ); /// assert_eq!( walk.next(), None ); /// ``` #[inline] pub fn next( &mut self ) -> Option<Visit<T>> { self.walk.next() } /// Set the cursor to the current node's parent and returns it, or `None` if it has no parent. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,TreeWalk}; /// let tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ); /// let mut walk = TreeWalk::from( tree ); /// assert_eq!( walk.get(), Some( Visit::Begin( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// assert_eq!( walk.to_parent(), Some( Visit::End( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() ))); /// ``` #[inline] pub fn to_parent( &mut self ) -> Option<Visit<T>> { self.walk.to_parent() } /// Returns the parent of current node, or `None` if it has no parent. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,TreeWalk}; /// let tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ); /// let mut walk = TreeWalk::from( tree ); /// assert_eq!( walk.get(), Some( Visit::Begin( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() ))); /// assert_eq!( walk.get_parent(), None ); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// assert_eq!( walk.get_parent(), Some( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() )); /// ``` #[inline] pub fn get_parent( &self ) -> Option<&Node<T>> { self.walk.get_parent() } /// Set the cursor to the current node's `n`-th child and returns it, or `None` if it has no child. /// Notice that `n == 0` indicating the first child. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,TreeWalk}; /// let tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ); /// let mut walk = TreeWalk::from( tree ); /// assert_eq!( walk.get(), Some( Visit::Begin( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() ))); /// walk.to_child( 1 ); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(4)/tr(5)/tr(6)).root() ))); /// ``` #[inline] pub fn to_child( &mut self, n: usize ) -> Option<Visit<T>> { self.walk.to_child(n) } /// Set the cursor to the current node's next `n`-th sibling and returns it, or `None` if such sibling does not exist. /// Returns the current node if n == 0. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,TreeWalk}; /// let tree = tr(0) / tr(1)/tr(2)/tr(3); /// let mut walk = TreeWalk::from( tree ); /// assert_eq!( walk.next(), Some( Visit::Leaf( tr(1).root() ))); /// assert_eq!( walk.to_sib( 0 ), Some( Visit::Leaf( tr(1).root() ))); /// assert_eq!( walk.to_sib( 2 ), Some( Visit::Leaf( tr(3).root() ))); /// ``` #[inline] pub fn to_sib( &mut self, n: usize ) -> Option<Visit<T>> { self.walk.to_sib(n) } /// Revisit a `Node` that reached `Visit::End`. /// No effect on `Visit::Begin` or `Visit::Leaf`. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,TreeWalk}; /// let tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ); /// let mut walk = TreeWalk::from( tree ); /// for _ in 0..3 { /// for _ in 0..3 { /// walk.revisit(); /// assert_eq!( walk.get(), Some( Visit::Begin( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() ))); /// walk.forward(); /// for _ in 0..3 { /// walk.revisit(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(2).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(3).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( (tr(1)/tr(2)/tr(3)).root() ))); /// } /// walk.forward(); /// for _ in 0..3 { /// walk.revisit(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(4)/tr(5)/tr(6)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(5).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(6).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( (tr(4)/tr(5)/tr(6)).root() ))); /// } /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( ( tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) ) ).root() ))); /// } /// walk.forward(); /// assert_eq!( walk.get(), None ); /// walk.forward(); /// assert_eq!( walk.get(), None ); /// } /// ``` #[inline] pub fn revisit( &mut self ) { self.walk.revisit(); } } impl<T> From<Tree<T>> for TreeWalk<T> { fn from( tree: Tree<T> ) -> Self { let mut walk = Walk::<T>::default(); walk.on_node( tree.root ); TreeWalk{ tree, walk } } } impl<T> Into<Tree<T>> for TreeWalk<T> { fn into( self ) -> Tree<T> { self.tree }} /// Forest traversal #[derive( Default )] pub struct ForestWalk<T> { forest : Forest<T>, walk : Walk<T>, } unsafe impl<T:Send> Send for TreeWalk<T> {} unsafe impl<T:Sync> Sync for TreeWalk<T> {} impl<T> ForestWalk<T> { /// Returns the current node in the forest traversal, or `None` if the traversal is completed. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,ForestWalk}; /// let forest = -tr(1)-tr(2)-tr(3); /// let walk = ForestWalk::from( forest ); /// assert_eq!( walk.get(), Some( Visit::Leaf( tr(1).root() ))); /// ``` #[inline] pub fn get( &self ) -> Option<Visit<T>> { self.walk.get() } /// Depth first search on `ForestWalk`. /// Preorder or postorder at will. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,ForestWalk}; /// let forest = - ( tr(1)/tr(2)/tr(3) ) - ( tr(4)/tr(5)/tr(6) ); /// let mut walk = ForestWalk::from( forest ); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(2).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(3).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( (tr(1)/tr(2)/tr(3)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(4)/tr(5)/tr(6)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(5).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(6).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( (tr(4)/tr(5)/tr(6)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), None ); /// walk.forward(); /// assert_eq!( walk.get(), None ); /// walk.forward(); /// ``` #[inline] pub fn forward( &mut self ) { self.walk.forward(); } /// Advance the cursor and return the newly visited node. /// /// NOTICE: the FIRST node in the traversal can NOT be accessed via next() call. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,ForestWalk}; /// let forest = -tr(1)-tr(2)-tr(3); /// let mut walk = ForestWalk::from( forest ); /// assert_eq!( walk.next(), Some( Visit::Leaf( tr(2).root() ))); /// assert_eq!( walk.next(), Some( Visit::Leaf( tr(3).root() ))); /// assert_eq!( walk.next(), None ); /// assert_eq!( walk.next(), None ); /// ``` #[inline] pub fn next( &mut self ) -> Option<Visit<T>> { self.walk.next() } /// Set the cursor to the current node's parent and returns it, or `None` if it has no parent. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,ForestWalk}; /// let forest = - ( tr(1)/tr(2)/tr(3) ) - ( tr(4)/tr(5)/tr(6) ); /// let mut walk = ForestWalk::from( forest ); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(2).root() ))); /// assert_eq!( walk.to_parent(), Some( Visit::End( (tr(1)/tr(2)/tr(3)).root() ))); /// ``` #[inline] pub fn to_parent( &mut self ) -> Option<Visit<T>> { self.walk.to_parent() } /// Returns the parent of current node, or `None` if it has no parent. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,ForestWalk}; /// let forest = - ( tr(1)/tr(2)/tr(3) ) - ( tr(4)/tr(5)/tr(6) ); /// let mut walk = ForestWalk::from( forest ); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// assert_eq!( walk.get_parent(), None ); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(2).root() ))); /// assert_eq!( walk.get_parent(), Some( (tr(1)/tr(2)/tr(3)).root() )); /// ``` #[inline] pub fn get_parent( &self ) -> Option<&Node<T>> { self.walk.get_parent() } /// Set the cursor to the current node's `n`-th child and returns it, or `None` if it has no child. /// Notice that `n == 0` indicating the first child. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,ForestWalk}; /// let forest = - ( tr(1)/tr(2)/tr(3) ) - ( tr(4)/tr(5)/tr(6) ); /// let mut walk = ForestWalk::from( forest ); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// walk.to_child( 1 ); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(3).root() ))); /// ``` #[inline] pub fn to_child( &mut self, n: usize ) -> Option<Visit<T>> { self.walk.to_child(n) } /// Set the cursor to the current node's next `n`-th sibling and returns it, or `None` if such sibling does not exist. /// Returns the current node if n == 0. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,ForestWalk}; /// let forest = -tr(1)-tr(2)-tr(3); /// let mut walk = ForestWalk::from( forest ); /// assert_eq!( walk.get(), Some( Visit::Leaf( tr(1).root() ))); /// assert_eq!( walk.to_sib( 0 ), Some( Visit::Leaf( tr(1).root() ))); /// assert_eq!( walk.to_sib( 2 ), Some( Visit::Leaf( tr(3).root() ))); /// ``` #[inline] pub fn to_sib( &mut self, n: usize ) -> Option<Visit<T>> { self.walk.to_sib(n) } /// Revisit a `Node` that reached `Visit::End`. /// No effect on `Visit::Begin` or `Visit::Leaf`. /// /// # Examples /// /// ``` /// use trees::linked::singly::{tr,Visit,ForestWalk}; /// let forest = - ( tr(1)/tr(2)/tr(3) ) - ( tr(4)/tr(5)/tr(6) ); /// let mut walk = ForestWalk::from( forest ); /// for _ in 0..3 { /// walk.revisit(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// for _ in 0..3 { /// walk.revisit(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(1)/tr(2)/tr(3)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(2).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(3).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( (tr(1)/tr(2)/tr(3)).root() ))); /// } /// walk.forward(); /// for _ in 0..3 { /// walk.revisit(); /// assert_eq!( walk.get(), Some( Visit::Begin( (tr(4)/tr(5)/tr(6)).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(5).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::Leaf ( tr(6).root() ))); /// walk.forward(); /// assert_eq!( walk.get(), Some( Visit::End ( (tr(4)/tr(5)/tr(6)).root() ))); /// } /// walk.forward(); /// } /// ``` #[inline] pub fn revisit( &mut self ) { self.walk.revisit(); } } impl<T> From<Forest<T>> for ForestWalk<T> { fn from( forest: Forest<T> ) -> Self { let mut walk = Walk::<T>::default(); if !forest.is_empty() { walk.on_forest( unsafe{ forest.head() as *const Node<T> }); } ForestWalk{ forest, walk } } } impl<T> Into<Forest<T>> for ForestWalk<T> { fn into( self ) -> Forest<T> { self.forest }} unsafe impl<T:Send> Send for ForestWalk<T> {} unsafe impl<T:Sync> Sync for ForestWalk<T> {}