btree_slab/generic/
set.rs

1use crate::generic::{map, node::Node, BTreeMap};
2use cc_traits::{SimpleCollectionMut, SimpleCollectionRef, Slab, SlabMut};
3use std::{
4	borrow::Borrow,
5	cmp::Ordering,
6	hash::{Hash, Hasher},
7	iter::{DoubleEndedIterator, ExactSizeIterator, FromIterator, FusedIterator, Peekable},
8	ops::RangeBounds,
9};
10
11/// A set based on a B-Tree.
12///
13/// See [`BTreeMap`]'s documentation for a detailed discussion of this collection's performance benefits and drawbacks.
14///
15/// It is a logic error for an item to be modified in such a way that the item's ordering relative
16/// to any other item, as determined by the [`Ord`] trait, changes while it is in the set. This is
17/// normally only possible through [`Cell`], [`RefCell`], global state, I/O, or unsafe code.
18///
19/// [`Ord`]: core::cmp::Ord
20/// [`Cell`]: core::cell::Cell
21/// [`RefCell`]: core::cell::RefCell
22pub struct BTreeSet<T, C> {
23	map: BTreeMap<T, (), C>,
24}
25
26impl<T, C> BTreeSet<T, C> {
27	/// Makes a new, empty `BTreeSet`.
28	///
29	/// # Example
30	///
31	/// ```
32	/// # #![allow(unused_mut)]
33	/// use btree_slab::BTreeSet;
34	///
35	/// let mut set: BTreeSet<i32> = BTreeSet::new();
36	/// ```
37	#[inline]
38	pub fn new() -> Self
39	where
40		C: Default,
41	{
42		Self::default()
43	}
44
45	/// Returns the number of elements in the set.
46	///
47	/// # Example
48	///
49	/// ```
50	/// use btree_slab::BTreeSet;
51	///
52	/// let mut v = BTreeSet::new();
53	/// assert_eq!(v.len(), 0);
54	/// v.insert(1);
55	/// assert_eq!(v.len(), 1);
56	/// ```
57	#[inline]
58	pub fn len(&self) -> usize {
59		self.map.len()
60	}
61
62	/// Returns `true` if the set contains no elements.
63	///
64	/// # Example
65	///
66	/// ```
67	/// use btree_slab::BTreeSet;
68	///
69	/// let mut v = BTreeSet::new();
70	/// assert!(v.is_empty());
71	/// v.insert(1);
72	/// assert!(!v.is_empty());
73	/// ```
74	#[inline]
75	pub fn is_empty(&self) -> bool {
76		self.len() == 0
77	}
78}
79
80impl<T, C: Default> Default for BTreeSet<T, C> {
81	fn default() -> Self {
82		BTreeSet {
83			map: BTreeMap::default(),
84		}
85	}
86}
87
88impl<T, C: Slab<Node<T, ()>>> BTreeSet<T, C>
89where
90	C: SimpleCollectionRef,
91{
92	/// Gets an iterator that visits the values in the `BTreeSet` in ascending order.
93	///
94	/// # Examples
95	///
96	/// ```
97	/// use btree_slab::BTreeSet;
98	///
99	/// let set: BTreeSet<usize> = [1, 2, 3].iter().cloned().collect();
100	/// let mut set_iter = set.iter();
101	/// assert_eq!(set_iter.next(), Some(&1));
102	/// assert_eq!(set_iter.next(), Some(&2));
103	/// assert_eq!(set_iter.next(), Some(&3));
104	/// assert_eq!(set_iter.next(), None);
105	/// ```
106	///
107	/// Values returned by the iterator are returned in ascending order:
108	///
109	/// ```
110	/// use btree_slab::BTreeSet;
111	///
112	/// let set: BTreeSet<usize> = [3, 1, 2].iter().cloned().collect();
113	/// let mut set_iter = set.iter();
114	/// assert_eq!(set_iter.next(), Some(&1));
115	/// assert_eq!(set_iter.next(), Some(&2));
116	/// assert_eq!(set_iter.next(), Some(&3));
117	/// assert_eq!(set_iter.next(), None);
118	/// ```
119	#[inline]
120	pub fn iter(&self) -> Iter<T, C> {
121		Iter {
122			inner: self.map.keys(),
123		}
124	}
125}
126
127impl<T: Ord, C: Slab<Node<T, ()>>> BTreeSet<T, C>
128where
129	C: SimpleCollectionRef,
130{
131	/// Returns `true` if the set contains a value.
132	///
133	/// The value may be any borrowed form of the set's value type,
134	/// but the ordering on the borrowed form *must* match the
135	/// ordering on the value type.
136	///
137	/// # Example
138	///
139	/// ```
140	/// use btree_slab::BTreeSet;
141	///
142	/// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
143	/// assert_eq!(set.contains(&1), true);
144	/// assert_eq!(set.contains(&4), false);
145	/// ```
146	#[inline]
147	pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
148	where
149		T: Borrow<Q>,
150		Q: Ord,
151	{
152		self.map.contains_key(value)
153	}
154
155	/// Returns a reference to the value in the set, if any, that is equal to the given value.
156	///
157	/// The value may be any borrowed form of the set's value type,
158	/// but the ordering on the borrowed form *must* match the
159	/// ordering on the value type.
160	///
161	/// # Example
162	///
163	/// ```
164	/// use btree_slab::BTreeSet;
165	///
166	/// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
167	/// assert_eq!(set.get(&2), Some(&2));
168	/// assert_eq!(set.get(&4), None);
169	/// ```
170	#[inline]
171	pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
172	where
173		T: Borrow<Q>,
174		Q: Ord,
175	{
176		match self.map.get_key_value(value) {
177			Some((t, ())) => Some(t),
178			None => None,
179		}
180	}
181
182	/// Constructs a double-ended iterator over a sub-range of elements in the set.
183	/// The simplest way is to use the range syntax `min..max`, thus `range(min..max)` will
184	/// yield elements from min (inclusive) to max (exclusive).
185	/// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example
186	/// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive
187	/// range from 4 to 10.
188	///
189	/// # Example
190	///
191	/// ```
192	/// use btree_slab::BTreeSet;
193	/// use std::ops::Bound::Included;
194	///
195	/// let mut set = BTreeSet::new();
196	/// set.insert(3);
197	/// set.insert(5);
198	/// set.insert(8);
199	/// for &elem in set.range((Included(&4), Included(&8))) {
200	///     println!("{}", elem);
201	/// }
202	/// assert_eq!(Some(&5), set.range(4..).next());
203	/// ```
204	#[inline]
205	pub fn range<K: ?Sized, R>(&self, range: R) -> Range<T, C>
206	where
207		K: Ord,
208		T: Borrow<K>,
209		R: RangeBounds<K>,
210	{
211		Range {
212			inner: self.map.range(range),
213		}
214	}
215
216	/// Visits the values representing the union,
217	/// i.e., all the values in `self` or `other`, without duplicates,
218	/// in ascending order.
219	///
220	/// # Example
221	///
222	/// ```
223	/// use btree_slab::BTreeSet;
224	///
225	/// let mut a = BTreeSet::new();
226	/// a.insert(1);
227	///
228	/// let mut b = BTreeSet::new();
229	/// b.insert(2);
230	///
231	/// let union: Vec<_> = a.union(&b).cloned().collect();
232	/// assert_eq!(union, [1, 2]);
233	/// ```
234	#[inline]
235	pub fn union<'a, D: Slab<Node<T, ()>>>(
236		&'a self,
237		other: &'a BTreeSet<T, D>,
238	) -> Union<'a, T, C, D>
239	where
240		D: SimpleCollectionRef,
241	{
242		Union {
243			it1: self.iter().peekable(),
244			it2: other.iter().peekable(),
245		}
246	}
247
248	/// Visits the values representing the intersection,
249	/// i.e., the values that are both in `self` and `other`,
250	/// in ascending order.
251	///
252	/// # Example
253	///
254	/// ```
255	/// use btree_slab::BTreeSet;
256	///
257	/// let mut a = BTreeSet::new();
258	/// a.insert(1);
259	/// a.insert(2);
260	///
261	/// let mut b = BTreeSet::new();
262	/// b.insert(2);
263	/// b.insert(3);
264	///
265	/// let intersection: Vec<_> = a.intersection(&b).cloned().collect();
266	/// assert_eq!(intersection, [2]);
267	/// ```
268	#[inline]
269	pub fn intersection<'a, D: Slab<Node<T, ()>>>(
270		&'a self,
271		other: &'a BTreeSet<T, D>,
272	) -> Intersection<'a, T, C, D>
273	where
274		D: SimpleCollectionRef,
275	{
276		Intersection {
277			it1: self.iter(),
278			it2: other.iter().peekable(),
279		}
280	}
281
282	/// Visits the values representing the difference,
283	/// i.e., the values that are in `self` but not in `other`,
284	/// in ascending order.
285	///
286	/// # Example
287	///
288	/// ```
289	/// use btree_slab::BTreeSet;
290	///
291	/// let mut a = BTreeSet::new();
292	/// a.insert(1);
293	/// a.insert(2);
294	///
295	/// let mut b = BTreeSet::new();
296	/// b.insert(2);
297	/// b.insert(3);
298	///
299	/// let diff: Vec<_> = a.difference(&b).cloned().collect();
300	/// assert_eq!(diff, [1]);
301	/// ```
302	#[inline]
303	pub fn difference<'a, D: Slab<Node<T, ()>>>(
304		&'a self,
305		other: &'a BTreeSet<T, D>,
306	) -> Difference<'a, T, C, D>
307	where
308		D: SimpleCollectionRef,
309	{
310		Difference {
311			it1: self.iter(),
312			it2: other.iter().peekable(),
313		}
314	}
315
316	/// Visits the values representing the symmetric difference,
317	/// i.e., the values that are in `self` or in `other` but not in both,
318	/// in ascending order.
319	///
320	/// # Example
321	///
322	/// ```
323	/// use btree_slab::BTreeSet;
324	///
325	/// let mut a = BTreeSet::new();
326	/// a.insert(1);
327	/// a.insert(2);
328	///
329	/// let mut b = BTreeSet::new();
330	/// b.insert(2);
331	/// b.insert(3);
332	///
333	/// let sym_diff: Vec<_> = a.symmetric_difference(&b).cloned().collect();
334	/// assert_eq!(sym_diff, [1, 3]);
335	/// ```
336	#[inline]
337	pub fn symmetric_difference<'a, D: Slab<Node<T, ()>>>(
338		&'a self,
339		other: &'a BTreeSet<T, D>,
340	) -> SymmetricDifference<'a, T, C, D>
341	where
342		D: SimpleCollectionRef,
343	{
344		SymmetricDifference {
345			it1: self.iter().peekable(),
346			it2: other.iter().peekable(),
347		}
348	}
349
350	/// Returns `true` if `self` has no elements in common with `other`.
351	/// This is equivalent to checking for an empty intersection.
352	///
353	/// # Example
354	///
355	/// ```
356	/// use btree_slab::BTreeSet;
357	///
358	/// let a: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
359	/// let mut b = BTreeSet::new();
360	///
361	/// assert_eq!(a.is_disjoint(&b), true);
362	/// b.insert(4);
363	/// assert_eq!(a.is_disjoint(&b), true);
364	/// b.insert(1);
365	/// assert_eq!(a.is_disjoint(&b), false);
366	/// ```
367	#[inline]
368	pub fn is_disjoint<D: Slab<Node<T, ()>>>(&self, other: &BTreeSet<T, D>) -> bool
369	where
370		D: SimpleCollectionRef,
371	{
372		self.intersection(other).next().is_none()
373	}
374
375	/// Returns `true` if the set is a subset of another,
376	/// i.e., `other` contains at least all the values in `self`.
377	///
378	/// # Example
379	///
380	/// ```
381	/// use btree_slab::BTreeSet;
382	///
383	/// let sup: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
384	/// let mut set = BTreeSet::new();
385	///
386	/// assert_eq!(set.is_subset(&sup), true);
387	/// set.insert(2);
388	/// assert_eq!(set.is_subset(&sup), true);
389	/// set.insert(4);
390	/// assert_eq!(set.is_subset(&sup), false);
391	/// ```
392	#[inline]
393	pub fn is_subset<D: Slab<Node<T, ()>>>(&self, other: &BTreeSet<T, D>) -> bool
394	where
395		D: SimpleCollectionRef,
396	{
397		self.difference(other).next().is_none()
398	}
399
400	/// Returns `true` if the set is a superset of another,
401	/// i.e., `self` contains at least all the values in `other`.
402	///
403	/// # Example
404	///
405	/// ```
406	/// use btree_slab::BTreeSet;
407	///
408	/// let sub: BTreeSet<_> = [1, 2].iter().cloned().collect();
409	/// let mut set = BTreeSet::new();
410	///
411	/// assert_eq!(set.is_superset(&sub), false);
412	///
413	/// set.insert(0);
414	/// set.insert(1);
415	/// assert_eq!(set.is_superset(&sub), false);
416	///
417	/// set.insert(2);
418	/// assert_eq!(set.is_superset(&sub), true);
419	/// ```
420	#[inline]
421	pub fn is_superset<D: Slab<Node<T, ()>>>(&self, other: &BTreeSet<T, D>) -> bool
422	where
423		D: SimpleCollectionRef,
424	{
425		other.is_subset(self)
426	}
427
428	/// Returns a reference to the first value in the set, if any.
429	/// This value is always the minimum of all values in the set.
430	///
431	/// # Example
432	///
433	/// ```
434	/// use btree_slab::BTreeSet;
435	///
436	/// let mut map = BTreeSet::new();
437	/// assert_eq!(map.first(), None);
438	/// map.insert(1);
439	/// assert_eq!(map.first(), Some(&1));
440	/// map.insert(2);
441	/// assert_eq!(map.first(), Some(&1));
442	/// ```
443	#[inline]
444	pub fn first(&self) -> Option<&T> {
445		self.map.first_key_value().map(|(k, _)| k)
446	}
447
448	/// Returns a reference to the last value in the set, if any.
449	/// This value is always the maximum of all values in the set.
450	///
451	/// # Example
452	///
453	/// ```
454	/// use btree_slab::BTreeSet;
455	///
456	/// let mut map = BTreeSet::new();
457	/// assert_eq!(map.first(), None);
458	/// map.insert(1);
459	/// assert_eq!(map.last(), Some(&1));
460	/// map.insert(2);
461	/// assert_eq!(map.last(), Some(&2));
462	/// ```
463	#[inline]
464	pub fn last(&self) -> Option<&T> {
465		self.map.last_key_value().map(|(k, _)| k)
466	}
467}
468
469impl<T: Ord, C: SlabMut<Node<T, ()>>> BTreeSet<T, C>
470where
471	C: SimpleCollectionRef,
472	C: SimpleCollectionMut,
473{
474	/// Clears the set, removing all values.
475	///
476	/// # Examples
477	///
478	/// ```
479	/// use btree_slab::BTreeSet;
480	///
481	/// let mut v = BTreeSet::new();
482	/// v.insert(1);
483	/// v.clear();
484	/// assert!(v.is_empty());
485	/// ```
486	#[inline]
487	pub fn clear(&mut self)
488	where
489		C: cc_traits::Clear,
490	{
491		self.map.clear()
492	}
493
494	/// Adds a value to the set.
495	///
496	/// If the set did not have this value present, `true` is returned.
497	///
498	/// If the set did have this value present, `false` is returned, and the
499	/// entry is not updated. See the [module-level documentation] for more.
500	///
501	/// [module-level documentation]: index.html#insert-and-complex-keys
502	///
503	/// # Example
504	///
505	/// ```
506	/// use btree_slab::BTreeSet;
507	///
508	/// let mut set = BTreeSet::new();
509	///
510	/// assert_eq!(set.insert(2), true);
511	/// assert_eq!(set.insert(2), false);
512	/// assert_eq!(set.len(), 1);
513	/// ```
514	#[inline]
515	pub fn insert(&mut self, element: T) -> bool
516	where
517		T: Ord,
518	{
519		self.map.insert(element, ()).is_none()
520	}
521
522	/// Removes a value from the set. Returns whether the value was
523	/// present in the set.
524	///
525	/// The value may be any borrowed form of the set's value type,
526	/// but the ordering on the borrowed form *must* match the
527	/// ordering on the value type.
528	///
529	/// # Example
530	///
531	/// ```
532	/// use btree_slab::BTreeSet;
533	///
534	/// let mut set = BTreeSet::new();
535	///
536	/// set.insert(2);
537	/// assert_eq!(set.remove(&2), true);
538	/// assert_eq!(set.remove(&2), false);
539	/// ```
540	#[inline]
541	pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
542	where
543		T: Borrow<Q>,
544		Q: Ord,
545	{
546		self.map.remove(value).is_some()
547	}
548
549	/// Removes and returns the value in the set, if any, that is equal to the given one.
550	///
551	/// The value may be any borrowed form of the set's value type,
552	/// but the ordering on the borrowed form *must* match the
553	/// ordering on the value type.
554	///
555	/// # Examples
556	///
557	/// ```
558	/// use btree_slab::BTreeSet;
559	///
560	/// let mut set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
561	/// assert_eq!(set.take(&2), Some(2));
562	/// assert_eq!(set.take(&2), None);
563	/// ```
564	#[inline]
565	pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
566	where
567		T: Borrow<Q>,
568		Q: Ord,
569	{
570		self.map.take(value).map(|(t, _)| t)
571	}
572
573	/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
574	/// one. Returns the replaced value.
575	///
576	/// # Example
577	///
578	/// ```
579	/// use btree_slab::BTreeSet;
580	///
581	/// let mut set = BTreeSet::new();
582	/// set.insert(Vec::<i32>::new());
583	///
584	/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
585	/// set.replace(Vec::with_capacity(10));
586	/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
587	/// ```
588	#[inline]
589	pub fn replace(&mut self, value: T) -> Option<T> {
590		self.map.replace(value, ()).map(|(t, ())| t)
591	}
592
593	/// Removes the first value from the set and returns it, if any.
594	/// The first value is always the minimum value in the set.
595	///
596	/// # Example
597	///
598	/// ```
599	/// use btree_slab::BTreeSet;
600	///
601	/// let mut set = BTreeSet::new();
602	///
603	/// set.insert(1);
604	/// while let Some(n) = set.pop_first() {
605	///     assert_eq!(n, 1);
606	/// }
607	/// assert!(set.is_empty());
608	/// ```
609	#[inline]
610	pub fn pop_first(&mut self) -> Option<T> {
611		self.map.pop_first().map(|kv| kv.0)
612	}
613
614	/// Removes the last value from the set and returns it, if any.
615	/// The last value is always the maximum value in the set.
616	///
617	/// # Example
618	///
619	/// ```
620	/// use btree_slab::BTreeSet;
621	///
622	/// let mut set = BTreeSet::new();
623	///
624	/// set.insert(1);
625	/// while let Some(n) = set.pop_last() {
626	///     assert_eq!(n, 1);
627	/// }
628	/// assert!(set.is_empty());
629	/// ```
630	#[inline]
631	pub fn pop_last(&mut self) -> Option<T> {
632		self.map.pop_last().map(|kv| kv.0)
633	}
634
635	/// Retains only the elements specified by the predicate.
636	///
637	/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
638	///
639	/// # Example
640	///
641	/// ```
642	/// use btree_slab::BTreeSet;
643	///
644	/// let xs = [1, 2, 3, 4, 5, 6];
645	/// let mut set: BTreeSet<i32> = xs.iter().cloned().collect();
646	/// // Keep only the even numbers.
647	/// set.retain(|&k| k % 2 == 0);
648	/// assert!(set.iter().eq([2, 4, 6].iter()));
649	/// ```
650	#[inline]
651	pub fn retain<F>(&mut self, mut f: F)
652	where
653		F: FnMut(&T) -> bool,
654	{
655		self.drain_filter(|v| !f(v));
656	}
657
658	/// Moves all elements from `other` into `Self`, leaving `other` empty.
659	///
660	/// # Example
661	///
662	/// ```
663	/// use btree_slab::BTreeSet;
664	///
665	/// let mut a = BTreeSet::new();
666	/// a.insert(1);
667	/// a.insert(2);
668	/// a.insert(3);
669	///
670	/// let mut b = BTreeSet::new();
671	/// b.insert(3);
672	/// b.insert(4);
673	/// b.insert(5);
674	///
675	/// a.append(&mut b);
676	///
677	/// assert_eq!(a.len(), 5);
678	/// assert_eq!(b.len(), 0);
679	///
680	/// assert!(a.contains(&1));
681	/// assert!(a.contains(&2));
682	/// assert!(a.contains(&3));
683	/// assert!(a.contains(&4));
684	/// assert!(a.contains(&5));
685	/// ```
686	#[inline]
687	pub fn append(&mut self, other: &mut Self)
688	where
689		C: Default,
690	{
691		self.map.append(&mut other.map);
692	}
693
694	/// Creates an iterator which uses a closure to determine if a value should be removed.
695	///
696	/// If the closure returns true, then the value is removed and yielded.
697	/// If the closure returns false, the value will remain in the list and will not be yielded
698	/// by the iterator.
699	///
700	/// If the iterator is only partially consumed or not consumed at all, each of the remaining
701	/// values will still be subjected to the closure and removed and dropped if it returns true.
702	///
703	/// It is unspecified how many more values will be subjected to the closure
704	/// if a panic occurs in the closure, or if a panic occurs while dropping a value, or if the
705	/// `DrainFilter` itself is leaked.
706	///
707	/// # Example
708	///
709	/// Splitting a set into even and odd values, reusing the original set:
710	///
711	/// ```
712	/// use btree_slab::BTreeSet;
713	///
714	/// let mut set: BTreeSet<i32> = (0..8).collect();
715	/// let evens: BTreeSet<_> = set.drain_filter(|v| v % 2 == 0).collect();
716	/// let odds = set;
717	/// assert_eq!(evens.into_iter().collect::<Vec<_>>(), vec![0, 2, 4, 6]);
718	/// assert_eq!(odds.into_iter().collect::<Vec<_>>(), vec![1, 3, 5, 7]);
719	/// ```
720	#[inline]
721	pub fn drain_filter<'a, F>(&'a mut self, pred: F) -> DrainFilter<'a, T, C, F>
722	where
723		F: 'a + FnMut(&T) -> bool,
724	{
725		DrainFilter::new(self, pred)
726	}
727}
728
729impl<T: Clone, C: Clone> Clone for BTreeSet<T, C> {
730	#[inline]
731	fn clone(&self) -> Self {
732		BTreeSet {
733			map: self.map.clone(),
734		}
735	}
736
737	#[inline]
738	fn clone_from(&mut self, other: &Self) {
739		self.map.clone_from(&other.map);
740	}
741}
742
743impl<T: Ord, C: SlabMut<Node<T, ()>> + Default> FromIterator<T> for BTreeSet<T, C>
744where
745	C: SimpleCollectionRef,
746	C: SimpleCollectionMut,
747{
748	#[inline]
749	fn from_iter<I>(iter: I) -> Self
750	where
751		I: IntoIterator<Item = T>,
752	{
753		let mut set = BTreeSet::new();
754		set.extend(iter);
755		set
756	}
757}
758
759impl<T, C: SlabMut<Node<T, ()>>> IntoIterator for BTreeSet<T, C>
760where
761	C: SimpleCollectionRef,
762	C: SimpleCollectionMut,
763{
764	type Item = T;
765	type IntoIter = IntoIter<T, C>;
766
767	#[inline]
768	fn into_iter(self) -> IntoIter<T, C> {
769		IntoIter {
770			inner: self.map.into_keys(),
771		}
772	}
773}
774
775impl<'a, T, C: SlabMut<Node<T, ()>>> IntoIterator for &'a BTreeSet<T, C>
776where
777	C: SimpleCollectionRef,
778{
779	type Item = &'a T;
780	type IntoIter = Iter<'a, T, C>;
781
782	#[inline]
783	fn into_iter(self) -> Iter<'a, T, C> {
784		self.iter()
785	}
786}
787
788impl<T: Ord, C: SlabMut<Node<T, ()>>> Extend<T> for BTreeSet<T, C>
789where
790	C: SimpleCollectionRef,
791	C: SimpleCollectionMut,
792{
793	#[inline]
794	fn extend<I>(&mut self, iter: I)
795	where
796		I: IntoIterator<Item = T>,
797	{
798		for t in iter {
799			self.insert(t);
800		}
801	}
802}
803
804impl<'a, T: 'a + Ord + Copy, C: SlabMut<Node<T, ()>>> Extend<&'a T> for BTreeSet<T, C>
805where
806	C: SimpleCollectionRef,
807	C: SimpleCollectionMut,
808{
809	#[inline]
810	fn extend<I>(&mut self, iter: I)
811	where
812		I: IntoIterator<Item = &'a T>,
813	{
814		self.extend(iter.into_iter().copied())
815	}
816}
817
818impl<T, L: PartialEq<T>, C: Slab<Node<T, ()>>, D: Slab<Node<L, ()>>> PartialEq<BTreeSet<L, D>>
819	for BTreeSet<T, C>
820where
821	C: SimpleCollectionRef,
822	D: SimpleCollectionRef,
823{
824	#[inline]
825	fn eq(&self, other: &BTreeSet<L, D>) -> bool {
826		self.map.eq(&other.map)
827	}
828}
829
830impl<T: Eq, C: Slab<Node<T, ()>>> Eq for BTreeSet<T, C> where C: SimpleCollectionRef {}
831
832impl<T, L: PartialOrd<T>, C: Slab<Node<T, ()>>, D: Slab<Node<L, ()>>> PartialOrd<BTreeSet<L, D>>
833	for BTreeSet<T, C>
834where
835	C: SimpleCollectionRef,
836	D: SimpleCollectionRef,
837{
838	#[inline]
839	fn partial_cmp(&self, other: &BTreeSet<L, D>) -> Option<Ordering> {
840		self.map.partial_cmp(&other.map)
841	}
842}
843
844impl<T: Ord, C: Slab<Node<T, ()>>> Ord for BTreeSet<T, C>
845where
846	C: SimpleCollectionRef,
847{
848	#[inline]
849	fn cmp(&self, other: &BTreeSet<T, C>) -> Ordering {
850		self.map.cmp(&other.map)
851	}
852}
853
854impl<T: Hash, C: Slab<Node<T, ()>>> Hash for BTreeSet<T, C>
855where
856	C: SimpleCollectionRef,
857{
858	#[inline]
859	fn hash<H: Hasher>(&self, h: &mut H) {
860		self.map.hash(h)
861	}
862}
863
864pub struct Iter<'a, T, C> {
865	inner: map::Keys<'a, T, (), C>,
866}
867
868impl<'a, T, C: Slab<Node<T, ()>>> Iterator for Iter<'a, T, C>
869where
870	C: SimpleCollectionRef,
871{
872	type Item = &'a T;
873
874	#[inline]
875	fn size_hint(&self) -> (usize, Option<usize>) {
876		self.inner.size_hint()
877	}
878
879	#[inline]
880	fn next(&mut self) -> Option<&'a T> {
881		self.inner.next()
882	}
883}
884
885impl<'a, T, C: Slab<Node<T, ()>>> DoubleEndedIterator for Iter<'a, T, C>
886where
887	C: SimpleCollectionRef,
888{
889	#[inline]
890	fn next_back(&mut self) -> Option<&'a T> {
891		self.inner.next_back()
892	}
893}
894
895impl<'a, T, C: Slab<Node<T, ()>>> FusedIterator for Iter<'a, T, C> where C: SimpleCollectionRef {}
896impl<'a, T, C: Slab<Node<T, ()>>> ExactSizeIterator for Iter<'a, T, C> where C: SimpleCollectionRef {}
897
898pub struct IntoIter<T, C> {
899	inner: map::IntoKeys<T, (), C>,
900}
901
902impl<T, C: SlabMut<Node<T, ()>>> Iterator for IntoIter<T, C>
903where
904	C: SimpleCollectionRef,
905	C: SimpleCollectionMut,
906{
907	type Item = T;
908
909	#[inline]
910	fn size_hint(&self) -> (usize, Option<usize>) {
911		self.inner.size_hint()
912	}
913
914	#[inline]
915	fn next(&mut self) -> Option<T> {
916		self.inner.next()
917	}
918}
919
920impl<T, C: SlabMut<Node<T, ()>>> DoubleEndedIterator for IntoIter<T, C>
921where
922	C: SimpleCollectionRef,
923	C: SimpleCollectionMut,
924{
925	#[inline]
926	fn next_back(&mut self) -> Option<T> {
927		self.inner.next_back()
928	}
929}
930
931impl<T, C: SlabMut<Node<T, ()>>> FusedIterator for IntoIter<T, C>
932where
933	C: SimpleCollectionRef,
934	C: SimpleCollectionMut,
935{
936}
937impl<T, C: SlabMut<Node<T, ()>>> ExactSizeIterator for IntoIter<T, C>
938where
939	C: SimpleCollectionRef,
940	C: SimpleCollectionMut,
941{
942}
943
944pub struct Union<'a, T, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>>
945where
946	C: SimpleCollectionRef,
947	D: SimpleCollectionRef,
948{
949	it1: Peekable<Iter<'a, T, C>>,
950	it2: Peekable<Iter<'a, T, D>>,
951}
952
953impl<'a, T: Ord, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>> Iterator for Union<'a, T, C, D>
954where
955	C: SimpleCollectionRef,
956	D: SimpleCollectionRef,
957{
958	type Item = &'a T;
959
960	#[inline]
961	fn size_hint(&self) -> (usize, Option<usize>) {
962		let len1 = self.it1.len();
963		let len2 = self.it2.len();
964
965		(std::cmp::min(len1, len2), Some(std::cmp::max(len1, len2)))
966	}
967
968	#[inline]
969	fn next(&mut self) -> Option<&'a T> {
970		match (self.it1.peek(), self.it2.peek()) {
971			(Some(v1), Some(v2)) => match v1.cmp(v2) {
972				Ordering::Equal => {
973					self.it1.next();
974					self.it2.next()
975				}
976				Ordering::Less => self.it1.next(),
977				Ordering::Greater => self.it2.next(),
978			},
979			(Some(_), None) => self.it1.next(),
980			(None, Some(_)) => self.it2.next(),
981			(None, None) => None,
982		}
983	}
984}
985
986impl<'a, T: Ord, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>> FusedIterator for Union<'a, T, C, D>
987where
988	C: SimpleCollectionRef,
989	D: SimpleCollectionRef,
990{
991}
992
993pub struct Intersection<'a, T, C, D: Slab<Node<T, ()>>>
994where
995	D: SimpleCollectionRef,
996{
997	it1: Iter<'a, T, C>,
998	it2: Peekable<Iter<'a, T, D>>,
999}
1000
1001impl<'a, T: Ord, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>> Iterator for Intersection<'a, T, C, D>
1002where
1003	C: SimpleCollectionRef,
1004	D: SimpleCollectionRef,
1005{
1006	type Item = &'a T;
1007
1008	#[inline]
1009	fn size_hint(&self) -> (usize, Option<usize>) {
1010		let len1 = self.it1.len();
1011		let len2 = self.it2.len();
1012
1013		(0, Some(std::cmp::min(len1, len2)))
1014	}
1015
1016	#[inline]
1017	fn next(&mut self) -> Option<&'a T> {
1018		loop {
1019			match self.it1.next() {
1020				Some(value) => {
1021					let keep = loop {
1022						match self.it2.peek() {
1023							Some(other) => match value.cmp(other) {
1024								Ordering::Equal => break true,
1025								Ordering::Greater => {
1026									self.it2.next();
1027								}
1028								Ordering::Less => break false,
1029							},
1030							None => break false,
1031						}
1032					};
1033
1034					if keep {
1035						break Some(value);
1036					}
1037				}
1038				None => break None,
1039			}
1040		}
1041	}
1042}
1043
1044impl<'a, T: Ord, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>> FusedIterator
1045	for Intersection<'a, T, C, D>
1046where
1047	C: SimpleCollectionRef,
1048	D: SimpleCollectionRef,
1049{
1050}
1051
1052pub struct Difference<'a, T, C, D: Slab<Node<T, ()>>>
1053where
1054	D: SimpleCollectionRef,
1055{
1056	it1: Iter<'a, T, C>,
1057	it2: Peekable<Iter<'a, T, D>>,
1058}
1059
1060impl<'a, T: Ord, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>> Iterator for Difference<'a, T, C, D>
1061where
1062	C: SimpleCollectionRef,
1063	D: SimpleCollectionRef,
1064{
1065	type Item = &'a T;
1066
1067	#[inline]
1068	fn size_hint(&self) -> (usize, Option<usize>) {
1069		let len1 = self.it1.len();
1070		let len2 = self.it2.len();
1071
1072		(len1.saturating_sub(len2), Some(self.it1.len()))
1073	}
1074
1075	#[inline]
1076	fn next(&mut self) -> Option<&'a T> {
1077		loop {
1078			match self.it1.next() {
1079				Some(value) => {
1080					let keep = loop {
1081						match self.it2.peek() {
1082							Some(other) => match value.cmp(other) {
1083								Ordering::Equal => break false,
1084								Ordering::Greater => {
1085									self.it2.next();
1086								}
1087								Ordering::Less => break true,
1088							},
1089							None => break true,
1090						}
1091					};
1092
1093					if keep {
1094						break Some(value);
1095					}
1096				}
1097				None => break None,
1098			}
1099		}
1100	}
1101}
1102
1103impl<'a, T: Ord, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>> FusedIterator
1104	for Difference<'a, T, C, D>
1105where
1106	C: SimpleCollectionRef,
1107	D: SimpleCollectionRef,
1108{
1109}
1110
1111pub struct SymmetricDifference<'a, T, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>>
1112where
1113	C: SimpleCollectionRef,
1114	D: SimpleCollectionRef,
1115{
1116	it1: Peekable<Iter<'a, T, C>>,
1117	it2: Peekable<Iter<'a, T, D>>,
1118}
1119
1120impl<'a, T: Ord, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>> Iterator
1121	for SymmetricDifference<'a, T, C, D>
1122where
1123	C: SimpleCollectionRef,
1124	D: SimpleCollectionRef,
1125{
1126	type Item = &'a T;
1127
1128	#[inline]
1129	fn size_hint(&self) -> (usize, Option<usize>) {
1130		let len1 = self.it1.len();
1131		let len2 = self.it2.len();
1132
1133		(0, len1.checked_add(len2))
1134	}
1135
1136	#[inline]
1137	fn next(&mut self) -> Option<&'a T> {
1138		loop {
1139			match (self.it1.peek(), self.it2.peek()) {
1140				(Some(v1), Some(v2)) => match v1.cmp(v2) {
1141					Ordering::Equal => {
1142						self.it1.next();
1143						self.it2.next();
1144					}
1145					Ordering::Less => break self.it1.next(),
1146					Ordering::Greater => break self.it2.next(),
1147				},
1148				(Some(_), None) => break self.it1.next(),
1149				(None, Some(_)) => break self.it2.next(),
1150				(None, None) => break None,
1151			}
1152		}
1153	}
1154}
1155
1156impl<'a, T: Ord, C: Slab<Node<T, ()>>, D: Slab<Node<T, ()>>> FusedIterator
1157	for SymmetricDifference<'a, T, C, D>
1158where
1159	C: SimpleCollectionRef,
1160	D: SimpleCollectionRef,
1161{
1162}
1163
1164pub struct DrainFilter<'a, T, C: SlabMut<Node<T, ()>>, F>
1165where
1166	F: FnMut(&T) -> bool,
1167	C: SimpleCollectionRef,
1168	C: SimpleCollectionMut,
1169{
1170	pred: F,
1171
1172	inner: map::DrainFilterInner<'a, T, (), C>,
1173}
1174
1175impl<'a, T: 'a, C: SlabMut<Node<T, ()>>, F> DrainFilter<'a, T, C, F>
1176where
1177	F: FnMut(&T) -> bool,
1178	C: SimpleCollectionRef,
1179	C: SimpleCollectionMut,
1180{
1181	#[inline]
1182	pub fn new(set: &'a mut BTreeSet<T, C>, pred: F) -> Self {
1183		DrainFilter {
1184			pred,
1185			inner: map::DrainFilterInner::new(&mut set.map),
1186		}
1187	}
1188}
1189
1190impl<'a, T, C: SlabMut<Node<T, ()>>, F> FusedIterator for DrainFilter<'a, T, C, F>
1191where
1192	F: FnMut(&T) -> bool,
1193	C: SimpleCollectionRef,
1194	C: SimpleCollectionMut,
1195{
1196}
1197
1198impl<'a, T, C: SlabMut<Node<T, ()>>, F> Iterator for DrainFilter<'a, T, C, F>
1199where
1200	F: FnMut(&T) -> bool,
1201	C: SimpleCollectionRef,
1202	C: SimpleCollectionMut,
1203{
1204	type Item = T;
1205
1206	#[inline]
1207	fn size_hint(&self) -> (usize, Option<usize>) {
1208		self.inner.size_hint()
1209	}
1210
1211	#[inline]
1212	fn next(&mut self) -> Option<T> {
1213		let pred = &mut self.pred;
1214		self.inner.next(&mut |t, _| (*pred)(t)).map(|(t, ())| t)
1215	}
1216}
1217
1218impl<'a, T, C: SlabMut<Node<T, ()>>, F> Drop for DrainFilter<'a, T, C, F>
1219where
1220	F: FnMut(&T) -> bool,
1221	C: SimpleCollectionRef,
1222	C: SimpleCollectionMut,
1223{
1224	fn drop(&mut self) {
1225		loop {
1226			if self.next().is_none() {
1227				break;
1228			}
1229		}
1230	}
1231}
1232
1233pub struct Range<'a, T, C> {
1234	inner: map::Range<'a, T, (), C>,
1235}
1236
1237impl<'a, T, C: Slab<Node<T, ()>>> Iterator for Range<'a, T, C>
1238where
1239	C: SimpleCollectionRef,
1240{
1241	type Item = &'a T;
1242
1243	#[inline]
1244	fn size_hint(&self) -> (usize, Option<usize>) {
1245		self.inner.size_hint()
1246	}
1247
1248	#[inline]
1249	fn next(&mut self) -> Option<&'a T> {
1250		self.inner.next().map(|(k, ())| k)
1251	}
1252}
1253
1254impl<'a, T, C: Slab<Node<T, ()>>> DoubleEndedIterator for Range<'a, T, C>
1255where
1256	C: SimpleCollectionRef,
1257{
1258	#[inline]
1259	fn next_back(&mut self) -> Option<&'a T> {
1260		self.inner.next_back().map(|(k, ())| k)
1261	}
1262}
1263
1264impl<'a, T, C: Slab<Node<T, ()>>> FusedIterator for Range<'a, T, C> where C: SimpleCollectionRef {}