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

7 lines
6.3 KiB
Lua
Raw Normal View History

-- Penlight 1.11.0-1 | /lua/pl/array2d.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,b,c,d,e,f=_G.tonumber,_G.tostring,_G.io,_G.ipairs,_G.string,_G.table;local setmetatable,getmetatable=setmetatable,getmetatable;local g=require'pl.tablex'local h=require'pl.utils'local i=require'pl.types'local j,k,l,m,n,o,p=g.imap,g.map,g.reduce,g.keys,g.map2,g.set,g.index_by;local q=f.remove;local r,s,t=h.splitv,h.fprintf,h.assert_arg;local u=e.byte;local v=c.stdout;local w=math.min;local x={}local function y(z,A)local B=getmetatable(z)if B then setmetatable(A,B)end;return A end;local function C(D)return setmetatable(D,require('pl.List'))end;function x.size(E)t(1,E,'table')return#E,#E[1]end;do local function F(G,H)return G[H]end;function x.column(E,I)t(1,E,'table')return C(j(F,E,I))end end;local J=x.column;function x.row(E,K)t(1,E,'table')local L=E[K]local M={}for N,O in d(L)do M[N]=O end;return C(M)end;function x.map(P,E,Q)t(2,E,'table')P=h.function_arg(1,P)return y(E,j(function(L)return j(P,L,Q)end,E))end;function x.reduce_rows(P,E)t(1,E,'table')return k(function(L)return l(P,L)end,E)end;function x.reduce_cols(P,E)t(1,E,'table')return k(function(R)return l(P,J(E,R))end,m(E[1]))end;function x.reduce2(S,T,E)t(3,E,'table')local U=x.reduce_rows(T,E)return l(S,U)end;function x.map2(P,V,W,E,X,Q)t(1,E,'table')t(2,X,'table')P=h.function_arg(1,P)if V==1 and W==2 then return j(function(L)return n(P,E,L,Q)end,X)elseif V==2 and W==1 then return j(function(L)return n(P,L,X,Q)end,E)elseif V==1 and W==1 then return n(P,E,X)elseif V==2 and W==2 then return n(function(Y,Z)return n(P,Y,Z,Q)end,E,X)end end;function x.product(P,_,a0)P=h.function_arg(1,P)t(2,_,'table')t(3,a0,'table')local D={}for K,O in d(a0)do D[K]=k(P,_,O)end;return D end;function x.flatten(G)local D={}local H=1;local a1,a2=x.size(G)for M=1,a1 do local L=G[M]for R=1,a2 do D[H]=L[R]H=H+1 end end;return C(D)end;function x.reshape(G,a3,a4)local a5,a6=x.size(G)local a7=a5*a6/a3;local D={}local a8,a9=1,1;for K=1,a3 do local L={}for I=1,a7 do L[I]=G[a8][a9]if not a4 then a9=a9+1;if a9>a6 then a8=a8+1;a9=1 end else a8=a8+1;if a8>a5 then a9=a9+1;a8=1 end end end;D[K]=L end;return y(G,D)end;function x.transpose(G)t(1,G,'table')local aa,R=x.size(G)return x.reshape(G,R,true)end;function x.swap_rows(G,ab,ac)t(1,G,'table')G[ab],G[ac]=G[ac],G[ab]return G end;function x.swap_cols(G,ad,ae)t(1,G,'table')for aa,L in d(G)do L[ad],L[ae]=L[ae],L[ad]end;return G end;function x.extract_rows(G,af)return y(G,p(G,af))end;function x.extract_cols(G,ag)t(1,G,'table')local D={}for K=1,#G do D[K]=p(G[K],ag)end;return y(G,D)end;x.remove_row=q;function x.remove_col(G,I)t(1,G,'table')for K=1,#G do q(G[K],I)end end;do local function ah(ai)local M,R=ai:match'R(%d+)C(%d+)'if M then M,R=a(M),a(R)return M,R end;R,M=ai:match'(%a+)(%d+)'if R then local aj=0;for K=1,#R do aj=aj*26+u(R:sub(K,K))-u'A'+1 end;return a(M),aj end;error('bad cell specifier: '..ai)end;function x.parse_range(ai)t(1,ai,'string')ai=ai:upper()if ai:find':'then local ak,al=r(ai,':')local ab,ad=ah(ak)local ac,ae=ah(al)return ab,ad,ac,ae else local K,I=ah(ai)return K,I end end end;function x.range(G,am)t(1,G,'table')return x.slice(G,x.parse_range(am))end;local an;do local function ao(O,ap)if not O then return O end;if O<0 then O=ap+O+1 end;if O<1 then O=1 end;if O>ap then O=ap end;return O end;function x.default_range(G,ab,ad,ac,ae)local a5,a6=x.size(G)ab=ao(ab or 1,a5)ad=ao(ad or 1,a6)ac=ao(ac or a5,a5)ae=ao(ae or a6,a6)return ab,ad,ac,ae end;an=x.default_range end;function x.slice(G,ab,ad,ac,ae)t(1,G,'table')ab,ad,ac,ae=an(G,ab,ad,ac,ae)local D={}for K=ab,ac do local aq;local L=G[K]if ad==ae then aq=L[ad]else aq={}for I=ad,ae do aq[#aq+1]=L[I]end end;D[#D+1]=aq end;if ab==ac then D=D[1]end;return y(G,D)end;function x.set(G,ar,ab,ad,ac,ae)ab,ad,ac,ae=an(G,ab,ad,ac,ae)local K=ab;if i.is_callable(ar)then local as=ar;ar=function(I)return as(K,I)end end;while K<=ac do o(G[K],ar,ad,ae)K=K+1 end end;function x.write(G,P,at,ab,ad,ac,ae)t(1,G,'table')P=P or v;local au;if at then au=function(L,I)s(P,at,L[I])end else au=function(L,I)P:write(b(L[I]),' ')end end;local function av()P:write'\n'end;x.forall(G,au,av,ab,ad,ac,ae)end;function x.forall