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>
impl<T, const ARRAY_LEN: usize> Bag<T, ARRAY_LEN>
Sourcepub fn push(&self, val: T)
pub fn push(&self, val: T)
Pushes an instance of T.
§Examples
use sdd::Bag;
let bag: Bag<usize> = Bag::default();
bag.push(11);Sourcepub fn pop_all<B, F>(&self, init: B, fold: F) -> Bwhere
F: FnMut(B, T) -> B,
pub fn pop_all<B, F>(&self, init: B, fold: F) -> Bwhere
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());Sourcepub fn len(&self) -> usize
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);Sourcepub const fn iter_mut(&mut self) -> IterMut<'_, T, ARRAY_LEN> ⓘ
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> FromIterator<T> for Bag<T, ARRAY_LEN>
impl<T, const ARRAY_LEN: usize> FromIterator<T> for Bag<T, ARRAY_LEN>
Source§impl<'b, T, const ARRAY_LEN: usize> IntoIterator for &'b mut Bag<T, ARRAY_LEN>
impl<'b, T, const ARRAY_LEN: usize> IntoIterator for &'b mut Bag<T, ARRAY_LEN>
Source§impl<T, const ARRAY_LEN: usize> IntoIterator for Bag<T, ARRAY_LEN>
impl<T, const ARRAY_LEN: usize> IntoIterator for Bag<T, ARRAY_LEN>
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>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more