Options
Public/Protected
  • Public
  • Public/Protected
  • All
Menu

GoJS API 文档

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(function(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

constructor

  • 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.

    Parameters

    • Optional coll: Iterable<T> | Array<T>

      an optional collection of items to add.

    Returns Set

Properties

Read-only count : number

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

Read-only iterator : Iterator<T>

  • 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 . . .
      }

Read-only size : number

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

Methods

add

  • 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.

addAll

  • 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> | Array<T>

      the collection of items to add.

    Returns Set<T>

    This modified Set.

Virtual all

  • all(pred: function(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: function(a: T): boolean

      This function must not have any side-effects.

      Returns boolean

      True if all predicate calls are true; false otherwise.

    Virtual any

    • any(pred: function(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: function(a: T): boolean

        This function must not have any side-effects.

        Returns boolean

        True if any predicate call is true; false otherwise.

      clear

      • 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

      • 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.

      containsAll

      • 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

      containsAny

      • 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

      Virtual copy

      • 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

      • 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 Remove.

        Returns boolean

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

      Virtual each

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

        since

        1.4

        Parameters

        • func: function(a: T): void

          This function must not modify the collection.

          Returns Set<T>

          This Set itself.

        first

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

          Returns T | null

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

        has

        • 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

        • 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 insert.

          Returns boolean

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

        removeAll

        • 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> | Array<T>

            the collection of items to remove.

          Returns Set<T>

          This modified Set.

        retainAll

        • 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

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

          Returns Array<T>

          A copy of the Set in Array form.

        toList

        • 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.

        加入 GoJS 交流群
        GoJS 交流群 (769862113)