Mapping

Trait Mapping 

Source
pub trait Mapping<K, V>: IndexMut<K, Output = V> + IntoIterator<Item = V> {
    // Required methods
    fn map<VV>(self, f: impl FnMut(V) -> VV) -> impl Mapping<K, VV>;
    fn iter<'a>(&'a self) -> impl Iterator<Item = &'a V>
       where V: 'a;
    fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut V>
       where V: 'a;
    unsafe fn get_unchecked(&self, key: K) -> &V;
    unsafe fn get_unchecked_mut(&mut self, key: K) -> &mut V;
}
Expand description

A trait for associative containers that map keys to values.

This trait provides a common interface for data structures that associate keys with values, similar to hash maps or arrays. It’s used extensively throughout the library for creating mappings from node/edge indices to computed values.

§Type Parameters

  • K: The key type (typically node or edge indices)
  • V: The value type

§Examples

use gotgraph::prelude::*;

let mut graph: VecGraph<i32, &str> = VecGraph::default();
graph.scope_mut(|mut ctx| {
    ctx.add_node(10);
    ctx.add_node(20);
});

graph.scope(|ctx| {
    // Create a mapping that doubles each node's value
    let doubled = ctx.init_node_map(|_tag, &value| value * 2);
     
    for node_tag in ctx.node_indices() {
        println!("Doubled: {}", doubled[node_tag]);
    }
});

Required Methods§

Source

fn map<VV>(self, f: impl FnMut(V) -> VV) -> impl Mapping<K, VV>

Transform this mapping by applying a function to each value.

Creates a new mapping with the same keys but transformed values.

§Parameters
  • f: A function that transforms each value from type V to type VV
§Returns

A new mapping with transformed values.

Source

fn iter<'a>(&'a self) -> impl Iterator<Item = &'a V>
where V: 'a,

Returns an iterator over references to the values in this mapping.

The order of iteration is implementation-defined.

Source

fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut V>
where V: 'a,

Returns an iterator over mutable references to the values in this mapping.

The order of iteration is implementation-defined.

Source

unsafe fn get_unchecked(&self, key: K) -> &V

Gets a reference to the value associated with the given key without bounds checking.

§Safety

The caller must ensure that the key exists in the mapping. Calling this method with a non-existent key results in undefined behavior.

§Parameters
  • key: The key to look up
§Returns

A reference to the value associated with the key.

Source

unsafe fn get_unchecked_mut(&mut self, key: K) -> &mut V

Gets a mutable reference to the value associated with the given key without bounds checking.

§Safety

The caller must ensure that the key exists in the mapping. Calling this method with a non-existent key results in undefined behavior.

§Parameters
  • key: The key to look up
§Returns

A mutable reference to the value associated with the key.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'scope, K, V, M: Mapping<K, V>> Mapping<EdgeTag<'scope, K>, V> for ContextEdgeMap<'scope, K, V, M>

Source§

impl<'scope, K, V, M: Mapping<K, V>> Mapping<NodeTag<'scope, K>, V> for ContextNodeMap<'scope, K, V, M>