Struct jupiter::ig::symbols::SymbolMap

source ·
pub struct SymbolMap<V> { /* private fields */ }
Expand description

Provides a efficient lookup map using Symbol as key.

This map is optimized as we know for sure, that the key is an i32. Also, we will use this map for objects within Node and therefore know that most commonly there will be only a few keys (most probably < 8 and almost always < 32).

Therefore we simply use a Vec of tuples to store the keys and their value. We also keep this vector sorted by the key so that a lookup can be done via binary_search.

Using this approach easily beats HashMap in both, performance and memory consumption.

Implementations§

source§

impl<V: Default> SymbolMap<V>

source

pub fn new() -> Self

Creates a new and empty map.

§Examples
let mut map = SymbolMap::<i32>::new();
assert_eq!(map.len(), 0);
source

pub fn get(&self, key: Symbol) -> Option<&V>

Retrieves the value for the given key.

§Examples
let mut map = SymbolMap::<i32>::new();

map.put(42, 1);

assert_eq!(map.get(42).unwrap(), &1);
assert_eq!(map.get(99), None);
source

pub fn get_or_insert( &mut self, key: Symbol, factory: impl FnMut() -> V ) -> &mut V

Retrieves the value for the given key or creates a new one in none exists.

If a new value is created, Default::default is invoked and a mutable reference is returned.

§Examples
let mut map = SymbolMap::<i32>::new();

map.put(42, 1);

assert_eq!(map.get_or_insert(42, || 1), &1);
assert_eq!(map.get_or_insert(23, || 0), &0);
source

pub fn get_mut(&mut self, key: Symbol) -> Option<&mut V>

Retrieves the value for the given key as mutable reference.

§Examples
let mut map = SymbolMap::<String>::new();

map.put(42, "Hello".to_owned());
map.get_mut(42).unwrap().push_str(" World");

assert_eq!(map.get(42).unwrap().as_str(), "Hello World");
source

pub fn put(&mut self, key: Symbol, value: V)

Puts a value in the for the given key.

§Examples
let mut map = SymbolMap::<String>::new();

map.put(42, "Hello".to_owned());

assert_eq!(map.get(42).unwrap().as_str(), "Hello");
source

pub fn len(&self) -> usize

Counts the number of entries in the map.

§Examples
let mut map = SymbolMap::<String>::new();

// If an entry is put in the map, its size is incremented...
map.put(42, "Hello".to_owned());
assert_eq!(map.len(), 1);
    
// If the entry is overwritten, the size remains the same...
map.put(42, "Hello".to_owned());
assert_eq!(map.len(), 1);

// If another entry is added, its size grows once again...
map.put(99, "World".to_owned());
assert_eq!(map.len(), 2);
source

pub fn is_empty(&self) -> bool

Determines if this map is empty.

§Examples
let mut map = SymbolMap::<String>::new();

assert_eq!(map.is_empty(), true);
map.put(42, "Hello".to_owned());
assert_eq!(map.is_empty(), false);
source

pub fn capacity(&self) -> usize

Reports the capacity currently reserved.

§Examples
let mut map = SymbolMap::<String>::new();

let initial_capacity = map.capacity();

for i in 1..=initial_capacity+1 {
    map.put(i as i32, "Hello".to_owned());
}

assert_eq!(map.capacity() > initial_capacity, true);
source

pub fn entries(&self) -> Iter<'_, (Symbol, V)>

Provides an iterator over all entries in the map.

§Examples
let mut map = SymbolMap::<String>::new();

map.put(42, "Hello".to_owned());
map.put(99, "World".to_owned());

let string = map.entries().map(|(key, value)| format!("{}: {}", key, value)).join(", ");
assert_eq!(string, "42: Hello, 99: World");

Trait Implementations§

source§

impl<V: Debug> Debug for SymbolMap<V>

source§

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

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

impl<V: Default> Default for SymbolMap<V>

source§

fn default() -> SymbolMap<V>

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

impl<V: PartialEq> PartialEq for SymbolMap<V>

source§

fn eq(&self, other: &SymbolMap<V>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<V: Eq> Eq for SymbolMap<V>

source§

impl<V> StructuralPartialEq for SymbolMap<V>

Auto Trait Implementations§

§

impl<V> Freeze for SymbolMap<V>

§

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

§

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

§

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

§

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

§

impl<V> UnwindSafe for SymbolMap<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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more