Struct xarray::XArray

source ·
pub struct XArray<I, M = NoneMark>
where I: ItemEntry, M: Into<XMark>,
{ /* private fields */ }
Expand description

XArray is an abstract data type functioning like an expansive array of items where each item must be an 8-byte object, such as Arc<T> or Box<T>.

User-stored pointers must have a minimum alignment of 4 bytes. XArray facilitates efficient sequential access to adjacent entries, supporting multiple concurrent reads and exclusively allowing one write operation at a time.

§Features

Copy-on-write (COW): If items within XArray implement the Clone trait, cloning can leverage a COW mechanism. A clone of an XArray initially shares the head node with the original, avoiding immediate deep copying. If a mutable operation is required on either XArray, a deep copy of the relevant nodes is made first, ensuring isolated operations.

Cursors: Interaction with XArray is mediated through Cursor and CursorMut. A Cursor requires an immutable reference, while CursorMut requires a mutable reference. As such, multiple Cursor instances can coexist, but CursorMut operations are singular, reflecting the behavior of shared (&) and exclusive (&mut) references. Cursors offer precise index positioning and traversal capabilities in the XArray.

Marking: XArray enables marking of individual items or the XArray itself for user convenience. Items and the XArray can have up to three distinct marks by default, with each mark independently maintained. Users can use self-defined types as marks by implementing the From<Type> trait for XMark. Marking is also applicable to internal nodes, indicating marked descendant nodes, though such marking is not transparent to users.

§Example

use std::sync::Arc;
use std::sync::{Mutex, MutexGuard};
use xarray::*;

let mut xarray_arc: XArray<Arc<i32>> = XArray::new();
let value = Arc::new(10);
xarray_arc.store(10, value);
assert!(*xarray_arc.load(10).unwrap().as_ref() == 10);

let mut xarray_clone = xarray_arc.clone();
assert!(*xarray_clone.load(10).unwrap().as_ref() == 10);
let value = Arc::new(100);
xarray_clone.store(10, value);

assert!(*xarray_arc.load(10).unwrap().as_ref() == 10);
assert!(*xarray_clone.load(10).unwrap().as_ref() == 100);

The XArray concept was originally introduced by Linux, which keeps the data structure of Linux Radix Trees.

Implementations§

source§

impl<I: ItemEntry, M: Into<XMark>> XArray<I, M>

source

pub const fn new() -> Self

Makes a new, empty XArray.

source

pub fn set_mark(&mut self, mark: M)

Marks the XArray with the input mark.

source

pub fn unset_mark(&mut self, mark: M)

Unsets the input mark for the XArray.

source

pub fn is_marked(&self, mark: M) -> bool

Checks whether the XArray is marked with the input mark.

source

pub fn load(&self, index: u64) -> Option<I::Ref<'_>>

Loads the index-th item.

If the target item exists, it will be returned with Some(_), otherwise, None will be returned.

source

pub fn store(&mut self, index: u64, item: I) -> Option<I>

Stores the provided item in the XArray at the target index, and returns the old item if some item was previously stored in the same position.

source

pub fn unset_mark_all(&mut self, mark: M)

Unsets the input mark for all of the items in the XArray.

source

pub fn remove(&mut self, index: u64) -> Option<I>

Removes the XEntry in the XArray at the target index, and returns the removed item if some item was previously stored in the same position.

source

pub fn cursor(&self, index: u64) -> Cursor<'_, I, M>

Creates a Cursor to perform read-related operations in the XArray.

source

pub fn cursor_mut(&mut self, index: u64) -> CursorMut<'_, I, M>

Creates a CursorMut to perform read- and write-related operations in the XArray.

source

pub fn range(&self, range: Range<u64>) -> Range<'_, I, M>

Creates a Range which can be immutably iterated over the indexes corresponding to the specified range.

Trait Implementations§

source§

impl<I: ItemEntry + Clone, M: Into<XMark>> Clone for XArray<I, M>

source§

fn clone(&self) -> Self

Clones the XArray with the COW mechanism.

1.0.0§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<I, M> Freeze for XArray<I, M>

§

impl<I, M> RefUnwindSafe for XArray<I, M>
where M: RefUnwindSafe, I: RefUnwindSafe,

§

impl<I, M> Send for XArray<I, M>
where M: Send, I: Send + Sync,

§

impl<I, M> Sync for XArray<I, M>
where M: Sync, I: Sync + Send,

§

impl<I, M> Unpin for XArray<I, M>
where M: Unpin, I: Unpin,

§

impl<I, M> UnwindSafe for XArray<I, M>
where M: UnwindSafe, I: UnwindSafe + RefUnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

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

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

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

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

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.