diff options
| author | ruki <[email protected]> | 2021-08-14 00:22:26 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-08-14 00:22:26 +0800 |
| commit | 07987f21cfa52d66080df8a2a4af5f86e608ab8e (patch) | |
| tree | 3219f02e4fdefb31cfd27fa768bbddcaf08c9058 | |
| parent | c5f2a58cf57ad72d4e2bc39a6dc196550804d013 (diff) | |
add table.reverse_unique
| -rw-r--r-- | xmake/core/base/table.lua | 29 | ||||
| -rw-r--r-- | xmake/modules/lib/detect/find_package.lua | 10 |
2 files changed, 26 insertions, 13 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(...) diff --git a/xmake/modules/lib/detect/find_package.lua b/xmake/modules/lib/detect/find_package.lua index b38e55e39..d3c9e57d1 100644 --- a/xmake/modules/lib/detect/find_package.lua +++ b/xmake/modules/lib/detect/find_package.lua @@ -38,15 +38,7 @@ function _concat_packages(a, b) if k == "links" then if type(v) == "table" and #v > 1 then -- we need ensure link orders when removing repeat values - local v2 = {} - local map = {} - for _, _v in irpairs(v) do - if not map[_v] then - table.insert(v2, 1, _v) - map[_v] = true - end - end - v = v2 + v = table.reverse_unique(v) end else v = table.unique(v) |
