diff options
| author | ruki <[email protected]> | 2021-11-14 23:25:19 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-11-14 23:25:19 +0800 |
| commit | 474998e342c779f71ff5591ddc936304411fa46a (patch) | |
| tree | 9efa50d7cf7fa857e0b163e6aa0335cb62a76745 | |
| parent | 8ea70c741c233e8ddc1523cc8f2e91eb7baa2de7 (diff) | |
add table.find_xxx
| -rw-r--r-- | tests/modules/table/test.lua | 11 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 64 |
2 files changed, 71 insertions, 4 deletions
diff --git a/tests/modules/table/test.lua b/tests/modules/table/test.lua index fc17fcb62..5ed17620c 100644 --- a/tests/modules/table/test.lua +++ b/tests/modules/table/test.lua @@ -1,4 +1,11 @@ function test_remove_if(t) - t:are_equal(table.remove_if({1, 2, 3, 4, 5, 6}, function (t, i, v) return (v % 2) == 0 end), {1, 3, 5}) - t:are_equal(table.remove_if({a = 1, b = 2, c = 3}, function (t, i, v) return (v % 2) == 0 end), {a = 1, c = 3}) + t:are_equal(table.remove_if({1, 2, 3, 4, 5, 6}, function (i, v) return (v % 2) == 0 end), {1, 3, 5}) + t:are_equal(table.remove_if({a = 1, b = 2, c = 3}, function (i, v) return (v % 2) == 0 end), {a = 1, c = 3}) +end + +function test_find_if(t) + t:are_equal(table.find_if({1, 2, 3, 4, 5, 6}, function (i, v) return (v % 2) == 0 end), {2, 4, 6}) + t:are_equal(table.find_first_if({1, 2, 3, 4, 5, 6}, function (i, v) return (v % 2) == 0 end), 2) + t:are_equal(table.find({1, 2, 4, 4, 5, 6}, 4), {3, 4}) + t:are_equal(table.find_first({1, 2, 3, 4, 5, 6}, 4), 4) end diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index 4f59e7a65..2bf776151 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -455,13 +455,13 @@ end function table.remove_if(tbl, pred) if table.is_array(tbl) then for i = #tbl, 1, -1 do - if pred(tbl, i, tbl[i]) then + if pred(i, tbl[i]) then table.remove(tbl, i) end end else for k, v in pairs(tbl) do - if pred(tbl, k, v) then + if pred(k, v) then tbl[k] = nil end end @@ -469,5 +469,65 @@ function table.remove_if(tbl, pred) return tbl end +-- return indices or keys for the given value +function table.find(tbl, value) + local result + if table.is_array(tbl) then + for i, v in ipairs(tbl) do + if v == value then + result = result or {} + table.insert(result, i) + end + end + else + for k, v in pairs(tbl) do + if v == value then + result = result or {} + table.insert(result, k) + end + end + end + return result +end + +-- return indices or keys if predicate is matched +function table.find_if(tbl, pred) + local result + if table.is_array(tbl) then + for i, v in ipairs(tbl) do + if pred(i, v) then + result = result or {} + table.insert(result, i) + end + end + else + for k, v in pairs(tbl) do + if pred(k, v) then + result = result or {} + table.insert(result, k) + end + end + end + return result +end + +-- return first index for the given value +function table.find_first(tbl, value) + for i, v in ipairs(tbl) do + if v == value then + return i + end + end +end + +-- return first index if predicate is matched +function table.find_first_if(tbl, pred) + for i, v in ipairs(tbl) do + if pred(i, v) then + return i + end + end +end + -- return module: table return table |
