Skip to content

Commit

Permalink
Add basic range mut (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firstyear authored Jan 10, 2024
1 parent 88b4964 commit f7790d8
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 49 deletions.
15 changes: 12 additions & 3 deletions src/bptree/impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::internals::bptree::cursor::CursorReadOps;
use crate::internals::bptree::cursor::{CursorRead, CursorWrite, SuperBlock};
use crate::internals::bptree::iter::{Iter, RangeIter, KeyIter, ValueIter};
use crate::internals::bptree::mutiter::RangeMutIter;
use crate::internals::lincowcell::LinCowCellCapable;
use std::borrow::Borrow;
use std::fmt::Debug;
Expand Down Expand Up @@ -277,12 +278,20 @@ impl<K: Clone + Ord + Debug + Sync + Send + 'static, V: Clone + Sync + Send + 's
self.inner.as_mut().get_mut_ref(key)
}

// range_mut

// entry
/// Iterate over a mutable range of values
pub fn range_mut<R, T>(&mut self, range: R) -> RangeMutIter<K, V>
where
K: Borrow<T>,
T: Ord + ?Sized,
R: RangeBounds<T>,
{
self.inner.as_mut().range_mut(range)
}

// iter_mut

// entry

#[cfg(test)]
pub(crate) fn tree_density(&self) -> (usize, usize) {
self.inner.as_ref().tree_density()
Expand Down
6 changes: 2 additions & 4 deletions src/hashmap/asynch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Sen
}
}

impl<
K: Hash + Eq + Clone + Debug + Sync + Send + 'static,
V: Clone + Sync + Send + 'static,
> HashMapWriteTxn<'_, K, V>
impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Send + 'static>
HashMapWriteTxn<'_, K, V>
{
/// Commit the changes from this write transaction. Readers after this point
/// will be able to percieve these changes.
Expand Down
6 changes: 2 additions & 4 deletions src/hashmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Sen
}
}

impl<
K: Hash + Eq + Clone + Debug + Sync + Send + 'static,
V: Clone + Sync + Send + 'static,
> HashMapWriteTxn<'_, K, V>
impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Send + 'static>
HashMapWriteTxn<'_, K, V>
{
/*
pub(crate) fn get_txid(&self) -> u64 {
Expand Down
6 changes: 2 additions & 4 deletions src/hashtrie/asynch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Sen
}
}

impl<
K: Hash + Eq + Clone + Debug + Sync + Send + 'static,
V: Clone + Sync + Send + 'static,
> HashTrieWriteTxn<'_, K, V>
impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Send + 'static>
HashTrieWriteTxn<'_, K, V>
{
/// Commit the changes from this write transaction. Readers after this point
/// will be able to percieve these changes.
Expand Down
12 changes: 4 additions & 8 deletions src/hashtrie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Sen
}
}

impl<
K: Hash + Eq + Clone + Debug + Sync + Send + 'static,
V: Clone + Sync + Send + 'static,
> HashTrieWriteTxn<'_, K, V>
impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Send + 'static>
HashTrieWriteTxn<'_, K, V>
{
#[cfg(feature = "arcache")]
pub(crate) fn get_txid(&self) -> u64 {
Expand Down Expand Up @@ -105,10 +103,8 @@ impl<
}
}

impl<
K: Hash + Eq + Clone + Debug + Sync + Send + 'static,
V: Clone + Sync + Send + 'static,
> HashTrieReadTxn<'_, K, V>
impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Send + 'static>
HashTrieReadTxn<'_, K, V>
{
#[cfg(feature = "arcache")]
pub(crate) fn get_txid(&self) -> u64 {
Expand Down
18 changes: 14 additions & 4 deletions src/internals/bptree/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt::Debug;
use std::mem;

use super::iter::{Iter, KeyIter, RangeIter, ValueIter};
use super::mutiter::RangeMutIter;
use super::states::*;
use std::iter::Extend;
use std::ops::RangeBounds;
Expand Down Expand Up @@ -250,7 +251,7 @@ pub(crate) trait CursorReadOps<K: Clone + Ord + Debug, V: Clone> {
panic!("Tree depth exceeded max limit (65536). This may indicate memory corruption.");
}

fn range<R, T>(&self, range: R) -> RangeIter<K, V>
fn range<'n, R, T>(&'n self, range: R) -> RangeIter<'n, '_, K, V>
where
K: Borrow<T>,
T: Ord + ?Sized,
Expand All @@ -259,15 +260,15 @@ pub(crate) trait CursorReadOps<K: Clone + Ord + Debug, V: Clone> {
RangeIter::new(self.get_root(), range, self.len())
}

fn kv_iter(&self) -> Iter<K, V> {
fn kv_iter<'n>(&'n self) -> Iter<'n, '_, K, V> {
Iter::new(self.get_root(), self.len())
}

fn k_iter(&self) -> KeyIter<K, V> {
fn k_iter<'n>(&'n self) -> KeyIter<'n, '_, K, V> {
KeyIter::new(self.get_root(), self.len())
}

fn v_iter(&self) -> ValueIter<K, V> {
fn v_iter<'n>(&'n self) -> ValueIter<'n, '_, K, V> {
ValueIter::new(self.get_root(), self.len())
}

Expand Down Expand Up @@ -556,6 +557,15 @@ impl<K: Clone + Ord + Debug, V: Clone> CursorWrite<K, V> {
pub(crate) fn tree_density(&self) -> (usize, usize) {
self.get_root_ref().tree_density()
}

pub(crate) fn range_mut<'n, R, T>(&'n mut self, range: R) -> RangeMutIter<'n, '_, K, V>
where
K: Borrow<T>,
T: Ord + ?Sized,
R: RangeBounds<T>,
{
RangeMutIter::new(self, range)
}
}

impl<K: Clone + Ord + Debug, V: Clone> Extend<(K, V)> for CursorWrite<K, V> {
Expand Down
45 changes: 24 additions & 21 deletions src/internals/bptree/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,23 +308,23 @@ impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for RevLeafIter<'a, K, V> {
// Wrappers

/// Iterator over references to Key Value pairs stored in the map.
pub struct Iter<'a, K, V>
pub struct Iter<'n, 'a, K, V>
where
K: Ord + Clone + Debug,
V: Clone,
{
iter: RangeIter<'a, K, V>,
iter: RangeIter<'n, 'a, K, V>,
}

impl<K: Clone + Ord + Debug, V: Clone> Iter<'_, K, V> {
impl<K: Clone + Ord + Debug, V: Clone> Iter<'_, '_, K, V> {
pub(crate) fn new(root: *mut Node<K, V>, length: usize) -> Self {
let bounds: (Bound<K>, Bound<K>) = (Bound::Unbounded, Bound::Unbounded);
let iter = RangeIter::new(root, bounds, length);
Iter { iter }
}
}

impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for Iter<'a, K, V> {
impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for Iter<'_, 'a, K, V> {
type Item = (&'a K, &'a V);

/// Yield the next key value reference, or `None` if exhausted.
Expand All @@ -342,31 +342,31 @@ impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for Iter<'a, K, V> {
}
}

impl<K: Clone + Ord + Debug, V: Clone> DoubleEndedIterator for Iter<'_, K, V> {
impl<K: Clone + Ord + Debug, V: Clone> DoubleEndedIterator for Iter<'_, '_, K, V> {
/// Yield the next key value reference, or `None` if exhausted.
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}

/// Iterater over references to Keys stored in the map.
pub struct KeyIter<'a, K, V>
pub struct KeyIter<'n, 'a, K, V>
where
K: Ord + Clone + Debug,
V: Clone,
{
iter: Iter<'a, K, V>,
iter: Iter<'n, 'a, K, V>,
}

impl<K: Clone + Ord + Debug, V: Clone> KeyIter<'_, K, V> {
pub(crate) fn new(root: *mut Node<K, V>, length: usize) -> Self {
impl<K: Clone + Ord + Debug, V: Clone> KeyIter<'_, '_, K, V> {
pub(crate) fn new<'n>(root: *mut Node<K, V>, length: usize) -> Self {
KeyIter {
iter: Iter::new(root, length),
}
}
}

impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for KeyIter<'a, K, V> {
impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for KeyIter<'_, 'a, K, V> {
type Item = &'a K;

/// Yield the next key value reference, or `None` if exhausted.
Expand All @@ -379,31 +379,31 @@ impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for KeyIter<'a, K, V> {
}
}

impl<K: Clone + Ord + Debug, V: Clone> DoubleEndedIterator for KeyIter<'_, K, V> {
impl<K: Clone + Ord + Debug, V: Clone> DoubleEndedIterator for KeyIter<'_, '_, K, V> {
/// Yield the next key value reference, or `None` if exhausted.
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|(k, _)| k)
}
}

/// Iterater over references to Values stored in the map.
pub struct ValueIter<'a, K, V>
pub struct ValueIter<'n, 'a, K, V>
where
K: Ord + Clone + Debug,
V: Clone,
{
iter: Iter<'a, K, V>,
iter: Iter<'n, 'a, K, V>,
}

impl<K: Clone + Ord + Debug, V: Clone> ValueIter<'_, K, V> {
impl<K: Clone + Ord + Debug, V: Clone> ValueIter<'_, '_, K, V> {
pub(crate) fn new(root: *mut Node<K, V>, length: usize) -> Self {
ValueIter {
iter: Iter::new(root, length),
}
}
}

impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for ValueIter<'a, K, V> {
impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for ValueIter<'_, 'a, K, V> {
type Item = &'a V;

/// Yield the next key value reference, or `None` if exhausted.
Expand All @@ -416,15 +416,15 @@ impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for ValueIter<'a, K, V> {
}
}

impl<K: Clone + Ord + Debug, V: Clone> DoubleEndedIterator for ValueIter<'_, K, V> {
impl<K: Clone + Ord + Debug, V: Clone> DoubleEndedIterator for ValueIter<'_, '_, K, V> {
/// Yield the next key value reference, or `None` if exhausted.
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|(_, v)| v)
}
}

/// Iterator over references to Key Value pairs stored, bounded by a range.
pub struct RangeIter<'a, K, V>
pub struct RangeIter<'n, 'a, K, V>
where
K: Ord + Clone + Debug,
V: Clone,
Expand All @@ -434,14 +434,15 @@ where
right_iter: RevLeafIter<'a, K, V>,
phantom_k: PhantomData<&'a K>,
phantom_v: PhantomData<&'a V>,
phantom_root: PhantomData<&'n ()>,
}

impl<K, V> RangeIter<'_, K, V>
impl<K, V> RangeIter<'_, '_, K, V>
where
K: Clone + Ord + Debug,
V: Clone,
{
pub(crate) fn new<R, T>(root: *mut Node<K, V>, range: R, length: usize) -> Self
pub(crate) fn new<'n, R, T>(root: *mut Node<K, V>, range: R, length: usize) -> Self
where
T: Ord + ?Sized,
K: Borrow<T>,
Expand Down Expand Up @@ -566,6 +567,7 @@ where
right_iter,
phantom_k: PhantomData,
phantom_v: PhantomData,
phantom_root: PhantomData,
}
}

Expand All @@ -577,11 +579,12 @@ where
right_iter: RevLeafIter::new_base(),
phantom_k: PhantomData,
phantom_v: PhantomData,
phantom_root: PhantomData,
}
}
}

impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for RangeIter<'a, K, V> {
impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for RangeIter<'_, 'a, K, V> {
type Item = (&'a K, &'a V);

/// Yield the next key value reference, or `None` if exhausted.
Expand Down Expand Up @@ -628,7 +631,7 @@ impl<'a, K: Clone + Ord + Debug, V: Clone> Iterator for RangeIter<'a, K, V> {
}
}

impl<K: Clone + Ord + Debug, V: Clone> DoubleEndedIterator for RangeIter<'_, K, V> {
impl<K: Clone + Ord + Debug, V: Clone> DoubleEndedIterator for RangeIter<'_, '_, K, V> {
/// Yield the next key value reference, or `None` if exhausted.
fn next_back(&mut self) -> Option<Self::Item> {
loop {
Expand Down
1 change: 1 addition & 0 deletions src/internals/bptree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
pub(crate) mod macros;
pub(crate) mod cursor;
pub mod iter;
pub mod mutiter;
pub(crate) mod node;
pub(crate) mod states;
Loading

0 comments on commit f7790d8

Please sign in to comment.