@itwin/core-bentley
Version:
Bentley JavaScript core components
66 lines • 2.87 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderedSet = exports.ReadonlyOrderedSet = void 0;
const SortedArray_1 = require("./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
*/
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 = SortedArray_1.shallowClone) {
this._array = new SortedArray_1.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]();
}
}
exports.ReadonlyOrderedSet = ReadonlyOrderedSet;
/** A mutable [[ReadonlyOrderedSet]].
* @public
*/
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 = SortedArray_1.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);
}
}
exports.OrderedSet = OrderedSet;
//# sourceMappingURL=OrderedSet.js.map