Set

The Set class provides a powerful and efficient way to manage collections of unique items. It delivers fast lookups and ordered iteration, making it ideal for tracking and manipulating distinct elements. Quickly add and remove items, safely iterate while modifying the set, and perform essential set operations like union and intersection with other sets. With features for determining size, checking for element presence, and converting to a list, the Set class is perfect for applications needing dynamic management of unique data.

Usage

To use this class, add the following require at the top of your script:

local Set = require 'engine/data/set'

Reference

class Set
module:

A set implementation with fast lookup and ordered iteration.

_rm: dict[any, boolean]

?

static new(values?)

Create a new set

Parameters:

values? (list[any]) – List of initial values

Return type:

Set

add(value)

Add a value to the set

Parameters:

value (any)

remove(value)

Remove a value from the set

Parameters:

value (any)

removeIf(predicate)

Remove all elements matching a predicate

Parameters:

predicate (fun(any):boolean)

forEach(func)

Iterate over each element in the set

Parameters:

func (fun(any):boolean or nil)

union(other)

Get the union of this set and another

Parameters:

other (Set)

Return type:

Set

intersection(other)

Get the intersection of this set and another

Parameters:

other (Set)

Return type:

Set

size()

Get the number of elements in the set

Return type:

number

contains(value)

Check if set contains a value

Parameters:

value (any)

Return type:

boolean

toList()

Return all elements as a list

Return type:

list[any]