use crate::collections::IndexMap;
use core::{borrow::Borrow, hash::Hash, iter::FusedIterator, ops::Index};
#[derive(Debug, Clone)]
pub struct IndexSet<T> {
inner: IndexMap<T, ()>,
}
impl<T> Default for IndexSet<T> {
#[inline]
fn default() -> Self {
Self {
inner: IndexMap::default(),
}
}
}
impl<T> IndexSet<T> {
#[inline]
pub fn clear(&mut self) {
self.inner.clear()
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[inline]
pub fn iter(&self) -> Iter<'_, T> {
Iter {
inner: self.inner.iter(),
}
}
}
impl<T> IndexSet<T>
where
T: Eq + Hash + Ord + Clone,
{
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional);
}
#[inline]
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq + Ord,
{
self.inner.contains_key(value)
}
#[inline]
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Q: Hash + Eq + Ord,
{
self.inner.get_key_value(value).map(|(x, &())| x)
}
pub fn get_index_of<Q>(&self, value: &Q) -> Option<usize>
where
T: Borrow<Q>,
Q: Hash + Eq + Ord + ?Sized,
{
let (index, _, _) = self.inner.get_full(value)?;
Some(index)
}
#[inline]
pub fn insert(&mut self, value: T) -> bool {
self.inner.insert(value, ()).is_none()
}
#[inline]
pub fn swap_remove<Q: ?Sized>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq + Ord,
{
self.inner.swap_remove(value).is_some()
}
pub fn replace(&mut self, value: T) -> Option<T> {
let removed = self.inner.swap_remove_entry(&value);
self.inner.insert(value, ());
removed.map(|(key, _value)| key)
}
pub fn is_disjoint(&self, other: &Self) -> bool {
if self.len() <= other.len() {
self.iter().all(move |value| !other.contains(value))
} else {
other.iter().all(move |value| !self.contains(value))
}
}
pub fn is_subset(&self, other: &Self) -> bool {
self.len() <= other.len() && self.iter().all(move |value| other.contains(value))
}
#[inline]
pub fn is_superset(&self, other: &Self) -> bool {
other.is_subset(self)
}
}
impl<T> Index<usize> for IndexSet<T>
where
T: Hash + Eq + Ord,
{
type Output = T;
#[inline]
fn index(&self, index: usize) -> &T {
let Some((value, _)) = self.inner.get_index(index) else {
panic!("out of bounds index: {index}");
};
value
}
}
impl<T> FromIterator<T> for IndexSet<T>
where
T: Hash + Eq + Ord + Clone,
{
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
Self {
inner: <IndexMap<T, ()>>::from_iter(iter.into_iter().map(|value| (value, ()))),
}
}
}
impl<'a, T> IntoIterator for &'a IndexSet<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T> Extend<T> for IndexSet<T>
where
T: Hash + Eq + Ord + Clone,
{
fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) {
self.inner.extend(iter.into_iter().map(|value| (value, ())))
}
}
#[derive(Debug, Clone)]
pub struct Iter<'a, T> {
inner: <&'a IndexMap<T, ()> as IntoIterator>::IntoIter,
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(key, _value)| key)
}
}
impl<'a, T> ExactSizeIterator for Iter<'a, T> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<'a, T> FusedIterator for Iter<'a, T> {}
impl<T> IntoIterator for IndexSet<T> {
type Item = T;
type IntoIter = IntoIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter {
inner: self.inner.into_iter(),
}
}
}
#[derive(Debug)]
pub struct IntoIter<T> {
inner: <IndexMap<T, ()> as IntoIterator>::IntoIter,
}
impl<T> Iterator for IntoIter<T> {
type Item = T;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(key, _value)| key)
}
}
impl<T> ExactSizeIterator for IntoIter<T> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<T> FusedIterator for IntoIter<T> {}
#[cfg(feature = "serde")]
impl<T> serde::Serialize for IndexSet<T>
where
T: serde::Serialize + Eq + Hash + Ord,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serde::Serialize::serialize(&self.inner, serializer)
}
}
#[cfg(feature = "serde")]
impl<'a, T> serde::Deserialize<'a> for IndexSet<T>
where
T: serde::Deserialize<'a> + Eq + Hash + Ord + Clone,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'a>,
{
Ok(IndexSet {
inner: serde::Deserialize::deserialize(deserializer)?,
})
}
}
impl<T> PartialEq for IndexSet<T>
where
T: PartialEq + Hash + Ord,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
fn ne(&self, other: &Self) -> bool {
self.inner != other.inner
}
}
impl<T> Eq for IndexSet<T> where T: Eq + Hash + Ord {}