pub struct MoveArena<M> { /* private fields */ }Expand description
Arena allocator for moves with O(1) reset.
Instead of allocating a new Vecreset() simply
sets the length to 0 without running destructors (moves are Copy-like
in practice since they contain only primitives and small inline data).
§Performance
| Operation | Vec per step | MoveArena |
|---|---|---|
| Alloc | O(n) heap | O(1) bump |
| Cleanup | O(n) drop | O(1) reset |
| Memory | n * size_of | Reused |
§Example
use solverforge_solver::heuristic::r#move::MoveArena;
let mut arena: MoveArena<i32> = MoveArena::new();
// Step 1: generate and evaluate moves
arena.push(1);
arena.push(2);
arena.push(3);
assert_eq!(arena.len(), 3);
// Step 2: reset and reuse (O(1)!)
arena.reset();
assert!(arena.is_empty());
arena.push(10);
arena.push(20);
for mov in arena.iter() {
assert!(*mov >= 10);
}Implementations§
Source§impl<M> MoveArena<M>
impl<M> MoveArena<M>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new arena with pre-allocated capacity.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the arena, making it empty.
Drops all moves except the one that was taken (if any).
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut M>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut M>
Returns a mutable iterator over the moves.
Sourcepub fn take(&mut self, index: usize) -> M
pub fn take(&mut self, index: usize) -> M
Takes ownership of a move by index.
Only one move can be taken per step. Call reset() before taking another.
§Panics
Panics if index >= len or if a move was already taken.
§Example
use solverforge_solver::heuristic::r#move::MoveArena;
let mut arena: MoveArena<String> = MoveArena::new();
arena.push("first".to_string());
arena.push("second".to_string());
// Take ownership of the move at index 1
let taken = arena.take(1);
assert_eq!(taken, "second");
// Reset before next step
arena.reset();Sourcepub fn extend<I: IntoIterator<Item = M>>(&mut self, iter: I)
pub fn extend<I: IntoIterator<Item = M>>(&mut self, iter: I)
Extends the arena from an iterator.
Trait Implementations§
Auto Trait Implementations§
impl<M> Freeze for MoveArena<M>
impl<M> RefUnwindSafe for MoveArena<M>where
M: RefUnwindSafe,
impl<M> Send for MoveArena<M>where
M: Send,
impl<M> Sync for MoveArena<M>where
M: Sync,
impl<M> Unpin for MoveArena<M>where
M: Unpin,
impl<M> UnwindSafe for MoveArena<M>where
M: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more