From c7c6a848dcce7931838b0619707327debdda8d14 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 29 May 2021 22:49:50 +0100 Subject: [PATCH] wea.table_unique(): Add API function --- worldeditadditions/utils/tables.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/worldeditadditions/utils/tables.lua b/worldeditadditions/utils/tables.lua index cbaf2a0..98dcbef 100644 --- a/worldeditadditions/utils/tables.lua +++ b/worldeditadditions/utils/tables.lua @@ -79,3 +79,22 @@ function worldeditadditions.table_map(tbl, func) end end +--- Builds a new table with the elements of the given table appearing at most once. +-- @param tbl table The table of values to make unique. +-- @returns table A new table containing the values of the given table appearing at most once. +function worldeditadditions.table_unique(tbl) + local newtbl = {} + for i,value in ipairs(tbl) do + local seen = false + for j,seenvalue in ipairs(newtbl) do + if value == seenvalue then + seen = true + break + end + end + if not seen then + table.insert(newtbl, value) + end + end + return newtbl +end