Skip to content

Commit 910d6ff

Browse files
redbaronsparkprime
authored andcommitted
Merge sort in setUnion (#603)
1 parent 8ac54da commit 910d6ff

7 files changed

+40
-26
lines changed

stdlib/std.jsonnet

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,8 +1162,22 @@ limitations under the License.
11621162
std.length(std.setInter([x], arr, keyF)) > 0,
11631163

11641164
setUnion(a, b, keyF=id)::
1165-
// NOTE: order matters, values in `a` win due to sort being stable
1166-
std.set(a + b, keyF),
1165+
// NOTE: order matters, values in `a` win
1166+
local aux(a, b, i, j, acc) =
1167+
if i >= std.length(a) then
1168+
acc + b[j:]
1169+
else if j >= std.length(b) then
1170+
acc + a[i:]
1171+
else
1172+
local ak = keyF(a[i]);
1173+
local bk = keyF(b[j]);
1174+
if ak == bk then
1175+
aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict
1176+
else if ak < bk then
1177+
aux(a, b, i + 1, j, acc + [a[i]]) tailstrict
1178+
else
1179+
aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;
1180+
aux(a, b, 0, 0, []),
11671181

11681182
setInter(a, b, keyF=id)::
11691183
local aux(a, b, i, j, acc) =
@@ -1183,7 +1197,7 @@ limitations under the License.
11831197
if i >= std.length(a) then
11841198
acc
11851199
else if j >= std.length(b) then
1186-
aux(a, b, i + 1, j, acc + [a[i]]) tailstrict
1200+
acc + a[i:]
11871201
else
11881202
if keyF(a[i]) == keyF(b[j]) then
11891203
aux(a, b, i + 1, j + 1, acc) tailstrict
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RUNTIME ERROR: cannot test equality of functions
2-
std.jsonnet:1266:9-34 function <anonymous>
2+
std.jsonnet:1280:9-34 function <anonymous>
33
error.equality_function.jsonnet:17:1-33
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_array.jsonnet:18:18-32 thunk <array_element>
3-
std.jsonnet:1246:29-33 thunk <b>
4-
std.jsonnet:1246:21-33 function <anonymous>
5-
std.jsonnet:1246:21-33 function <aux>
6-
std.jsonnet:1249:15-31 function <anonymous>
7-
std.jsonnet:1250:11-23
3+
std.jsonnet:1260:29-33 thunk <b>
4+
std.jsonnet:1260:21-33 function <anonymous>
5+
std.jsonnet:1260:21-33 function <aux>
6+
std.jsonnet:1263:15-31 function <anonymous>
7+
std.jsonnet:1264:11-23
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_object.jsonnet:18:22-36 object <b>
3-
std.jsonnet:1260:50-54 thunk <b>
4-
std.jsonnet:1260:42-54 function <anonymous>
5-
std.jsonnet:1260:42-54 function <aux>
6-
std.jsonnet:1263:15-31 function <anonymous>
7-
std.jsonnet:1264:11-23
3+
std.jsonnet:1274:50-54 thunk <b>
4+
std.jsonnet:1274:42-54 function <anonymous>
5+
std.jsonnet:1274:42-54 function <aux>
6+
std.jsonnet:1277:15-31 function <anonymous>
7+
std.jsonnet:1278:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.invariant.equality.jsonnet:17:10-15 thunk <object_assert>
3-
std.jsonnet:1260:42-46 thunk <a>
4-
std.jsonnet:1260:42-54 function <anonymous>
5-
std.jsonnet:1260:42-54 function <anonymous>
6-
std.jsonnet:1264:11-23
3+
std.jsonnet:1274:42-46 thunk <a>
4+
std.jsonnet:1274:42-54 function <anonymous>
5+
std.jsonnet:1274:42-54 function <anonymous>
6+
std.jsonnet:1278:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.obj_assert.fail1.jsonnet:20:23-29 thunk <object_assert>
3-
std.jsonnet:1260:42-46 thunk <a>
4-
std.jsonnet:1260:42-54 function <anonymous>
5-
std.jsonnet:1260:42-54 function <anonymous>
6-
std.jsonnet:1264:11-23
3+
std.jsonnet:1274:42-46 thunk <a>
4+
std.jsonnet:1274:42-54 function <anonymous>
5+
std.jsonnet:1274:42-54 function <anonymous>
6+
std.jsonnet:1278:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: foo was not equal to bar
22
error.obj_assert.fail2.jsonnet:20:32-65 thunk <object_assert>
3-
std.jsonnet:1260:42-46 thunk <a>
4-
std.jsonnet:1260:42-54 function <anonymous>
5-
std.jsonnet:1260:42-54 function <anonymous>
6-
std.jsonnet:1264:11-23
3+
std.jsonnet:1274:42-46 thunk <a>
4+
std.jsonnet:1274:42-54 function <anonymous>
5+
std.jsonnet:1274:42-54 function <anonymous>
6+
std.jsonnet:1278:11-23

0 commit comments

Comments
 (0)