1- use std:: fmt:: Debug ;
2- use std:: hash:: Hash ;
1+ use oxc:: allocator:: Allocator ;
32
4- pub trait Idx : Debug + Clone + Copy + PartialEq + Eq + Hash {
5- fn new ( depth : usize , parent : usize ) -> Self ;
6- fn depth ( self ) -> usize ;
7- fn parent ( self ) -> usize ;
3+ use crate :: utils:: box_bump:: { BoxBump , Idx } ;
4+
5+ pub struct StackedTreeId < I : Idx > {
6+ depth : usize ,
7+ parent : Option < I > ,
88}
99
1010#[ macro_export]
1111macro_rules! define_stacked_tree_idx {
1212 ( $v: vis struct $type: ident; ) => {
13- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
14- $v struct $type {
15- depth: u32 ,
16- parent: u32 ,
17- }
18-
19- impl $crate:: scope:: stacked_tree:: Idx for $type {
20- #[ inline( always) ]
21- fn new( depth: usize , parent: usize ) -> Self {
22- Self { depth: depth as u32 , parent: parent as u32 }
23- }
24- #[ inline( always) ]
25- fn depth( self ) -> usize {
26- self . depth as usize
27- }
28- #[ inline( always) ]
29- fn parent( self ) -> usize {
30- self . parent as usize
31- }
13+ $crate:: define_box_bump_idx! {
14+ $v struct $type for $crate:: scope:: stacked_tree:: StackedTreeId <$type>;
3215 }
3316 } ;
3417}
3518
3619struct StackedTreeItem < I : Idx , T > {
3720 id : I ,
38- id_idx : usize ,
3921 data : T ,
4022}
4123
42- pub struct StackedTree < I : Idx , T > {
43- ids : Vec < I > ,
24+ pub struct StackedTree < ' a , I : Idx , T > {
25+ ids : BoxBump < ' a , I , StackedTreeId < I > > ,
4426 stack : Vec < StackedTreeItem < I , T > > ,
4527 pub root : I ,
4628}
4729
48- impl < I : Idx , T > StackedTree < I , T > {
49- pub fn new ( root_data : T ) -> Self {
50- let root_id = I :: new ( 0 , 0 ) ;
51- let root_item = StackedTreeItem { id : root_id, id_idx : 0 , data : root_data } ;
52- StackedTree { ids : vec ! [ root_id] , stack : vec ! [ root_item] , root : root_id }
30+ impl < ' a , I : Idx , T > StackedTree < ' a , I , T > {
31+ pub fn new ( allocator : & ' a Allocator , root_data : T ) -> Self {
32+ let ids = BoxBump :: new ( allocator) ;
33+ let root_id = ids. alloc ( StackedTreeId { depth : 0 , parent : None } ) ;
34+ let root_item = StackedTreeItem { id : root_id, data : root_data } ;
35+ StackedTree { ids, stack : vec ! [ root_item] , root : root_id }
5336 }
5437
5538 pub fn current_id ( & self ) -> I {
@@ -89,10 +72,11 @@ impl<I: Idx, T> StackedTree<I, T> {
8972 }
9073
9174 pub fn push ( & mut self , data : T ) -> I {
92- let id = I :: new ( self . stack . len ( ) , self . stack . last ( ) . unwrap ( ) . id_idx ) ;
93- let id_idx = self . ids . len ( ) ;
94- self . stack . push ( StackedTreeItem { id, id_idx, data } ) ;
95- self . ids . push ( id) ;
75+ let id = self . ids . alloc ( StackedTreeId {
76+ depth : self . stack . len ( ) ,
77+ parent : Some ( self . stack . last ( ) . unwrap ( ) . id ) ,
78+ } ) ;
79+ self . stack . push ( StackedTreeItem { id, data } ) ;
9680 id
9781 }
9882
@@ -104,20 +88,24 @@ impl<I: Idx, T> StackedTree<I, T> {
10488 self . stack [ depth] . id
10589 }
10690
91+ fn get_depth ( & self , id : I ) -> usize {
92+ self . ids . get ( id) . depth
93+ }
94+
10795 fn get_parent ( & self , id : I ) -> Option < I > {
108- if id . depth ( ) == 0 { None } else { Some ( self . ids [ id . parent ( ) ] ) }
96+ self . ids . get ( id ) . parent
10997 }
11098
11199 pub fn find_lca ( & self , another : I ) -> ( usize , I ) {
112100 let current_depth = self . stack . len ( ) - 1 ;
113- let another_depth = another . depth ( ) ;
101+ let another_depth = self . get_depth ( another ) ;
114102 let min_depth = current_depth. min ( another_depth) ;
115103
116104 let mut another = another;
117105 for _ in min_depth..another_depth {
118106 another = self . get_parent ( another) . unwrap ( ) ;
119107 }
120- debug_assert_eq ! ( min_depth, another . depth ( ) ) ;
108+ debug_assert_eq ! ( min_depth, self . get_depth ( another ) ) ;
121109
122110 let mut depth = min_depth;
123111 loop {
0 commit comments