36
36
) ]
37
37
38
38
use std:: fmt:: { self , Debug , Formatter } ;
39
- use std:: num:: NonZeroUsize ;
40
39
41
40
/// Vec-backed ID-tree.
42
41
///
43
42
/// Always contains at least a root node.
43
+ #[ derive( Clone , PartialEq , Eq , Hash ) ]
44
44
pub struct Tree < T > {
45
- // Safety note: node at index 0 is uninitialized!
46
45
vec : Vec < Node < T > > ,
47
46
}
48
47
49
- impl < T > Clone for Tree < T > where T : Clone {
50
- fn clone ( & self ) -> Self {
51
- let mut vec = Vec :: with_capacity ( self . vec . len ( ) ) ;
52
- // See Tree::with_capacity
53
- unsafe {
54
- vec. set_len ( 1 ) ;
55
- }
56
- vec. extend ( self . vec [ 1 ..] . iter ( ) . cloned ( ) ) ;
57
- Tree { vec }
58
- }
59
- }
60
-
61
- impl < T > std:: hash:: Hash for Tree < T > where T : std:: hash:: Hash {
62
- fn hash < H > ( & self , state : & mut H ) where H : std:: hash:: Hasher {
63
- self . vec [ 1 ..] . hash ( state)
64
- }
65
- }
66
-
67
- impl < T > Eq for Tree < T > where T : Eq { }
68
- impl < T > PartialEq for Tree < T > where T : PartialEq {
69
- fn eq ( & self , other : & Self ) -> bool {
70
- self . vec [ 1 ..] == other. vec [ 1 ..]
71
- }
72
- }
73
-
74
-
75
48
/// Node ID.
76
49
///
77
50
/// Index into a `Tree`-internal `Vec`.
78
51
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
79
- pub struct NodeId ( NonZeroUsize ) ;
80
-
81
- const ROOT : NodeId = NodeId ( unsafe { NonZeroUsize :: new_unchecked ( 1 ) } ) ;
52
+ pub struct NodeId ( usize ) ;
82
53
83
54
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
84
55
struct Node < T > {
@@ -89,13 +60,6 @@ struct Node<T> {
89
60
value : T ,
90
61
}
91
62
92
- fn _static_assert_size_of_node ( ) {
93
- // "Instanciating" the generic `transmute` function without calling it
94
- // still triggers the magic compile-time check
95
- // that input and output types have the same `size_of()`.
96
- let _ = std:: mem:: transmute :: < Node < ( ) > , [ usize ; 5 ] > ;
97
- }
98
-
99
63
impl < T > Node < T > {
100
64
fn new ( value : T ) -> Self {
101
65
Node {
@@ -149,42 +113,33 @@ impl<'a, T: 'a> PartialEq for NodeRef<'a, T> {
149
113
impl < T > Tree < T > {
150
114
/// Creates a tree with a root node.
151
115
pub fn new ( root : T ) -> Self {
152
- Self :: with_capacity ( root, 1 )
116
+ Tree { vec : vec ! [ Node :: new ( root) ] }
153
117
}
154
118
155
119
/// Creates a tree with a root node and the specified capacity.
156
120
pub fn with_capacity ( root : T , capacity : usize ) -> Self {
157
- let mut vec = Vec :: with_capacity ( capacity. saturating_add ( 1 ) ) ;
158
- // The node at index 0 is unused and uninitialized.
159
- // This allows using NonZeroUsize directly as an index.
160
- //
161
- // Safety: we requested at least 1 of capacity, so this is in bounds.
162
- // It is up to the rest of the crate to not access this uninitialized node.
163
- unsafe {
164
- vec. set_len ( 1 ) ;
165
- }
166
- // The root node is at index 1
121
+ let mut vec = Vec :: with_capacity ( capacity) ;
167
122
vec. push ( Node :: new ( root) ) ;
168
123
Tree { vec }
169
124
}
170
125
171
126
/// Returns a reference to the specified node.
172
127
pub fn get ( & self , id : NodeId ) -> Option < NodeRef < T > > {
173
- self . vec . get ( id. 0 . get ( ) ) . map ( |node| NodeRef { id, node, tree : self } )
128
+ self . vec . get ( id. 0 ) . map ( |node| NodeRef { id, node, tree : self } )
174
129
}
175
130
176
131
/// Returns a mutator of the specified node.
177
132
pub fn get_mut ( & mut self , id : NodeId ) -> Option < NodeMut < T > > {
178
- let exists = self . vec . get ( id. 0 . get ( ) ) . map ( |_| ( ) ) ;
133
+ let exists = self . vec . get ( id. 0 ) . map ( |_| ( ) ) ;
179
134
exists. map ( move |_| NodeMut { id, tree : self } )
180
135
}
181
136
182
137
unsafe fn node ( & self , id : NodeId ) -> & Node < T > {
183
- self . vec . get_unchecked ( id. 0 . get ( ) )
138
+ self . vec . get_unchecked ( id. 0 )
184
139
}
185
140
186
141
unsafe fn node_mut ( & mut self , id : NodeId ) -> & mut Node < T > {
187
- self . vec . get_unchecked_mut ( id. 0 . get ( ) )
142
+ self . vec . get_unchecked_mut ( id. 0 )
188
143
}
189
144
190
145
/// Returns a reference to the specified node.
@@ -199,19 +154,17 @@ impl<T> Tree<T> {
199
154
200
155
/// Returns a reference to the root node.
201
156
pub fn root ( & self ) -> NodeRef < T > {
202
- unsafe { self . get_unchecked ( ROOT ) }
157
+ unsafe { self . get_unchecked ( NodeId ( 0 ) ) }
203
158
}
204
159
205
160
/// Returns a mutator of the root node.
206
161
pub fn root_mut ( & mut self ) -> NodeMut < T > {
207
- unsafe { self . get_unchecked_mut ( ROOT ) }
162
+ unsafe { self . get_unchecked_mut ( NodeId ( 0 ) ) }
208
163
}
209
164
210
165
/// Creates an orphan node.
211
166
pub fn orphan ( & mut self , value : T ) -> NodeMut < T > {
212
- // Safety: vec.len() starts at 2 in Self::with_capacity and never shrinks,
213
- // so it is non-zero.
214
- let id = NodeId ( unsafe { NonZeroUsize :: new_unchecked ( self . vec . len ( ) ) } ) ;
167
+ let id = NodeId ( self . vec . len ( ) ) ;
215
168
self . vec . push ( Node :: new ( value) ) ;
216
169
unsafe { self . get_unchecked_mut ( id) }
217
170
}
@@ -675,8 +628,8 @@ macro_rules! tree {
675
628
impl < T : Debug > Debug for Tree < T > {
676
629
fn fmt ( & self , f : & mut Formatter ) -> Result < ( ) , fmt:: Error > {
677
630
use iter:: Edge ;
678
- write ! ( f, "Tree {{" ) ?;
679
631
if f. alternate ( ) {
632
+ write ! ( f, "Tree {{" ) ?;
680
633
for edge in self . root ( ) . traverse ( ) {
681
634
match edge {
682
635
Edge :: Open ( node) if node. has_children ( ) => {
@@ -700,12 +653,7 @@ impl<T: Debug> Debug for Tree<T> {
700
653
}
701
654
write ! ( f, " }}" )
702
655
} else {
703
- write ! ( f, "Tree {{ [<uninitialized>" ) ?;
704
- for node in & self . vec [ 1 ..] {
705
- write ! ( f, ", " ) ?;
706
- node. fmt ( f) ?;
707
- }
708
- write ! ( f, "] }}" )
656
+ f. debug_struct ( "Tree" ) . field ( "vec" , & self . vec ) . finish ( )
709
657
}
710
658
}
711
659
}
0 commit comments