@@ -72,7 +72,7 @@ impl TreeNode {
72
72
#[ allow( missing_docs) ]
73
73
pub struct Tree < T : TreeItem > {
74
74
pub nodes : Vec < TreeNode > ,
75
- pub items : Vec < TreeIndex < T > > ,
75
+ pub items : HashMap < Uuid , TreeIndex < T > > ,
76
76
path_to_node : HashMap < Vec < String > , usize > ,
77
77
}
78
78
@@ -81,7 +81,7 @@ impl<T: TreeItem> Tree<T> {
81
81
pub fn from_items ( items : Vec < T > ) -> Self {
82
82
let mut tree = Tree {
83
83
nodes : Vec :: new ( ) ,
84
- items : Vec :: new ( ) ,
84
+ items : HashMap :: new ( ) ,
85
85
path_to_node : HashMap :: new ( ) ,
86
86
} ;
87
87
@@ -95,8 +95,7 @@ impl<T: TreeItem> Tree<T> {
95
95
// add items
96
96
for ( index, item) in sorted_items. iter ( ) . enumerate ( ) {
97
97
let tree_index = TreeIndex :: new ( index, item) ;
98
- tree. items . push ( tree_index. clone ( ) ) ;
99
-
98
+ tree. items . insert ( item. id ( ) , tree_index. clone ( ) ) ;
100
99
tree. add_item ( tree_index) ;
101
100
}
102
101
@@ -118,39 +117,51 @@ impl<T: TreeItem> Tree<T> {
118
117
self . nodes . push ( node) ;
119
118
}
120
119
121
- #[ allow( missing_docs) ]
120
+ ///
121
+ /// Returns an optional node item for a given tree item id.
122
+ ///
123
+ /// This contains the item, its children (or an empty vector), and its parent (if it has one)
122
124
pub fn get_item_by_id ( & self , tree_item_id : Uuid ) -> Option < NodeItem < T > > {
123
- let item = self . items . iter ( ) . find ( |i| i . data . id ( ) == tree_item_id) ;
125
+ let item = self . items . get ( & tree_item_id) ;
124
126
125
127
if let Some ( item) = item {
126
128
let node = self . nodes . get ( item. id ) ?;
127
129
128
130
// Get the parent if it exists
129
- let parent = node. parent_idx . and_then ( |pid| self . nodes . get ( pid) ) ;
130
-
131
- // Get all children nodes
132
- let children: Vec < & TreeNode > = node
131
+ let parent = node
132
+ . parent_idx
133
+ . and_then ( |pid| self . nodes . get ( pid) )
134
+ . and_then ( |p| self . items . get ( & p. item_id ) )
135
+ . map ( |p| p. data . clone ( ) ) ;
136
+
137
+ // Get any children
138
+ let children: Vec < T > = node
133
139
. children_idx
134
140
. iter ( )
135
141
. filter_map ( |& child_id| self . nodes . get ( child_id) )
136
- . collect ( ) ;
137
-
138
- // Get corresponding items
139
- let parent_item = parent. and_then ( |p| self . items . get ( p. id ) ) ;
140
- let children_items: Vec < & TreeIndex < T > > = children
141
- . iter ( )
142
- . filter_map ( |child| self . items . get ( child. id ) )
142
+ . filter_map ( |child| self . items . get ( & child. item_id ) )
143
+ . map ( |i| i. data . clone ( ) )
143
144
. collect ( ) ;
144
145
145
146
return Some ( NodeItem {
146
147
item : item. data . clone ( ) ,
147
- parent : parent_item . map ( |p| p . data . clone ( ) ) ,
148
- children : children_items . iter ( ) . map ( |i| i . data . clone ( ) ) . collect ( ) ,
148
+ parent,
149
+ children,
149
150
} ) ;
150
151
}
151
152
152
153
None
153
154
}
155
+
156
+ ///
157
+ /// Returns the list of root nodes with their children
158
+ pub fn get_root_items ( & self ) -> Vec < NodeItem < T > > {
159
+ self . nodes
160
+ . iter ( )
161
+ . filter ( |n| n. parent_idx . is_none ( ) )
162
+ . filter_map ( |n| self . get_item_by_id ( n. item_id ) )
163
+ . collect ( )
164
+ }
154
165
}
155
166
156
167
#[ cfg( test) ]
0 commit comments