Skip to content

Commit

Permalink
Merge pull request #27 from orxfun/fill-with-variants-to-initialize-m…
Browse files Browse the repository at this point in the history
…emory

fill-with variants to initialize memory on growth
  • Loading branch information
orxfun committed Aug 12, 2024
2 parents 89bf9cd + f8f870c commit e45c3ad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "orx-pinned-vec"
version = "3.2.0"
version = "3.3.0"
edition = "2021"
authors = ["orxfun <orx.ugur.arikan@gmail.com>"]
description = "`PinnedVec` trait defines the interface for vectors which guarantee that elements added to the vector are pinned to their memory locations unless explicitly changed."
Expand All @@ -10,4 +10,4 @@ keywords = ["vec", "array", "vector", "pinned", "memory"]
categories = ["data-structures", "rust-patterns"]

[dependencies]
orx-pseudo-default = "1.2.0"
orx-pseudo-default = "1.2"
27 changes: 27 additions & 0 deletions src/concurrent_pinned_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub trait ConcurrentPinnedVec<T> {
range: R,
) -> <Self::P as PinnedVec<T>>::SliceMutIter<'_>;

/// Returns an iterator of slices to the elements extending over positions `range` of the vector.
fn slices<R: RangeBounds<usize>>(&self, range: R) -> <Self::P as PinnedVec<T>>::SliceIter<'_>;

// capacity

/// Returns the maximum possible capacity that the vector can concurrently grow to without requiring a `&mut self` reference.
Expand All @@ -86,6 +89,30 @@ pub trait ConcurrentPinnedVec<T> {
/// Then, `grow_to` will succeed.
fn grow_to(&self, new_capacity: usize) -> Result<usize, PinnedVecGrowthError>;

/// Tries to concurrently grow the capacity of the vector to at least `new_capacity`. Returns:
/// * Ok of the new capacity if succeeds
/// * Err otherwise.
///
/// Behavior of this method is deterministic.
/// The method always succeeds (fails) if `new_capacity <= self.max_capacity()` (otherwise).
///
/// If the method returns an error, `reserve_maximum_concurrent_capacity` method can be used;
/// however, with a `&mut self` reference.
/// Then, `grow_to` will succeed.
///
/// During growth:
///
/// * length of the vector is increased to its new capacity;
/// * the elements in the range `len..capacity` are filled with the values
/// obtained by repeatedly calling the function `fill_with`.
fn grow_to_and_fill_with<F>(
&self,
new_capacity: usize,
fill_with: F,
) -> Result<usize, PinnedVecGrowthError>
where
F: Fn() -> T;

/// Increases the `maximum_capacity` to the `new_maximum_capacity`.
///
/// # Safety
Expand Down
10 changes: 10 additions & 0 deletions src/into_concurrent_pinned_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ pub trait IntoConcurrentPinnedVec<T>: PinnedVec<T> {

/// Converts the pinned vector into its concurrent wrapper.
fn into_concurrent(self) -> Self::ConPinnedVec;

/// Converts the pinned vector into its concurrent wrapper.
/// During conversion:
///
/// * length of the vector is increased to its capacity;
/// * the elements in the range `len..capacity` are filled with the values
/// obtained by repeatedly calling the function `fill_with`.
fn into_concurrent_filled_with<F>(self, fill_with: F) -> Self::ConPinnedVec
where
F: Fn() -> T;
}

0 comments on commit e45c3ad

Please sign in to comment.