1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00
RedMew/utils/list_utils.lua

167 lines
4.1 KiB
Lua
Raw Normal View History

-- Functions that perform actions on tables
2017-08-02 10:12:57 +02:00
local function assert_argument_valid(a, arg_type)
arg_type = arg_type or 'table'
if type(a) ~= arg_type then
error("bad argument #1 to '" .. debug.getinfo(2, 'n').name .. "' (table expected, got " .. type(a) .. ')', 3)
end
2017-08-02 10:12:57 +02:00
end
2018-11-22 11:39:07 +02:00
--- Removes a specific element from a table
-- @param t table to search
-- @param element to search for
2017-08-02 10:12:57 +02:00
table.remove_element = function(t, element)
assert_argument_valid(t)
for k, v in pairs(t) do
if v == element then
table.remove(t, k)
break
end
2017-08-02 10:12:57 +02:00
end
end
table.add_all = function(t1, t2)
assert_argument_valid(t1)
assert_argument_valid(t2)
for k, v in pairs(t2) do
if tonumber(k) then
table.insert(t1, v)
else
t1[k] = v
end
2017-08-02 10:12:57 +02:00
end
end
table.size = function(t)
assert_argument_valid(t)
local size = 0
for _ in pairs(t) do
size = size + 1
end
return size
2017-08-02 10:12:57 +02:00
end
2018-11-22 11:39:07 +02:00
--- Checks if a table contains an element
-- @param t table to search
-- @param e element to search for
-- @returns the index of an element or -1
2017-08-02 10:12:57 +02:00
table.index_of = function(t, e)
assert_argument_valid(t)
local i = 1
for _, v in pairs(t) do
if v == e then
return i
end
i = i + 1
2017-08-02 10:12:57 +02:00
end
return -1
2017-08-02 10:12:57 +02:00
end
2018-11-22 11:39:07 +02:00
--- Checks if a table contains an element
-- @param t table to search
-- @param e element to search for
-- @returns true or false
2017-08-02 10:12:57 +02:00
table.contains = function(t, e)
assert_argument_valid(t)
return table.index_of(t, e) > -1
2017-08-02 10:12:57 +02:00
end
table.set = function(t, index, element)
assert_argument_valid(t)
assert_argument_valid(index, 'number')
local i = 1
for k in pairs(t) do
if i == index then
t[k] = element
return nil
end
i = i + 1
2017-08-02 10:12:57 +02:00
end
error('Index out of bounds', 2)
2017-08-02 10:12:57 +02:00
end
table.get = function(t, index)
assert_argument_valid(t)
assert_argument_valid(index, 'number')
local i = 1
for k in pairs(t) do
if i == index then
return t[k]
end
i = i + 1
2017-08-02 10:12:57 +02:00
end
error('Index out of bounds', 2)
2017-08-02 10:12:57 +02:00
end
2018-01-21 17:32:07 +02:00
2018-11-22 11:39:07 +02:00
--- Chooses a random entry from a table
table.get_random = function(this_table)
return this_table[math.random(#this_table)]
end
--- Chooses a random entry from a weighted table
-- @param weight_table a table of tables with items and their weights
-- @param item_index the index of the items
-- @param weight_index the index of the weights
-- @returns a random item with weighting
-- @see features.chat_triggers.hodor
table.get_random_weighted = function(weighted_table, item_index, weight_index)
local total_weight = 0
for _, w in pairs(weighted_table) do
total_weight = total_weight + w[weight_index]
end
local index = math.random(total_weight)
local weight_sum = 0
for _, w in pairs(weighted_table) do
weight_sum = weight_sum + w[weight_index]
if weight_sum >= index then
return w[item_index]
end
end
end
2018-01-21 17:32:07 +02:00
--[[
Returns the index where t[index] == target.
If there is no such index, returns a negative vaule such that bit32.bnot(value) is
2018-01-21 17:32:07 +02:00
the index that the vaule should be inserted to keep the list ordered.
t must be a list in ascending order for the return value to be valid.
2018-01-21 17:32:07 +02:00
Usage example:
local t = {1,3,5,7,9}
local x = 5
local index = table.binary_search(t, x)
if index < 0 then
game.print("value not found, smallest index where t[index] > x is: " .. bit32.bnot(index))
2018-01-21 17:32:07 +02:00
else
game.print("value found at index: " .. index)
end
2018-01-21 17:32:07 +02:00
]]
table.binary_search =
function(t, target)
--For some reason bit32.bnot doesn't return negative numbers so I'm using ~x = -1 - x instead.
2018-01-21 17:32:07 +02:00
assert_argument_valid(t)
assert_argument_valid(target, 'number')
2018-01-21 17:32:07 +02:00
local lower = 1
local upper = #t
2018-01-21 17:32:07 +02:00
if upper == 0 then
return -2 -- ~1
2018-01-21 17:32:07 +02:00
end
repeat
local mid = math.floor((lower + upper) / 2)
local value = t[mid]
if value == target then
return mid
elseif value < target then
lower = mid + 1
else
upper = mid - 1
end
2018-01-21 17:32:07 +02:00
until lower > upper
2018-01-21 17:32:07 +02:00
return -1 - lower -- ~lower
end