@itwin/core-bentley
Version:
Bentley JavaScript core components
61 lines • 2.63 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Collections
*/
import { shallowClone, SortedArray } from "./SortedArray";
/** A read-only equivalent of `Set<T>` that maintains its elements in sorted order as specified by a comparison function.
* Iteration returns elements in the order specified by the comparison function, as opposed to `Set` which returns elements in insertion order.
* Implemented in terms of [[SortedArray]].
* @public
*/
export class ReadonlyOrderedSet {
_array;
/** Construct a new ReadonlyOrderedSet<T>.
* @param compare The function used to compare elements within the set, determining their ordering.
* @param clone The function invoked to clone a new element for insertion into the set. The default implementation simply returns its input.
*/
constructor(compare, clone = shallowClone) {
this._array = new SortedArray(compare, false, clone);
}
/** The number of elements in the set. */
get size() {
return this._array.length;
}
/** Returns true if `value` is present in the set. */
has(value) {
return -1 !== this._array.indexOf(value);
}
/** Iterate over the elements in sorted order (as opposed to `Set`'s iterator, which returns elements in insertion order). */
[Symbol.iterator]() {
return this._array[Symbol.iterator]();
}
}
/** A mutable [[ReadonlyOrderedSet]].
* @public
*/
export class OrderedSet extends ReadonlyOrderedSet {
/** Construct a new OrderedSet<T>.
* @param compare The function used to compare elements within the set, determining their ordering.
* @param clone The function invoked to clone a new element for insertion into the set. The default implementation simply returns its input.
*/
constructor(compare, clone = shallowClone) {
super(compare, clone);
}
/** Remove all elements from the set. */
clear() {
this._array.clear();
}
/** Add the specified element to the set. Returns this set. */
add(value) {
this._array.insert(value);
return this;
}
/** Removes the specified element from the set. Returns `true` if the element was present. */
delete(value) {
return -1 !== this._array.remove(value);
}
}
//# sourceMappingURL=OrderedSet.js.map