set.lua

This adds a new data type Set.

To make this class available include this file at the beginning of a script via:

include "scripting/set.lua"
class Set(iteratable)

A set is a collection of items. Each item must compare uniquely in the set, otherwise it is discarded; that is a Set never contains the same item more than once.

It works with all types that are either unique (strings) or provide a __hash property. wl.map.Field and wl.map.MapObject implement such a property

size

(RO) Due to a bug in Lua 5.1, one cannot use the ‘#’ operator to get the number of items in a set. Use this property instead.

add(item)

Add this item to the set

discard(item)

Remove this item from the set. Does nothing if the item is not in the set.

contains(item)

Returns true if the item is contained in this set, false otherwise

pop_at(n)

Returns the n-th item of this set and removes it. Note that the only way to get to this item is by iterating, so this function scales linearly with n.

items()

Iterator function that allows easy iterating of all items.

s = Set:new{"a","b","c"}
for i in s:items() do print(i) end
union(other_set)

Returns a new set that is the union of both sets. This is also overloaded to the ‘+’ operator

subtract(other_set)

Returns a new set that contains all values of this set that are not in other_set. This is also overloaded to the ‘-’ operator.

intersection(other_set)

Returns a new set that contains all values of this set that are also in other_set. This is also overloaded to the ‘*’ operator.