Created
December 19, 2019 18:27
-
-
Save howmanysmall/44c055a9273769eca9e6b7b4e0c66a0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local table = {} | |
function table.pack(...) | |
return{n = select("#", ...), ...} | |
end | |
function table.move(a1, f, e, t, a2) | |
a2 = a2 or a1 | |
t = t + e | |
for i = e, f, -1 do | |
t = t - 1 | |
a2[t] = a1[i] | |
end | |
return a2 | |
end | |
function table.insert(Table, ...) | |
local PlusOne = #Table + 1 | |
local TotalArguments, Position, Value = select("#", ...), ... | |
if TotalArguments == 1 then | |
Position, Value = PlusOne, Position | |
elseif TotalArguments == 2 then | |
Position = Position - Position % 1 | |
else | |
error("wrong number of arguments to 'insert'", 2) | |
end | |
for Index = PlusOne - 1, Position, -1 do | |
Table[Index + 1] = Table[Index] | |
end | |
Table[Position] = Value | |
end | |
function table.remove(Table, Index) | |
local Length = #Table | |
Index = Index and Index % 1 == 0 and Index or Length | |
local Value = Table[Index] | |
while Index < Length do | |
Table[Index] = Table[Index + 1] | |
Index = Index + 1 | |
end | |
Table[Index] = nil | |
return Value | |
end | |
local function Pivot(Table, Function, A, B) | |
local M = B - A | |
if M > 2 then | |
local C = A + (M - M % 2) / 2 | |
local X, Y, Z = Table[A], Table[B], Table[C] | |
if not Function(X, Y) then | |
X, Y, A, B = Y, X, B, A | |
end | |
if not Function(Y, Z) then | |
Y, B = Z, C | |
end | |
if not Function(X, Y) then | |
Y, B = X, A | |
end | |
return B, Y | |
else | |
return B, Table[B] | |
end | |
end | |
local function DEFAULT_FUNCTION(A, B) | |
return A < B | |
end | |
local function QuickSort(Table, Function, B, E) | |
if B < E then | |
local i, j, k, val = B, E, Pivot(Table, Function, B, E) | |
while i < j do | |
while i < j and Function(Table[i], val) do | |
i = i + 1 | |
end | |
while i < j and not Function(Table[j], val) do | |
j = j - 1 | |
end | |
if i < j then | |
Table[i], Table[j] = Table[j], Table[i] | |
if i == k then | |
k = j | |
end -- update pivot position | |
i, j = i + 1, j - 1 | |
end | |
end | |
if i ~= k and not Function(Table[i], val) then | |
Table[i], Table[k] = val, Table[i] | |
k = i -- update pivot position | |
end | |
QuickSort(Table, Function, B, i == k and i - 1 or i) | |
return QuickSort(Table, Function, i + 1, E) | |
end | |
end | |
function table.sort(Table, Function) | |
Function = Function or DEFAULT_FUNCTION | |
return QuickSort(Table, Function, 1, #Table) | |
end | |
local function UnpackHelper(Array, Index, Jndex, ...) | |
if Jndex < Index then | |
return ... | |
else | |
return UnpackHelper(Array, Index, Jndex - 1, Array[Jndex], ...) | |
end | |
end | |
function table.unpack(Array, Index, Jndex) | |
return UnpackHelper(Array, Index or 1, Jndex or #Array) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment