summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-12 14:07:07 +0800
committerOpportunityLiu <[email protected]>2019-07-12 14:07:07 +0800
commita6fbe448d1c1f39d424217189677ba7bababf65f (patch)
treea723c7aabfc0dc3fc67320bbdfa7b58559723f32
parent741274711841086019bdafc3eacab0345eea789f (diff)
add hashset
-rw-r--r--xmake/core/base/hashset.lua98
-rw-r--r--xmake/core/sandbox/modules/import/core/base/hashset.lua36
2 files changed, 134 insertions, 0 deletions
diff --git a/xmake/core/base/hashset.lua b/xmake/core/base/hashset.lua
new file mode 100644
index 000000000..7ec5bd4a0
--- /dev/null
+++ b/xmake/core/base/hashset.lua
@@ -0,0 +1,98 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2019, TBOOX Open Source Group.
+--
+-- @author OpportunityLiu
+-- @file hashset.lua
+--
+
+-- define module
+local hashset = hashset or {}
+local hashset_impl = hashset.__index or {}
+
+-- load modules
+local table = require("base/table")
+
+-- representaion for nil key
+hashset._NIL = setmetatable({}, {__tostring = function() return "nil" end })
+
+function hashset._to_key(key)
+ if key == nil then
+ key = hashset._NIL
+ end
+ return key
+end
+
+-- make a new hashset
+function hashset.new()
+ return setmetatable({ _DATA = {} }, hashset)
+end
+
+-- construct from list of items
+function hashset.of(...)
+ local result = hashset.new()
+ local data = table.pack(...)
+ for i = 1, data.n do
+ result._DATA[hashset._to_key(data[i])] = true
+ end
+ return result
+end
+
+-- construct from an array
+function hashset.from(array)
+ assert(array)
+ return hashset.of(table.unpack(array))
+end
+
+-- check value is in hashset
+function hashset_impl:has(value)
+ value = hashset._to_key(value)
+ return self._DATA[value] or false
+end
+
+-- insert value to hashset, returns false if value has already in the hashset
+function hashset_impl:insert(value)
+ value = hashset._to_key(value)
+ local result = self._DATA[value] or false
+ if not result then
+ self._DATA[value] = true
+ end
+ return result
+end
+
+-- remove value from hashset, returns false if value is not in the hashset
+function hashset_impl:remove(value)
+ value = hashset._to_key(value)
+ local result = self._DATA[value] or false
+ if result then
+ self._DATA[value] = nil
+ end
+ return result
+end
+
+-- convert hashset to an array, nil in the set will be ignored
+function hashset_impl:to_array()
+ local result = {}
+ for k,_ in pairs(self._DATA) do
+ if k ~= hashset._NIL then
+ table.insert(result, k)
+ end
+ end
+ return result
+end
+
+-- return module
+hashset.__index = hashset_impl
+return hashset
diff --git a/xmake/core/sandbox/modules/import/core/base/hashset.lua b/xmake/core/sandbox/modules/import/core/base/hashset.lua
new file mode 100644
index 000000000..81c833f57
--- /dev/null
+++ b/xmake/core/sandbox/modules/import/core/base/hashset.lua
@@ -0,0 +1,36 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2019, TBOOX Open Source Group.
+--
+-- @author OpportunityLiu
+-- @file hashset.lua
+--
+
+-- load modules
+local hashset = require("base/hashset")
+
+
+-- define module
+local sandbox_hashset = sandbox_hashset or {}
+
+-- inherit some builtin interfaces
+sandbox_hashset.new = hashset.new
+sandbox_hashset.of = hashset.of
+sandbox_hashset.from = hashset.from
+
+-- return module
+return sandbox_hashset
+
+