From f25d8f008f7668fba27646abe2081c4681315162 Mon Sep 17 00:00:00 2001 From: Weida Zhao Date: Fri, 1 Nov 2024 07:51:01 -0700 Subject: [PATCH] Update std.jsonnet --- stdlib/std.jsonnet | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/stdlib/std.jsonnet b/stdlib/std.jsonnet index fccbe26d5..0a6af5c47 100644 --- a/stdlib/std.jsonnet +++ b/stdlib/std.jsonnet @@ -1470,8 +1470,20 @@ limitations under the License. std.uniq(std.sort(arr, keyF), keyF), setMember(x, arr, keyF=id):: - // TODO(dcunnin): Binary chop for O(log n) complexity - std.length(std.setInter([x], arr, keyF)) > 0, + local xk = keyF(x); + local binarySearch(i, j) = + if i > j then + false + else + local m = i + ((j - i) >> 1); + local mk = keyF(arr[m]); + if mk < xk then + binarySearch(m + 1, j) + else if mk > xk then + binarySearch(i, m - 1) + else + true; + binarySearch(0, std.length(arr) - 1), setUnion(a, b, keyF=id):: // NOTE: order matters, values in `a` win