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
11pub struct BTreeSet<T, C> {
23 map: BTreeMap<T, (), C>,
24}
25
26impl<T, C> BTreeSet<T, C> {
27 #[inline]
38 pub fn new() -> Self
39 where
40 C: Default,
41 {
42 Self::default()
43 }
44
45 #[inline]
58 pub fn len(&self) -> usize {
59 self.map.len()
60 }
61
62 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[inline]
444 pub fn first(&self) -> Option<&T> {
445 self.map.first_key_value().map(|(k, _)| k)
446 }
447
448 #[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 #[inline]
487 pub fn clear(&mut self)
488 where
489 C: cc_traits::Clear,
490 {
491 self.map.clear()
492 }
493
494 #[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 #[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 #[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 #[inline]
589 pub fn replace(&mut self, value: T) -> Option<T> {
590 self.map.replace(value, ()).map(|(t, ())| t)
591 }
592
593 #[inline]
610 pub fn pop_first(&mut self) -> Option<T> {
611 self.map.pop_first().map(|kv| kv.0)
612 }
613
614 #[inline]
631 pub fn pop_last(&mut self) -> Option<T> {
632 self.map.pop_last().map(|kv| kv.0)
633 }
634
635 #[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 #[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 #[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 {}