@@ -28,6 +28,15 @@ pub fn singleton[V : Compare](value : V) -> T[V] {
28
28
29
29
///|
30
30
/// Initialize an set from an array.
31
+ pub fn from_array [V : Compare ](array : Array [V ]) -> T [V ] {
32
+ let set = new ()
33
+ for i = 0 ; i < array .length (); i = i + 1 {
34
+ set .add (array [i ])
35
+ }
36
+ set
37
+ }
38
+
39
+ ///| @alert deprecated "Use @sorted_set.from_array instead"
31
40
pub fn of [V : Compare ](array : Array [V ]) -> T [V ] {
32
41
let set = new ()
33
42
for i = 0 ; i < array .length (); i = i + 1 {
@@ -651,20 +660,20 @@ fn delete_node[V : Compare](root : Node[V], value : V) -> (Node[V]?, Bool) {
651
660
}
652
661
653
662
test "copy" {
654
- let set = of ([1 , 2 , 3 , 4 , 5 ])
663
+ let set = from_array ([1 , 2 , 3 , 4 , 5 ])
655
664
let copied_set = set .copy ()
656
665
inspect! (copied_set , content = "@sorted_set.of([1, 2, 3, 4, 5])" )
657
666
inspect! (set .debug_tree () == copied_set .debug_tree (), content = "true" )
658
- let set : T [Int ] = of ([])
667
+ let set : T [Int ] = from_array ([])
659
668
let copied_set = set .copy ()
660
669
inspect! (copied_set , content = "@sorted_set.of([])" )
661
670
inspect! (set .debug_tree () == copied_set .debug_tree (), content = "true" )
662
671
}
663
672
664
673
test "union" {
665
674
// Test 1: Union of two sets with no common elements
666
- let set1 = of ([1 , 2 , 3 ])
667
- let set2 = of ([4 , 5 , 6 ])
675
+ let set1 = from_array ([1 , 2 , 3 ])
676
+ let set2 = from_array ([4 , 5 , 6 ])
668
677
let set3 = set1 .union (set2 )
669
678
inspect! (set3 , content = "@sorted_set.of([1, 2, 3, 4, 5, 6])" )
670
679
inspect! (
@@ -673,15 +682,15 @@ test "union" {
673
682
)
674
683
675
684
// Test 2: Union of two sets with some common elements
676
- let set1 = of ([1 , 2 , 3 ])
677
- let set2 = of ([2 , 3 , 4 ])
685
+ let set1 = from_array ([1 , 2 , 3 ])
686
+ let set2 = from_array ([2 , 3 , 4 ])
678
687
let set3 = set1 .union (set2 )
679
688
inspect! (set3 , content = "@sorted_set.of([1, 2, 3, 4])" )
680
689
inspect! (set3 .debug_tree (), content = "([3]2,([1]1,_,_),([2]3,_,([1]4,_,_)))" )
681
690
682
691
// Test 3: Union of two sets where one is a subset of the other
683
- let set1 = of ([1 , 2 , 3 ])
684
- let set2 = of ([2 , 3 ])
692
+ let set1 = from_array ([1 , 2 , 3 ])
693
+ let set2 = from_array ([2 , 3 ])
685
694
let set3 = set1 .union (set2 )
686
695
inspect! (set3 , content = "@sorted_set.of([1, 2, 3])" )
687
696
inspect! (set3 .debug_tree (), content = "([2]2,([1]1,_,_),([1]3,_,_))" )
@@ -694,20 +703,20 @@ test "union" {
694
703
inspect! (set3 .debug_tree (), content = "_" )
695
704
696
705
// Test 5: Union of an empty set with a non-empty set
697
- let set1 = of ([1 , 2 , 3 ])
698
- let set2 = of ([])
706
+ let set1 = from_array ([1 , 2 , 3 ])
707
+ let set2 = from_array ([])
699
708
let set3 = set1 .union (set2 )
700
709
inspect! (set3 , content = "@sorted_set.of([1, 2, 3])" )
701
710
inspect! (set3 .debug_tree (), content = "([2]2,([1]1,_,_),([1]3,_,_))" )
702
- let set1 = of ([])
703
- let set2 = of ([1 , 2 , 3 ])
711
+ let set1 = from_array ([])
712
+ let set2 = from_array ([1 , 2 , 3 ])
704
713
let set3 = set1 .union (set2 )
705
714
inspect! (set3 , content = "@sorted_set.of([1, 2, 3])" )
706
715
inspect! (set3 .debug_tree (), content = "([2]2,([1]1,_,_),([1]3,_,_))" )
707
716
708
717
// Test 6: Union of two large sets with no common elements
709
- let set1 = of ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
710
- let set2 = of ([11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 ])
718
+ let set1 = from_array ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
719
+ let set2 = from_array ([11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 ])
711
720
let set3 = set1 .union (set2 )
712
721
inspect! (
713
722
set3 ,
@@ -719,8 +728,8 @@ test "union" {
719
728
)
720
729
721
730
// Test 7: Union of two large sets with some common elements
722
- let set1 = of ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
723
- let set2 = of ([6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 ])
731
+ let set1 = from_array ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
732
+ let set2 = from_array ([6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 ])
724
733
let set3 = set1 .union (set2 )
725
734
inspect! (
726
735
set3 ,
@@ -732,8 +741,8 @@ test "union" {
732
741
)
733
742
734
743
// Test 8: Union of two large sets where one is a subset of the other
735
- let set1 = of ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
736
- let set2 = of ([6 , 7 , 8 , 9 , 10 ])
744
+ let set1 = from_array ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
745
+ let set2 = from_array ([6 , 7 , 8 , 9 , 10 ])
737
746
let set3 = set1 .union (set2 )
738
747
inspect! (set3 , content = "@sorted_set.of([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" )
739
748
inspect! (
@@ -743,23 +752,23 @@ test "union" {
743
752
}
744
753
745
754
test "split" {
746
- let (l , r ) = split (of ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ]).root, 5 )
755
+ let (l , r ) = split (from_array ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ]).root, 5 )
747
756
inspect! (l , content = "Some(@sorted_set.of([1, 2, 3, 4]))" )
748
757
inspect! (r , content = "Some(@sorted_set.of([6, 7, 8, 9]))" )
749
- let (l , r ) = split (of ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ]).root, 0 )
758
+ let (l , r ) = split (from_array ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ]).root, 0 )
750
759
inspect! (l , content = "None" )
751
760
inspect! (r , content = "Some(@sorted_set.of([1, 2, 3, 4, 5, 6, 7, 8, 9]))" )
752
- let (l , r ) = split (of ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ]).root, 10 )
761
+ let (l , r ) = split (from_array ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ]).root, 10 )
753
762
inspect! (l , content = "Some(@sorted_set.of([1, 2, 3, 4, 5, 6, 7, 8, 9]))" )
754
763
inspect! (r , content = "None" )
755
- let (l , r ) = split (of ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ]).root, 4 )
764
+ let (l , r ) = split (from_array ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ]).root, 4 )
756
765
inspect! (l , content = "Some(@sorted_set.of([1, 2, 3]))" )
757
766
inspect! (r , content = "Some(@sorted_set.of([5, 6, 7, 8, 9]))" )
758
- let (l , r ) = split (of ([]).root, 7 )
767
+ let (l , r ) = split (from_array ([]).root, 7 )
759
768
inspect! (l , content = "None" )
760
769
inspect! (r , content = "None" )
761
770
let (l , r ) = split (
762
- of ([
771
+ from_array ([
763
772
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 ,
764
773
22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 ,
765
774
41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 ,
@@ -780,29 +789,29 @@ test "split" {
780
789
}
781
790
782
791
test "join" {
783
- let l = of ([13 , 8 , 17 , 1 , 11 , 15 , 25 , 6 ])
784
- let r = of ([27 , 28 , 40 , 35 , 33 ])
792
+ let l = from_array ([13 , 8 , 17 , 1 , 11 , 15 , 25 , 6 ])
793
+ let r = from_array ([27 , 28 , 40 , 35 , 33 ])
785
794
inspect! (
786
795
join (l .root, 26 , r .root),
787
796
content = "@sorted_set.of([1, 6, 8, 11, 13, 15, 17, 25, 26, 27, 28, 33, 35, 40])" ,
788
797
)
789
- let l = of ([3 , 2 , 5 , 1 , 4 ])
790
- let r = of ([7 ])
798
+ let l = from_array ([3 , 2 , 5 , 1 , 4 ])
799
+ let r = from_array ([7 ])
791
800
inspect! (
792
801
join (l .root, 6 , r .root),
793
802
content = "@sorted_set.of([1, 2, 3, 4, 5, 6, 7])" ,
794
803
)
795
- let l = of ([3 , 2 , 5 , 1 , 4 ])
796
- let r = of ([])
804
+ let l = from_array ([3 , 2 , 5 , 1 , 4 ])
805
+ let r = from_array ([])
797
806
inspect! (
798
807
join (l .root, 6 , r .root),
799
808
content = "@sorted_set.of([1, 2, 3, 4, 5, 6])" ,
800
809
)
801
- let l = of ([])
802
- let r = of ([])
810
+ let l = from_array ([])
811
+ let r = from_array ([])
803
812
inspect! (join (l .root, 6 , r .root), content = "@sorted_set.of([6])" )
804
- let l = of ([])
805
- let r = of ([7 , 8 , 9 , 10 , 11 , 12 ])
813
+ let l = from_array ([])
814
+ let r = from_array ([7 , 8 , 9 , 10 , 11 , 12 ])
806
815
inspect! (
807
816
join (l .root, 6 , r .root),
808
817
content = "@sorted_set.of([6, 7, 8, 9, 10, 11, 12])" ,
@@ -847,14 +856,14 @@ test "add multiple values" {
847
856
}
848
857
849
858
test "add_and_remove" {
850
- let set = of ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ])
859
+ let set = from_array ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 8 , 1 ])
851
860
set .remove (8 )
852
861
inspect! (set , content = "@sorted_set.of([1, 2, 3, 4, 5, 6, 7, 9])" )
853
862
inspect! (
854
863
set .debug_tree (),
855
864
content = "([4]5,([3]3,([2]2,([1]1,_,_),_),([1]4,_,_)),([2]7,([1]6,_,_),([1]9,_,_)))" ,
856
865
)
857
- let set = of ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
866
+ let set = from_array ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
858
867
859
868
// Test 1: Remove elements
860
869
set .remove (1 )
@@ -976,7 +985,7 @@ test "add_and_remove" {
976
985
set .remove (10 )
977
986
inspect! (set , content = "@sorted_set.of([])" )
978
987
inspect! (set .debug_tree (), content = "_" )
979
- let set = of ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 1 ])
988
+ let set = from_array ([7 , 2 , 9 , 4 , 5 , 6 , 3 , 1 ])
980
989
set .remove (3 )
981
990
inspect! (set , content = "@sorted_set.of([1, 2, 4, 5, 6, 7, 9])" )
982
991
inspect! (
0 commit comments