@head @module set @title set: Set type

The Set class implements a set of objects. Any objects that can be used as @ref{Map} keys can be added to a set. The Set class enforces the set constraint: any object may be included in a set at most a single time. An object can be added to a set even when the set already contains that object, but the operation will have no effect. The order of item additions and removals is not significant — a set object only records which objects are currently included in the set, but the objects have no specific order. @h2 Class Set<T> @implements Iterable @supertypes @class Set() @desc Construct an empty set. @end @class Set(iterable as Iterable) @desc Construct and initialize a set. Iterate over the contents of the argument, which must be an iterable object, and add them to the set. Example: @example Set([1, 3, "x"]) -- Set with items 1, 3 and "x" @end @end

Set methods

@fun length() as Int @desc Return the number of objects in the set. @end @fun add(object as T) @desc Add an object to the set. If the object is already in the set, this operation does nothing. @end @fun remove(object as T) @desc Remove an object from the set. Do nothing if the object is not included in the set. @end @fun iterator() as Iterator @desc Return an iterator object that iterates over all the objects in the set in an arbitrary order. @end @fun copy() as Set @desc Return a copy of the set with the same items as the original set. @note Only item references are copied to the new set — the items itself are not duplicated. @end @end

Set operations

Set objects support the following operations: @op x in set @optype{Object in Set -> Boolean} @desc Test whether a set contains a specific object. @end @op for x in set @optype{for T in Set} @desc Set contents can be iterated with a for loop. The objects are iterated in an arbitrary order. @end @op set1 == set2 @optype{Set == Object} @desc Compare sets for equality. Two sets are equal if they contain the same objects. @end @op Str(set) @desc Return a string representation of a set. @end @op Hash(set) @desc Return the hash value of a set. @end