clean history

This commit is contained in:
2026-05-19 10:22:44 -07:00
parent 08e8656ecf
commit bc2d1b126e
+27
View File
@@ -424,6 +424,33 @@ where
state.undo();
}
}
// history includes cycles
let mut state_index: std::collections::HashMap<_, _> = state
.history()
.iter()
.enumerate()
.map(|(i, snapshot)| (snapshot.state().clone(), i))
.collect();
// find the longest range where the start and end are the same state
while let Some(longest_range) = state
.history()
.iter()
.enumerate()
.filter_map(|(index, snapshot)| {
let &last_index = state_index.get(snapshot.state())?;
let longness = last_index - index;
(longness != 0).then_some(index..last_index)
})
.max_by_key(|range| range.len())
{
state.state.history.drain(longest_range);
for (i, snapshot) in state.history().iter().enumerate() {
state_index.insert(snapshot.state().clone(), i);
}
}
Some(state.state.history)
}
}