|
| 1 | +-- Ternary operator |
| 2 | +function TS_ITE(condition, v1f, v2f) |
| 3 | + if condition then |
| 4 | + return v1f() |
| 5 | + else |
| 6 | + return v2f() |
| 7 | + end |
| 8 | +end |
| 9 | + |
| 10 | +-- Set data structure implementation |
| 11 | +Set = Set or {} |
| 12 | +Set.__index = Set |
| 13 | +function Set.new(construct, ...) |
| 14 | + local instance = setmetatable({}, Set) |
| 15 | + Set.constructor(instance, ...) |
| 16 | + return instance |
| 17 | +end |
| 18 | +function Set.constructor(self, other) |
| 19 | + self._items = {} |
| 20 | + if other then |
| 21 | + for a, _ in pairs(other) do |
| 22 | + self._items[a] = true |
| 23 | + end |
| 24 | + end |
| 25 | +end |
| 26 | +function Set.add(self, item) self._items[item] = true end |
| 27 | +function Set.contains(self, item) return self._items[item] ~= nil end |
| 28 | +function Set.remove(self, item) |
| 29 | + local contains = Set.contains(self, item) |
| 30 | + self._items[item] = nil |
| 31 | + return contains |
| 32 | +end |
| 33 | +function Set.items(self) |
| 34 | + local out = {} |
| 35 | + for item, _ in pairs(self._items) do |
| 36 | + table.insert(out, item) |
| 37 | + end |
| 38 | + return out |
| 39 | +end |
| 40 | +function Set.count(self) |
| 41 | + local count = 0 |
| 42 | + for item, _ in pairs(self._items) do |
| 43 | + count = count + 1 |
| 44 | + end |
| 45 | + return count |
| 46 | +end |
| 47 | + |
| 48 | +-- Set data structure implementation |
| 49 | +Map = Map or {} |
| 50 | +Map.__index = Map |
| 51 | +function Map.new(construct, ...) |
| 52 | + local instance = setmetatable({}, Map) |
| 53 | + Map.constructor(instance, ...) |
| 54 | + return instance |
| 55 | +end |
| 56 | +function Map.constructor(self, other) |
| 57 | + self._items = {} |
| 58 | + if other then |
| 59 | + for k, v in pairs(other) do |
| 60 | + self._items[k] = v |
| 61 | + end |
| 62 | + end |
| 63 | +end |
| 64 | +function Map.put(self, key, value) self._items[key] = value end |
| 65 | +function Map.containsKey(self, key) return self._items[key] ~= nil end |
| 66 | +function Map.remove(self, key) |
| 67 | + local contains = self.containsKey(self, key) |
| 68 | + self._items[key] = nil |
| 69 | + return contains |
| 70 | +end |
| 71 | +function Map.get(self, key) return self._items[key] end |
| 72 | +function Map.keys(self) |
| 73 | + local out = {} |
| 74 | + for k, v in pairs(self._items) do |
| 75 | + table.insert(out, k) |
| 76 | + end |
| 77 | + return out |
| 78 | +end |
| 79 | +function Map.values(self) |
| 80 | + local out = {} |
| 81 | + for k, v in pairs(self._items) do |
| 82 | + table.insert(out, v) |
| 83 | + end |
| 84 | + return out |
| 85 | +end |
| 86 | +function Map.items(self) |
| 87 | + local out = {} |
| 88 | + for k, v in pairs(self._items) do |
| 89 | + table.insert(out, {key=k, value=v}) |
| 90 | + end |
| 91 | + return out |
| 92 | +end |
| 93 | +function Map.count(self) |
| 94 | + local count = 0 |
| 95 | + for k, v in pairs(self._items) do |
| 96 | + count = count + 1 |
| 97 | + end |
| 98 | + return count |
| 99 | +end |
0 commit comments