summaryrefslogtreecommitdiff
path: root/xmake/core/base/table.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2021-08-14 00:22:26 +0800
committerruki <[email protected]>2021-08-14 00:22:26 +0800
commit07987f21cfa52d66080df8a2a4af5f86e608ab8e (patch)
tree3219f02e4fdefb31cfd27fa768bbddcaf08c9058 /xmake/core/base/table.lua
parentc5f2a58cf57ad72d4e2bc39a6dc196550804d013 (diff)
add table.reverse_unique
Diffstat (limited to 'xmake/core/base/table.lua')
-rw-r--r--xmake/core/base/table.lua29
1 files changed, 25 insertions, 4 deletions
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua
index edd0f42fb..c86a90988 100644
--- a/xmake/core/base/table.lua
+++ b/xmake/core/base/table.lua
@@ -240,21 +240,17 @@ end
-- remove repeat from the given array
function table.unique(array, barrier)
-
if table.is_array(array) then
if table.getn(array) ~= 1 then
local exists = {}
local unique = {}
for _, v in ipairs(array) do
-
-- exists barrier? clear the current existed items
if barrier and barrier(v) then
exists = {}
end
-
-- add unique item
if not exists[v] then
- -- v will not be nil
exists[v] = true
table.insert(unique, v)
end
@@ -265,6 +261,31 @@ function table.unique(array, barrier)
return array
end
+-- reverse to remove repeat from the given array
+function table.reverse_unique(array, barrier)
+ if table.is_array(array) then
+ if table.getn(array) ~= 1 then
+ local exists = {}
+ local unique = {}
+ local n = #array
+ for i = 1, n do
+ local v = array[n - i + 1]
+ -- exists barrier? clear the current existed items
+ if barrier and barrier(v) then
+ exists = {}
+ end
+ -- add unique item
+ if not exists[v] then
+ exists[v] = true
+ table.insert(unique, 1, v)
+ end
+ end
+ array = unique
+ end
+ end
+ return array
+end
+
-- pack arguments into a table
-- polyfill of lua 5.2, @see https://www.lua.org/manual/5.2/manual.html#pdf-table.pack
function table.pack(...)