From f343c1c0b725dd686630b79e84917bcf5e32d7ca Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Tue, 11 May 2021 10:59:59 +1000 Subject: [PATCH] [modified] optimised `table.insert_sorted` and made it _actually_ insert at the back of the range instead of at the first equal found position (not always the same) --- tablex.lua | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tablex.lua b/tablex.lua index 020fa83..2f73040 100644 --- a/tablex.lua +++ b/tablex.lua @@ -68,27 +68,24 @@ function tablex.is_sorted(t, less) end --insert to the first position before the first larger element in the table +-- ({1, 2, 2, 3}, 2) -> {1, 2, 2, 2 (inserted here), 3} --if this is used on an already sorted table, the table will remain sorted and not need re-sorting ---(you can check if the table is sorted and sort if needed if you don't know) +--(you can sort beforehand if you don't know) --return the table for possible chaining function tablex.insert_sorted(t, v, less) less = less or default_less local low = 1 local high = #t - local match while low <= high do local mid = math.floor((low + high) / 2) local mid_val = t[mid] if less(v, mid_val) then high = mid - 1 - elseif less(mid_val, v) then - low = mid + 1 else - match = mid - break + low = mid + 1 end end - table.insert(t, match or low, v) + table.insert(t, low, v) return t end