Struct SetList

Source
pub struct SetList<K, V, S> { /* private fields */ }
Expand description

A list containg unique key-value pairs.

This is a list, but all items are laid on a single sequential memory block. Therefore, we can expect more speed in terms of iteration than standard linked list, LinkedList, but it requires more memory footprint.

§NOTE

Current implementation doesn’t concern about ZST.

Implementations§

Source§

impl<K, V, S> SetList<K, V, S>
where S: BuildHasher + Default,

Source

pub fn new(dummy: V) -> Self

Creates a new empty list.

SetList requires dummy head node for now, so you can put in any values.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let list = SetList::<char, &'static str, RandomState>::new("");
Source§

impl<K, V, S> SetList<K, V, S>

Source

pub fn len(&self) -> usize

Returns number of items.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
assert_eq!(list.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the list is empty.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<char, &'static str, RandomState>::default();
assert!(list.is_empty());
Source

pub fn front(&self) -> Option<&V>

Returns a shared reference to the front item.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");
assert_eq!(list.front(), Some(&"alpha"));
Source

pub fn front_mut(&mut self) -> Option<&mut V>

Returns a mutable reference to the front item.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");
assert_eq!(list.front_mut(), Some(&mut "alpha"));
Source

pub fn back(&self) -> Option<&V>

Returns a shared reference to the last item.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");
assert_eq!(list.back(), Some(&"beta"));
Source

pub fn back_mut(&mut self) -> Option<&mut V>

Returns a mutable reference to the last item.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");
assert_eq!(list.back_mut(), Some(&mut "beta"));
Source

pub fn values(&self) -> Values<'_, V, S>

Returns an iterator visiting all values in order.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");

for v in list.values() {
    println!("{v}"); // Prints out "alpha" and "beta".
}
Source

pub fn values_mut(&mut self) -> ValuesMut<'_, V, S>

Returns a mutable iterator visiting all values in order.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha".to_owned());
list.push_back('b', "beta".to_owned());

for v in list.values_mut() {
    v.push('*');
    println!("{v}"); // Prints out "alpha*" and "beta*".
}
Source

pub fn values_from(&self, cur: ListPos) -> Values<'_, V, S>

Returns an iterator visiting values from the given position.

§Panics

Panics if the position is not valid one for the list.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");
list.push_back('g', "gamma");

let pos = list.get_position(&'b').unwrap();
for v in list.values_from(pos) {
    println!("{v}"); // Prints out "beta" and "gamma".
}
Source

pub fn values_mut_from(&mut self, cur: ListPos) -> ValuesMut<'_, V, S>

Returns a mutable iterator visiting values from the given position.

§Panics

Panics if the position is not valid one for the list.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha".to_owned());
list.push_back('b', "beta".to_owned());
list.push_back('g', "gamma".to_owned());

let pos = list.get_position(&'b').unwrap();
for v in list.values_mut_from(pos) {
    v.push('*');
    println!("{v}"); // Prints out "beta*" and "gamma*".
}
Source

pub fn into_values(self) -> IntoValues<V, S>

Creates an iterator visiting all values in order by consuming the list.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");

for v in list.into_values() {
    println!("{v}");
}
Source

pub fn into_values_from(self, cur: ListPos) -> IntoValues<V, S>

Creates an iterator visiting values from the given position by consuming the list.

§Panics

Panics if the position is not valid one for the list.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");
list.push_back('g', "gamma");

let pos = list.get_position(&'b').unwrap();
for v in list.into_values_from(pos) {
    println!("{v}"); // Prints out "beta" and "gamma".
}
Source

pub fn iter_next(&self, cur: ListPos) -> Option<(ListPos, &V)>

Returns a shared reference to a value at the given position with the next position.

If the given position is not valid one for the list, returns None.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");

let pos = list.get_position(&'b').unwrap();
let (_, v) = list.iter_next(pos).unwrap();
assert_eq!(v, &"beta");
Source

pub fn iter_next_mut(&mut self, cur: ListPos) -> Option<(ListPos, &mut V)>

Returns a mutable reference to a value at the given position with the next position.

If the given position is not valid one for the list, returns None.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");

let pos = list.get_position(&'b').unwrap();
let (_, v) = list.iter_next_mut(pos).unwrap();
assert_eq!(v, &mut "beta");
Source

pub fn first_position(&self) -> ListPos

Returns the first position of the list.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('b', "beta");

let pos = list.first_position();
let (_, v) = list.iter_next(pos).unwrap();
assert_eq!(v, &"alpha");
Source§

impl<K, V, S> SetList<K, V, S>
where K: Hash + Eq, S: BuildHasher,

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if the vector contains the given key.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
assert!(list.contains_key(&'a'));
Source

pub fn get_position<Q>(&self, key: &Q) -> Option<ListPos>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieves a position of a value corresponding to the given key.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
let pos = list.get_position(&'a').unwrap();
Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieves a shared reference to a value corresponding to the given key.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
assert_eq!(list.get(&'a'), Some(&"alpha"));
Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieves a mutable reference to a value corresponding to the given key.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
assert_eq!(list.get_mut(&'a'), Some(&mut "alpha"));
Source

pub fn push_back(&mut self, key: K, value: V) -> bool

Appends the given key-value pair to the end of the list.

However, if the list already contains the key, nothing takes place and returns false.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
assert!(!list.push_back('a', "beta"));
assert_eq!(list.back(), Some(&"alpha"));
Source

pub fn push_front(&mut self, key: K, value: V) -> bool

Inserts the given key-value pair to the beginning of the list.

However, if the list already contains the key, nothing takes place and returns false.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_front('a', "alpha");
assert!(!list.push_front('a', "beta"));
assert_eq!(list.front(), Some(&"alpha"));
Source

pub fn insert<Q>(&mut self, key: K, value: V, after: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Inserts the given key-value pair after a node corresponding to after.

However, if the list already contains the key or doesn’t contain after, nothing takes place and returns false.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
list.push_back('g', "gamma");
list.insert('b', "beta", &'a');
Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes an item corresponding to the given key.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
assert_eq!(list.remove(&'a'), Some("alpha"));
Source

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes an item corresponding to the given key then resturns key-value pair.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let mut list = SetList::<_, _, RandomState>::default();
list.push_back('a', "alpha");
assert_eq!(list.remove_entry(&'a'), Some(('a', "alpha")));
Source§

impl<K, V, S> SetList<K, V, S>
where V: Clone, S: BuildHasher,

Source

pub fn values_as_vec(&self) -> Vec<V>

Creates Vec from this list.

Trait Implementations§

Source§

impl<K, V, S> Clone for SetList<K, V, S>
where K: Clone, V: Clone, S: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug, S: Debug> Debug for SetList<K, V, S>

Source§

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

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

impl<K, V, S> Default for SetList<K, V, S>
where V: Default, S: BuildHasher + Default,

Source§

fn default() -> Self

Creates a new empty list.

§Examples
use my_ecs::ds::SetList;
use std::hash::RandomState;

let list = SetList::<char, String, RandomState>::default();
Source§

impl<K, V, S> Display for SetList<K, V, S>
where V: Display, S: BuildHasher,

Source§

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

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

impl<K, V, S> From<&[(K, V)]> for SetList<K, V, S>
where K: Hash + Eq + Clone, V: Default + Clone, S: BuildHasher + Default,

Source§

fn from(value: &[(K, V)]) -> Self

Converts to this type from the input type.
Source§

impl<K, V, S> IntoIterator for SetList<K, V, S>
where S: BuildHasher,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V, S>

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<K, V, S> Resource for SetList<K, V, S>
where K: Send + 'static, V: Send + 'static, S: Send + 'static,

Auto Trait Implementations§

§

impl<K, V, S> Freeze for SetList<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for SetList<K, V, S>

§

impl<K, V, S> Send for SetList<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for SetList<K, V, S>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S> Unpin for SetList<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnwindSafe for SetList<K, V, S>
where K: UnwindSafe, S: UnwindSafe, 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.