Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Map wrapper over raw node operations #12

Closed
declanvk opened this issue Apr 26, 2022 · 3 comments
Closed

Implement Map wrapper over raw node operations #12

declanvk opened this issue Apr 26, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@declanvk
Copy link
Owner

Problem

  • Currently all the tree operations are unsafe free functions which operate on the OpaqueNotePtr<V> type.
  • This is an issue because these is not a very friendly interface and requires using unsafe blocks and fulfilling some of those requirements.
  • I want a drop in replacement for the BTreeMap and BTreeSet types from the standard library.

Solution Sketch

  • Copy over the APIs from BTreeMap and BTreeSet and update the types and parameters.

Standard Library BTreeMap API Outline

impl<K, V> BTreeMap<K, V> {
    pub fn new() -> BTreeMap<K, V>;

    pub fn clear(&mut self);

    pub fn get<Q>(&self, key: &Q) -> Option<&V> where
        K: Borrow<Q> + Ord,
        Q: Ord + ?Sized;

    pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)> where
        K: Borrow<Q> + Ord,
        Q: Ord + ?Sized; 

    pub fn first_key_value(&self) -> Option<(&K, &V)> where
        K: Ord;

    pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> where
        K: Ord;

    pub fn pop_first(&mut self) -> Option<(K, V)> where
        K: Ord;

    pub fn last_key_value(&self) -> Option<(&K, &V)> where
        K: Ord;

    pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> where
        K: Ord;

    pub fn pop_last(&mut self) -> Option<(K, V)> where
        K: Ord;

    pub fn contains_key<Q>(&self, key: &Q) -> bool where
        K: Borrow<Q> + Ord,
        Q: Ord + ?Sized;

    pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> where
        K: Borrow<Q> + Ord,
        Q: Ord + ?Sized;

    pub fn insert(&mut self, key: K, value: V) -> Option<V> where
        K: Ord;

    pub fn try_insert(
        &mut self,
        key: K,
        value: V
    ) -> Result<&mut V, OccupiedError<'_, K, V>> where
        K: Ord;

    pub fn remove<Q>(&mut self, key: &Q) -> Option<V> where
        K: Borrow<Q> + Ord,
        Q: Ord + ?Sized;

    pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> where
        K: Borrow<Q> + Ord,
        Q: Ord + ?Sized;

    pub fn retain<F>(&mut self, f: F) where
        K: Ord,
        F: FnMut(&K, &mut V) -> bool;

    pub fn append(&mut self, other: &mut BTreeMap<K, V>) where
        K: Ord;

    pub fn range<T, R>(&self, range: R) -> Range<'_, K, V> where
        T: Ord + ?Sized,
        K: Borrow<T> + Ord,
        R: RangeBounds<T>;

    pub fn range_mut<T, R>(&mut self, range: R) -> RangeMut<'_, K, V> where
        T: Ord + ?Sized,
        K: Borrow<T> + Ord,
        R: RangeBounds<T>; 

    pub fn entry(&mut self, key: K) -> Entry<'_, K, V> where
        K: Ord;

    pub fn split_off<Q>(&mut self, key: &Q) -> BTreeMap<K, V> where
        Q: Ord + ?Sized,
        K: Borrow<Q> + Ord;

    pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, K, V, F> where
        K: Ord,
        F: FnMut(&K, &mut V) -> bool;

    pub fn into_keys(self) -> IntoKeys<K, V>;


    pub fn into_values(self) -> IntoValues<K, V>;

    pub fn iter(&self) -> Iter<'_, K, V>;

    pub fn iter_mut(&mut self) -> IterMut<'_, K, V>;

    pub fn keys(&self) -> Keys<'_, K, V>;

    pub fn values(&self) -> Values<'_, K, V>;

    pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>;

    pub fn len(&self) -> usize;

    pub fn is_empty(&self) -> bool;
}
@declanvk declanvk self-assigned this Apr 26, 2022
@declanvk declanvk added the enhancement New feature or request label Aug 20, 2022
declanvk added a commit that referenced this issue Nov 17, 2022
**Description**
Implements most of issue #12.

This change introduces a new module that contains a safe wapper over
the unsafe node APIs. It is based directly on the API of the BTreeMap
that is present in the standard library.

Some function are not implemented, currently they are marked with
`todo!()`.

Also added in a fuzz target which covers the integration of the raw
node APIs into the safe wrapper.

**Motivation**
This change is needed so that this crate can be useful to people
without forcing them to use lots of unsafe functions. This wrapper
takes care of enforcing safety conditions and matches most expecations
around which APIs a map container should provide.

**Testing Done**
 - `cargo test`
 - `cargo miri test`
 - `cargo clippy`
 - `cargo fuzz run fuzz_tree_map_api`
 - `cargo doc`
@declanvk
Copy link
Owner Author

Needs examples in doc comments for all APIs.

declanvk added a commit that referenced this issue Nov 17, 2022
**Description**
Implements most of issue #12.

This change introduces a new module that contains a safe wapper over
the unsafe node APIs. It is based directly on the API of the BTreeMap
that is present in the standard library.

Some function are not implemented, currently they are marked with
`todo!()`.

Also added in a fuzz target which covers the integration of the raw
node APIs into the safe wrapper.

**Motivation**
This change is needed so that this crate can be useful to people
without forcing them to use lots of unsafe functions. This wrapper
takes care of enforcing safety conditions and matches most expecations
around which APIs a map container should provide.

**Testing Done**
 - `cargo test`
 - `cargo miri test`
 - `cargo clippy`
 - `cargo fuzz run fuzz_tree_map_api`
 - `cargo doc`
@declanvk declanvk changed the title Implement Map/Set wrapper over raw node operations Implement Map wrapper over raw node operations Nov 18, 2022
@declanvk
Copy link
Owner Author

declanvk commented Nov 18, 2022

Remaining work after 543d273 was merged:

  • Add examples to all doc comments
  • Add equivalent TreeSet implementation, copying from BTreeSet API outline
  • Implement range/range_mut iterators
  • Implement drain_filter
  • Implement split_off
  • Implement append

@declanvk
Copy link
Owner Author

declanvk commented Feb 13, 2023

Created issues for remaining work #24 and #25, closing.

Most of code was done in 543d273

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant