Khác biệt giữa bản sửa đổi của “Mô đun:TableTools”
n Đã thay đổi mức khóa của “Mô đun:TableTools” ([Sửa đổi=Cấm mọi thành viên (trừ bảo quản viên)] (vô thời hạn) [Di chuyển=Cấm mọi thành viên (trừ bảo quản viên)] (vô thời hạn)) |
n Đã nhập 1 phiên bản |
||
| (Không hiển thị 4 phiên bản của 3 người dùng ở giữa) | |||
| Dòng 1: | Dòng 1: | ||
--[[ | --[[ | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
-- | -- TableTools -- | ||
-- -- | -- -- | ||
-- This module includes a number of functions for dealing with Lua tables. -- | -- This module includes a number of functions for dealing with Lua tables. -- | ||
| Dòng 18: | Dòng 18: | ||
local checkType = libraryUtil.checkType | local checkType = libraryUtil.checkType | ||
local checkTypeMulti = libraryUtil.checkTypeMulti | local checkTypeMulti = libraryUtil.checkTypeMulti | ||
-- Safely require Mô đun:Exponential search | |||
local success, expSearch = pcall(require, 'Mô đun:Exponential search') | |||
if not success then | |||
expSearch = nil | |||
end | |||
--[[ | --[[ | ||
| Dòng 372: | Dòng 378: | ||
copy = {} | copy = {} | ||
for orig_key, orig_value in pairs(orig) do | for orig_key, orig_value in pairs(orig) do | ||
copy[ | copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen) | ||
end | end | ||
already_seen[orig] = copy | already_seen[orig] = copy | ||
| Dòng 379: | Dòng 385: | ||
local mt = getmetatable(orig) | local mt = getmetatable(orig) | ||
if mt ~= nil then | if mt ~= nil then | ||
local mt_copy = | local mt_copy = _deepCopy(mt, includeMetatable, already_seen) | ||
setmetatable(copy, mt_copy) | setmetatable(copy, mt_copy) | ||
already_seen[mt] = mt_copy | already_seen[mt] = mt_copy | ||
| Dòng 415: | Dòng 421: | ||
--[[ | --[[ | ||
-- Finds the length of an array, or of a quasi-array with keys such | -- Finds the length of an array, or of a quasi-array with keys such | ||
-- as "data1", "data2", etc., using an | -- as "data1", "data2", etc., using an exponential search algorithm. | ||
-- It is similar to the operator #, but may return | -- It is similar to the operator #, but may return | ||
-- a different value when there are gaps in the array portion of the table. | -- a different value when there are gaps in the array portion of the table. | ||
| Dòng 425: | Dòng 431: | ||
function p.length(t, prefix) | function p.length(t, prefix) | ||
checkType('length', 1, t, 'table') | checkType('length', 1, t, 'table') | ||
checkType('length', 2, prefix, 'string', true) | checkType('length', 2, prefix, 'string', true) | ||
return expSearch(function(i) | |||
local | if expSearch then | ||
return expSearch(function(i) | |||
key = prefix .. tostring(i) | local key = prefix and (prefix .. tostring(i)) or i | ||
return t[key] ~= nil | |||
end) or 0 | |||
else | |||
-- Phương án dự phòng (fallback) nếu chưa tạo Mô đun:Exponential search | |||
local i = 1 | |||
while true do | |||
local key = prefix and (prefix .. tostring(i)) or i | |||
if t[key] == nil then | |||
return i - 1 | |||
end | |||
i = i + 1 | |||
end | end | ||
end | |||
end | |||
end | end | ||
function p.inArray(arr, valueToFind) | function p.inArray(arr, valueToFind) | ||
checkType("inArray", 1, arr, "table") | checkType("inArray", 1, arr, "table") | ||
for _, v in ipairs(arr) do | for _, v in ipairs(arr) do | ||