[go: up one dir, main page]

Bag

Struct Bag 

Source
pub struct Bag<T, const ARRAY_LEN: usize = sdd::::bag::Bag::{constant#0}> { /* private fields */ }
Expand description

Bag is a lock-free concurrent unordered instance container.

Bag is a linearizable concurrent instance container where ARRAY_LEN instances are stored in a fixed-size array, and the rest are managed by its backup container; this makes a Bag especially efficient if the expected number of instances does not exceed ARRAY_LEN.

The maximum value of ARRAY_LEN is limited to usize::BITS / 2 which is the default value.

Implementations§

Source§

impl<T, const ARRAY_LEN: usize> Bag<T, ARRAY_LEN>

Source

pub const fn new() -> Bag<T, ARRAY_LEN>

Creates an empty Bag.

§Examples
use sdd::Bag;

let bag: Bag<usize, 16> = Bag::new();
Source

pub fn push(&self, val: T)

Pushes an instance of T.

§Examples
use sdd::Bag;

let bag: Bag<usize> = Bag::default();

bag.push(11);
Source

pub fn try_push(&self, val: T) -> Result<(), T>

Tries to push an instance of T only if the primary storage is not full.

§Errors

Returns an error containing the supplied value if the primary storage is full.

§Examples
use sdd::Bag;

let bag: Bag<usize, 1> = Bag::new();

assert!(bag.try_push(11).is_ok());
assert_eq!(bag.try_push(17), Err(17));
Source

pub fn pop(&self) -> Option<T>

Pops an instance in the Bag if not empty.

§Examples
use sdd::Bag;

let bag: Bag<usize> = Bag::default();

bag.push(37);

assert_eq!(bag.pop(), Some(37));
assert!(bag.pop().is_none());
Source

pub fn pop_all<B, F>(&self, init: B, fold: F) -> B
where F: FnMut(B, T) -> B,

Pops all the entries at once and folds them into an accumulator.

§Examples
use sdd::Bag;

let bag: Bag<usize> = Bag::default();

bag.push(7);
bag.push(17);
bag.push(37);

assert_eq!(bag.pop_all(0, |a, v| a + v), 61);

bag.push(47);
assert_eq!(bag.pop(), Some(47));
assert!(bag.pop().is_none());
assert!(bag.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of entries in the Bag.

This method iterates over all the entry arrays in the Bag to count the number of entries; therefore, its time complexity is O(N).

§Examples
use sdd::Bag;

let bag: Bag<usize> = Bag::default();
assert_eq!(bag.len(), 0);

bag.push(7);
assert_eq!(bag.len(), 1);

for v in 0..64 {
   bag.push(v);
}
bag.pop();
assert_eq!(bag.len(), 64);
Source

pub fn is_empty(&self) -> bool

Returns true if the Bag is empty.

§Examples
use sdd::Bag;

let bag: Bag<usize> = Bag::default();
assert!(bag.is_empty());

bag.push(7);
assert!(!bag.is_empty());

assert_eq!(bag.pop(), Some(7));
assert!(bag.is_empty());
Source

pub const fn iter_mut(&mut self) -> IterMut<'_, T, ARRAY_LEN>

Returns an iterator over the contained instances for modification.

§Examples
use sdd::Bag;

let mut bag: Bag<usize> = Bag::default();

bag.push(3);
bag.push(3);

assert_eq!(bag.iter_mut().count(), 2);
bag.iter_mut().for_each(|e| { *e += 1; });

assert_eq!(bag.pop(), Some(4));
assert_eq!(bag.pop(), Some(4));
assert!(bag.pop().is_none());

Trait Implementations§

Source§

impl<T, const ARRAY_LEN: usize> Debug for Bag<T, ARRAY_LEN>
where T: Debug,

Source§

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

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

impl<T> Default for Bag<T, sdd::::bag::{impl#1}::{constant#0}>

Source§

fn default() -> Bag<T, sdd::::bag::{impl#1}::{constant#0}>

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

impl<T, const ARRAY_LEN: usize> Drop for Bag<T, ARRAY_LEN>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, const ARRAY_LEN: usize> FromIterator<T> for Bag<T, ARRAY_LEN>

Source§

fn from_iter<I>(iter: I) -> Bag<T, ARRAY_LEN>
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<'b, T, const ARRAY_LEN: usize> IntoIterator for &'b mut Bag<T, ARRAY_LEN>

Source§

type IntoIter = IterMut<'b, T, ARRAY_LEN>

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

type Item = &'b mut T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> <&'b mut Bag<T, ARRAY_LEN> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, const ARRAY_LEN: usize> IntoIterator for Bag<T, ARRAY_LEN>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, ARRAY_LEN>

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

fn into_iter(self) -> <Bag<T, ARRAY_LEN> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T, const ARRAY_LEN: usize = sdd::::bag::Bag::{constant#0}> !Freeze for Bag<T, ARRAY_LEN>

§

impl<T, const ARRAY_LEN: usize = sdd::::bag::Bag::{constant#0}> !RefUnwindSafe for Bag<T, ARRAY_LEN>

§

impl<T, const ARRAY_LEN: usize> Send for Bag<T, ARRAY_LEN>
where T: Send,

§

impl<T, const ARRAY_LEN: usize> Sync for Bag<T, ARRAY_LEN>
where T: Send + Sync,

§

impl<T, const ARRAY_LEN: usize> Unpin for Bag<T, ARRAY_LEN>
where T: Unpin,

§

impl<T, const ARRAY_LEN: usize> UnwindSafe for Bag<T, ARRAY_LEN>
where T: 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.