Skip to content

Commit

Permalink
Better Big O (not-elm#7)
Browse files Browse the repository at this point in the history
* feature: Use incremental search for key matcher.

* chore: Bump version to 0.4.0.

* feature: Key matcher uses incremental search.

* feature: Button matcher uses incremental search.

* excise: Remove covec.

* style: Run cargo fmt.

* hack: Missed during conflict resolution.

* chore: Set trie-rs to new version 0.4.

* hack: Comment out info! statement.
  • Loading branch information
shanecelis authored May 3, 2024
1 parent abc2fa8 commit 5a43f8e
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 103 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ All notable changes to this project will be documented in this file.

## [0.4.0] - 2024-04-23

## [0.4.0] - 2024-04-23

### Features

- Generalize to running one-shot systems instead of firing events.
- Use a plugin, can configure schedule and system set.
- Use plugin.
- Can configure schedule and system set.
- Remove `GamepadEvent`; use system with input `In<Gamepad>`.
- Add IntoCondSystem that adds `only_if()` conditions to `IntoSystems`.
- Add `IntoCondSystem` that adds `only_if()` conditions to `IntoSystems`.
- Add `only_if` example.
- Add prelude module for glob imports.
- Add `prelude` module for glob imports.

### Refactor

- Hollow out lib so it's just `mod` and `pub use` statements.
- Extract sets of functionality from `lib.rs` into `chord.rs`, `cache.rs` and `plugin.rs`.
- Extract simulated tests into `tests/` directory.
- Extract tests into `tests/simulated.rs`.

## [0.3.0] - 2024-03-08

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ path = "examples/multiple_input.rs"

[dependencies]
bevy = { version = "0.13", default-features = false, features = [] }
trie-rs = { version = "0.3" }
trie-rs = { version = "0.4" }
keyseq = { version = "0.2.3", features = [ "bevy" ] }

[dev-dependencies]
Expand Down
58 changes: 52 additions & 6 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
//! Cache the trie for reuse.
use crate::input_sequence::InputSequence;
use bevy::ecs::system::Resource;
use trie_rs::map::{Trie, TrieBuilder};
use bevy::{ecs::system::Resource, log::info, reflect::TypePath};
use std::{collections::HashMap, hash::Hash};
use trie_rs::{
inc_search::{IncSearch, Position},
map::{Trie, TrieBuilder},
};

/// Contains the trie for the input sequences.
#[derive(Resource)]
pub struct InputSequenceCache<A, In> {
pub(crate) trie: Option<Trie<A, InputSequence<A, In>>>,
trie: Option<Trie<A, InputSequence<A, In>>>,
position: HashMap<In, Position>,
}

impl<A, In> InputSequenceCache<A, In>
where
A: Ord + Clone + Send + Sync + 'static,
In: Send + Sync + Clone + '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
/// the cache has been invalidated, build and cache a new trie using the
Expand All @@ -25,13 +31,53 @@ where
for sequence in sequences {
builder.insert(sequence.acts.clone(), sequence.clone());
}
// info!(
// "Building trie for {} input sequences.",
// A::short_type_path()
// );
assert!(
self.position.is_empty(),
"Position should be none when rebuilding trie"
);
builder.build()
})
}

/// Store a search.
pub fn store(&mut self, key: In, position: Position) {
self.position.insert(key, position);
}

/// Recall a search OR create a new search.
pub fn recall<'a, 'b>(
&'b mut self,
key: In,
sequences: impl Iterator<Item = &'a InputSequence<A, In>>,
) -> IncSearch<'a, A, InputSequence<A, In>>
where
'b: 'a,
{
let position = self.position.get(&key).cloned();
let trie = self.trie(sequences);
position
.map(move |p| IncSearch::resume(trie, p))
.unwrap_or_else(move || trie.inc_search())
}
}

impl<A, In> InputSequenceCache<A, In> {
/// Clears the cache.
pub fn reset(&mut self) {
self.trie = None;
self.position.clear();
}
}

impl<A, In> Default for InputSequenceCache<A, In> {
fn default() -> Self {
Self { trie: None }
Self {
trie: None,
position: HashMap::new(),
}
}
}
21 changes: 0 additions & 21 deletions src/covec.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/frame_time.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::time_limit::TimeLimit;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub(crate) struct FrameTime {
pub(crate) frame: u32,
pub(crate) time: f32,
Expand Down
24 changes: 11 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@
#![doc = include_str!("../README.md")]
#![forbid(missing_docs)]

pub use keyseq::{
bevy::{pkey as key, pkeyseq as keyseq},
Modifiers,
};

pub use chord::KeyChord;
pub use plugin::InputSequencePlugin;
pub use time_limit::TimeLimit;

pub mod action;
mod cache;
pub mod cache;
mod chord;
pub mod cond_system;
mod covec;
mod frame_time;
pub mod input_sequence;
mod plugin;
mod time_limit;

/// Convenient splat import
pub mod prelude {
pub use std::time::Duration;
pub use keyseq::{
bevy::{pkey as key, pkeyseq as keyseq},
Modifiers,
};

pub use crate::input_sequence::{ButtonSequence, InputSequence, KeySequence};
/// Convenient glob import
pub mod prelude {
pub use super::input_sequence::{ButtonSequence, InputSequence, KeySequence};
pub use super::{action, keyseq, InputSequencePlugin, Modifiers, TimeLimit};

pub use super::{action, InputSequencePlugin, keyseq, Modifiers, TimeLimit};
pub use super::chord::KeyChord;
pub use super::cond_system::IntoCondSystem;
pub use std::time::Duration;
}

Loading

0 comments on commit 5a43f8e

Please sign in to comment.