Skip to main content

NumberKeyMap

Struct NumberKeyMap 

Source
pub struct NumberKeyMap<V> { /* private fields */ }
Expand description

A small, specialized hash map keyed by usize values.

It is optimized for zero-misses and so optimized for 99+% reading operations.

It is optimized for integer keys and expects an external invariant that usize::MAX marks vacant slots.

§Example

use std::sync::{Mutex, RwLock};
use orengine_utils::NumberKeyMap;

static POOLS: RwLock<NumberKeyMap<Mutex<Vec<Box<[u8]>>>>> = RwLock::new(NumberKeyMap::new());

fn acquire_from_pool(size: usize) -> Box<[u8]> {
    if let Some(v) = POOLS.read().unwrap().get(size) {
        v.lock().unwrap().pop().unwrap_or_else(|| vec![0; size].into_boxed_slice())
    } else {
        vec![0; size].into_boxed_slice()
    }
}

fn put_to_pool(buf: Box<[u8]>) {
    let read_pool = POOLS.read().unwrap();

    if let Some(v) = read_pool.get(buf.len()) {
        v.lock().unwrap().push(buf);
        return;
    }

    drop(read_pool);

    let mut write_pool = POOLS.write().unwrap();
    let res = write_pool.insert(buf.len(), Mutex::new(vec![buf]));

    if let Err(mut v) = res {
        let buf = v.get_mut().unwrap().pop().unwrap();

        write_pool.get(buf.len()).unwrap().lock().unwrap().push(buf);
    }
}

Implementations§

Source§

impl<V> NumberKeyMap<V>

Source

pub const fn new() -> Self

Create an empty NumberKeyMap.

Source

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

Retrieve a reference to a value stored under key, if present.

If the slot is occupied and contains the requested key, a reference to the value is returned.

§Panics

This function panics if key is equal to usize::MAX.

Source

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

Retrieve a mutable reference to a value stored under key, if present.

If the slot is occupied and contains the requested key, a reference to the value is returned.

§Panics

This function panics if key is equal to usize::MAX.

Source

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

Insert a key/value pair into the map.

§Note

This operation is very expensive! If you want to call it frequently, consider using a HashMap instead.

§Errors

Returns Err(V) if the key already exists in the map; otherwise returns Ok(()).

§Panics

This function panics if key is equal to usize::MAX

Source

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

Removes an item from the map and returns it if it exists.

§Panics

This function panics if key is equal to usize::MAX

Source

pub fn clear_with(&mut self, func: impl Fn((usize, V)))

Clears the NumberKeyMap with the provided function.

Source

pub fn clear(&mut self)

Clears the NumberKeyMap.

Source§

impl<V> NumberKeyMap<V>

Source

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

Iterate immutably over all (key, &value).

Source

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

Iterate mutably over all (key, &mut value).

Source§

impl<V: 'static> NumberKeyMap<V>

Source

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

Remove all entries and yield owned (key, value).

Trait Implementations§

Source§

impl<V> Default for NumberKeyMap<V>

Source§

fn default() -> Self

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

impl<V> Drop for NumberKeyMap<V>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<V> IntoIterator for NumberKeyMap<V>

Source§

type Item = (usize, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<V> Send for NumberKeyMap<V>

Source§

impl<V> Sync for NumberKeyMap<V>

Auto Trait Implementations§

§

impl<V> Freeze for NumberKeyMap<V>

§

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

§

impl<V> Unpin for NumberKeyMap<V>

§

impl<V> UnwindSafe for NumberKeyMap<V>
where V: RefUnwindSafe,

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.