XArray
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
- Cursors: Provide cursors for precise and efficient iteration over the array. Cursors have both immutable and mutable versions. One can hold multiple immutable cursors or hold a mutable cursor exclusively at a time.
- Marking: Provide ability to mark entries and the XArray itself for easy state tracking.
- Generics: Generic implementation that can work with any entry type that fits the use case.
- Copy-on-Write (COW): Efficient cloning of XArrays with shared structure until mutation.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
This crate is developed in no_std environment, but std users can still use this crate with --feature="std":
The following section covers how to interact with XArray including creating an XArray, using cursors, marking, cloning, and more.
Creating an XArray:
// In std environment
extern crate alloc;
use Arc;
use XArray;
// Create a new XArray instance
let mut xarray: = new;
- Users should declare the type of items (Arc) stored in the XArray, and the item type should implement
ItemEntrytrait. - We implement
ItemEntryforalloc::sync::Arcandalloc::sync::Boxby default, hence std users can use them directly.
Using Cursor
extern crate alloc;
use Arc;
use XArray;
let mut xarray_arc: = new;
let mut cursor = xarray_arc.cursor_mut;
// Store the Arc at the index range 0~10000.
for i in 0..10000
cursor.reset_to;
for i in 0..10000
Using Marks
Here is an example of using marks for the stored pages in the XArray, where PageMark represents the states of each individual Page:
extern crate alloc;
use Arc;
use ;
let mut pages: = new;
let mut cursor = pages.cursor_mut;
cursor.store;
// Mark the Page as DirtyPage.
cursor.set_mark.unwrap;
assert!;
- Items and the
XArraycan have up to three distinct marks by default, with each mark independently maintained. - Users need to use a struct to represent the marks that need to be used. For the situation where multiple marks are required, these marks are typically encapsulated within an enumeration class.
- If users want to use a struct
Mfor marks, they should implementFrom<M>trait forXMarkand declareMin the generics list of XArray.
Copy-On-Write (COW) Clone
use Arc;
use ;
let mut xarray: = new;
// Store values
let value = new;
xarray.store;
assert_eq!;
// Clone the XArray
let mut xarray_clone = xarray.clone;
assert_eq!;
// Store a new value in the clone
let new_value = new;
xarray_clone.store;
// The original XArray is unaffected by changes in the clone
assert_eq!;
assert_eq!;
Iteration
use Arc;
use XArray;
let mut xarray: = new;
// Store item to even index in the range 100~200.
for i in 100..200
// Iterate at the range 100~200.
let mut count = 0;
for item in xarray.range
assert_eq!;
License
XArray primarily use the Mozilla Public License (MPL), Version 2.0.