KoboldAI-Client/extern/lualibs/pl/tablex.lua

7 lines
9.3 KiB
Lua
Raw Normal View History

-- Penlight 1.11.0-1 | /lua/pl/tablex.lua | https://github.com/lunarmodules/Penlight | License: MIT | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (C) 2009-2016 Steve Donovan, David Manura.
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local a=require('pl.utils')local b=require('pl.types')local getmetatable,setmetatable,require=getmetatable,setmetatable,require;local c,d,e=table.sort,table.insert,table.remove;local f=math.min;local pairs,type,g,select,tostring=pairs,type,a.unpack,select,tostring;local h=a.function_arg;local i=a.assert_arg;local j={}local function k(l,m,n)local o=getmetatable(m)or n and require('pl.'..n)return o and setmetatable(l,o)or l end;local function p(q)return setmetatable(q,require('pl.List'))end;local function r(s)return setmetatable(s,require('pl.Map'))end;local function t(u,v)error(('argument %d is not %s'):format(u,v),3)end;local function w(u,x)if not b.is_indexable(x)then t(u,"indexable")end end;local function y(u,x)if not b.is_iterable(x)then t(u,"iterable")end end;local function z(u,x)if not b.is_writeable(x)then t(u,"writeable")end end;function j.update(A,B)z(1,A)y(2,B)for C,D in pairs(B)do A[C]=D end;return A end;function j.size(E)y(1,E)local F=0;for C in pairs(E)do F=F+1 end;return F end;function j.copy(E)y(1,E)local l={}for C,D in pairs(E)do l[C]=D end;return l end;local function G(E,H)if type(E)~='table'then return E end;if H[E]then return H[E]end;y(1,E)local l={}H[E]=l;local o=getmetatable(E)for C,D in pairs(E)do C=G(C,H)D=G(D,H)l[C]=D end;setmetatable(l,o)return l end;function j.deepcopy(E)return G(E,{})end;local I=math.abs;local function J(A,B,K,L,H)if H[A]and H[A][B]then return true end;local M=type(A)local N=type(B)if M~=N then return false end;if M~='table'then if M=='number'and L then return I(A-B)<L end;return A==B end;local o=getmetatable(A)if not K and o and o.__eq then return A==B end;for O in pairs(A)do if B[O]==nil then return false end end;for P in pairs(B)do if A[P]==nil then return false end end;H[A]=H[A]or{}H[A][B]=true;for O,Q in pairs(A)do local R=B[O]if not J(Q,R,K,L,H)then return false end end;return true end;function j.deepcompare(A,B,K,L)return J(A,B,K,L,{})end;function j.compare(A,B,S)w(1,A)w(2,B)if#A~=#B then return false end;S=h(3,S)for C=1,#A do if not S(A[C],B[C])then return false end end;return true end;function j.compare_no_order(A,B,S)w(1,A)w(2,B)if S then S=h(3,S)end;if#A~=#B then return false end;local T={}for F=1,#A do local x=A[F]local U;for V=1,#B do if not T[V]then local W;if S then W=S(x,B[V])else W=x==B[V]end;if W then U=V;break end end end;if not U then return false end;T[U]=true end;return true end;function j.find(E,x,u)w(1,E)u=u or 1;if u<0 then u=#E+u+1 end;for F=u,#E do if E[F]==x then return F end end;return nil end;function j.rfind(E,x,u)w(1,E)u=u or#E;if u<0 then u=#E+u+1 end;for F=u,1,-1 do if E[F]==x then return F end end;return nil end;function j.find_if(E,S,X)y(1,E)S=h(2,S)for C,D in pairs(E)do local Y=S(D,X)if Y then return C,Y end end;return nil end;function j.index_by(m,u)w(1,m)w(2,u)local l={}for F=1,#u do l[F]=m[u[F]]end;return k(l,m,'List')end;function j.map(Z,E,...)y(1,E)Z=h(1,Z)local l={}for C,D in pairs(E)do l[C]=Z(D,...)end;return k(l,E)end;function j.imap(Z,E,...)w(1,E)Z=h(1,Z)local l={}for F=1,#E do l[F]=Z(E[F],...)or false end;return k(l,E,'List')end;function j.map_named_method(_,E,...)a.assert_string(1,_)w(2,E)local l={}for F=1,#E do local x=E[F]local Z=x[_]l[F]=Z(x,...)end;return k(l,E,'List')end;function j.transform(Z,E,...)y(1,E)Z=h(1,Z)for C,D in pairs(E)do E[C]=Z(D,...)end end;function j.range(a0,a1,a2)local l;a2=a2 or 1;if a0==a1 then l={a0}elseif a0>a1 and a2>0 or a1>a0 and a2<0 then l={}else local C=1;l={}for F=a0,a1,a2 do l[C]=F;C=C+1 end end;return p(l)end;function j.map2(Z,A,B,...)y(1,A)y(2,B)Z=h(1,Z)local l={}for C,D in pairs(A)do l[C]=Z(D,B[C],...)end;return k(l,A,'List')end;function j.imap2(Z,A,B,...)w(2,A)w(3,B)Z=h(1,Z)local l,a3={},math.min(#A,#B)for F=1,a3 do l[F]=Z(A[F],B[F],...)end;return l end;function j.reduce(Z,E,a4)w(2,E)Z=h(1,Z)local a3=#E;if a3==0 then return a4 end;local l=a4 and Z(a4,E[1])or E[1]for F=2,a3 do l=Z(l,E[F])end;return l end;function j.foreach(E,Z,...)y(1,E)Z=h(2,Z)for C,D in pairs(E)do Z(D,C,...)end end;function j.foreachi(E,Z,...)w(1,E)Z=h(2,Z)for F=1,#E do Z(E[F],F,...)end end;function j.mapn(Z,...)Z=h(1,Z)loca