expression

Repair dense index mappings after unordered removal

A dense vector can remove an entry cheaply without preserving order by moving its last entry into the removed slot. A separate ID-to-index map must repair the moved entry and delete the removed ID. This is a small data-oriented storage model, not an ECS with generation-safe handles. Expected result (NOT RUN): dense: [10, 30], index of 30: 1 Common mistake: Keeping the old index of the last entry after swap_remove. Static Rust example; not a verified ByteMind executable assessment.

Formal Syntax & Structure

rust syntaxrust
swap_remove(index); update moved ID index

Working Example

Example Snippetrust
use std::collections::BTreeMap;fn main() {    let mut dense = vec![10u32, 20, 30];    let mut indices = BTreeMap::from([(10, 0usize), (20, 1), (30, 2)]);    let i = indices.remove(&20).unwrap();    dense.swap_remove(i);    if i < dense.len() { indices.insert(dense[i], i); }    println!("dense: {dense:?}, index of 30: {}", indices[&30]);}
Rust Reference