summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunity <[email protected]>2020-01-17 13:34:23 +0800
committerOpportunity <[email protected]>2020-01-17 13:34:23 +0800
commitdc25ac7a87256a0470f523017ee790b29e9b6f2d (patch)
tree12dba72ccc57aa08620b95d98b2714cc2be55c19
parent8098258ec6c24647698d64a38df1c89c3845e3bb (diff)
add iterator
-rw-r--r--xmake/core/base/hashset.lua15
1 files changed, 14 insertions, 1 deletions
diff --git a/xmake/core/base/hashset.lua b/xmake/core/base/hashset.lua
index 7122c1b3f..a30bd69f7 100644
--- a/xmake/core/base/hashset.lua
+++ b/xmake/core/base/hashset.lua
@@ -27,7 +27,7 @@ local table = require("base/table")
local todisplay = require("base/todisplay")
-- representaion for nil key
-hashset._NIL = setmetatable({}, { __todisplay = function() return "${color.dump.keyword}nil${reset}" end, __tostring = function() return "symbol(nil)" end })
+hashset._NIL = setmetatable({}, { __todisplay = function() return "${reset}${color.dump.keyword}nil${reset}" end, __tostring = function() return "symbol(nil)" end })
function hashset:__todisplay()
return string.format("hashset${reset}(%s) {%s}", todisplay(self._SIZE), table.concat(table.imap(table.keys(self._DATA), function (i, k)
@@ -108,6 +108,19 @@ function hashset_impl:to_array()
return result
end
+-- iterate keys of hashtable
+-- for _, key in instance:keys() do ... end
+function hashset_impl:keys()
+ return function (table, key)
+ local k, _ = next(table._DATA, key)
+ if k == hashset._NIL then
+ return k, nil
+ else
+ return k, k
+ end
+ end, self, nil
+end
+
-- get size of hashset
function hashset_impl:size()
return self._SIZE