Skip to main content

Module history

Module history 

Source
Expand description

History stack for undo/redo operations.

This module provides the HistoryManager which maintains dual stacks for undo and redo operations with support for:

  • Memory limits: Oldest commands evicted when budget exceeded
  • Depth limits: Maximum number of commands in history
  • Branch handling: New actions clear the redo stack
  • Command merging: Consecutive similar commands batched together

§Invariants

  1. total_bytes always equals sum of size_bytes() for all commands
  2. undo_stack.len() <= config.max_depth (after any operation)
  3. total_bytes <= config.max_bytes (after any operation, if enforced)
  4. Redo stack is cleared whenever a new command is pushed

§Memory Model

Commands are stored in VecDeque for O(1) eviction from the front. Memory accounting uses each command’s size_bytes() method.

push(cmd5)
┌───────────────────────────────────────────────┐
│ Undo Stack: [cmd1, cmd2, cmd3, cmd4, cmd5]    │
│ Redo Stack: []                                 │
└───────────────────────────────────────────────┘

undo() x2
┌───────────────────────────────────────────────┐
│ Undo Stack: [cmd1, cmd2, cmd3]                │
│ Redo Stack: [cmd4, cmd5]                       │
└───────────────────────────────────────────────┘

push(cmd6)  <-- new branch, clears redo
┌───────────────────────────────────────────────┐
│ Undo Stack: [cmd1, cmd2, cmd3, cmd6]          │
│ Redo Stack: []                                 │
└───────────────────────────────────────────────┘

Structs§

HistoryConfig
Configuration for the history manager.
HistoryManager
Manager for undo/redo history.