11//! Cache the trie for reuse.
22use crate :: input_sequence:: InputSequence ;
3- use bevy:: { ecs:: system:: Resource , prelude:: { In , Entity , GamepadButton } } ;
3+ use bevy:: {
4+ ecs:: prelude:: Resource ,
5+ prelude:: { Entity , GamepadButton , In } ,
6+ } ;
47use std:: collections:: HashMap ;
58use trie_rs:: {
69 inc_search:: { IncSearch , Position } ,
@@ -14,8 +17,7 @@ pub struct ButtonSequenceCache {
1417 position : HashMap < Entity , Position > ,
1518}
1619
17- impl ButtonSequenceCache
18- {
20+ impl ButtonSequenceCache {
1921 /// Retrieve the cached trie without iterating through `sequences`. Or if
2022 /// the cache has been invalidated, build and cache a new trie using the
2123 /// `sequences` iterator.
@@ -24,7 +26,8 @@ impl ButtonSequenceCache
2426 sequences : impl Iterator < Item = & ' a InputSequence < GamepadButton , In < Entity > > > ,
2527 ) -> & Trie < GamepadButton , InputSequence < GamepadButton , In < Entity > > > {
2628 self . trie . get_or_insert_with ( || {
27- let mut builder: TrieBuilder < GamepadButton , InputSequence < GamepadButton , In < Entity > > > = TrieBuilder :: new ( ) ;
29+ let mut builder: TrieBuilder < GamepadButton , InputSequence < GamepadButton , In < Entity > > > =
30+ TrieBuilder :: new ( ) ;
2831 for sequence in sequences {
2932 builder. insert ( sequence. acts . clone ( ) , sequence. clone ( ) ) ;
3033 }
@@ -61,57 +64,57 @@ impl ButtonSequenceCache
6164 . unwrap_or_else ( move || trie. inc_search ( ) )
6265 }
6366
64- // impl<'i, A, I> ButtonSequenceCache<'i, A, I>
65- // where
66- // A: Ord + Clone + Send + Sync + TypePath + 'static,
67- // I: SystemInput + Send + Sync,
68- // I::Inner<'i>: Clone + Eq + Hash + 'static,
69- // {
70- // // /// Retrieve the cached trie without iterating through `sequences`. Or if
71- // // /// the cache has been invalidated, build and cache a new trie using the
72- // // /// `sequences` iterator.
73- // // pub fn trie<'a>(
74- // // &mut self,
75- // // sequences: impl Iterator<Item = &'a InputSequence<A, I>>,
76- // // ) -> &Trie<A, InputSequence<A, I>> {
77- // // self.trie.get_or_insert_with(|| {
78- // // let mut builder: TrieBuilder<A, InputSequence<A, I>> = TrieBuilder::new();
79- // // for sequence in sequences {
80- // // builder.insert(sequence.acts.clone(), sequence.clone());
81- // // }
82- // // // info!(
83- // // // "Building trie for {} input sequences.",
84- // // // A::short_type_path()
85- // // // );
86- // // assert!(
87- // // self.position.is_empty(),
88- // // "Position should be none when rebuilding trie"
89- // // );
90- // // builder.build()
91- // // })
92- // // }
67+ // impl<'i, A, I> ButtonSequenceCache<'i, A, I>
68+ // where
69+ // A: Ord + Clone + Send + Sync + TypePath + 'static,
70+ // I: SystemInput + Send + Sync,
71+ // I::Inner<'i>: Clone + Eq + Hash + 'static,
72+ // {
73+ // // /// Retrieve the cached trie without iterating through `sequences`. Or if
74+ // // /// the cache has been invalidated, build and cache a new trie using the
75+ // // /// `sequences` iterator.
76+ // // pub fn trie<'a>(
77+ // // &mut self,
78+ // // sequences: impl Iterator<Item = &'a InputSequence<A, I>>,
79+ // // ) -> &Trie<A, InputSequence<A, I>> {
80+ // // self.trie.get_or_insert_with(|| {
81+ // // let mut builder: TrieBuilder<A, InputSequence<A, I>> = TrieBuilder::new();
82+ // // for sequence in sequences {
83+ // // builder.insert(sequence.acts.clone(), sequence.clone());
84+ // // }
85+ // // // info!(
86+ // // // "Building trie for {} input sequences.",
87+ // // // A::short_type_path()
88+ // // // );
89+ // // assert!(
90+ // // self.position.is_empty(),
91+ // // "Position should be none when rebuilding trie"
92+ // // );
93+ // // builder.build()
94+ // // })
95+ // // }
9396
94- // // /// Store a search.
95- // // pub fn store(&mut self, key: I, position: Position) {
96- // // self.position.insert(key, position);
97- // // }
97+ // // /// Store a search.
98+ // // pub fn store(&mut self, key: I, position: Position) {
99+ // // self.position.insert(key, position);
100+ // // }
98101
99- // // /// Recall a search OR create a new search.
100- // // pub fn recall<'a, 'b>(
101- // // &'b mut self,
102- // // key: I,
103- // // sequences: impl Iterator<Item = &'a InputSequence<A, I>>,
104- // // ) -> IncSearch<'a, A, InputSequence<A, I>>
105- // // where
106- // // 'b: 'a,
107- // // {
108- // // let position = self.position.get(&key).cloned();
109- // // let trie = self.trie(sequences);
110- // // position
111- // // .map(move |p| IncSearch::resume(trie, p))
112- // // .unwrap_or_else(move || trie.inc_search())
113- // // }
114- // }
102+ // // /// Recall a search OR create a new search.
103+ // // pub fn recall<'a, 'b>(
104+ // // &'b mut self,
105+ // // key: I,
106+ // // sequences: impl Iterator<Item = &'a InputSequence<A, I>>,
107+ // // ) -> IncSearch<'a, A, InputSequence<A, I>>
108+ // // where
109+ // // 'b: 'a,
110+ // // {
111+ // // let position = self.position.get(&key).cloned();
112+ // // let trie = self.trie(sequences);
113+ // // position
114+ // // .map(move |p| IncSearch::resume(trie, p))
115+ // // .unwrap_or_else(move || trie.inc_search())
116+ // // }
117+ // }
115118
116119 /// Clears the cache.
117120 pub fn reset ( & mut self ) {
0 commit comments