Skip to content

Commit

Permalink
excise: Remove covec.
Browse files Browse the repository at this point in the history
  • Loading branch information
shanecelis committed Apr 23, 2024
1 parent 8aaed04 commit a0d84cb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 62 deletions.
11 changes: 8 additions & 3 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! Cache the trie for reuse.
use crate::input_sequence::InputSequence;
use bevy::ecs::system::Resource;
use bevy::{
ecs::system::Resource,
log::info,
reflect::TypePath,
};
use trie_rs::{
map::{Trie, TrieBuilder},
inc_search::{IncSearch,
Expand All @@ -17,7 +21,7 @@ pub struct InputSequenceCache<A, In> {

impl<A, In> InputSequenceCache<A, In>
where
A: Ord + Clone + Send + Sync + 'static,
A: Ord + Clone + Send + Sync + TypePath + 'static,
In: Send + Sync + Clone + Eq + Hash + 'static,
{
/// Retrieve the cached trie without iterating through `sequences`. Or if
Expand All @@ -32,7 +36,8 @@ where
for sequence in sequences {
builder.insert(sequence.acts.clone(), sequence.clone());
}
assert!(self.position.len() == 0, "Position should be none when rebuilding trie");
info!("Building trie for {} input sequences.", A::short_type_path());
assert!(self.position.is_empty(), "Position should be none when rebuilding trie");
builder.build()
})
}
Expand Down
21 changes: 0 additions & 21 deletions src/covec.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod action;
pub mod cache;
mod chord;
pub mod cond_system;
mod covec;
mod frame_time;
pub mod input_sequence;
mod plugin;
Expand Down
43 changes: 6 additions & 37 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ use std::collections::{VecDeque, HashMap};
use crate::{
cache::InputSequenceCache,
chord::is_modifier,
covec::Covec,
frame_time::FrameTime,
input_sequence::{ButtonSequence, InputSequence, KeySequence},
KeyChord, Modifiers,
};
use trie_rs::{
map::Trie,
inc_search::{IncSearch, Answer},
};
use trie_rs::inc_search::{IncSearch, Answer};

/// ButtonInput sequence plugin.
pub struct InputSequencePlugin {
Expand Down Expand Up @@ -205,14 +201,14 @@ fn button_sequence_matcher(
}
};

last_times.push_front(now.clone());
last_times.push_back(now.clone());
let start = &last_times[0];
let mut search = cache.recall(button.gamepad, sequences.iter().by_ref());
for seq in inc_consume_input(&mut search, std::iter::once(button.button_type)) {
if seq
.time_limit
.as_ref()
.map(|limit| (&now - &start).has_timedout(limit))
.map(|limit| (&now - start).has_timedout(limit))
.unwrap_or(false)
{
// Sequence timed out.
Expand All @@ -230,7 +226,7 @@ fn button_sequence_matcher(

#[allow(clippy::too_many_arguments)]
fn key_sequence_matcher(
sequences: Query<&InputSequence<KeyChord, ()>>,
sequences: Query<&KeySequence>,
time: Res<Time>,
keys: Res<ButtonInput<KeyCode>>,
mut last_times: Local<VecDeque<FrameTime>>,
Expand All @@ -248,7 +244,7 @@ fn key_sequence_matcher(
.filter(|k| ! is_modifier(**k))
.map(|k| {
let chord = KeyChord(mods, *k);
last_times.push_front(now.clone());
last_times.push_back(now.clone());
chord
})
.peekable();
Expand All @@ -264,7 +260,7 @@ fn key_sequence_matcher(
if seq
.time_limit
.as_ref()
.map(|limit| (&now - &start).has_timedout(limit))
.map(|limit| (&now - start).has_timedout(limit))
.unwrap_or(false)
{
// Sequence timed out.
Expand Down Expand Up @@ -313,30 +309,3 @@ where
}
})
}

fn consume_input<'a, K, V>(trie: &'a Trie<K, V>, input: &mut Vec<K>) -> impl Iterator<Item = &'a V>
where
K: Clone + Eq + Ord,
{
let mut result = vec![];
let mut min_prefix = None;
for i in 0..input.len() {
if let Some(seq) = trie.exact_match(&input[i..]) {
result.push(seq);
}
if min_prefix.is_none() && trie.is_prefix(&input[i..]) {
min_prefix = Some(i);
// let _ = input.drain(0..i);
// return result.into_iter();
}
}
match min_prefix {
Some(i) => {
let _ = input.drain(0..i);
}
None => {
input.clear();
}
}
result.into_iter()
}

0 comments on commit a0d84cb

Please sign in to comment.