-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FlurryHashSet insert() and contains()
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! A concurrent hash set backed by FlurryHashMap. | ||
use crossbeam::epoch; | ||
use std::collections::hash_map::RandomState; | ||
use std::hash::Hash; | ||
|
||
use crate::FlurryHashMap; | ||
|
||
/// A concurrent hash set. | ||
#[derive(Debug)] | ||
pub struct FlurryHashSet<T, S = RandomState> { | ||
map: FlurryHashMap<T, (), S>, | ||
} | ||
|
||
impl<T> FlurryHashSet<T, RandomState> | ||
where | ||
T: Sync + Send + Clone + Hash + Eq, | ||
{ | ||
/// Creates a new, empty set with the default initial table size (16). | ||
pub fn new() -> Self { | ||
Self { | ||
map: FlurryHashMap::<T, ()>::new(), | ||
} | ||
} | ||
|
||
/// Adds a value to the set. | ||
/// | ||
/// If the set did not have this value present, true is returned. | ||
/// | ||
/// If the set did have this value present, false is returned. | ||
pub fn insert(&self, value: T) -> bool { | ||
let guard = epoch::pin(); | ||
let old = self.map.insert(value, (), &guard); | ||
old.is_none() | ||
} | ||
|
||
/// Returns true if the set contains a value. | ||
pub fn contains(&self, value: &T) -> bool { | ||
self.map.contains_key(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use flurry::set::FlurryHashSet; | ||
|
||
#[test] | ||
fn new() { | ||
let _set = FlurryHashSet::<usize>::new(); | ||
} | ||
|
||
#[test] | ||
fn insert() { | ||
let set = FlurryHashSet::<usize>::new(); | ||
let did_set = set.insert(42); | ||
assert!(did_set); | ||
|
||
let did_set = set.insert(42); | ||
assert!(!did_set); | ||
} | ||
|
||
#[test] | ||
fn insert_contains() { | ||
let set = FlurryHashSet::<usize>::new(); | ||
set.insert(42); | ||
|
||
assert!(set.contains(&42)); | ||
assert!(!set.contains(&43)); | ||
} |