InverseSupply

Struct InverseSupply 

Source
pub struct InverseSupply<V>
where V: Eq + Hash,
{ /* private fields */ }
Expand description

Index-based inverse variable supply.

For a chained variable where entity.previous = value, this supply answers: “Given value, which entity index has entities[idx].previous == value?”

Returns entity indices - caller accesses actual entity via solution.entities[idx].

§Example

use solverforge_core::domain::supply::InverseSupply;

let mut supply: InverseSupply<i32> = InverseSupply::new();

// Entity at index 0 points to value 42
supply.insert(42, 0);

// Entity at index 1 points to value 99
supply.insert(99, 1);

assert_eq!(supply.get(&42), Some(0));
assert_eq!(supply.get(&99), Some(1));
assert_eq!(supply.get(&100), None);

Implementations§

Source§

impl<V> InverseSupply<V>
where V: Eq + Hash,

Source

pub fn new() -> Self

Creates a new empty inverse supply.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new inverse supply with pre-allocated capacity.

Source

pub fn get(&self, value: &V) -> Option<usize>

Gets the entity index that points to the given value.

Returns None if no entity currently points to this value.

Source

pub fn insert(&mut self, value: V, entity_idx: usize) -> Option<usize>

Registers that an entity index now points to a value.

Returns the previous entity index if this value was already mapped.

Source

pub fn remove(&mut self, value: &V) -> Option<usize>

Removes the mapping for a value.

Returns the entity index that was mapped, if any.

Source

pub fn update(&mut self, old_value: Option<&V>, new_value: V, entity_idx: usize)

Updates the mapping: removes old value, inserts new.

Use this when an entity changes which value it points to.

Source

pub fn clear(&mut self)

Clears all mappings.

Source

pub fn len(&self) -> usize

Returns the number of tracked mappings.

Source

pub fn is_empty(&self) -> bool

Returns true if no mappings exist.

Source

pub fn iter(&self) -> impl Iterator<Item = (&V, &usize)>

Returns an iterator over all (value, entity_index) pairs.

Trait Implementations§

Source§

impl<V> Debug for InverseSupply<V>
where V: Eq + Hash + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V> Default for InverseSupply<V>
where V: Eq + Hash,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<V> Freeze for InverseSupply<V>

§

impl<V> RefUnwindSafe for InverseSupply<V>
where V: RefUnwindSafe,

§

impl<V> Send for InverseSupply<V>
where V: Send,

§

impl<V> Sync for InverseSupply<V>
where V: Sync,

§

impl<V> Unpin for InverseSupply<V>
where V: Unpin,

§

impl<V> UnwindSafe for InverseSupply<V>
where V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.