1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
//! A set based on splay tree.
use crate::iter;
use crate::tree_core;
use crate::vec_like;
use std::borrow::Borrow;
use std::cmp;
use std::iter::Peekable;
use std::ops;

/// A set based on splay tree.
///
/// A splay tree based set is a self-adjusting data structure.
/// It performs insertion, removal and look-up in `O(log n)` amortized time.
///
/// It is a logic error for a key to be modified in such a way that
/// the key's ordering relative to any other key,
/// as determined by the `Ord` trait, changes while it is in the map.
/// This is normally only possible through `Cell`, `RefCell`, global state, I/O, or unsafe code.
///
/// # Examples
/// ```
/// use splay_tree::SplaySet;
///
/// let mut set = SplaySet::new();
///
/// set.insert("foo");
/// set.insert("bar");
/// set.insert("baz");
/// assert_eq!(set.len(), 3);
///
/// assert!(set.contains("bar"));
/// assert!(set.remove("bar"));
/// assert!(!set.contains("bar"));
///
/// assert_eq!(vec!["baz", "foo"], set.into_iter().collect::<Vec<_>>());
/// ```
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SplaySet<T> {
    tree: tree_core::Tree<T, ()>,
}
impl<T> SplaySet<T>
where
    T: Ord,
{
    /// Makes a new SplaySet
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let set: SplaySet<()> = SplaySet::new();
    /// assert!(set.is_empty());
    /// ```
    pub fn new() -> Self {
        SplaySet {
            tree: tree_core::Tree::new(),
        }
    }

    /// Clears the set, removing all values.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.clear();
    /// assert!(set.is_empty());
    /// ```
    pub fn clear(&mut self) {
        self.tree = tree_core::Tree::new();
    }

    /// Returns true if the set contains a value.
    ///
    /// The value may be any borrowed form of the set's value type,
    /// but the ordering on the borrowed form _must_ match the ordering on the value type.
    ///
    /// Because `SplaySet` is a self-adjusting amortized data structure,
    /// this function requires the `mut` qualifier for `self`.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// assert!(set.contains("foo"));
    /// assert!(!set.contains("bar"));
    /// ```
    pub fn contains<Q: ?Sized>(&mut self, value: &Q) -> bool
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.tree.contains_key(value)
    }

    /// Immutable version of [`SplaySet::contains()`].
    ///
    /// Note that this method could be less efficient than the mutable version.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// assert!(set.contains_immut("foo"));
    /// assert!(!set.contains_immut("bar"));
    /// ```
    pub fn contains_immut<Q: ?Sized>(&self, value: &Q) -> bool
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.tree.contains_key_immut(value)
    }

    /// Returns a reference to the value in the set, if any, that is equal to the given value.
    ///
    /// The value may be any borrowed form of the set's value type,
    /// but the ordering on the borrowed form _must_ match the ordering on the value type.
    ///
    /// Because `SplaySet` is a self-adjusting amortized data structure,
    /// this function requires the `mut` qualifier for `self`.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// assert_eq!(set.get("foo"), Some(&"foo"));
    /// assert_eq!(set.get("bar"), None);
    /// ```
    pub fn get<Q: ?Sized>(&mut self, value: &Q) -> Option<&T>
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        if self.tree.get(value).is_some() {
            Some(&self.tree.root_ref().key)
        } else {
            None
        }
    }

    /// Immutable version of [`SplaySet::get()`].
    ///
    /// Note that this method could be less efficient than the mutable version.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// assert_eq!(set.get_immut("foo"), Some(&"foo"));
    /// assert_eq!(set.get_immut("bar"), None);
    /// ```
    pub fn get_immut<Q: ?Sized>(&self, value: &Q) -> Option<&T>
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.tree.get_immut(value).map(|x| x.0)
    }

    /// Finds a minimum element which
    /// satisfies "greater than or equal to `value`" condition in the set.
    ///
    /// The value may be any borrowed form of the set's value type,
    /// but the ordering on the borrowed form _must_ match the ordering on the value type.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.find_lower_bound(&0), Some(&1));
    /// assert_eq!(set.find_lower_bound(&1), Some(&1));
    /// assert_eq!(set.find_lower_bound(&4), None);
    /// ```
    pub fn find_lower_bound<Q: ?Sized>(&mut self, value: &Q) -> Option<&T>
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.tree.find_lower_bound(value)
    }

    /// Immutable version of [`SplaySet::find_lower_bound()`].
    ///
    /// Note that this method could be less efficient than the mutable version.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.find_lower_bound_immut(&0), Some(&1));
    /// assert_eq!(set.find_lower_bound_immut(&1), Some(&1));
    /// assert_eq!(set.find_lower_bound_immut(&4), None);
    /// ```
    pub fn find_lower_bound_immut<Q: ?Sized>(&self, value: &Q) -> Option<&T>
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.tree.find_lower_bound_immut(value)
    }

    /// Finds a minimum element which satisfies "greater than `value`" condition in the set.
    ///
    /// The value may be any borrowed form of the set's value type,
    /// but the ordering on the borrowed form _must_ match the ordering on the value type.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.find_upper_bound(&0), Some(&1));
    /// assert_eq!(set.find_upper_bound(&1), Some(&3));
    /// assert_eq!(set.find_upper_bound(&4), None);
    /// ```
    pub fn find_upper_bound<Q: ?Sized>(&mut self, value: &Q) -> Option<&T>
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.tree.find_upper_bound(value)
    }

    /// Immutable version of [`SplaySet::find_upper_bound()`].
    ///
    /// Note that this method could be less efficient than the mutable version.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.find_upper_bound_immut(&0), Some(&1));
    /// assert_eq!(set.find_upper_bound_immut(&1), Some(&3));
    /// assert_eq!(set.find_upper_bound_immut(&4), None);
    /// ```
    pub fn find_upper_bound_immut<Q: ?Sized>(&self, value: &Q) -> Option<&T>
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.tree.find_upper_bound_immut(value)
    }

    /// Gets the minimum value in the map.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.smallest(), Some(&1));
    /// ```
    pub fn smallest(&mut self) -> Option<&T> {
        self.tree.get_lftmost().map(|(v, _)| v)
    }

    /// Immutable version of [`SplaySet::smallest()`].
    ///
    /// Note that this method could be less efficient than the mutable version.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.smallest_immut(), Some(&1));
    /// ```
    pub fn smallest_immut(&self) -> Option<&T> {
        self.tree.get_lftmost_immut().map(|(v, _)| v)
    }

    /// Takes the minimum value in the map.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.take_smallest(), Some(1));
    /// assert_eq!(set.take_smallest(), Some(3));
    /// assert_eq!(set.take_smallest(), None);
    /// ```
    pub fn take_smallest(&mut self) -> Option<T> {
        self.tree.take_lftmost().map(|(v, _)| v)
    }

    /// Gets the maximum value in the map.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.largest(), Some(&3));
    /// ```
    pub fn largest(&mut self) -> Option<&T> {
        self.tree.get_rgtmost().map(|(v, _)| v)
    }

    /// Immutable version of [`SplaySet::largest()`].
    ///
    /// Note that this method could be less efficient than the mutable version.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.largest_immut(), Some(&3));
    /// ```
    pub fn largest_immut(&self) -> Option<&T> {
        self.tree.get_rgtmost_immut().map(|(v, _)| v)
    }

    /// Takes the maximum value in the map.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert(1);
    /// set.insert(3);
    ///
    /// assert_eq!(set.take_largest(), Some(3));
    /// assert_eq!(set.take_largest(), Some(1));
    /// assert_eq!(set.take_largest(), None);
    /// ```
    pub fn take_largest(&mut self) -> Option<T> {
        self.tree.take_rgtmost().map(|(v, _)| v)
    }

    /// Adds a value to the set.
    ///
    /// If the set did not have this value present, `true` is returned.
    ///
    /// If the set did have this value present, `false` is returned,
    /// and the entry is not updated.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// assert!(set.insert("foo"));
    /// assert!(!set.insert("foo"));
    /// assert_eq!(set.len(), 1);
    /// ```
    pub fn insert(&mut self, value: T) -> bool {
        self.tree.insert(value, ()).is_none()
    }

    /// Adds a value to the set, replacing the existing value, if any,
    /// that is equal to the given one.
    /// Returns the replaced value.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// assert_eq!(set.replace("foo"), None);
    /// assert_eq!(set.replace("foo"), Some("foo"));
    /// ```
    pub fn replace(&mut self, value: T) -> Option<T> {
        let old = self.take(&value);
        self.insert(value);
        old
    }

    /// Removes a value from the set. Returns `true` is the value was present in the set.
    ///
    /// The value may be any borrowed form of the set's value type,
    /// but the ordering on the borrowed form _must_ match the ordering on the value type.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// assert_eq!(set.remove("foo"), true);
    /// assert_eq!(set.remove("foo"), false);
    /// ```
    pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.tree.remove(value).is_some()
    }

    /// Removes and returns the value in the set, if any, that is equal to the given one.
    ///
    /// The value may be any borrowed form of the set's value type,
    /// but the ordering on the borrowed form _must_ match the ordering on the value type.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// assert_eq!(set.take("foo"), Some("foo"));
    /// assert_eq!(set.take("foo"), None);
    /// ```
    pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        if self.contains(value) {
            self.tree.pop_root().map(|(e, _)| e)
        } else {
            None
        }
    }

    /// Visits the values representing the difference, in ascending order.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![2, 3, 4].into_iter().collect();
    ///
    /// assert_eq!(a.difference(&b).cloned().collect::<Vec<_>>(),
    ///            [1]);
    /// ```
    pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, T> {
        Difference(self.iter().peekable(), other.iter().peekable())
    }

    /// Visits the values representing the symmetric difference, in ascending order.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![2, 3, 4].into_iter().collect();
    ///
    /// assert_eq!(a.symmetric_difference(&b).cloned().collect::<Vec<_>>(),
    ///            [1, 4]);
    /// ```
    pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, T> {
        SymmetricDifference(self.iter().peekable(), other.iter().peekable())
    }

    /// Visits the values representing the intersection, in ascending order.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![2, 3, 4].into_iter().collect();
    ///
    /// assert_eq!(a.intersection(&b).cloned().collect::<Vec<_>>(),
    ///            [2, 3]);
    /// ```
    pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T> {
        Intersection(self.iter().peekable(), other.iter().peekable())
    }

    /// Visits the values representing the union, in ascending order.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![2, 3, 4].into_iter().collect();
    ///
    /// assert_eq!(a.union(&b).cloned().collect::<Vec<_>>(),
    ///            [1, 2, 3, 4]);
    /// ```
    pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T> {
        Union(self.iter().peekable(), other.iter().peekable())
    }

    /// Returns `true` if the set has no elements in common with `other`.
    /// This is equivalent to checking for an empty intersection.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![2, 3, 4].into_iter().collect();
    /// let c: SplaySet<_> = vec![4, 5, 6].into_iter().collect();
    ///
    /// assert!(!a.is_disjoint(&b));
    /// assert!(!b.is_disjoint(&c));
    /// assert!(a.is_disjoint(&c));
    /// assert!(c.is_disjoint(&a));
    /// ```
    pub fn is_disjoint(&self, other: &Self) -> bool {
        self.intersection(other).count() == 0
    }

    /// Returns `true` if the set is a subset of another.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![2, 3, 4].into_iter().collect();
    /// let c: SplaySet<_> = vec![1, 2, 3, 4].into_iter().collect();
    ///
    /// assert!(!a.is_subset(&b));
    /// assert!(!b.is_subset(&a));
    /// assert!(!c.is_subset(&a));
    /// assert!(a.is_subset(&c));
    /// assert!(b.is_subset(&c));
    /// assert!(c.is_subset(&c));
    /// ```
    pub fn is_subset(&self, other: &Self) -> bool {
        self.difference(other).count() == 0
    }

    /// Returns `true` if the set is a superset of another.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![2, 3, 4].into_iter().collect();
    /// let c: SplaySet<_> = vec![1, 2, 3, 4].into_iter().collect();
    ///
    /// assert!(!a.is_superset(&b));
    /// assert!(!b.is_superset(&a));
    /// assert!(!a.is_superset(&c));
    /// assert!(c.is_superset(&a));
    /// assert!(c.is_superset(&b));
    /// assert!(c.is_superset(&c));
    /// ```
    pub fn is_superset(&self, other: &Self) -> bool {
        other.is_subset(self)
    }

    /// Returns a vector like mutable view of the set.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// {
    ///     let mut vec = set.as_vec_like_mut();
    ///     vec.push("baz");
    ///
    ///     assert_eq!(vec.get(0), Some(&"foo"));
    ///     assert_eq!(vec.get(2), Some(&"baz"));
    ///
    ///     assert_eq!(vec.find_index(&"bar"), Some(1));
    ///
    ///     assert_eq!(vec.iter().cloned().collect::<Vec<_>>(),
    ///                ["foo", "bar", "baz"]);
    /// }
    /// assert_eq!(set.iter().cloned().collect::<Vec<_>>(),
    ///            ["bar", "baz", "foo"]);
    /// ```
    pub fn as_vec_like_mut(&mut self) -> VecLikeMut<T> {
        VecLikeMut::new(&mut self.tree)
    }
}
impl<T> SplaySet<T> {
    /// Returns the number of elements in the set.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// assert_eq!(set.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.tree.len()
    }

    /// Returns true if the set contains no elements.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// assert!(set.is_empty());
    ///
    /// set.insert("foo");
    /// assert!(!set.is_empty());
    ///
    /// set.clear();
    /// assert!(set.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Gets an iterator over the SplaySet's contents, in sorted order.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// assert_eq!(set.iter().collect::<Vec<_>>(), [&"bar", &"baz", &"foo"]);
    /// ```
    pub fn iter(&self) -> Iter<T> {
        Iter::new(self)
    }

    /// Returns a vector like view of the set.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// {
    ///     let mut vec = set.as_vec_like();
    ///     assert_eq!(vec.get(0), Some(&"foo"));
    ///     assert_eq!(vec.get(1), Some(&"bar"));
    ///
    ///     assert_eq!(vec.iter().cloned().collect::<Vec<_>>(),
    ///                ["foo", "bar"]);
    /// }
    /// assert_eq!(set.iter().cloned().collect::<Vec<_>>(),
    ///            ["bar", "foo"]);
    /// ```
    pub fn as_vec_like(&self) -> VecLike<T> {
        VecLike::new(&self.tree)
    }
}
impl<T> Default for SplaySet<T>
where
    T: Ord,
{
    fn default() -> Self {
        SplaySet::new()
    }
}
impl<T> std::iter::FromIterator<T> for SplaySet<T>
where
    T: Ord,
{
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        let mut set = SplaySet::new();
        for x in iter {
            set.insert(x);
        }
        set
    }
}
impl<T> IntoIterator for SplaySet<T> {
    type Item = T;
    type IntoIter = IntoIter<T>;
    fn into_iter(self) -> Self::IntoIter {
        IntoIter(self.tree.into_iter())
    }
}
impl<'a, T> IntoIterator for &'a SplaySet<T> {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;
    fn into_iter(self) -> Self::IntoIter {
        Iter::new(self)
    }
}
impl<T> Extend<T> for SplaySet<T>
where
    T: Ord,
{
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = T>,
    {
        for x in iter {
            self.insert(x);
        }
    }
}
impl<'a, T> Extend<&'a T> for SplaySet<T>
where
    T: Copy + 'a + Ord,
{
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = &'a T>,
    {
        for x in iter {
            self.insert(*x);
        }
    }
}
impl<'a, 'b, T> ops::Sub<&'b SplaySet<T>> for &'a SplaySet<T>
where
    T: Ord + Clone,
{
    type Output = SplaySet<T>;

    /// Returns the difference of `self` and `rhs` as a new `SplaySet<T>`.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![3, 4, 5].into_iter().collect();
    ///
    /// assert_eq!((&a - &b).into_iter().collect::<Vec<_>>(),
    ///            [1, 2]);
    /// ```
    fn sub(self, rhs: &SplaySet<T>) -> SplaySet<T> {
        self.difference(rhs).cloned().collect()
    }
}
impl<'a, 'b, T> ops::BitXor<&'b SplaySet<T>> for &'a SplaySet<T>
where
    T: Ord + Clone,
{
    type Output = SplaySet<T>;

    /// Returns the symmetric difference of `self` and `rhs` as a new `SplaySet<T>`.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![3, 4, 5].into_iter().collect();
    ///
    /// assert_eq!((&a ^ &b).into_iter().collect::<Vec<_>>(),
    ///            [1, 2, 4, 5]);
    /// ```
    fn bitxor(self, rhs: &SplaySet<T>) -> SplaySet<T> {
        self.symmetric_difference(rhs).cloned().collect()
    }
}
impl<'a, 'b, T> ops::BitAnd<&'b SplaySet<T>> for &'a SplaySet<T>
where
    T: Ord + Clone,
{
    type Output = SplaySet<T>;

    /// Returns the intersection of `self` and `rhs` as a new `SplaySet<T>`.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![3, 4, 5].into_iter().collect();
    ///
    /// assert_eq!((&a & &b).into_iter().collect::<Vec<_>>(),
    ///            [3]);
    /// ```
    fn bitand(self, rhs: &SplaySet<T>) -> SplaySet<T> {
        self.intersection(rhs).cloned().collect()
    }
}
impl<'a, 'b, T> ops::BitOr<&'b SplaySet<T>> for &'a SplaySet<T>
where
    T: Ord + Clone,
{
    type Output = SplaySet<T>;

    /// Returns the union of `self` and `rhs` as a new `SplaySet<T>`.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let a: SplaySet<_> = vec![1, 2, 3].into_iter().collect();
    /// let b: SplaySet<_> = vec![3, 4, 5].into_iter().collect();
    ///
    /// assert_eq!((&a | &b).into_iter().collect::<Vec<_>>(),
    ///            [1, 2, 3, 4, 5]);
    /// ```
    fn bitor(self, rhs: &SplaySet<T>) -> SplaySet<T> {
        self.union(rhs).cloned().collect()
    }
}

/// An Iterator over a SplaySet items.
pub struct Iter<'a, T: 'a>(iter::Iter<'a, T, ()>);
impl<'a, T: 'a> Iter<'a, T> {
    fn new(set: &'a SplaySet<T>) -> Self {
        Iter(set.tree.iter())
    }
}
impl<'a, T: 'a> Iterator for Iter<'a, T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|(e, _)| e)
    }
}

/// An owning iterator over a SplaySet's items.
pub struct IntoIter<T>(iter::IntoIter<T, ()>);
impl<T> Iterator for IntoIter<T> {
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|(e, _)| e)
    }
}

fn item_cmp<T>(a: Option<&T>, b: Option<&T>) -> Option<cmp::Ordering>
where
    T: Ord,
{
    match (a, b) {
        (None, None) => None,
        (Some(_), None) => Some(cmp::Ordering::Less),
        (None, Some(_)) => Some(cmp::Ordering::Greater),
        (Some(a), Some(b)) => Some(a.cmp(b)),
    }
}

/// A lazy iterator producing elements in the set difference (in-order).
pub struct Difference<'a, T: 'a>(Peekable<Iter<'a, T>>, Peekable<Iter<'a, T>>);
impl<'a, T: 'a> Iterator for Difference<'a, T>
where
    T: Ord,
{
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match item_cmp(self.0.peek(), self.1.peek()) {
                None => return None,
                Some(cmp::Ordering::Less) => return self.0.next(),
                Some(cmp::Ordering::Greater) => {
                    self.1.next();
                }
                Some(cmp::Ordering::Equal) => {
                    self.0.next();
                    self.1.next();
                }
            }
        }
    }
}

/// A lazy iterator producing elements in the set symmetric difference (in-order).
pub struct SymmetricDifference<'a, T: 'a>(Peekable<Iter<'a, T>>, Peekable<Iter<'a, T>>);
impl<'a, T: 'a> Iterator for SymmetricDifference<'a, T>
where
    T: Ord,
{
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match item_cmp(self.0.peek(), self.1.peek()) {
                None => return None,
                Some(cmp::Ordering::Less) => return self.0.next(),
                Some(cmp::Ordering::Greater) => return self.1.next(),
                Some(cmp::Ordering::Equal) => {
                    self.0.next();
                    self.1.next();
                }
            }
        }
    }
}

/// A lazy iterator producing elements in the set intersection (in-order).
pub struct Intersection<'a, T: 'a>(Peekable<Iter<'a, T>>, Peekable<Iter<'a, T>>);
impl<'a, T: 'a> Iterator for Intersection<'a, T>
where
    T: Ord,
{
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match item_cmp(self.0.peek(), self.1.peek()) {
                None => return None,
                Some(cmp::Ordering::Less) => {
                    self.0.next();
                }
                Some(cmp::Ordering::Greater) => {
                    self.1.next();
                }
                Some(cmp::Ordering::Equal) => {
                    self.0.next();
                    return self.1.next();
                }
            }
        }
    }
}

/// A lazy iterator producing elements in the set union (in-order).
pub struct Union<'a, T: 'a>(Peekable<Iter<'a, T>>, Peekable<Iter<'a, T>>);
impl<'a, T: 'a> Iterator for Union<'a, T>
where
    T: Ord,
{
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        match item_cmp(self.0.peek(), self.1.peek()) {
            None => None,
            Some(cmp::Ordering::Less) => self.0.next(),
            Some(cmp::Ordering::Greater) => self.1.next(),
            Some(cmp::Ordering::Equal) => {
                self.0.next();
                self.1.next()
            }
        }
    }
}

/// A vector like view of a set.
#[derive(Debug, Clone)]
pub struct VecLike<'a, T: 'a> {
    inner: vec_like::VecLike<'a, T, ()>,
}
impl<'a, T: 'a> VecLike<'a, T> {
    fn new(tree: &'a tree_core::Tree<T, ()>) -> Self {
        VecLike {
            inner: vec_like::VecLike::new(tree),
        }
    }

    /// Returns the element of the vector at the given index,
    /// or `None` if the index is out of bounds.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// let vec = set.as_vec_like();
    /// assert_eq!(vec.get(0), Some(&"foo"));
    /// assert_eq!(vec.get(1), Some(&"bar"));
    /// assert_eq!(vec.get(2), Some(&"baz"));
    /// assert_eq!(vec.get(3), None);
    /// ```
    pub fn get(&self, index: usize) -> Option<&'a T> {
        self.inner.get(index).map(|(v, _)| v)
    }

    /// Returns the first element of the vector, or `None` if it is empty.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// let vec = set.as_vec_like();
    /// assert_eq!(vec.first(), Some(&"foo"));
    /// ```
    pub fn first(&self) -> Option<&'a T> {
        self.inner.first().map(|(v, _)| v)
    }

    /// Returns the last element of the vector, or `None` if it is empty.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// let vec = set.as_vec_like();
    /// assert_eq!(vec.last(), Some(&"baz"));
    /// ```
    pub fn last(&self) -> Option<&'a T> {
        self.inner.last().map(|(v, _)| v)
    }

    /// Gets an iterator over the vector's elements, in positional order (low to high).
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// assert_eq!(set.iter().cloned().collect::<Vec<_>>(), ["bar", "baz", "foo"]);
    ///
    /// let vec = set.as_vec_like();
    /// assert_eq!(vec.iter().cloned().collect::<Vec<_>>(), ["foo", "bar", "baz"]);
    /// ```
    pub fn iter(&self) -> VecLikeIter<'a, T> {
        VecLikeIter(self.inner.iter())
    }

    /// Returns the number of elements in the vector like set.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// {
    ///     let vec = set.as_vec_like();
    ///     assert_eq!(vec.len(), 1);
    /// }
    /// ```
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns `true` if the vector like set contains no elements.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let set = SplaySet::<usize>::new();
    /// let vec = set.as_vec_like();
    /// assert!(vec.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// A vector like mutable view of a set.
#[derive(Debug)]
pub struct VecLikeMut<'a, T: 'a> {
    inner: vec_like::VecLikeMut<'a, T, ()>,
}
impl<'a, T: 'a> VecLikeMut<'a, T>
where
    T: Ord,
{
    /// Appends a new element to the back of the vector like set.
    ///
    /// If the set did not have this value present, `true` is returnd.
    ///
    /// If the set did have this value present, `false` is returnd,
    /// and the entry is not appended.
    ///
    /// This is a synonym of the `SplaySet::insert` function.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// let mut vec = set.as_vec_like_mut();
    ///
    /// assert_eq!(vec.push("foo"), true);
    /// assert_eq!(vec.len(), 1);
    ///
    /// assert_eq!(vec.push("foo"), false);
    /// assert_eq!(vec.len(), 1);
    ///
    /// assert_eq!(vec.push("bar"), true);
    /// assert_eq!(vec.len(), 2);
    ///
    /// assert_eq!(vec.find_index("foo"), Some(0));
    /// assert_eq!(vec.get(0), Some(&"foo"));
    /// ```
    pub fn push(&mut self, value: T) -> bool {
        self.inner.push(value, ())
    }

    /// Removes the last element from the vector like set and returns it, or `None` if it is empty.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// {
    ///     let mut vec = set.as_vec_like_mut();
    ///     assert_eq!(vec.pop(), Some("bar"));
    ///     assert_eq!(vec.pop(), Some("foo"));
    ///     assert_eq!(vec.pop(), None);
    /// }
    /// assert!(set.is_empty());
    /// ```
    pub fn pop(&mut self) -> Option<T> {
        self.inner.pop().map(|(v, _)| v)
    }

    /// Returns the index of the element that is equal to the given value,
    /// or `None` if the collection does not have no such element.
    ///
    /// Because underlying `SplaySet` is a self-adjusting amortized data structure,
    /// this function requires the `mut` qualifier for `self`.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// let mut vec = set.as_vec_like_mut();
    /// assert_eq!(vec.find_index("foo"), Some(0));
    /// assert_eq!(vec.find_index("baz"), Some(2));
    /// assert_eq!(vec.find_index("qux"), None);
    /// ```
    pub fn find_index<Q: ?Sized>(&mut self, value: &Q) -> Option<usize>
    where
        T: Borrow<Q>,
        Q: Ord,
    {
        self.inner.find_index(value)
    }
}
impl<'a, T: 'a> VecLikeMut<'a, T> {
    fn new(tree: &'a mut tree_core::Tree<T, ()>) -> Self {
        VecLikeMut {
            inner: vec_like::VecLikeMut::new(tree),
        }
    }

    /// Returns the element of the vector at the given index,
    /// or `None` if the index is out of bounds.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// let vec = set.as_vec_like_mut();
    /// assert_eq!(vec.get(0), Some(&"foo"));
    /// assert_eq!(vec.get(1), Some(&"bar"));
    /// assert_eq!(vec.get(2), Some(&"baz"));
    /// assert_eq!(vec.get(3), None);
    /// ```
    pub fn get(&self, index: usize) -> Option<&T> {
        self.inner.get(index).map(|(v, _)| v)
    }

    /// Returns the first element of the vector, or `None` if it is empty.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// let vec = set.as_vec_like_mut();
    /// assert_eq!(vec.first(), Some(&"foo"));
    /// ```
    pub fn first(&self) -> Option<&T> {
        self.inner.first().map(|(v, _)| v)
    }

    /// Returns the last element of the vector, or `None` if it is empty.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// let vec = set.as_vec_like_mut();
    /// assert_eq!(vec.last(), Some(&"baz"));
    /// ```
    pub fn last(&self) -> Option<&T> {
        self.inner.last().map(|(v, _)| v)
    }

    /// Gets an iterator over the vector's elements, in positional order (low to high).
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// set.insert("bar");
    /// set.insert("baz");
    ///
    /// assert_eq!(set.iter().cloned().collect::<Vec<_>>(), ["bar", "baz", "foo"]);
    ///
    /// let vec = set.as_vec_like_mut();
    /// assert_eq!(vec.iter().cloned().collect::<Vec<_>>(), ["foo", "bar", "baz"]);
    /// ```
    pub fn iter(&self) -> VecLikeIter<T> {
        VecLikeIter(self.inner.iter())
    }

    /// Returns the number of elements in the vector like set.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    /// set.insert("foo");
    /// {
    ///     let mut vec = set.as_vec_like_mut();
    ///     vec.push("bar");
    ///     assert_eq!(vec.len(), 2);
    /// }
    /// assert_eq!(set.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns `true` if the vector like set contains no elements.
    ///
    /// # Examples
    /// ```
    /// use splay_tree::SplaySet;
    ///
    /// let mut set = SplaySet::new();
    ///
    /// let mut vec = set.as_vec_like_mut();
    /// assert!(vec.is_empty());
    ///
    /// vec.push(0);
    /// assert!(!vec.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// An iterator over a VecLike's elements
pub struct VecLikeIter<'a, T: 'a>(vec_like::Iter<'a, T, ()>);
impl<'a, T: 'a> Iterator for VecLikeIter<'a, T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|(v, _)| v)
    }
}