Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Set<T>

Hierarchy

  • Set

NOTE: For 2.0 the constructor argument has changed. Set now optionally accepts a collection, and only checks types in TypeScript.

An unordered iterable collection that cannot contain two instances of the same value. In TypeScript it is a generic class that enforces at compile-time the type of elements that may be added to the Set.

An example usage:

  var set = new go.Set();  // In TypeScript: new go.Set<string>();
set.add("orange");
set.add("apple");
set.add("orange");
// now set.count === 2
// and set.contains("orange") === true
// and set.contains("banana") === false

You can iterate over the items in a Set:

  var it = aSet.iterator;
while (it.next()) {
. . . it.value . . .
}

Or:

  aSet.each(val => {
. . . val . . .
});

Although not precisely implementing the features of the EcmaScript 6 Set class, this GoJS Set class has synonyms for the following methods and property:

The constructor now takes an optional Iterable or Array argument that provides the initial elements for the new Set.

Note that GoJS iteration is quite different than ES6 iteration, so that functionality has not been made somewhat compatible. These collection classes were defined in GoJS before the ES6 collection classes were proposed.

Type parameters

  • T

Implements

Index

Constructors

  • There are two possible constructors:

    new go.Set(), for JavaScript

    new go.Set<T>() for TypeScript

    In TypeScript, the optional generic argument describes the type of values that this Set may hold.

    For example, the expression:

    // TypeScript:
    new go.Set<go.Point>()

    Creates a new Set that may only contain Points.

    Type parameters

    • T

    Parameters

    • Optional coll: Iterable<T> | T[]

      an optional collection of items to add.

    Returns Set<T>

Properties

  • This read-only property is the number of elements in the Set.

  • Gets an object that you can use for iterating over the Set. The value will be a member of the Set. Typical usage:

      var it = aSet.iterator;
    while (it.next()) {
    . . . " value: " + it.value . . .
    }
  • This read-only property is the number of elements in the Set.

Methods

  • add(val: T): Set<T>
  • Adds a given value to the Set, if not already present.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • val: T

      The value to add to the Set; must not be null.

    Returns Set<T>

    This modified Set.

  • Adds all of the values of a collection to this Set.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • coll: Iterable<T> | T[]

      the collection of items to add.

    Returns Set<T>

    This modified Set.

  • all(pred: (a: T) => boolean): boolean
  • This is true if all invocations of the given predicate on items in the collection are true.

    Call the given predicate on each item in the collection. As soon as a call returns false, this returns false. Otherwise this returns true. For an empty collection this returns true.

    since

    1.4

    Parameters

    • pred: (a: T) => boolean

      This function must not have any side-effects.

        • (a: T): boolean
        • Parameters

          • a: T

          Returns boolean

    Returns boolean

    True if all predicate calls are true; false otherwise.

  • any(pred: (a: T) => boolean): boolean
  • This is true if any invocation of the given predicate on items in the collection is true.

    Call the given predicate on each item in the collection. As soon as a call returns true, this returns true. Otherwise this returns false. For an empty collection this returns false.

    since

    1.4

    Parameters

    • pred: (a: T) => boolean

      This function must not have any side-effects.

        • (a: T): boolean
        • Parameters

          • a: T

          Returns boolean

    Returns boolean

    True if any predicate call is true; false otherwise.

  • clear(): void
  • Clears the Set. This sets the count to zero.

    Be careful not to call this method while iterating over the collection.

    Returns void

  • contains(val: T): boolean
  • Returns whether the given value is in this Set.

    Parameters

    • val: T

      The value to check.

    Returns boolean

    Whether or not the value is contained within the Set.

  • Returns true if all of the values of a given collection are in this Set.

    Parameters

    • coll: Iterable<T>

      the collection of items to check for.

    Returns boolean

  • Returns true if any of the values of a given collection are in this Set.

    Parameters

    • coll: Iterable<T>

      the collection of items to check for.

    Returns boolean

  • Makes a shallow copy of this Set. The values are not copied, so if they are objects they may continue to be shared with the original Set.

    Returns Set<T>

    The new Set with the same elements.

  • delete(val: T): boolean
  • Removes a value (if found) from the Set.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • val: T

      The value to insert.

    Returns boolean

    true if the value was found and removed, false otherwise.

  • each(func: (a: T) => void): Set<T>
  • Call the given function on each item in the collection.

    since

    1.4

    Parameters

    • func: (a: T) => void

      This function must not modify the collection.

        • (a: T): void
        • Parameters

          • a: T

          Returns void

    Returns Set<T>

    This Set itself.

  • first(): T
  • Returns the first item in the collection, or null if there is none.

    Returns T

    This returns null if there are no items in the collection.

  • has(val: T): boolean
  • Returns whether the given value is in this Set.

    Parameters

    • val: T

      The value to check.

    Returns boolean

    Whether or not the value is contained within the Set.

  • remove(val: T): boolean
  • Removes a value (if found) from the Set.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • val: T

      The value to remove.

    Returns boolean

    true if the value was found and removed, false otherwise.

  • Removes all of the values of a collection from this Set.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • coll: Iterable<T> | T[]

      the collection of items to remove.

    Returns Set<T>

    This modified Set.

  • Removes from this Set all items that are not in the given collection.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • coll: Iterable<T>

      the collection of items that should be kept in this Set.

    Returns Set<T>

    This modified Set.

  • toArray(): T[]
  • Produces a JavaScript Array from the contents of this Set.

    Returns T[]

    A copy of the Set in Array form.

  • Converts the Set to a List. Because there is no ordering within a Set, the values in the List may be in any order.

    Returns List<T>

    A copy of the contents of this Set in List form.