Skip to content

Commit e3bf1b0

Browse files
illusory0x0peter-jerry-ye
authored andcommitted
deprecate: inconsistent api @sorted_set.of
1 parent 0ac3614 commit e3bf1b0

File tree

3 files changed

+99
-71
lines changed

3 files changed

+99
-71
lines changed

sorted_set/set.mbt

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ pub fn singleton[V : Compare](value : V) -> T[V] {
2828

2929
///|
3030
/// 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"
3140
pub fn of[V : Compare](array : Array[V]) -> T[V] {
3241
let set = new()
3342
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) {
651660
}
652661

653662
test "copy" {
654-
let set = of([1, 2, 3, 4, 5])
663+
let set = from_array([1, 2, 3, 4, 5])
655664
let copied_set = set.copy()
656665
inspect!(copied_set, content="@sorted_set.of([1, 2, 3, 4, 5])")
657666
inspect!(set.debug_tree() == copied_set.debug_tree(), content="true")
658-
let set : T[Int] = of([])
667+
let set : T[Int] = from_array([])
659668
let copied_set = set.copy()
660669
inspect!(copied_set, content="@sorted_set.of([])")
661670
inspect!(set.debug_tree() == copied_set.debug_tree(), content="true")
662671
}
663672

664673
test "union" {
665674
// 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])
668677
let set3 = set1.union(set2)
669678
inspect!(set3, content="@sorted_set.of([1, 2, 3, 4, 5, 6])")
670679
inspect!(
@@ -673,15 +682,15 @@ test "union" {
673682
)
674683

675684
// 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])
678687
let set3 = set1.union(set2)
679688
inspect!(set3, content="@sorted_set.of([1, 2, 3, 4])")
680689
inspect!(set3.debug_tree(), content="([3]2,([1]1,_,_),([2]3,_,([1]4,_,_)))")
681690

682691
// 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])
685694
let set3 = set1.union(set2)
686695
inspect!(set3, content="@sorted_set.of([1, 2, 3])")
687696
inspect!(set3.debug_tree(), content="([2]2,([1]1,_,_),([1]3,_,_))")
@@ -694,20 +703,20 @@ test "union" {
694703
inspect!(set3.debug_tree(), content="_")
695704

696705
// 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([])
699708
let set3 = set1.union(set2)
700709
inspect!(set3, content="@sorted_set.of([1, 2, 3])")
701710
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])
704713
let set3 = set1.union(set2)
705714
inspect!(set3, content="@sorted_set.of([1, 2, 3])")
706715
inspect!(set3.debug_tree(), content="([2]2,([1]1,_,_),([1]3,_,_))")
707716

708717
// 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])
711720
let set3 = set1.union(set2)
712721
inspect!(
713722
set3,
@@ -719,8 +728,8 @@ test "union" {
719728
)
720729

721730
// 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])
724733
let set3 = set1.union(set2)
725734
inspect!(
726735
set3,
@@ -732,8 +741,8 @@ test "union" {
732741
)
733742

734743
// 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])
737746
let set3 = set1.union(set2)
738747
inspect!(set3, content="@sorted_set.of([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])")
739748
inspect!(
@@ -743,23 +752,23 @@ test "union" {
743752
}
744753

745754
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)
747756
inspect!(l, content="Some(@sorted_set.of([1, 2, 3, 4]))")
748757
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)
750759
inspect!(l, content="None")
751760
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)
753762
inspect!(l, content="Some(@sorted_set.of([1, 2, 3, 4, 5, 6, 7, 8, 9]))")
754763
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)
756765
inspect!(l, content="Some(@sorted_set.of([1, 2, 3]))")
757766
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)
759768
inspect!(l, content="None")
760769
inspect!(r, content="None")
761770
let (l, r) = split(
762-
of([
771+
from_array([
763772
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
764773
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
765774
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" {
780789
}
781790

782791
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])
785794
inspect!(
786795
join(l.root, 26, r.root),
787796
content="@sorted_set.of([1, 6, 8, 11, 13, 15, 17, 25, 26, 27, 28, 33, 35, 40])",
788797
)
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])
791800
inspect!(
792801
join(l.root, 6, r.root),
793802
content="@sorted_set.of([1, 2, 3, 4, 5, 6, 7])",
794803
)
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([])
797806
inspect!(
798807
join(l.root, 6, r.root),
799808
content="@sorted_set.of([1, 2, 3, 4, 5, 6])",
800809
)
801-
let l = of([])
802-
let r = of([])
810+
let l = from_array([])
811+
let r = from_array([])
803812
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])
806815
inspect!(
807816
join(l.root, 6, r.root),
808817
content="@sorted_set.of([6, 7, 8, 9, 10, 11, 12])",
@@ -847,14 +856,14 @@ test "add multiple values" {
847856
}
848857

849858
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])
851860
set.remove(8)
852861
inspect!(set, content="@sorted_set.of([1, 2, 3, 4, 5, 6, 7, 9])")
853862
inspect!(
854863
set.debug_tree(),
855864
content="([4]5,([3]3,([2]2,([1]1,_,_),_),([1]4,_,_)),([2]7,([1]6,_,_),([1]9,_,_)))",
856865
)
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])
858867

859868
// Test 1: Remove elements
860869
set.remove(1)
@@ -976,7 +985,7 @@ test "add_and_remove" {
976985
set.remove(10)
977986
inspect!(set, content="@sorted_set.of([])")
978987
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])
980989
set.remove(3)
981990
inspect!(set, content="@sorted_set.of([1, 2, 4, 5, 6, 7, 9])")
982991
inspect!(

0 commit comments

Comments
 (0)