#[cfg(feature = "rayon")]
pub use ::rayon::set as rayon;
#[cfg(not(has_std))]
use std::vec::Vec;
#[cfg(has_std)]
use std::collections::hash_map::RandomState;
use std::cmp::Ordering;
use std::fmt;
use std::iter::{FromIterator, Chain};
use std::hash::{Hash, BuildHasher};
use std::ops::RangeFull;
use std::ops::{BitAnd, BitOr, BitXor, Sub};
use std::slice;
use std::vec;
use super::{IndexMap, Equivalent, Entries};
type Bucket<T> = super::Bucket<T, ()>;
#[derive(Clone)]
#[cfg(has_std)]
pub struct IndexSet<T, S = RandomState> {
map: IndexMap<T, (), S>,
}
#[cfg(not(has_std))]
#[derive(Clone)]
pub struct IndexSet<T, S> {
map: IndexMap<T, (), S>,
}
impl<T, S> Entries for IndexSet<T, S> {
type Entry = Bucket<T>;
fn into_entries(self) -> Vec<Self::Entry> {
self.map.into_entries()
}
fn as_entries(&self) -> &[Self::Entry] {
self.map.as_entries()
}
fn as_entries_mut(&mut self) -> &mut [Self::Entry] {
self.map.as_entries_mut()
}
fn with_entries<F>(&mut self, f: F)
where F: FnOnce(&mut [Self::Entry])
{
self.map.with_entries(f);
}
}
impl<T, S> fmt::Debug for IndexSet<T, S>
where T: fmt::Debug + Hash + Eq,
S: BuildHasher,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if cfg!(not(feature = "test_debug")) {
f.debug_set().entries(self.iter()).finish()
} else {
f.debug_struct("IndexSet").field("map", &self.map).finish()
}
}
}
#[cfg(has_std)]
impl<T> IndexSet<T> {
pub fn new() -> Self {
IndexSet { map: IndexMap::new() }
}
pub fn with_capacity(n: usize) -> Self {
IndexSet { map: IndexMap::with_capacity(n) }
}
}
impl<T, S> IndexSet<T, S> {
pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self
where S: BuildHasher
{
IndexSet { map: IndexMap::with_capacity_and_hasher(n, hash_builder) }
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn with_hasher(hash_builder: S) -> Self
where S: BuildHasher
{
IndexSet { map: IndexMap::with_hasher(hash_builder) }
}
pub fn hasher(&self) -> &S
where S: BuildHasher
{
self.map.hasher()
}
pub fn capacity(&self) -> usize {
self.map.capacity()
}
}
impl<T, S> IndexSet<T, S>
where T: Hash + Eq,
S: BuildHasher,
{
pub fn clear(&mut self) {
self.map.clear();
}
pub fn reserve(&mut self, additional: usize) {
self.map.reserve(additional);
}
pub fn insert(&mut self, value: T) -> bool {
self.map.insert(value, ()).is_none()
}
pub fn insert_full(&mut self, value: T) -> (usize, bool) {
use super::map::Entry::*;
match self.map.entry(value) {
Occupied(e) => (e.index(), false),
Vacant(e) => {
let index = e.index();
e.insert(());
(index, true)
}
}
}
pub fn iter(&self) -> Iter<T> {
Iter {
iter: self.map.keys().iter
}
}
pub fn difference<'a, S2>(&'a self, other: &'a IndexSet<T, S2>) -> Difference<'a, T, S2>
where S2: BuildHasher
{
Difference {
iter: self.iter(),
other,
}
}
pub fn symmetric_difference<'a, S2>(&'a self, other: &'a IndexSet<T, S2>)
-> SymmetricDifference<'a, T, S, S2>
where S2: BuildHasher
{
SymmetricDifference {
iter: self.difference(other).chain(other.difference(self)),
}
}
pub fn intersection<'a, S2>(&'a self, other: &'a IndexSet<T, S2>) -> Intersection<'a, T, S2>
where S2: BuildHasher
{
Intersection {
iter: self.iter(),
other,
}
}
pub fn union<'a, S2>(&'a self, other: &'a IndexSet<T, S2>) -> Union<'a, T, S>
where S2: BuildHasher
{
Union {
iter: self.iter().chain(other.difference(self)),
}
}
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
where Q: Hash + Equivalent<T>,
{
self.map.contains_key(value)
}
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
where Q: Hash + Equivalent<T>,
{
self.map.get_full(value).map(|(_, x, &())| x)
}
pub fn get_full<Q: ?Sized>(&self, value: &Q) -> Option<(usize, &T)>
where Q: Hash + Equivalent<T>,
{
self.map.get_full(value).map(|(i, x, &())| (i, x))
}
pub fn replace(&mut self, value: T) -> Option<T>
{
use super::map::Entry::*;
match self.map.entry(value) {
Vacant(e) => { e.insert(()); None },
Occupied(e) => Some(e.replace_key()),
}
}
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
where Q: Hash + Equivalent<T>,
{
self.swap_remove(value)
}
pub fn swap_remove<Q: ?Sized>(&mut self, value: &Q) -> bool
where Q: Hash + Equivalent<T>,
{
self.map.swap_remove(value).is_some()
}
pub fn shift_remove<Q: ?Sized>(&mut self, value: &Q) -> bool
where Q: Hash + Equivalent<T>,
{
self.map.shift_remove(value).is_some()
}
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
where Q: Hash + Equivalent<T>,
{
self.swap_take(value)
}
pub fn swap_take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
where Q: Hash + Equivalent<T>,
{
self.map.swap_remove_full(value).map(|(_, x, ())| x)
}
pub fn shift_take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
where Q: Hash + Equivalent<T>,
{
self.map.shift_remove_full(value).map(|(_, x, ())| x)
}
pub fn swap_remove_full<Q: ?Sized>(&mut self, value: &Q) -> Option<(usize, T)>
where Q: Hash + Equivalent<T>,
{
self.map.swap_remove_full(value).map(|(i, x, ())| (i, x))
}
pub fn shift_remove_full<Q: ?Sized>(&mut self, value: &Q) -> Option<(usize, T)>
where Q: Hash + Equivalent<T>,
{
self.map.shift_remove_full(value).map(|(i, x, ())| (i, x))
}
pub fn pop(&mut self) -> Option<T> {
self.map.pop().map(|(x, ())| x)
}
pub fn retain<F>(&mut self, mut keep: F)
where F: FnMut(&T) -> bool,
{
self.map.retain(move |x, &mut ()| keep(x))
}
pub fn sort(&mut self)
where T: Ord,
{
self.map.sort_keys()
}
pub fn sort_by<F>(&mut self, mut compare: F)
where F: FnMut(&T, &T) -> Ordering,
{
self.map.sort_by(move |a, _, b, _| compare(a, b));
}
pub fn sorted_by<F>(self, mut cmp: F) -> IntoIter<T>
where F: FnMut(&T, &T) -> Ordering
{
IntoIter {
iter: self.map.sorted_by(move |a, &(), b, &()| cmp(a, b)).iter,
}
}
pub fn drain(&mut self, range: RangeFull) -> Drain<T> {
Drain {
iter: self.map.drain(range).iter,
}
}
}
impl<T, S> IndexSet<T, S> {
pub fn get_index(&self, index: usize) -> Option<&T> {
self.map.get_index(index).map(|(x, &())| x)
}
pub fn swap_remove_index(&mut self, index: usize) -> Option<T> {
self.map.swap_remove_index(index).map(|(x, ())| x)
}
pub fn shift_remove_index(&mut self, index: usize) -> Option<T> {
self.map.shift_remove_index(index).map(|(x, ())| x)
}
}
pub struct IntoIter<T> {
iter: vec::IntoIter<Bucket<T>>,
}
impl<T> Iterator for IntoIter<T> {
type Item = T;
iterator_methods!(Bucket::key);
}
impl<T> DoubleEndedIterator for IntoIter<T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::key)
}
}
impl<T> ExactSizeIterator for IntoIter<T> {
fn len(&self) -> usize {
self.iter.len()
}
}
impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
f.debug_list().entries(iter).finish()
}
}
pub struct Iter<'a, T: 'a> {
iter: slice::Iter<'a, Bucket<T>>,
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
iterator_methods!(Bucket::key_ref);
}
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::key_ref)
}
}
impl<'a, T> ExactSizeIterator for Iter<'a, T> {
fn len(&self) -> usize {
self.iter.len()
}
}
impl<'a, T> Clone for Iter<'a, T> {
fn clone(&self) -> Self {
Iter { iter: self.iter.clone() }
}
}
impl<'a, T: fmt::Debug> fmt::Debug for Iter<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
pub struct Drain<'a, T: 'a> {
iter: vec::Drain<'a, Bucket<T>>,
}
impl<'a, T> Iterator for Drain<'a, T> {
type Item = T;
iterator_methods!(Bucket::key);
}
impl<'a, T> DoubleEndedIterator for Drain<'a, T> {
double_ended_iterator_methods!(Bucket::key);
}
impl<'a, T, S> IntoIterator for &'a IndexSet<T, S>
where T: Hash + Eq,
S: BuildHasher,
{
type Item = &'a T;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T, S> IntoIterator for IndexSet<T, S>
where T: Hash + Eq,
S: BuildHasher,
{
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
IntoIter {
iter: self.map.into_iter().iter,
}
}
}
impl<T, S> FromIterator<T> for IndexSet<T, S>
where T: Hash + Eq,
S: BuildHasher + Default,
{
fn from_iter<I: IntoIterator<Item=T>>(iterable: I) -> Self {
let iter = iterable.into_iter().map(|x| (x, ()));
IndexSet { map: IndexMap::from_iter(iter) }
}
}
impl<T, S> Extend<T> for IndexSet<T, S>
where T: Hash + Eq,
S: BuildHasher,
{
fn extend<I: IntoIterator<Item=T>>(&mut self, iterable: I) {
let iter = iterable.into_iter().map(|x| (x, ()));
self.map.extend(iter);
}
}
impl<'a, T, S> Extend<&'a T> for IndexSet<T, S>
where T: Hash + Eq + Copy + 'a,
S: BuildHasher,
{
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iterable: I) {
let iter = iterable.into_iter().cloned();
self.extend(iter);
}
}
impl<T, S> Default for IndexSet<T, S>
where S: BuildHasher + Default,
{
fn default() -> Self {
IndexSet { map: IndexMap::default() }
}
}
impl<T, S1, S2> PartialEq<IndexSet<T, S2>> for IndexSet<T, S1>
where T: Hash + Eq,
S1: BuildHasher,
S2: BuildHasher
{
fn eq(&self, other: &IndexSet<T, S2>) -> bool {
self.len() == other.len() && self.is_subset(other)
}
}
impl<T, S> Eq for IndexSet<T, S>
where T: Eq + Hash,
S: BuildHasher
{
}
impl<T, S> IndexSet<T, S>
where T: Eq + Hash,
S: BuildHasher
{
pub fn is_disjoint<S2>(&self, other: &IndexSet<T, S2>) -> bool
where S2: BuildHasher
{
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<S2>(&self, other: &IndexSet<T, S2>) -> bool
where S2: BuildHasher
{
self.len() <= other.len() && self.iter().all(move |value| other.contains(value))
}
pub fn is_superset<S2>(&self, other: &IndexSet<T, S2>) -> bool
where S2: BuildHasher
{
other.is_subset(self)
}
}
pub struct Difference<'a, T: 'a, S: 'a> {
iter: Iter<'a, T>,
other: &'a IndexSet<T, S>,
}
impl<'a, T, S> Iterator for Difference<'a, T, S>
where T: Eq + Hash,
S: BuildHasher
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
while let Some(item) = self.iter.next() {
if !self.other.contains(item) {
return Some(item);
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, self.iter.size_hint().1)
}
}
impl<'a, T, S> DoubleEndedIterator for Difference<'a, T, S>
where T: Eq + Hash,
S: BuildHasher
{
fn next_back(&mut self) -> Option<Self::Item> {
while let Some(item) = self.iter.next_back() {
if !self.other.contains(item) {
return Some(item);
}
}
None
}
}
impl<'a, T, S> Clone for Difference<'a, T, S> {
fn clone(&self) -> Self {
Difference { iter: self.iter.clone(), ..*self }
}
}
impl<'a, T, S> fmt::Debug for Difference<'a, T, S>
where T: fmt::Debug + Eq + Hash,
S: BuildHasher
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
pub struct Intersection<'a, T: 'a, S: 'a> {
iter: Iter<'a, T>,
other: &'a IndexSet<T, S>,
}
impl<'a, T, S> Iterator for Intersection<'a, T, S>
where T: Eq + Hash,
S: BuildHasher
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
while let Some(item) = self.iter.next() {
if self.other.contains(item) {
return Some(item);
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, self.iter.size_hint().1)
}
}
impl<'a, T, S> DoubleEndedIterator for Intersection<'a, T, S>
where T: Eq + Hash,
S: BuildHasher
{
fn next_back(&mut self) -> Option<Self::Item> {
while let Some(item) = self.iter.next_back() {
if self.other.contains(item) {
return Some(item);
}
}
None
}
}
impl<'a, T, S> Clone for Intersection<'a, T, S> {
fn clone(&self) -> Self {
Intersection { iter: self.iter.clone(), ..*self }
}
}
impl<'a, T, S> fmt::Debug for Intersection<'a, T, S>
where T: fmt::Debug + Eq + Hash,
S: BuildHasher,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
pub struct SymmetricDifference<'a, T: 'a, S1: 'a, S2: 'a> {
iter: Chain<Difference<'a, T, S2>, Difference<'a, T, S1>>,
}
impl<'a, T, S1, S2> Iterator for SymmetricDifference<'a, T, S1, S2>
where T: Eq + Hash,
S1: BuildHasher,
S2: BuildHasher,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn fold<B, F>(self, init: B, f: F) -> B
where F: FnMut(B, Self::Item) -> B
{
self.iter.fold(init, f)
}
}
impl<'a, T, S1, S2> DoubleEndedIterator for SymmetricDifference<'a, T, S1, S2>
where T: Eq + Hash,
S1: BuildHasher,
S2: BuildHasher,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a, T, S1, S2> Clone for SymmetricDifference<'a, T, S1, S2> {
fn clone(&self) -> Self {
SymmetricDifference { iter: self.iter.clone() }
}
}
impl<'a, T, S1, S2> fmt::Debug for SymmetricDifference<'a, T, S1, S2>
where T: fmt::Debug + Eq + Hash,
S1: BuildHasher,
S2: BuildHasher,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
pub struct Union<'a, T: 'a, S: 'a> {
iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
}
impl<'a, T, S> Iterator for Union<'a, T, S>
where T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn fold<B, F>(self, init: B, f: F) -> B
where F: FnMut(B, Self::Item) -> B
{
self.iter.fold(init, f)
}
}
impl<'a, T, S> DoubleEndedIterator for Union<'a, T, S>
where T: Eq + Hash,
S: BuildHasher,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a, T, S> Clone for Union<'a, T, S> {
fn clone(&self) -> Self {
Union { iter: self.iter.clone() }
}
}
impl<'a, T, S> fmt::Debug for Union<'a, T, S>
where T: fmt::Debug + Eq + Hash,
S: BuildHasher,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<'a, 'b, T, S1, S2> BitAnd<&'b IndexSet<T, S2>> for &'a IndexSet<T, S1>
where T: Eq + Hash + Clone,
S1: BuildHasher + Default,
S2: BuildHasher,
{
type Output = IndexSet<T, S1>;
fn bitand(self, other: &'b IndexSet<T, S2>) -> Self::Output {
self.intersection(other).cloned().collect()
}
}
impl<'a, 'b, T, S1, S2> BitOr<&'b IndexSet<T, S2>> for &'a IndexSet<T, S1>
where T: Eq + Hash + Clone,
S1: BuildHasher + Default,
S2: BuildHasher,
{
type Output = IndexSet<T, S1>;
fn bitor(self, other: &'b IndexSet<T, S2>) -> Self::Output {
self.union(other).cloned().collect()
}
}
impl<'a, 'b, T, S1, S2> BitXor<&'b IndexSet<T, S2>> for &'a IndexSet<T, S1>
where T: Eq + Hash + Clone,
S1: BuildHasher + Default,
S2: BuildHasher,
{
type Output = IndexSet<T, S1>;
fn bitxor(self, other: &'b IndexSet<T, S2>) -> Self::Output {
self.symmetric_difference(other).cloned().collect()
}
}
impl<'a, 'b, T, S1, S2> Sub<&'b IndexSet<T, S2>> for &'a IndexSet<T, S1>
where T: Eq + Hash + Clone,
S1: BuildHasher + Default,
S2: BuildHasher,
{
type Output = IndexSet<T, S1>;
fn sub(self, other: &'b IndexSet<T, S2>) -> Self::Output {
self.difference(other).cloned().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use util::enumerate;
#[test]
fn it_works() {
let mut set = IndexSet::new();
assert_eq!(set.is_empty(), true);
set.insert(1);
set.insert(1);
assert_eq!(set.len(), 1);
assert!(set.get(&1).is_some());
assert_eq!(set.is_empty(), false);
}
#[test]
fn new() {
let set = IndexSet::<String>::new();
println!("{:?}", set);
assert_eq!(set.capacity(), 0);
assert_eq!(set.len(), 0);
assert_eq!(set.is_empty(), true);
}
#[test]
fn insert() {
let insert = [0, 4, 2, 12, 8, 7, 11, 5];
let not_present = [1, 3, 6, 9, 10];
let mut set = IndexSet::with_capacity(insert.len());
for (i, &elt) in enumerate(&insert) {
assert_eq!(set.len(), i);
set.insert(elt);
assert_eq!(set.len(), i + 1);
assert_eq!(set.get(&elt), Some(&elt));
}
println!("{:?}", set);
for &elt in ¬_present {
assert!(set.get(&elt).is_none());
}
}
#[test]
fn insert_full() {
let insert = vec![9, 2, 7, 1, 4, 6, 13];
let present = vec![1, 6, 2];
let mut set = IndexSet::with_capacity(insert.len());
for (i, &elt) in enumerate(&insert) {
assert_eq!(set.len(), i);
let (index, success) = set.insert_full(elt);
assert!(success);
assert_eq!(Some(index), set.get_full(&elt).map(|x| x.0));
assert_eq!(set.len(), i + 1);
}
let len = set.len();
for &elt in &present {
let (index, success) = set.insert_full(elt);
assert!(!success);
assert_eq!(Some(index), set.get_full(&elt).map(|x| x.0));
assert_eq!(set.len(), len);
}
}
#[test]
fn insert_2() {
let mut set = IndexSet::with_capacity(16);
let mut values = vec![];
values.extend(0..16);
values.extend(128..267);
for &i in &values {
let old_set = set.clone();
set.insert(i);
for value in old_set.iter() {
if set.get(value).is_none() {
println!("old_set: {:?}", old_set);
println!("set: {:?}", set);
panic!("did not find {} in set", value);
}
}
}
for &i in &values {
assert!(set.get(&i).is_some(), "did not find {}", i);
}
}
#[test]
fn insert_dup() {
let mut elements = vec![0, 2, 4, 6, 8];
let mut set: IndexSet<u8> = elements.drain(..).collect();
{
let (i, v) = set.get_full(&0).unwrap();
assert_eq!(set.len(), 5);
assert_eq!(i, 0);
assert_eq!(*v, 0);
}
{
let inserted = set.insert(0);
let (i, v) = set.get_full(&0).unwrap();
assert_eq!(set.len(), 5);
assert_eq!(inserted, false);
assert_eq!(i, 0);
assert_eq!(*v, 0);
}
}
#[test]
fn insert_order() {
let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23];
let mut set = IndexSet::new();
for &elt in &insert {
set.insert(elt);
}
assert_eq!(set.iter().count(), set.len());
assert_eq!(set.iter().count(), insert.len());
for (a, b) in insert.iter().zip(set.iter()) {
assert_eq!(a, b);
}
for (i, v) in (0..insert.len()).zip(set.iter()) {
assert_eq!(set.get_index(i).unwrap(), v);
}
}
#[test]
fn grow() {
let insert = [0, 4, 2, 12, 8, 7, 11];
let not_present = [1, 3, 6, 9, 10];
let mut set = IndexSet::with_capacity(insert.len());
for (i, &elt) in enumerate(&insert) {
assert_eq!(set.len(), i);
set.insert(elt);
assert_eq!(set.len(), i + 1);
assert_eq!(set.get(&elt), Some(&elt));
}
println!("{:?}", set);
for &elt in &insert {
set.insert(elt * 10);
}
for &elt in &insert {
set.insert(elt * 100);
}
for (i, &elt) in insert.iter().cycle().enumerate().take(100) {
set.insert(elt * 100 + i as i32);
}
println!("{:?}", set);
for &elt in ¬_present {
assert!(set.get(&elt).is_none());
}
}
#[test]
fn remove() {
let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23];
let mut set = IndexSet::new();
for &elt in &insert {
set.insert(elt);
}
assert_eq!(set.iter().count(), set.len());
assert_eq!(set.iter().count(), insert.len());
for (a, b) in insert.iter().zip(set.iter()) {
assert_eq!(a, b);
}
let remove_fail = [99, 77];
let remove = [4, 12, 8, 7];
for &value in &remove_fail {
assert!(set.swap_remove_full(&value).is_none());
}
println!("{:?}", set);
for &value in &remove {
let index = set.get_full(&value).unwrap().0;
assert_eq!(set.swap_remove_full(&value), Some((index, value)));
}
println!("{:?}", set);
for value in &insert {
assert_eq!(set.get(value).is_some(), !remove.contains(value));
}
assert_eq!(set.len(), insert.len() - remove.len());
assert_eq!(set.iter().count(), insert.len() - remove.len());
}
#[test]
fn swap_remove_index() {
let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23];
let mut set = IndexSet::new();
for &elt in &insert {
set.insert(elt);
}
let mut vector = insert.to_vec();
let remove_sequence = &[3, 3, 10, 4, 5, 4, 3, 0, 1];
for &rm in remove_sequence {
let out_vec = vector.swap_remove(rm);
let out_set = set.swap_remove_index(rm).unwrap();
assert_eq!(out_vec, out_set);
}
assert_eq!(vector.len(), set.len());
for (a, b) in vector.iter().zip(set.iter()) {
assert_eq!(a, b);
}
}
#[test]
fn partial_eq_and_eq() {
let mut set_a = IndexSet::new();
set_a.insert(1);
set_a.insert(2);
let mut set_b = set_a.clone();
assert_eq!(set_a, set_b);
set_b.swap_remove(&1);
assert_ne!(set_a, set_b);
let set_c: IndexSet<_> = set_b.into_iter().collect();
assert_ne!(set_a, set_c);
assert_ne!(set_c, set_a);
}
#[test]
fn extend() {
let mut set = IndexSet::new();
set.extend(vec![&1, &2, &3, &4]);
set.extend(vec![5, 6]);
assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![1, 2, 3, 4, 5, 6]);
}
#[test]
fn comparisons() {
let set_a: IndexSet<_> = (0..3).collect();
let set_b: IndexSet<_> = (3..6).collect();
let set_c: IndexSet<_> = (0..6).collect();
let set_d: IndexSet<_> = (3..9).collect();
assert!(!set_a.is_disjoint(&set_a));
assert!(set_a.is_subset(&set_a));
assert!(set_a.is_superset(&set_a));
assert!(set_a.is_disjoint(&set_b));
assert!(set_b.is_disjoint(&set_a));
assert!(!set_a.is_subset(&set_b));
assert!(!set_b.is_subset(&set_a));
assert!(!set_a.is_superset(&set_b));
assert!(!set_b.is_superset(&set_a));
assert!(!set_a.is_disjoint(&set_c));
assert!(!set_c.is_disjoint(&set_a));
assert!(set_a.is_subset(&set_c));
assert!(!set_c.is_subset(&set_a));
assert!(!set_a.is_superset(&set_c));
assert!(set_c.is_superset(&set_a));
assert!(!set_c.is_disjoint(&set_d));
assert!(!set_d.is_disjoint(&set_c));
assert!(!set_c.is_subset(&set_d));
assert!(!set_d.is_subset(&set_c));
assert!(!set_c.is_superset(&set_d));
assert!(!set_d.is_superset(&set_c));
}
#[test]
fn iter_comparisons() {
use std::iter::empty;
fn check<'a, I1, I2>(iter1: I1, iter2: I2)
where I1: Iterator<Item = &'a i32>,
I2: Iterator<Item = i32>,
{
assert!(iter1.cloned().eq(iter2));
}
let set_a: IndexSet<_> = (0..3).collect();
let set_b: IndexSet<_> = (3..6).collect();
let set_c: IndexSet<_> = (0..6).collect();
let set_d: IndexSet<_> = (3..9).rev().collect();
check(set_a.difference(&set_a), empty());
check(set_a.symmetric_difference(&set_a), empty());
check(set_a.intersection(&set_a), 0..3);
check(set_a.union(&set_a), 0..3);
check(set_a.difference(&set_b), 0..3);
check(set_b.difference(&set_a), 3..6);
check(set_a.symmetric_difference(&set_b), 0..6);
check(set_b.symmetric_difference(&set_a), (3..6).chain(0..3));
check(set_a.intersection(&set_b), empty());
check(set_b.intersection(&set_a), empty());
check(set_a.union(&set_b), 0..6);
check(set_b.union(&set_a), (3..6).chain(0..3));
check(set_a.difference(&set_c), empty());
check(set_c.difference(&set_a), 3..6);
check(set_a.symmetric_difference(&set_c), 3..6);
check(set_c.symmetric_difference(&set_a), 3..6);
check(set_a.intersection(&set_c), 0..3);
check(set_c.intersection(&set_a), 0..3);
check(set_a.union(&set_c), 0..6);
check(set_c.union(&set_a), 0..6);
check(set_c.difference(&set_d), 0..3);
check(set_d.difference(&set_c), (6..9).rev());
check(set_c.symmetric_difference(&set_d), (0..3).chain((6..9).rev()));
check(set_d.symmetric_difference(&set_c), (6..9).rev().chain(0..3));
check(set_c.intersection(&set_d), 3..6);
check(set_d.intersection(&set_c), (3..6).rev());
check(set_c.union(&set_d), (0..6).chain((6..9).rev()));
check(set_d.union(&set_c), (3..9).rev().chain(0..3));
}
#[test]
fn ops() {
let empty = IndexSet::<i32>::new();
let set_a: IndexSet<_> = (0..3).collect();
let set_b: IndexSet<_> = (3..6).collect();
let set_c: IndexSet<_> = (0..6).collect();
let set_d: IndexSet<_> = (3..9).rev().collect();
#[cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints, eq_op))]
{
assert_eq!(&set_a & &set_a, set_a);
assert_eq!(&set_a | &set_a, set_a);
assert_eq!(&set_a ^ &set_a, empty);
assert_eq!(&set_a - &set_a, empty);
}
assert_eq!(&set_a & &set_b, empty);
assert_eq!(&set_b & &set_a, empty);
assert_eq!(&set_a | &set_b, set_c);
assert_eq!(&set_b | &set_a, set_c);
assert_eq!(&set_a ^ &set_b, set_c);
assert_eq!(&set_b ^ &set_a, set_c);
assert_eq!(&set_a - &set_b, set_a);
assert_eq!(&set_b - &set_a, set_b);
assert_eq!(&set_a & &set_c, set_a);
assert_eq!(&set_c & &set_a, set_a);
assert_eq!(&set_a | &set_c, set_c);
assert_eq!(&set_c | &set_a, set_c);
assert_eq!(&set_a ^ &set_c, set_b);
assert_eq!(&set_c ^ &set_a, set_b);
assert_eq!(&set_a - &set_c, empty);
assert_eq!(&set_c - &set_a, set_b);
assert_eq!(&set_c & &set_d, set_b);
assert_eq!(&set_d & &set_c, set_b);
assert_eq!(&set_c | &set_d, &set_a | &set_d);
assert_eq!(&set_d | &set_c, &set_a | &set_d);
assert_eq!(&set_c ^ &set_d, &set_a | &(&set_d - &set_b));
assert_eq!(&set_d ^ &set_c, &set_a | &(&set_d - &set_b));
assert_eq!(&set_c - &set_d, set_a);
assert_eq!(&set_d - &set_c, &set_d - &set_b);
}
}