Skip to content

Commit

Permalink
skiplist: extract equivalent mod to a crate
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Dec 11, 2024
1 parent 45425b0 commit 70d909b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 49 deletions.
1 change: 1 addition & 0 deletions crossbeam-skiplist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ alloc = ["crossbeam-epoch/alloc"]
[dependencies]
crossbeam-epoch = { version = "0.9.17", path = "../crossbeam-epoch", default-features = false }
crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false }
equivalent-flipped = "0.1"

[dev-dependencies]
rand = "0.8"
Expand Down
50 changes: 1 addition & 49 deletions crossbeam-skiplist/src/equivalent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,4 @@

//! Traits for key comparison in maps.
use core::{borrow::Borrow, cmp::Ordering};

/// Key equivalence trait.
///
/// This trait allows hash table lookup to be customized. It has one blanket
/// implementation that uses the regular solution with `Borrow` and `Eq`, just
/// like `HashMap` does, so that you can pass `&str` to lookup into a map with
/// `String` keys and so on.
///
/// # Contract
///
/// The implementor **must** hash like `Q`, if it is hashable.
pub trait Equivalent<Q: ?Sized> {
/// Compare self to `key` and return `true` if they are equal.
fn equivalent(&self, key: &Q) -> bool;
}

impl<K: ?Sized, Q: ?Sized> Equivalent<Q> for K
where
K: Borrow<Q>,
Q: Eq,
{
#[inline]
fn equivalent(&self, key: &Q) -> bool {
PartialEq::eq(self.borrow(), key)
}
}

/// Key ordering trait.
///
/// This trait allows ordered map lookup to be customized. It has one blanket
/// implementation that uses the regular solution with `Borrow` and `Ord`, just
/// like `BTreeMap` does, so that you can pass `&str` to lookup into a map with
/// `String` keys and so on.
pub trait Comparable<Q: ?Sized>: Equivalent<Q> {
/// Compare self to `key` and return their ordering.
fn compare(&self, key: &Q) -> Ordering;
}

impl<K: ?Sized, Q: ?Sized> Comparable<Q> for K
where
K: Borrow<Q>,
Q: Ord,
{
#[inline]
fn compare(&self, key: &Q) -> Ordering {
Ord::cmp(self.borrow(), key)
}
}
pub use equivalent_flipped::*;

0 comments on commit 70d909b

Please sign in to comment.