Skip to content

Commit

Permalink
Use Option<&Cell> as dict root arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Aug 15, 2023
1 parent 1c4e3bd commit b6093b3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
24 changes: 12 additions & 12 deletions src/dict/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl SetMode {
/// Removes the value associated with key in dictionary.
/// Returns a tuple with a new dictionary cell and an optional removed value.
pub fn dict_remove_owned(
root: &Option<Cell>,
root: Option<&Cell>,
key: &mut CellSlice,
key_bit_len: u16,
allow_subtree: bool,
Expand Down Expand Up @@ -193,7 +193,7 @@ pub fn dict_remove_owned(
///
/// Returns a tuple with a new dict root, and changed flag.
pub fn dict_insert(
root: &Option<Cell>,
root: Option<&Cell>,
key: &mut CellSlice,
key_bit_len: u16,
value: &dyn Store,
Expand Down Expand Up @@ -228,7 +228,7 @@ pub fn dict_insert(
std::cmp::Ordering::Equal => {
// Check if we can replace the value
if !mode.can_replace() {
return Ok((root.clone(), false));
return Ok((root.cloned(), false));
}
// Replace the existing value
break ok!(make_leaf(prefix, key.remaining_bits(), value, finalizer));
Expand All @@ -237,7 +237,7 @@ pub fn dict_insert(
std::cmp::Ordering::Less if lcp.remaining_bits() < prefix.remaining_bits() => {
// Check if we can add a new value
if !mode.can_add() {
return Ok((root.clone(), false));
return Ok((root.cloned(), false));
}
break ok!(split_edge(
&remaining_data,
Expand Down Expand Up @@ -290,7 +290,7 @@ pub fn dict_insert(
///
/// Returns a tuple with a new dict root, changed flag and the previous value.
pub fn dict_insert_owned(
root: &Option<Cell>,
root: Option<&Cell>,
key: &mut CellSlice,
key_bit_len: u16,
value: &dyn Store,
Expand All @@ -313,7 +313,7 @@ pub fn dict_insert_owned(
return Err(Error::CellUnderflow);
}

let root = match root.as_ref() {
let root = match root {
Some(data) => data,
None if mode.can_add() => {
let data = ok!(make_leaf(key, key_bit_len, value, finalizer));
Expand Down Expand Up @@ -408,7 +408,7 @@ pub fn dict_insert_owned(

/// Returns a `CellSlice` of the value corresponding to the key.
pub fn dict_get<'a: 'b, 'b>(
root: &'a Option<Cell>,
root: Option<&'a Cell>,
key_bit_len: u16,
mut key: CellSlice<'b>,
) -> Result<Option<CellSlice<'a>>, Error> {
Expand Down Expand Up @@ -453,7 +453,7 @@ pub fn dict_get<'a: 'b, 'b>(

/// Returns cell slice parts of the value corresponding to the key.
pub fn dict_get_owned(
root: &Option<Cell>,
root: Option<&Cell>,
key_bit_len: u16,
mut key: CellSlice<'_>,
) -> Result<Option<CellSliceParts>, Error> {
Expand Down Expand Up @@ -513,7 +513,7 @@ pub fn dict_get_owned(

/// Returns cell slice parts of the value corresponding to the key.
pub fn dict_find_owned(
root: &Option<Cell>,
root: Option<&Cell>,
key_bit_len: u16,
mut key: CellSlice<'_>,
towards: DictBound,
Expand Down Expand Up @@ -702,7 +702,7 @@ pub fn dict_find_owned(

/// Finds the specified dict bound and returns a key and a value corresponding to the key.
pub fn dict_find_bound<'a: 'b, 'b>(
root: &'a Option<Cell>,
root: Option<&'a Cell>,
mut key_bit_len: u16,
bound: DictBound,
signed: bool,
Expand Down Expand Up @@ -748,7 +748,7 @@ pub fn dict_find_bound<'a: 'b, 'b>(

/// Finds the specified dict bound and returns a key and cell slice parts corresponding to the key.
pub fn dict_find_bound_owned(
root: &Option<Cell>,
root: Option<&Cell>,
mut key_bit_len: u16,
bound: DictBound,
signed: bool,
Expand Down Expand Up @@ -808,7 +808,7 @@ pub fn dict_find_bound_owned(

/// Removes the specified dict bound and returns a removed key and cell slice parts.
pub fn dict_remove_bound_owned(
root: &Option<Cell>,
root: Option<&Cell>,
mut key_bit_len: u16,
bound: DictBound,
signed: bool,
Expand Down
46 changes: 28 additions & 18 deletions src/dict/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<const N: u16> RawDict<N> {

/// Returns a `CellSlice` of the value corresponding to the key.
pub fn get<'a>(&'a self, key: CellSlice<'_>) -> Result<Option<CellSlice<'a>>, Error> {
dict_get(&self.0, N, key)
dict_get(self.0.as_ref(), N, key)
}

/// Computes the minimal key in dictionary that is lexicographically greater than `key`,
Expand All @@ -140,7 +140,7 @@ impl<const N: u16> RawDict<N> {
key: CellSlice<'_>,
signed: bool,
) -> Result<Option<(CellBuilder, CellSliceParts)>, Error> {
dict_find_owned(&self.0, N, key, DictBound::Max, false, signed)
dict_find_owned(self.0.as_ref(), N, key, DictBound::Max, false, signed)
}

/// Computes the maximal key in dictionary that is lexicographically smaller than `key`,
Expand All @@ -150,7 +150,7 @@ impl<const N: u16> RawDict<N> {
key: CellSlice<'_>,
signed: bool,
) -> Result<Option<(CellBuilder, CellSliceParts)>, Error> {
dict_find_owned(&self.0, N, key, DictBound::Min, false, signed)
dict_find_owned(self.0.as_ref(), N, key, DictBound::Min, false, signed)
}

/// Computes the minimal key in dictionary that is lexicographically greater than `key`,
Expand All @@ -160,7 +160,7 @@ impl<const N: u16> RawDict<N> {
key: CellSlice<'_>,
signed: bool,
) -> Result<Option<(CellBuilder, CellSliceParts)>, Error> {
dict_find_owned(&self.0, N, key, DictBound::Max, true, signed)
dict_find_owned(self.0.as_ref(), N, key, DictBound::Max, true, signed)
}

/// Computes the maximal key in dictionary that is lexicographically smaller than `key`,
Expand All @@ -170,22 +170,22 @@ impl<const N: u16> RawDict<N> {
key: CellSlice<'_>,
signed: bool,
) -> Result<Option<(CellBuilder, CellSliceParts)>, Error> {
dict_find_owned(&self.0, N, key, DictBound::Min, true, signed)
dict_find_owned(self.0.as_ref(), N, key, DictBound::Min, true, signed)
}

/// Returns cell slice parts of the value corresponding to the key.
pub fn get_owned(&self, key: CellSlice<'_>) -> Result<Option<CellSliceParts>, Error> {
dict_get_owned(&self.0, N, key)
dict_get_owned(self.0.as_ref(), N, key)
}

/// Returns the lowest key and a value corresponding to the key.
pub fn get_min(&self, signed: bool) -> Result<Option<(CellBuilder, CellSlice<'_>)>, Error> {
dict_find_bound(&self.0, N, DictBound::Min, signed)
dict_find_bound(self.0.as_ref(), N, DictBound::Min, signed)
}

/// Returns the largest key and a value corresponding to the key.
pub fn get_max(&self, signed: bool) -> Result<Option<(CellBuilder, CellSlice<'_>)>, Error> {
dict_find_bound(&self.0, N, DictBound::Max, signed)
dict_find_bound(self.0.as_ref(), N, DictBound::Max, signed)
}

/// Finds the specified dict bound and returns a key and a value corresponding to the key.
Expand All @@ -194,23 +194,23 @@ impl<const N: u16> RawDict<N> {
bound: DictBound,
signed: bool,
) -> Result<Option<(CellBuilder, CellSlice<'_>)>, Error> {
dict_find_bound(&self.0, N, bound, signed)
dict_find_bound(self.0.as_ref(), N, bound, signed)
}

/// Returns the lowest key and cell slice parts corresponding to the key.
pub fn get_min_owned(
&self,
signed: bool,
) -> Result<Option<(CellBuilder, CellSliceParts)>, Error> {
dict_find_bound_owned(&self.0, N, DictBound::Min, signed)
dict_find_bound_owned(self.0.as_ref(), N, DictBound::Min, signed)
}

/// Returns the largest key and cell slice parts corresponding to the key.
pub fn get_max_owned(
&self,
signed: bool,
) -> Result<Option<(CellBuilder, CellSliceParts)>, Error> {
dict_find_bound_owned(&self.0, N, DictBound::Max, signed)
dict_find_bound_owned(self.0.as_ref(), N, DictBound::Max, signed)
}

/// Finds the specified dict bound and returns a key and cell slice parts corresponding to the key.
Expand All @@ -219,12 +219,12 @@ impl<const N: u16> RawDict<N> {
bound: DictBound,
signed: bool,
) -> Result<Option<(CellBuilder, CellSliceParts)>, Error> {
dict_find_bound_owned(&self.0, N, bound, signed)
dict_find_bound_owned(self.0.as_ref(), N, bound, signed)
}

/// Returns `true` if the dictionary contains a value for the specified key.
pub fn contains_key(&self, key: CellSlice<'_>) -> Result<bool, Error> {
Ok(ok!(dict_get(&self.0, N, key)).is_some())
Ok(ok!(dict_get(self.0.as_ref(), N, key)).is_some())
}

/// Sets the value associated with the key in the dictionary.
Expand All @@ -235,7 +235,7 @@ impl<const N: u16> RawDict<N> {
finalizer: &mut dyn Finalizer,
) -> Result<bool, Error> {
let (new_root, changed) = ok!(dict_insert(
&self.0,
self.0.as_ref(),
&mut key,
N,
&value,
Expand All @@ -255,7 +255,7 @@ impl<const N: u16> RawDict<N> {
finalizer: &mut dyn Finalizer,
) -> Result<bool, Error> {
let (new_root, changed) = ok!(dict_insert(
&self.0,
self.0.as_ref(),
&mut key,
N,
value,
Expand All @@ -275,7 +275,7 @@ impl<const N: u16> RawDict<N> {
finalizer: &mut dyn Finalizer,
) -> Result<bool, Error> {
let (new_root, changed) = ok!(dict_insert(
&self.0,
self.0.as_ref(),
&mut key,
N,
value,
Expand All @@ -293,7 +293,13 @@ impl<const N: u16> RawDict<N> {
mut key: CellSlice<'_>,
finalizer: &mut dyn Finalizer,
) -> Result<Option<CellSliceParts>, Error> {
let (dict, removed) = ok!(dict_remove_owned(&self.0, &mut key, N, false, finalizer));
let (dict, removed) = ok!(dict_remove_owned(
self.0.as_ref(),
&mut key,
N,
false,
finalizer
));
self.0 = dict;
Ok(removed)
}
Expand All @@ -307,7 +313,11 @@ impl<const N: u16> RawDict<N> {
finalizer: &mut dyn Finalizer,
) -> Result<Option<DictOwnedEntry>, Error> {
let (dict, removed) = ok!(dict_remove_bound_owned(
&self.0, N, bound, signed, finalizer
self.0.as_ref(),
N,
bound,
signed,
finalizer
));
self.0 = dict;
Ok(removed)
Expand Down
16 changes: 8 additions & 8 deletions src/dict/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ where
{
let mut builder = CellBuilder::new();
ok!(key.store_into(&mut builder, &mut Cell::default_finalizer()));
Ok(ok!(dict_get(root, K::BITS, builder.as_data_slice())).is_some())
Ok(ok!(dict_get(root.as_ref(), K::BITS, builder.as_data_slice())).is_some())
}
contains_key_impl(&self.root, key.borrow())
}
Expand All @@ -168,7 +168,7 @@ where
let Some(mut value) = ({
let mut builder = CellBuilder::new();
ok!(key.store_into(&mut builder, &mut Cell::default_finalizer()));
ok!(dict_get(root, K::BITS, builder.as_data_slice()))
ok!(dict_get(root.as_ref(), K::BITS, builder.as_data_slice()))
}) else {
return Ok(None);
};
Expand Down Expand Up @@ -196,7 +196,7 @@ where
{
let mut builder = CellBuilder::new();
ok!(key.store_into(&mut builder, &mut Cell::default_finalizer()));
dict_get(root, K::BITS, builder.as_data_slice())
dict_get(root.as_ref(), K::BITS, builder.as_data_slice())
}

get_raw_impl(&self.root, key.borrow())
Expand Down Expand Up @@ -424,7 +424,7 @@ where
let mut builder = CellBuilder::new();
ok!(key.store_into(&mut builder, &mut Cell::default_finalizer()));
ok!(dict_find_owned(
root,
root.as_ref(),
K::BITS,
builder.as_data_slice(),
towards,
Expand Down Expand Up @@ -490,7 +490,7 @@ where
bound: DictBound,
signed: bool,
) -> Result<Option<(K, CellSlice<'_>)>, Error> {
let Some((key, value)) = ok!(dict_find_bound(&self.root, K::BITS, bound, signed)) else {
let Some((key, value)) = ok!(dict_find_bound(self.root.as_ref(), K::BITS, bound, signed)) else {
return Ok(None);
};
match K::from_raw_data(key.raw_data()) {
Expand All @@ -510,7 +510,7 @@ where
finalizer: &mut dyn Finalizer,
) -> Result<Option<(K, CellSliceParts)>, Error> {
let (dict, removed) = ok!(dict_remove_bound_owned(
&self.root,
self.root.as_ref(),
K::BITS,
bound,
signed,
Expand Down Expand Up @@ -554,7 +554,7 @@ where
let mut builder = CellBuilder::new();
ok!(key.store_into(&mut builder, &mut Cell::default_finalizer()));
dict_remove_owned(
root,
root.as_ref(),
&mut builder.as_data_slice(),
K::BITS,
false,
Expand Down Expand Up @@ -680,7 +680,7 @@ where
let mut key_builder = CellBuilder::new();
ok!(key.store_into(&mut key_builder, &mut Cell::default_finalizer()));
ok!(dict_insert(
&self.root,
self.root.as_ref(),
&mut key_builder.as_data_slice(),
K::BITS,
value,
Expand Down

0 comments on commit b6093b3

Please sign in to comment.