-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove the need for T to be copy from MiniSet as was done for MiniMap
- Loading branch information
Showing
9 changed files
with
46 additions
and
50 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
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 @@ | ||
use crate::fx::FxHashSet; | ||
use arrayvec::ArrayVec; | ||
use std::hash::Hash; | ||
/// Small-storage-optimized implementation of a set. | ||
/// | ||
/// Stores elements in a small array up to a certain length | ||
/// and switches to `HashSet` when that length is exceeded. | ||
pub enum MiniSet<T> { | ||
Array(ArrayVec<[T; 8]>), | ||
Set(FxHashSet<T>), | ||
} | ||
|
||
impl<T: Eq + Hash> MiniSet<T> { | ||
/// Creates an empty `MiniSet`. | ||
pub fn new() -> Self { | ||
MiniSet::Array(ArrayVec::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(&mut self, elem: T) -> bool { | ||
match self { | ||
MiniSet::Array(array) => { | ||
if array.iter().any(|e| *e == elem) { | ||
false | ||
} else { | ||
if let Err(error) = array.try_push(elem) { | ||
let mut set: FxHashSet<T> = array.drain(..).collect(); | ||
set.insert(error.element()); | ||
*self = MiniSet::Set(set); | ||
} | ||
true | ||
} | ||
} | ||
MiniSet::Set(set) => set.insert(elem), | ||
} | ||
} | ||
} |
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
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
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