pub struct FlatVecVec<T> { /* private fields */ }Expand description
A flat, cache-friendly replacement for Vec<Vec<T>>.
During the build phase, items are pushed into per-node inner Vecs
(same as before). After building is complete, flatten() compacts all
inner Vecs into a single contiguous Vec<T> with a (start, len) offset
table per node. All subsequent reads use the flat layout, eliminating
N heap allocations and pointer chasing.
§Lifecycle
new(n) → push_to(idx, item)* → sort_each_and_flatten(key_fn) → get_slice(idx)*
── build phase ── ── transition ── ── read phase ──Implementations§
Source§impl<T> FlatVecVec<T>
impl<T> FlatVecVec<T>
Sourcepub fn heap_bytes(&self, per_element_size: usize) -> usize
pub fn heap_bytes(&self, per_element_size: usize) -> usize
Approximate heap bytes retained. Sums capacity of the
flattened data + offsets tables and the per-node build
Vecs (in case sort_each_and_flatten hasn’t been called
yet). per_element_size should be size_of::<T>().
Sourcepub fn new(node_count: usize) -> Self
pub fn new(node_count: usize) -> Self
Create a new FlatVecVec with node_count empty slots (build phase).
Sourcepub fn push_to(&mut self, node_index: usize, item: T)
pub fn push_to(&mut self, node_index: usize, item: T)
Push an item to the inner Vec at node_index (build phase).
§Panics
Panics if already flattened or if node_index >= len().
Sourcepub fn build_mut(&mut self, node_index: usize) -> &mut Vec<T>
pub fn build_mut(&mut self, node_index: usize) -> &mut Vec<T>
Get a mutable reference to the inner Vec at node_index (build phase).
Sourcepub fn build_iter_mut(&mut self) -> IterMut<'_, Vec<T>>
pub fn build_iter_mut(&mut self) -> IterMut<'_, Vec<T>>
Iterate mutably over all inner Vecs (build phase, e.g. for clearing).
Sourcepub fn build_get(&self, node_index: usize) -> Option<&Vec<T>>
pub fn build_get(&self, node_index: usize) -> Option<&Vec<T>>
Get a reference to the inner Vec at node_index during build phase.
During read phase, returns None (use get_slice instead).
Sourcepub fn is_flattened(&self) -> bool
pub fn is_flattened(&self) -> bool
Returns true if this is in read (flattened) mode.
Sourcepub fn get_slice(&self, node_index: usize) -> &[T]
pub fn get_slice(&self, node_index: usize) -> &[T]
Get a slice for the node at node_index (read phase).
Returns empty slice if index is out of bounds or not yet flattened
(falls back to build-phase data if not yet flattened).
Sourcepub fn sort_each_and_flatten<K: Ord + Eq>(&mut self, key_fn: impl Fn(&T) -> K)
pub fn sort_each_and_flatten<K: Ord + Eq>(&mut self, key_fn: impl Fn(&T) -> K)
Flatten: sort each inner Vec by key, deduplicate by keeping the last
occurrence of each key (CSS cascade: later source order wins among
equal specificity), then compact into flat storage.
Drains all build-phase Vecs. After this call, only get_slice() works.
Sourcepub fn retain(&mut self, predicate: impl Fn(&T) -> bool)where
T: Clone,
pub fn retain(&mut self, predicate: impl Fn(&T) -> bool)where
T: Clone,
Rebuild flat storage, keeping only items matching predicate.
Must be called after flatten. Preserves per-node ordering.
Sourcepub fn retain_with_node_index(&mut self, predicate: impl Fn(usize, &T) -> bool)where
T: Clone,
pub fn retain_with_node_index(&mut self, predicate: impl Fn(usize, &T) -> bool)where
T: Clone,
Like retain, but passes each item’s owning node index to the predicate.
Must be called after flatten. Preserves per-node ordering.
Sourcepub fn extend_from(&mut self, other: &mut Self)
pub fn extend_from(&mut self, other: &mut Self)
Extend this FlatVecVec with all nodes from other (append for DOM merge).
Both must be in build phase, or both must be flattened.
Trait Implementations§
Source§impl<T: Clone> Clone for FlatVecVec<T>
impl<T: Clone> Clone for FlatVecVec<T>
Source§fn clone(&self) -> FlatVecVec<T>
fn clone(&self) -> FlatVecVec<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more