Created
October 15, 2025 05:47
-
-
Save magicoal-nerb/f184ef22b6376538695ace7ec24529db to your computer and use it in GitHub Desktop.
set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --!strict | |
| local Set = {} | |
| Set.__index = Set | |
| export type Set<T> = typeof(setmetatable({} :: { | |
| data: { [T]: boolean }, | |
| }, Set)) | |
| function Set.new() | |
| return setmetatable({ | |
| data = {}, | |
| }, Set) | |
| end | |
| function Set.contains<T>(self: Set<T>, what: T): boolean | |
| return self.data[what] ~= nil | |
| end | |
| function Set.insert<T>(self: Set<T>, what: T) | |
| self.data[what] = true | |
| end | |
| function Set.delete<T>(self: Set<T>, what: T) | |
| self.data[what] = nil | |
| end | |
| function Set.iterate<T>(self: Set<T>, callback: (T, ...any) -> (), ...: any) | |
| for key in self.data do | |
| callback(key, ...) | |
| end | |
| end | |
| return Set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment