|
15 | 15 |
|
16 | 16 | #[allow(missing_doc)]; |
17 | 17 |
|
18 | | - |
19 | | -use std::cmp; |
20 | 18 | use std::iterator::{Iterator, IteratorUtil, EnumerateIterator, FilterMapIterator, InvertIterator}; |
21 | 19 | use std::uint; |
22 | 20 | use std::util::replace; |
@@ -309,155 +307,6 @@ double_ended_iterator!(impl SmallIntMapMutIterator -> (uint, &'self mut T), get_ |
309 | 307 | pub type SmallIntMapMutRevIterator<'self, T> = InvertIterator<(uint, &'self mut T), |
310 | 308 | SmallIntMapMutIterator<'self, T>>; |
311 | 309 |
|
312 | | - |
313 | | -/// A set implemented on top of the SmallIntMap type. This set is always a set |
314 | | -/// of integers, and the space requirements are on the order of the highest |
315 | | -/// valued integer in the set. |
316 | | -pub struct SmallIntSet { |
317 | | - priv map: SmallIntMap<()> |
318 | | -} |
319 | | - |
320 | | -impl Container for SmallIntSet { |
321 | | - /// Return the number of elements in the map |
322 | | - fn len(&self) -> uint { |
323 | | - self.map.len() |
324 | | - } |
325 | | - |
326 | | - /// Return true if the map contains no elements |
327 | | - fn is_empty(&self) -> bool { self.len() == 0 } |
328 | | -} |
329 | | - |
330 | | -impl Mutable for SmallIntSet { |
331 | | - /// Clear the map, removing all key-value pairs. |
332 | | - fn clear(&mut self) { self.map.clear() } |
333 | | -} |
334 | | - |
335 | | -impl Set<uint> for SmallIntSet { |
336 | | - /// Return true if the set contains a value |
337 | | - fn contains(&self, value: &uint) -> bool { self.map.contains_key(value) } |
338 | | - |
339 | | - /// Return true if the set has no elements in common with `other`. |
340 | | - /// This is equivalent to checking for an empty uintersection. |
341 | | - fn is_disjoint(&self, other: &SmallIntSet) -> bool { |
342 | | - for self.each |v| { if other.contains(v) { return false } } |
343 | | - true |
344 | | - } |
345 | | - |
346 | | - /// Return true if the set is a subset of another |
347 | | - fn is_subset(&self, other: &SmallIntSet) -> bool { |
348 | | - for self.each |v| { if !other.contains(v) { return false } } |
349 | | - true |
350 | | - } |
351 | | - |
352 | | - /// Return true if the set is a superset of another |
353 | | - fn is_superset(&self, other: &SmallIntSet) -> bool { |
354 | | - other.is_subset(self) |
355 | | - } |
356 | | - |
357 | | - /// Visit the values representing the difference |
358 | | - fn difference(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool { |
359 | | - self.each(|v| other.contains(v) || f(v)) |
360 | | - } |
361 | | - |
362 | | - /// Visit the values representing the symmetric difference |
363 | | - fn symmetric_difference(&self, |
364 | | - other: &SmallIntSet, |
365 | | - f: &fn(&uint) -> bool) -> bool { |
366 | | - let len = cmp::max(self.map.v.len() ,other.map.v.len()); |
367 | | - |
368 | | - for uint::range(0, len) |i| { |
369 | | - if self.contains(&i) ^ other.contains(&i) { |
370 | | - if !f(&i) { return false; } |
371 | | - } |
372 | | - } |
373 | | - return true; |
374 | | - } |
375 | | - |
376 | | - /// Visit the values representing the uintersection |
377 | | - fn intersection(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool { |
378 | | - self.each(|v| !other.contains(v) || f(v)) |
379 | | - } |
380 | | - |
381 | | - /// Visit the values representing the union |
382 | | - fn union(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool { |
383 | | - let len = cmp::max(self.map.v.len() ,other.map.v.len()); |
384 | | - |
385 | | - for uint::range(0, len) |i| { |
386 | | - if self.contains(&i) || other.contains(&i) { |
387 | | - if !f(&i) { return false; } |
388 | | - } |
389 | | - } |
390 | | - return true; |
391 | | - } |
392 | | -} |
393 | | - |
394 | | -impl MutableSet<uint> for SmallIntSet { |
395 | | - /// Add a value to the set. Return true if the value was not already |
396 | | - /// present in the set. |
397 | | - fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) } |
398 | | - |
399 | | - /// Remove a value from the set. Return true if the value was |
400 | | - /// present in the set. |
401 | | - fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) } |
402 | | -} |
403 | | - |
404 | | -impl SmallIntSet { |
405 | | - /// Create an empty SmallIntSet |
406 | | - pub fn new() -> SmallIntSet { SmallIntSet{map: SmallIntMap::new()} } |
407 | | - |
408 | | - /// Visit all values in order |
409 | | - pub fn each(&self, f: &fn(&uint) -> bool) -> bool { self.map.each_key(f) } |
410 | | - |
411 | | - /// An iterator visiting all set members in ascending order. |
412 | | - /// Iterator element type is uint |
413 | | - pub fn iter<'r>(&'r self) -> SmallIntSetIterator<'r> { |
414 | | - SmallIntSetIterator { |
415 | | - iter: self.map.iter() |
416 | | - } |
417 | | - } |
418 | | - |
419 | | - /// An iterator visiting all set members in descending order. |
420 | | - /// Iterator element type is uint |
421 | | - pub fn rev_iter<'r>(&'r mut self) -> SmallIntSetRevIterator<'r> { |
422 | | - self.iter().invert() |
423 | | - } |
424 | | - |
425 | | -} |
426 | | - |
427 | | -pub struct SmallIntSetIterator<'self> { |
428 | | - priv iter: SmallIntMapIterator<'self, ()> |
429 | | -} |
430 | | - |
431 | | -impl<'self> Iterator<uint> for SmallIntSetIterator<'self> { |
432 | | - #[inline] |
433 | | - fn next(&mut self) -> Option<uint> { |
434 | | - let next_opt = self.iter.next(); |
435 | | - match next_opt { |
436 | | - None => { None } |
437 | | - Some((idx, _)) => { Some(idx) } |
438 | | - } |
439 | | - } |
440 | | - |
441 | | - #[inline] |
442 | | - fn size_hint(&self) -> (uint, Option<uint>) { |
443 | | - self.iter.size_hint() |
444 | | - } |
445 | | -} |
446 | | - |
447 | | -impl<'self> DoubleEndedIterator<uint> for SmallIntSetIterator<'self> { |
448 | | - #[inline] |
449 | | - fn next_back(&mut self) -> Option<uint> { |
450 | | - let next_opt = self.iter.next_back(); |
451 | | - match next_opt { |
452 | | - None => { None } |
453 | | - Some((idx, _)) => { Some(idx) } |
454 | | - } |
455 | | - } |
456 | | -} |
457 | | - |
458 | | -pub type SmallIntSetRevIterator<'self> = InvertIterator<uint, SmallIntSetIterator<'self>>; |
459 | | - |
460 | | - |
461 | 310 | #[cfg(test)] |
462 | 311 | mod test_map { |
463 | 312 |
|
@@ -732,221 +581,3 @@ mod bench { |
732 | 581 | find_seq_n(10_000, &mut m, bh); |
733 | 582 | } |
734 | 583 | } |
735 | | - |
736 | | -#[cfg(test)] |
737 | | -mod test_set { |
738 | | - |
739 | | - use super::SmallIntSet; |
740 | | - |
741 | | - #[test] |
742 | | - fn test_disjoint() { |
743 | | - let mut xs = SmallIntSet::new(); |
744 | | - let mut ys = SmallIntSet::new(); |
745 | | - assert!(xs.is_disjoint(&ys)); |
746 | | - assert!(ys.is_disjoint(&xs)); |
747 | | - assert!(xs.insert(5)); |
748 | | - assert!(ys.insert(11)); |
749 | | - assert!(xs.is_disjoint(&ys)); |
750 | | - assert!(ys.is_disjoint(&xs)); |
751 | | - assert!(xs.insert(7)); |
752 | | - assert!(xs.insert(19)); |
753 | | - assert!(xs.insert(4)); |
754 | | - assert!(ys.insert(2)); |
755 | | - assert!(xs.is_disjoint(&ys)); |
756 | | - assert!(ys.is_disjoint(&xs)); |
757 | | - assert!(ys.insert(7)); |
758 | | - assert!(!xs.is_disjoint(&ys)); |
759 | | - assert!(!ys.is_disjoint(&xs)); |
760 | | - } |
761 | | - |
762 | | - #[test] |
763 | | - fn test_subset_and_superset() { |
764 | | - let mut a = SmallIntSet::new(); |
765 | | - assert!(a.insert(0)); |
766 | | - assert!(a.insert(5)); |
767 | | - assert!(a.insert(11)); |
768 | | - assert!(a.insert(7)); |
769 | | - |
770 | | - let mut b = SmallIntSet::new(); |
771 | | - assert!(b.insert(0)); |
772 | | - assert!(b.insert(7)); |
773 | | - assert!(b.insert(19)); |
774 | | - assert!(b.insert(250)); |
775 | | - assert!(b.insert(11)); |
776 | | - assert!(b.insert(200)); |
777 | | - |
778 | | - assert!(!a.is_subset(&b)); |
779 | | - assert!(!a.is_superset(&b)); |
780 | | - assert!(!b.is_subset(&a)); |
781 | | - assert!(!b.is_superset(&a)); |
782 | | - |
783 | | - assert!(b.insert(5)); |
784 | | - |
785 | | - assert!(a.is_subset(&b)); |
786 | | - assert!(!a.is_superset(&b)); |
787 | | - assert!(!b.is_subset(&a)); |
788 | | - assert!(b.is_superset(&a)); |
789 | | - } |
790 | | - |
791 | | - #[test] |
792 | | - fn test_intersection() { |
793 | | - let mut a = SmallIntSet::new(); |
794 | | - let mut b = SmallIntSet::new(); |
795 | | - |
796 | | - assert!(a.insert(11)); |
797 | | - assert!(a.insert(1)); |
798 | | - assert!(a.insert(3)); |
799 | | - assert!(a.insert(77)); |
800 | | - assert!(a.insert(103)); |
801 | | - assert!(a.insert(5)); |
802 | | - |
803 | | - assert!(b.insert(2)); |
804 | | - assert!(b.insert(11)); |
805 | | - assert!(b.insert(77)); |
806 | | - assert!(b.insert(5)); |
807 | | - assert!(b.insert(3)); |
808 | | - |
809 | | - let mut i = 0; |
810 | | - let expected = [3, 5, 11, 77]; |
811 | | - for a.intersection(&b) |x| { |
812 | | - assert!(expected.contains(x)); |
813 | | - i += 1 |
814 | | - } |
815 | | - assert_eq!(i, expected.len()); |
816 | | - } |
817 | | - |
818 | | - #[test] |
819 | | - fn test_difference() { |
820 | | - let mut a = SmallIntSet::new(); |
821 | | - let mut b = SmallIntSet::new(); |
822 | | - |
823 | | - assert!(a.insert(1)); |
824 | | - assert!(a.insert(3)); |
825 | | - assert!(a.insert(5)); |
826 | | - assert!(a.insert(9)); |
827 | | - assert!(a.insert(11)); |
828 | | - |
829 | | - assert!(b.insert(3)); |
830 | | - assert!(b.insert(9)); |
831 | | - |
832 | | - let mut i = 0; |
833 | | - let expected = [1, 5, 11]; |
834 | | - for a.difference(&b) |x| { |
835 | | - assert!(expected.contains(x)); |
836 | | - i += 1 |
837 | | - } |
838 | | - assert_eq!(i, expected.len()); |
839 | | - } |
840 | | - |
841 | | - #[test] |
842 | | - fn test_symmetric_difference() { |
843 | | - let mut a = SmallIntSet::new(); |
844 | | - let mut b = SmallIntSet::new(); |
845 | | - |
846 | | - assert!(a.insert(1)); |
847 | | - assert!(a.insert(3)); |
848 | | - assert!(a.insert(5)); |
849 | | - assert!(a.insert(9)); |
850 | | - assert!(a.insert(11)); |
851 | | - |
852 | | - assert!(b.insert(3)); |
853 | | - assert!(b.insert(9)); |
854 | | - assert!(b.insert(14)); |
855 | | - assert!(b.insert(22)); |
856 | | - |
857 | | - let mut i = 0; |
858 | | - let expected = [1, 5, 11, 14, 22]; |
859 | | - for a.symmetric_difference(&b) |x| { |
860 | | - assert!(expected.contains(x)); |
861 | | - i += 1 |
862 | | - } |
863 | | - assert_eq!(i, expected.len()); |
864 | | - } |
865 | | - |
866 | | - #[test] |
867 | | - fn test_union() { |
868 | | - let mut a = SmallIntSet::new(); |
869 | | - let mut b = SmallIntSet::new(); |
870 | | - |
871 | | - assert!(a.insert(1)); |
872 | | - assert!(a.insert(3)); |
873 | | - assert!(a.insert(5)); |
874 | | - assert!(a.insert(9)); |
875 | | - assert!(a.insert(11)); |
876 | | - assert!(a.insert(16)); |
877 | | - assert!(a.insert(19)); |
878 | | - assert!(a.insert(24)); |
879 | | - |
880 | | - assert!(b.insert(1)); |
881 | | - assert!(b.insert(5)); |
882 | | - assert!(b.insert(9)); |
883 | | - assert!(b.insert(13)); |
884 | | - assert!(b.insert(19)); |
885 | | - |
886 | | - let mut i = 0; |
887 | | - let expected = [1, 3, 5, 9, 11, 13, 16, 19, 24]; |
888 | | - for a.union(&b) |x| { |
889 | | - assert!(expected.contains(x)); |
890 | | - i += 1 |
891 | | - } |
892 | | - assert_eq!(i, expected.len()); |
893 | | - } |
894 | | - |
895 | | - #[test] |
896 | | - fn test_iterator() { |
897 | | - let mut a = SmallIntSet::new(); |
898 | | - |
899 | | - assert!(a.insert(0)); |
900 | | - assert!(a.insert(1)); |
901 | | - assert!(a.insert(3)); |
902 | | - assert!(a.insert(6)); |
903 | | - assert!(a.insert(10)); |
904 | | - |
905 | | - let mut it = a.iter(); |
906 | | - assert_eq!(it.size_hint(), (0, Some(11))); |
907 | | - assert_eq!(it.next().unwrap(), 0); |
908 | | - assert_eq!(it.size_hint(), (0, Some(10))); |
909 | | - assert_eq!(it.next().unwrap(), 1); |
910 | | - assert_eq!(it.size_hint(), (0, Some(9))); |
911 | | - assert_eq!(it.next().unwrap(), 3); |
912 | | - assert_eq!(it.size_hint(), (0, Some(7))); |
913 | | - assert_eq!(it.next().unwrap(), 6); |
914 | | - assert_eq!(it.size_hint(), (0, Some(4))); |
915 | | - assert_eq!(it.next().unwrap(), 10); |
916 | | - assert_eq!(it.size_hint(), (0, Some(0))); |
917 | | - assert!(it.next().is_none()); |
918 | | - } |
919 | | - |
920 | | - #[test] |
921 | | - fn test_iterator_size_hints() { |
922 | | - let mut a = SmallIntSet::new(); |
923 | | - |
924 | | - assert!(a.insert(0)); |
925 | | - assert!(a.insert(1)); |
926 | | - assert!(a.insert(3)); |
927 | | - assert!(a.insert(6)); |
928 | | - assert!(a.insert(10)); |
929 | | - |
930 | | - assert_eq!(a.iter().size_hint(), (0, Some(11))); |
931 | | - assert_eq!(a.rev_iter().size_hint(), (0, Some(11))); |
932 | | - } |
933 | | - |
934 | | - #[test] |
935 | | - fn test_rev_iterator() { |
936 | | - let mut a = SmallIntSet::new(); |
937 | | - |
938 | | - assert!(a.insert(0)); |
939 | | - assert!(a.insert(1)); |
940 | | - assert!(a.insert(3)); |
941 | | - assert!(a.insert(6)); |
942 | | - assert!(a.insert(10)); |
943 | | - |
944 | | - let mut it = a.rev_iter(); |
945 | | - assert_eq!(it.next().unwrap(), 10); |
946 | | - assert_eq!(it.next().unwrap(), 6); |
947 | | - assert_eq!(it.next().unwrap(), 3); |
948 | | - assert_eq!(it.next().unwrap(), 1); |
949 | | - assert_eq!(it.next().unwrap(), 0); |
950 | | - assert!(it.next().is_none()); |
951 | | - } |
952 | | -} |
0 commit comments