Skip to main content

euv_core/reactive/selector/
struct.rs

1use super::*;
2
3/// Lightweight non-cryptographic 64-bit FxHash hasher for O(1) selector key lookups.
4#[derive(Clone, Copy, Debug, Default)]
5pub(crate) struct SelectorFxHasher {
6    /// Accumulated 64-bit hash state.
7    pub(crate) hash: u64,
8}
9
10/// Open-addressed hash table slot storing a cached hash, selector key, and boolean signal.
11#[derive(CustomDebug)]
12pub(crate) struct SelectorEntry<K> {
13    /// Precomputed 64-bit FxHash digest of `key` for fast mismatch rejection and rehashing.
14    pub(crate) hash: u64,
15    /// Key represented by this slot.
16    #[debug(skip)]
17    pub(crate) key: K,
18    /// Per-key boolean selection signal.
19    pub(crate) signal: Signal<bool>,
20}
21
22/// Compact open-addressed linear-probing table mapping selector keys to `Signal<bool>`.
23#[derive(CustomDebug)]
24pub(crate) struct SelectorTable<K> {
25    /// Power-of-two bucket storage (empty until the first key is registered).
26    #[debug(skip)]
27    pub(crate) entries: Vec<Option<SelectorEntry<K>>>,
28    /// Number of occupied slots currently stored in `entries`.
29    pub(crate) len: usize,
30}
31
32/// Consolidated selector state combining the active key, cached active signal, and keyed table.
33#[derive(CustomDebug)]
34pub(crate) struct SelectorState<K>
35where
36    K: Clone + Eq + Hash + 'static,
37{
38    /// Currently selected key value.
39    #[debug(skip)]
40    pub(crate) active_key: Option<K>,
41    /// Cached `Signal<bool>` for `active_key` to eliminate a table lookup on deselection.
42    pub(crate) active_sig: Option<Signal<bool>>,
43    /// Per-key boolean selection signal table.
44    #[debug(skip)]
45    pub(crate) table: SelectorTable<K>,
46}
47
48/// O(1) keyed reactive selector notifying only the previously selected and newly selected keys.
49#[derive(CustomDebug)]
50pub struct Selector<K>
51where
52    K: Clone + Eq + Hash + 'static,
53{
54    /// Shared selector state holding the active key, cached active signal, and keyed table.
55    #[debug(skip)]
56    pub(crate) state: Rc<RefCell<SelectorState<K>>>,
57    /// Effect watching the source key producer.
58    pub(crate) watcher: Effect,
59}