SsoHashSet/SsoHashMap API greatly expanded

Now both provide almost complete API of their non-SSO counterparts.
This commit is contained in:
Valerii Lashmanov 2020-09-24 01:21:31 -05:00
parent 5c224a484d
commit 0600b178aa
3 changed files with 865 additions and 24 deletions

View File

@ -1,32 +1,202 @@
use super::EitherIter;
use crate::fx::FxHashMap;
use arrayvec::ArrayVec;
use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
use std::iter::FromIterator;
use std::ops::Index;
/// Small-storage-optimized implementation of a map
/// made specifically for caching results.
/// Small-storage-optimized implementation of a map.
///
/// Stores elements in a small array up to a certain length
/// and switches to `HashMap` when that length is exceeded.
///
/// Implements subset of HashMap API.
///
/// Missing HashMap API:
/// all hasher-related
/// try_reserve (unstable)
/// shrink_to (unstable)
/// drain_filter (unstable)
/// into_keys/into_values (unstable)
/// all raw_entry-related
/// PartialEq/Eq (requires sorting the array)
/// Entry::or_insert_with_key (unstable)
/// Vacant/Occupied entries and related
#[derive(Clone)]
pub enum SsoHashMap<K, V> {
Array(ArrayVec<[(K, V); 8]>),
Map(FxHashMap<K, V>),
}
impl<K: Eq + Hash, V> SsoHashMap<K, V> {
impl<K, V> SsoHashMap<K, V> {
/// Creates an empty `SsoHashMap`.
pub fn new() -> Self {
SsoHashMap::Array(ArrayVec::new())
}
/// Inserts or updates value in the map.
pub fn insert(&mut self, key: K, value: V) {
/// Creates an empty `SsoHashMap` with the specified capacity.
pub fn with_capacity(cap: usize) -> Self {
let array = ArrayVec::new();
if array.capacity() >= cap {
SsoHashMap::Array(array)
} else {
SsoHashMap::Map(FxHashMap::with_capacity_and_hasher(cap, Default::default()))
}
}
/// Clears the map, removing all key-value pairs. Keeps the allocated memory
/// for reuse.
pub fn clear(&mut self) {
match self {
SsoHashMap::Array(array) => array.clear(),
SsoHashMap::Map(map) => map.clear(),
}
}
/// Returns the number of elements the map can hold without reallocating.
pub fn capacity(&self) -> usize {
match self {
SsoHashMap::Array(array) => array.capacity(),
SsoHashMap::Map(map) => map.capacity(),
}
}
/// Returns the number of elements in the map.
pub fn len(&self) -> usize {
match self {
SsoHashMap::Array(array) => array.len(),
SsoHashMap::Map(map) => map.len(),
}
}
/// Returns `true` if the map contains no elements.
pub fn is_empty(&self) -> bool {
match self {
SsoHashMap::Array(array) => array.is_empty(),
SsoHashMap::Map(map) => map.is_empty(),
}
}
/// An iterator visiting all key-value pairs in arbitrary order.
/// The iterator element type is `(&'a K, &'a V)`.
pub fn iter(&self) -> impl Iterator<Item = (&'_ K, &'_ V)> {
self.into_iter()
}
/// An iterator visiting all key-value pairs in arbitrary order,
/// with mutable references to the values.
/// The iterator element type is `(&'a K, &'a mut V)`.
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&'_ K, &'_ mut V)> {
self.into_iter()
}
/// An iterator visiting all keys in arbitrary order.
/// The iterator element type is `&'a K`.
pub fn keys(&self) -> impl Iterator<Item = &'_ K> {
match self {
SsoHashMap::Array(array) => EitherIter::Left(array.iter().map(|(k, _v)| k)),
SsoHashMap::Map(map) => EitherIter::Right(map.keys()),
}
}
/// An iterator visiting all values in arbitrary order.
/// The iterator element type is `&'a V`.
pub fn values(&self) -> impl Iterator<Item = &'_ V> {
match self {
SsoHashMap::Array(array) => EitherIter::Left(array.iter().map(|(_k, v)| v)),
SsoHashMap::Map(map) => EitherIter::Right(map.values()),
}
}
/// An iterator visiting all values mutably in arbitrary order.
/// The iterator element type is `&'a mut V`.
pub fn values_mut(&mut self) -> impl Iterator<Item = &'_ mut V> {
match self {
SsoHashMap::Array(array) => EitherIter::Left(array.iter_mut().map(|(_k, v)| v)),
SsoHashMap::Map(map) => EitherIter::Right(map.values_mut()),
}
}
/// Clears the map, returning all key-value pairs as an iterator. Keeps the
/// allocated memory for reuse.
pub fn drain(&mut self) -> impl Iterator<Item = (K, V)> + '_ {
match self {
SsoHashMap::Array(array) => EitherIter::Left(array.drain(..)),
SsoHashMap::Map(map) => EitherIter::Right(map.drain()),
}
}
}
impl<K: Eq + Hash, V> SsoHashMap<K, V> {
/// Changes underlying storage from array to hashmap
/// if array is full.
fn migrate_if_full(&mut self) {
if let SsoHashMap::Array(array) = self {
if array.is_full() {
*self = SsoHashMap::Map(array.drain(..).collect());
}
}
}
/// Reserves capacity for at least `additional` more elements to be inserted
/// in the `SsoHashMap`. The collection may reserve more space to avoid
/// frequent reallocations.
pub fn reserve(&mut self, additional: usize) {
match self {
SsoHashMap::Array(array) => {
for pair in array.iter_mut() {
if pair.0 == key {
pair.1 = value;
return;
if array.capacity() < (array.len() + additional) {
let mut map: FxHashMap<K, V> = array.drain(..).collect();
map.reserve(additional);
*self = SsoHashMap::Map(map);
}
}
SsoHashMap::Map(map) => map.reserve(additional),
}
}
/// Shrinks the capacity of the map as much as possible. It will drop
/// down as much as possible while maintaining the internal rules
/// and possibly leaving some space in accordance with the resize policy.
pub fn shrink_to_fit(&mut self) {
if let SsoHashMap::Map(map) = self {
let mut array = ArrayVec::new();
if map.len() <= array.capacity() {
array.extend(map.drain());
*self = SsoHashMap::Array(array);
} else {
map.shrink_to_fit();
}
}
}
/// Retains only the elements specified by the predicate.
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&K, &mut V) -> bool,
{
match self {
SsoHashMap::Array(array) => array.retain(|(k, v)| f(k, v)),
SsoHashMap::Map(map) => map.retain(f),
}
}
/// Inserts a key-value pair into the map.
///
/// If the map did not have this key present, [`None`] is returned.
///
/// If the map did have this key present, the value is updated, and the old
/// value is returned. The key is not updated, though; this matters for
/// types that can be `==` without being identical. See the [module-level
/// documentation] for more.
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
match self {
SsoHashMap::Array(array) => {
for (k, v) in array.iter_mut() {
if *k == key {
let old_value = std::mem::replace(v, value);
return Some(old_value);
}
}
if let Err(error) = array.try_push((key, value)) {
@ -35,27 +205,325 @@ impl<K: Eq + Hash, V> SsoHashMap<K, V> {
map.insert(key, value);
*self = SsoHashMap::Map(map);
}
None
}
SsoHashMap::Map(map) => {
map.insert(key, value);
}
SsoHashMap::Map(map) => map.insert(key, value),
}
}
/// Return value by key if any.
pub fn get(&self, key: &K) -> Option<&V> {
/// Removes a key from the map, returning the value at the key if the key
/// was previously in the map.
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashMap::Array(array) => {
for pair in array {
if pair.0 == *key {
return Some(&pair.1);
if let Some(index) = array.iter().position(|(k, _v)| k.borrow() == key) {
Some(array.swap_remove(index).1)
} else {
None
}
}
SsoHashMap::Map(map) => map.remove(key),
}
}
/// Removes a key from the map, returning the stored key and value if the
/// key was previously in the map.
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashMap::Array(array) => {
if let Some(index) = array.iter().position(|(k, _v)| k.borrow() == key) {
Some(array.swap_remove(index))
} else {
None
}
}
SsoHashMap::Map(map) => map.remove_entry(key),
}
}
/// Returns a reference to the value corresponding to the key.
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashMap::Array(array) => {
for (k, v) in array {
if k.borrow() == key {
return Some(v);
}
}
return None;
None
}
SsoHashMap::Map(map) => {
return map.get(key);
SsoHashMap::Map(map) => map.get(key),
}
}
/// Returns a mutable reference to the value corresponding to the key.
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashMap::Array(array) => {
for (k, v) in array {
if (*k).borrow() == key {
return Some(v);
}
}
None
}
SsoHashMap::Map(map) => map.get_mut(key),
}
}
/// Returns the key-value pair corresponding to the supplied key.
pub fn get_key_value<Q: ?Sized>(&self, key: &Q) -> Option<(&K, &V)>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashMap::Array(array) => {
for (k, v) in array {
if k.borrow() == key {
return Some((k, v));
}
}
None
}
SsoHashMap::Map(map) => map.get_key_value(key),
}
}
/// Returns `true` if the map contains a value for the specified key.
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashMap::Array(array) => array.iter().any(|(k, _v)| k.borrow() == key),
SsoHashMap::Map(map) => map.contains_key(key),
}
}
/// Gets the given key's corresponding entry in the map for in-place manipulation.
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
Entry { ssomap: self, key }
}
}
impl<K, V> Default for SsoHashMap<K, V> {
fn default() -> Self {
Self::new()
}
}
impl<K: Eq + Hash, V> FromIterator<(K, V)> for SsoHashMap<K, V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> SsoHashMap<K, V> {
let mut map: SsoHashMap<K, V> = Default::default();
map.extend(iter);
map
}
}
impl<K: Eq + Hash, V> Extend<(K, V)> for SsoHashMap<K, V> {
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = (K, V)>,
{
for (key, value) in iter.into_iter() {
self.insert(key, value);
}
}
fn extend_one(&mut self, (k, v): (K, V)) {
self.insert(k, v);
}
fn extend_reserve(&mut self, additional: usize) {
match self {
SsoHashMap::Array(array) => {
if array.capacity() < (array.len() + additional) {
let mut map: FxHashMap<K, V> = array.drain(..).collect();
map.extend_reserve(additional);
*self = SsoHashMap::Map(map);
}
}
SsoHashMap::Map(map) => map.extend_reserve(additional),
}
}
}
impl<'a, K, V> Extend<(&'a K, &'a V)> for SsoHashMap<K, V>
where
K: Eq + Hash + Copy,
V: Copy,
{
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
self.extend(iter.into_iter().map(|(k, v)| (k.clone(), v.clone())))
}
fn extend_one(&mut self, (&k, &v): (&'a K, &'a V)) {
self.insert(k, v);
}
fn extend_reserve(&mut self, additional: usize) {
Extend::<(K, V)>::extend_reserve(self, additional)
}
}
impl<K, V> IntoIterator for SsoHashMap<K, V> {
type IntoIter = EitherIter<
<ArrayVec<[(K, V); 8]> as IntoIterator>::IntoIter,
<FxHashMap<K, V> as IntoIterator>::IntoIter,
>;
type Item = <Self::IntoIter as Iterator>::Item;
fn into_iter(self) -> Self::IntoIter {
match self {
SsoHashMap::Array(array) => EitherIter::Left(array.into_iter()),
SsoHashMap::Map(map) => EitherIter::Right(map.into_iter()),
}
}
}
/// adapts Item of array reference iterator to Item of hashmap reference iterator.
fn adapt_array_ref_it<K, V>(pair: &'a (K, V)) -> (&'a K, &'a V) {
let (a, b) = pair;
(a, b)
}
/// adapts Item of array mut reference iterator to Item of hashmap mut reference iterator.
fn adapt_array_mut_it<K, V>(pair: &'a mut (K, V)) -> (&'a K, &'a mut V) {
let (a, b) = pair;
(a, b)
}
impl<'a, K, V> IntoIterator for &'a SsoHashMap<K, V> {
type IntoIter = EitherIter<
std::iter::Map<
<&'a ArrayVec<[(K, V); 8]> as IntoIterator>::IntoIter,
fn(&'a (K, V)) -> (&'a K, &'a V),
>,
<&'a FxHashMap<K, V> as IntoIterator>::IntoIter,
>;
type Item = <Self::IntoIter as Iterator>::Item;
fn into_iter(self) -> Self::IntoIter {
match self {
SsoHashMap::Array(array) => EitherIter::Left(array.into_iter().map(adapt_array_ref_it)),
SsoHashMap::Map(map) => EitherIter::Right(map.into_iter()),
}
}
}
impl<'a, K, V> IntoIterator for &'a mut SsoHashMap<K, V> {
type IntoIter = EitherIter<
std::iter::Map<
<&'a mut ArrayVec<[(K, V); 8]> as IntoIterator>::IntoIter,
fn(&'a mut (K, V)) -> (&'a K, &'a mut V),
>,
<&'a mut FxHashMap<K, V> as IntoIterator>::IntoIter,
>;
type Item = <Self::IntoIter as Iterator>::Item;
fn into_iter(self) -> Self::IntoIter {
match self {
SsoHashMap::Array(array) => EitherIter::Left(array.into_iter().map(adapt_array_mut_it)),
SsoHashMap::Map(map) => EitherIter::Right(map.into_iter()),
}
}
}
impl<K, V> fmt::Debug for SsoHashMap<K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}
impl<'a, K, Q: ?Sized, V> Index<&'a Q> for SsoHashMap<K, V>
where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
{
type Output = V;
fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key")
}
}
/// A view into a single entry in a map.
pub struct Entry<'a, K, V> {
ssomap: &'a mut SsoHashMap<K, V>,
key: K,
}
impl<'a, K: Eq + Hash, V> Entry<'a, K, V> {
/// Provides in-place mutable access to an occupied entry before any
/// potential inserts into the map.
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
if let Some(value) = self.ssomap.get_mut(&self.key) {
f(value);
}
self
}
/// Ensures a value is in the entry by inserting the default if empty, and returns
/// a mutable reference to the value in the entry.
pub fn or_insert(self, value: V) -> &'a mut V {
self.or_insert_with(|| value)
}
/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
self.ssomap.migrate_if_full();
match self.ssomap {
SsoHashMap::Array(array) => {
let key_ref = &self.key;
let found_index = array.iter().position(|(k, _v)| k == key_ref);
let index = if let Some(index) = found_index {
index
} else {
array.try_push((self.key, default())).unwrap();
array.len() - 1
};
&mut array[index].1
}
SsoHashMap::Map(map) => map.entry(self.key).or_insert_with(default),
}
}
/// Returns a reference to this entry's key.
pub fn key(&self) -> &K {
&self.key
}
}
impl<'a, K: Eq + Hash, V: Default> Entry<'a, K, V> {
/// Ensures a value is in the entry by inserting the default value if empty,
/// and returns a mutable reference to the value in the entry.
pub fn or_default(self) -> &'a mut V {
self.or_insert_with(Default::default)
}
}

View File

@ -1,3 +1,75 @@
use std::fmt;
use std::iter::ExactSizeIterator;
use std::iter::FusedIterator;
use std::iter::Iterator;
/// Iterator which may contain instance of
/// one of two specific implementations.
///
/// Used by both SsoHashMap and SsoHashSet.
#[derive(Clone)]
pub enum EitherIter<L, R> {
Left(L),
Right(R),
}
impl<L, R> Iterator for EitherIter<L, R>
where
L: Iterator,
R: Iterator<Item = L::Item>,
{
type Item = L::Item;
fn next(&mut self) -> Option<Self::Item> {
match self {
EitherIter::Left(l) => l.next(),
EitherIter::Right(r) => r.next(),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
match self {
EitherIter::Left(l) => l.size_hint(),
EitherIter::Right(r) => r.size_hint(),
}
}
}
impl<L, R> ExactSizeIterator for EitherIter<L, R>
where
L: ExactSizeIterator,
R: ExactSizeIterator,
EitherIter<L, R>: Iterator,
{
fn len(&self) -> usize {
match self {
EitherIter::Left(l) => l.len(),
EitherIter::Right(r) => r.len(),
}
}
}
impl<L, R> FusedIterator for EitherIter<L, R>
where
L: FusedIterator,
R: FusedIterator,
EitherIter<L, R>: Iterator,
{
}
impl<L, R> fmt::Debug for EitherIter<L, R>
where
L: fmt::Debug,
R: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EitherIter::Left(l) => l.fmt(f),
EitherIter::Right(r) => r.fmt(f),
}
}
}
mod map;
mod set;

View File

@ -1,26 +1,197 @@
use super::EitherIter;
use crate::fx::FxHashSet;
use arrayvec::ArrayVec;
use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
use std::iter::FromIterator;
/// 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.
///
/// Implements subset of HashSet API.
///
/// Missing HashSet API:
/// all hasher-related
/// try_reserve (unstable)
/// shrink_to (unstable)
/// drain_filter (unstable)
/// get_or_insert/get_or_insert_owned/get_or_insert_with (unstable)
/// difference/symmetric_difference/intersection/union
/// is_disjoint/is_subset/is_superset
/// PartialEq/Eq (requires sorting the array)
/// BitOr/BitAnd/BitXor/Sub
#[derive(Clone)]
pub enum SsoHashSet<T> {
Array(ArrayVec<[T; 8]>),
Set(FxHashSet<T>),
}
impl<T: Eq + Hash> SsoHashSet<T> {
impl<T> SsoHashSet<T> {
/// Creates an empty `SsoHashSet`.
pub fn new() -> Self {
SsoHashSet::Array(ArrayVec::new())
}
/// Creates an empty `SsoHashSet` with the specified capacity.
pub fn with_capacity(cap: usize) -> Self {
let array = ArrayVec::new();
if array.capacity() >= cap {
SsoHashSet::Array(array)
} else {
SsoHashSet::Set(FxHashSet::with_capacity_and_hasher(cap, Default::default()))
}
}
/// Clears the set, removing all values.
pub fn clear(&mut self) {
match self {
SsoHashSet::Array(array) => array.clear(),
SsoHashSet::Set(set) => set.clear(),
}
}
/// Returns the number of elements the set can hold without reallocating.
pub fn capacity(&self) -> usize {
match self {
SsoHashSet::Array(array) => array.capacity(),
SsoHashSet::Set(set) => set.capacity(),
}
}
/// Returns the number of elements in the set.
pub fn len(&self) -> usize {
match self {
SsoHashSet::Array(array) => array.len(),
SsoHashSet::Set(set) => set.len(),
}
}
/// Returns `true` if the set contains no elements.
pub fn is_empty(&self) -> bool {
match self {
SsoHashSet::Array(array) => array.is_empty(),
SsoHashSet::Set(set) => set.is_empty(),
}
}
/// An iterator visiting all elements in arbitrary order.
/// The iterator element type is `&'a T`.
pub fn iter(&'a self) -> impl Iterator<Item = &'a T> {
self.into_iter()
}
/// Clears the set, returning all elements in an iterator.
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_ {
match self {
SsoHashSet::Array(array) => EitherIter::Left(array.drain(..)),
SsoHashSet::Set(set) => EitherIter::Right(set.drain()),
}
}
}
impl<T: Eq + Hash> SsoHashSet<T> {
/// Reserves capacity for at least `additional` more elements to be inserted
/// in the `SsoHashSet`. The collection may reserve more space to avoid
/// frequent reallocations.
pub fn reserve(&mut self, additional: usize) {
match self {
SsoHashSet::Array(array) => {
if array.capacity() < (array.len() + additional) {
let mut set: FxHashSet<T> = array.drain(..).collect();
set.reserve(additional);
*self = SsoHashSet::Set(set);
}
}
SsoHashSet::Set(set) => set.reserve(additional),
}
}
/// Shrinks the capacity of the set as much as possible. It will drop
/// down as much as possible while maintaining the internal rules
/// and possibly leaving some space in accordance with the resize policy.
pub fn shrink_to_fit(&mut self) {
if let SsoHashSet::Set(set) = self {
let mut array = ArrayVec::new();
if set.len() <= array.capacity() {
array.extend(set.drain());
*self = SsoHashSet::Array(array);
} else {
set.shrink_to_fit();
}
}
}
/// Retains only the elements specified by the predicate.
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
match self {
SsoHashSet::Array(array) => array.retain(|v| f(v)),
SsoHashSet::Set(set) => set.retain(f),
}
}
/// Removes and returns the value in the set, if any, that is equal to the given one.
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
where
T: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashSet::Array(array) => {
if let Some(index) = array.iter().position(|val| val.borrow() == value) {
Some(array.swap_remove(index))
} else {
None
}
}
SsoHashSet::Set(set) => set.take(value),
}
}
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
/// one. Returns the replaced value.
pub fn replace(&mut self, value: T) -> Option<T> {
match self {
SsoHashSet::Array(array) => {
if let Some(index) = array.iter().position(|val| *val == value) {
let old_value = std::mem::replace(&mut array[index], value);
Some(old_value)
} else {
None
}
}
SsoHashSet::Set(set) => set.replace(value),
}
}
/// Returns a reference to the value in the set, if any, that is equal to the given value.
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashSet::Array(array) => {
if let Some(index) = array.iter().position(|val| val.borrow() == value) {
Some(&array[index])
} else {
None
}
}
SsoHashSet::Set(set) => set.get(value),
}
}
/// Adds a value to the set.
///
/// If the set did not have this value present, true is returned.
/// If the set did not have this value present, `true` is returned.
///
/// If the set did have this value present, false is returned.
/// If the set did have this value present, `false` is returned.
pub fn insert(&mut self, elem: T) -> bool {
match self {
SsoHashSet::Array(array) => {
@ -38,4 +209,134 @@ impl<T: Eq + Hash> SsoHashSet<T> {
SsoHashSet::Set(set) => set.insert(elem),
}
}
/// Removes a value from the set. Returns whether the value was
/// present in the set.
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashSet::Array(array) => {
if let Some(index) = array.iter().position(|val| val.borrow() == value) {
array.swap_remove(index);
true
} else {
false
}
}
SsoHashSet::Set(set) => set.remove(value),
}
}
/// Returns `true` if the set contains a value.
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
match self {
SsoHashSet::Array(array) => array.iter().any(|v| v.borrow() == value),
SsoHashSet::Set(set) => set.contains(value),
}
}
}
impl<T> Default for SsoHashSet<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: Eq + Hash> FromIterator<T> for SsoHashSet<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> SsoHashSet<T> {
let mut set: SsoHashSet<T> = Default::default();
set.extend(iter);
set
}
}
impl<T: Eq + Hash> Extend<T> for SsoHashSet<T> {
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = T>,
{
for val in iter.into_iter() {
self.insert(val);
}
}
fn extend_one(&mut self, item: T) {
self.insert(item);
}
fn extend_reserve(&mut self, additional: usize) {
match self {
SsoHashSet::Array(array) => {
if array.capacity() < (array.len() + additional) {
let mut set: FxHashSet<T> = array.drain(..).collect();
set.extend_reserve(additional);
*self = SsoHashSet::Set(set);
}
}
SsoHashSet::Set(set) => set.extend_reserve(additional),
}
}
}
impl<'a, T> Extend<&'a T> for SsoHashSet<T>
where
T: 'a + Eq + Hash + Copy,
{
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
fn extend_one(&mut self, &item: &'a T) {
self.insert(item);
}
fn extend_reserve(&mut self, additional: usize) {
Extend::<T>::extend_reserve(self, additional)
}
}
impl<T> IntoIterator for SsoHashSet<T> {
type IntoIter = EitherIter<
<ArrayVec<[T; 8]> as IntoIterator>::IntoIter,
<FxHashSet<T> as IntoIterator>::IntoIter,
>;
type Item = <Self::IntoIter as Iterator>::Item;
fn into_iter(self) -> Self::IntoIter {
match self {
SsoHashSet::Array(array) => EitherIter::Left(array.into_iter()),
SsoHashSet::Set(set) => EitherIter::Right(set.into_iter()),
}
}
}
impl<'a, T> IntoIterator for &'a SsoHashSet<T> {
type IntoIter = EitherIter<
<&'a ArrayVec<[T; 8]> as IntoIterator>::IntoIter,
<&'a FxHashSet<T> as IntoIterator>::IntoIter,
>;
type Item = <Self::IntoIter as Iterator>::Item;
fn into_iter(self) -> Self::IntoIter {
match self {
SsoHashSet::Array(array) => EitherIter::Left(array.into_iter()),
SsoHashSet::Set(set) => EitherIter::Right(set.into_iter()),
}
}
}
impl<T> fmt::Debug for SsoHashSet<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}