diff --git a/sjsonnet/src/sjsonnet/stdlib/SetModule.scala b/sjsonnet/src/sjsonnet/stdlib/SetModule.scala index 65f4a364..614f1b17 100644 --- a/sjsonnet/src/sjsonnet/stdlib/SetModule.scala +++ b/sjsonnet/src/sjsonnet/stdlib/SetModule.scala @@ -65,22 +65,19 @@ object SetModule extends AbstractFunctionModule { val out = new mutable.ArrayBuilder.ofRef[Lazy] // Set a reasonable size hint - in the worst case (no duplicates), we'll need arrValue.length elements out.sizeHint(arrValue.length) - for (v <- arrValue) { - val outResult = out.result() - if (outResult.length == 0) { + + var lastAddedKey: Val = null + var i = 0 + while (i < arrValue.length) { + val v = arrValue(i) + val vKey = + if (keyF.isInstanceOf[Val.False]) v.force + else keyF.asInstanceOf[Val.Func].apply1(v, pos.noOffset)(ev, TailstrictModeDisabled) + if (lastAddedKey == null || !ev.equal(vKey, lastAddedKey)) { out.+=(v) - } else if (keyF.isInstanceOf[Val.False]) { - if (!ev.equal(outResult.last.force, v.force)) { - out.+=(v) - } - } else if (!keyF.isInstanceOf[Val.False]) { - val keyFFunc = keyF.asInstanceOf[Val.Func] - val o1Key = keyFFunc.apply1(v, pos.noOffset)(ev, TailstrictModeDisabled) - val o2Key = keyFFunc.apply1(outResult.last, pos.noOffset)(ev, TailstrictModeDisabled) - if (!ev.equal(o1Key, o2Key)) { - out.+=(v) - } + lastAddedKey = vKey } + i += 1 } Val.Arr(pos, out.result())