@@ -172,14 +172,17 @@ impl<'a, T> PathTrieIter<'a, T> {
172172}
173173
174174pub struct ConsolidatingPathTrie {
175+ children_consolidation : bool ,
176+
175177 trie : PathTrie < ( ) > ,
176178}
177179
178180impl ConsolidatingPathTrie {
179181 const CHILDREN_CONSOLIDATION_THRESHOLD : usize = 10 ;
180182
181- pub fn new ( ) -> Self {
183+ pub fn new ( children_consolidation : bool ) -> Self {
182184 Self {
185+ children_consolidation,
183186 trie : PathTrie :: new ( ) ,
184187 }
185188 }
@@ -192,14 +195,16 @@ impl ConsolidatingPathTrie {
192195 let inserted = self . trie . insert ( path, ( ) ) ;
193196 inserted. remove_children ( ) ;
194197
195- for ancestor_path in path. ancestors ( ) . skip ( 1 ) {
196- if let Some ( parent_node) = self . trie . get_node_mut ( ancestor_path)
197- && parent_node. children_len ( ) >= Self :: CHILDREN_CONSOLIDATION_THRESHOLD
198- {
199- parent_node. remove_children ( ) ;
200- parent_node. set_value ( ( ) ) ;
201- } else {
202- break ;
198+ if self . children_consolidation {
199+ for ancestor_path in path. ancestors ( ) . skip ( 1 ) {
200+ if let Some ( parent_node) = self . trie . get_node_mut ( ancestor_path)
201+ && parent_node. children_len ( ) >= Self :: CHILDREN_CONSOLIDATION_THRESHOLD
202+ {
203+ parent_node. remove_children ( ) ;
204+ parent_node. set_value ( ( ) ) ;
205+ } else {
206+ break ;
207+ }
203208 }
204209 }
205210 }
@@ -235,54 +240,68 @@ mod tests {
235240
236241 #[ test]
237242 fn consolidate_no_siblings ( ) {
238- let mut ct = ConsolidatingPathTrie :: new ( ) ;
239- ct. insert ( PathBuf :: from ( "/a/b" ) ) ;
240- ct. insert ( PathBuf :: from ( "/a/c" ) ) ;
241- assert_eq ! (
242- ct. values( ) ,
243- vec![ PathBuf :: from( "/a/b" ) , PathBuf :: from( "/a/c" ) ]
244- ) ;
243+ for children_consolidation in [ true , false ] {
244+ let mut ct = ConsolidatingPathTrie :: new ( children_consolidation) ;
245+ ct. insert ( PathBuf :: from ( "/a/b" ) ) ;
246+ ct. insert ( PathBuf :: from ( "/a/c" ) ) ;
247+ assert_eq ! (
248+ ct. values( ) ,
249+ vec![ PathBuf :: from( "/a/b" ) , PathBuf :: from( "/a/c" ) ]
250+ ) ;
251+ }
245252 }
246253
247254 #[ test]
248255 fn consolidate_no_siblings2 ( ) {
249- let mut ct = ConsolidatingPathTrie :: new ( ) ;
250- ct. insert ( PathBuf :: from ( "/a/b1" ) ) ;
251- ct. insert ( PathBuf :: from ( "/a/b2" ) ) ;
252- assert_eq ! (
253- ct. values( ) ,
254- vec![ PathBuf :: from( "/a/b1" ) , PathBuf :: from( "/a/b2" ) ]
255- ) ;
256+ for children_consolidation in [ true , false ] {
257+ let mut ct = ConsolidatingPathTrie :: new ( children_consolidation) ;
258+ ct. insert ( PathBuf :: from ( "/a/b1" ) ) ;
259+ ct. insert ( PathBuf :: from ( "/a/b2" ) ) ;
260+ assert_eq ! (
261+ ct. values( ) ,
262+ vec![ PathBuf :: from( "/a/b1" ) , PathBuf :: from( "/a/b2" ) ]
263+ ) ;
264+ }
256265 }
257266
258267 #[ test]
259268 fn consolidate_children ( ) {
260- let mut ct = ConsolidatingPathTrie :: new ( ) ;
261- ct. insert ( PathBuf :: from ( "/a/b" ) ) ;
262- ct. insert ( PathBuf :: from ( "/a/b/c" ) ) ;
263- assert_eq ! ( ct. values( ) , vec![ PathBuf :: from( "/a/b" ) ] ) ;
269+ for children_consolidation in [ true , false ] {
270+ let mut ct = ConsolidatingPathTrie :: new ( children_consolidation) ;
271+ ct. insert ( PathBuf :: from ( "/a/b" ) ) ;
272+ ct. insert ( PathBuf :: from ( "/a/b/c" ) ) ;
273+ assert_eq ! ( ct. values( ) , vec![ PathBuf :: from( "/a/b" ) ] ) ;
274+ }
264275 }
265276
266277 #[ test]
267278 fn consolidate_parent ( ) {
268- let mut ct = ConsolidatingPathTrie :: new ( ) ;
269- ct. insert ( PathBuf :: from ( "/a/b/c" ) ) ;
270- ct. insert ( PathBuf :: from ( "/a/b" ) ) ;
271- assert_eq ! ( ct. values( ) , vec![ PathBuf :: from( "/a/b" ) ] ) ;
279+ for children_consolidation in [ true , false ] {
280+ let mut ct = ConsolidatingPathTrie :: new ( children_consolidation) ;
281+ ct. insert ( PathBuf :: from ( "/a/b/c" ) ) ;
282+ ct. insert ( PathBuf :: from ( "/a/b" ) ) ;
283+ assert_eq ! ( ct. values( ) , vec![ PathBuf :: from( "/a/b" ) ] ) ;
284+ }
272285 }
273286
274287 #[ test]
275288 fn consolidate_to_single_parent ( ) {
276- let mut cr = ConsolidatingPathTrie :: new ( ) ;
289+ let mut cr = ConsolidatingPathTrie :: new ( true ) ;
277290 for i in 1 ..=ConsolidatingPathTrie :: CHILDREN_CONSOLIDATION_THRESHOLD {
278291 cr. insert ( PathBuf :: from ( format ! ( "/a/b/c{i}" ) ) ) ;
279292 }
280293 assert_eq ! ( cr. values( ) , vec![ PathBuf :: from( "/a/b" ) ] ) ;
294+
295+ let mut cr = ConsolidatingPathTrie :: new ( false ) ;
296+ for i in 1 ..=ConsolidatingPathTrie :: CHILDREN_CONSOLIDATION_THRESHOLD {
297+ cr. insert ( PathBuf :: from ( format ! ( "/a/b/c{i}" ) ) ) ;
298+ }
299+ assert ! ( cr. values( ) . len( ) > 1 ) ;
281300 }
282301
283302 #[ test]
284303 fn consolidate_to_single_parent_nested1 ( ) {
285- let mut cr = ConsolidatingPathTrie :: new ( ) ;
304+ let mut cr = ConsolidatingPathTrie :: new ( true ) ;
286305 for i in 1 ..ConsolidatingPathTrie :: CHILDREN_CONSOLIDATION_THRESHOLD {
287306 cr. insert ( PathBuf :: from ( format ! ( "/a/b/c{i}" ) ) ) ;
288307 }
@@ -294,7 +313,7 @@ mod tests {
294313
295314 #[ test]
296315 fn consolidate_to_single_parent_nested2 ( ) {
297- let mut cr = ConsolidatingPathTrie :: new ( ) ;
316+ let mut cr = ConsolidatingPathTrie :: new ( true ) ;
298317 cr. insert ( PathBuf :: from ( "/a/b/c1" ) ) ;
299318 cr. insert ( PathBuf :: from ( "/a/b/c2" ) ) ;
300319 for i in 1 ..=ConsolidatingPathTrie :: CHILDREN_CONSOLIDATION_THRESHOLD {
0 commit comments