| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const NONE = Symbol("not sorted");
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * A subset of Set that offers sorting functionality
|
|---|
| 12 | * @template T item type in set
|
|---|
| 13 | * @extends {Set<T>}
|
|---|
| 14 | */
|
|---|
| 15 | class SortableSet extends Set {
|
|---|
| 16 | /**
|
|---|
| 17 | * Create a new sortable set
|
|---|
| 18 | * @template T
|
|---|
| 19 | * @typedef {(a: T, b: T) => number} SortFunction
|
|---|
| 20 | * @param {Iterable<T>=} initialIterable The initial iterable value
|
|---|
| 21 | * @param {SortFunction<T>=} defaultSort Default sorting function
|
|---|
| 22 | */
|
|---|
| 23 | constructor(initialIterable, defaultSort) {
|
|---|
| 24 | super(initialIterable);
|
|---|
| 25 | /**
|
|---|
| 26 | * @private
|
|---|
| 27 | * @type {undefined | SortFunction<T>}
|
|---|
| 28 | */
|
|---|
| 29 | this._sortFn = defaultSort;
|
|---|
| 30 | /**
|
|---|
| 31 | * @private
|
|---|
| 32 | * @type {typeof NONE | undefined | ((a: T, b: T) => number)}}
|
|---|
| 33 | */
|
|---|
| 34 | this._lastActiveSortFn = NONE;
|
|---|
| 35 | /**
|
|---|
| 36 | * @private
|
|---|
| 37 | * @template R
|
|---|
| 38 | * @type {Map<(set: SortableSet<T>) => EXPECTED_ANY, EXPECTED_ANY> | undefined}
|
|---|
| 39 | */
|
|---|
| 40 | this._cache = undefined;
|
|---|
| 41 | /**
|
|---|
| 42 | * @private
|
|---|
| 43 | * @template R
|
|---|
| 44 | * @type {Map<(set: SortableSet<T>) => EXPECTED_ANY, EXPECTED_ANY> | undefined}
|
|---|
| 45 | */
|
|---|
| 46 | this._cacheOrderIndependent = undefined;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Returns itself.
|
|---|
| 51 | * @param {T} value value to add to set
|
|---|
| 52 | * @returns {this} returns itself
|
|---|
| 53 | */
|
|---|
| 54 | add(value) {
|
|---|
| 55 | this._lastActiveSortFn = NONE;
|
|---|
| 56 | this._invalidateCache();
|
|---|
| 57 | this._invalidateOrderedCache();
|
|---|
| 58 | super.add(value);
|
|---|
| 59 | return this;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Returns true if value existed in set, false otherwise.
|
|---|
| 64 | * @param {T} value value to delete
|
|---|
| 65 | * @returns {boolean} true if value existed in set, false otherwise
|
|---|
| 66 | */
|
|---|
| 67 | delete(value) {
|
|---|
| 68 | this._invalidateCache();
|
|---|
| 69 | this._invalidateOrderedCache();
|
|---|
| 70 | return super.delete(value);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Describes how this clear operation behaves.
|
|---|
| 75 | * @returns {void}
|
|---|
| 76 | */
|
|---|
| 77 | clear() {
|
|---|
| 78 | this._invalidateCache();
|
|---|
| 79 | this._invalidateOrderedCache();
|
|---|
| 80 | return super.clear();
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Sort with a comparer function
|
|---|
| 85 | * @param {SortFunction<T> | undefined} sortFn Sorting comparer function
|
|---|
| 86 | * @returns {void}
|
|---|
| 87 | */
|
|---|
| 88 | sortWith(sortFn) {
|
|---|
| 89 | if (this.size <= 1 || sortFn === this._lastActiveSortFn) {
|
|---|
| 90 | // already sorted - nothing to do
|
|---|
| 91 | return;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /** @type {T[]} */
|
|---|
| 95 | const sortedArray = [...this].sort(sortFn);
|
|---|
| 96 | super.clear();
|
|---|
| 97 | for (let i = 0; i < sortedArray.length; i += 1) {
|
|---|
| 98 | super.add(sortedArray[i]);
|
|---|
| 99 | }
|
|---|
| 100 | this._lastActiveSortFn = sortFn;
|
|---|
| 101 | this._invalidateCache();
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | sort() {
|
|---|
| 105 | this.sortWith(this._sortFn);
|
|---|
| 106 | return this;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Get data from cache
|
|---|
| 111 | * @template R
|
|---|
| 112 | * @param {(set: SortableSet<T>) => R} fn function to calculate value
|
|---|
| 113 | * @returns {R} returns result of fn(this), cached until set changes
|
|---|
| 114 | */
|
|---|
| 115 | getFromCache(fn) {
|
|---|
| 116 | if (this._cache === undefined) {
|
|---|
| 117 | this._cache = new Map();
|
|---|
| 118 | } else {
|
|---|
| 119 | const result = this._cache.get(fn);
|
|---|
| 120 | const data = /** @type {R} */ (result);
|
|---|
| 121 | if (data !== undefined) {
|
|---|
| 122 | return data;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | const newData = fn(this);
|
|---|
| 126 | this._cache.set(fn, newData);
|
|---|
| 127 | return newData;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * Get data from cache (ignoring sorting)
|
|---|
| 132 | * @template R
|
|---|
| 133 | * @param {(set: SortableSet<T>) => R} fn function to calculate value
|
|---|
| 134 | * @returns {R} returns result of fn(this), cached until set changes
|
|---|
| 135 | */
|
|---|
| 136 | getFromUnorderedCache(fn) {
|
|---|
| 137 | if (this._cacheOrderIndependent === undefined) {
|
|---|
| 138 | this._cacheOrderIndependent = new Map();
|
|---|
| 139 | } else {
|
|---|
| 140 | const result = this._cacheOrderIndependent.get(fn);
|
|---|
| 141 | const data = /** @type {R} */ (result);
|
|---|
| 142 | if (data !== undefined) {
|
|---|
| 143 | return data;
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | const newData = fn(this);
|
|---|
| 147 | this._cacheOrderIndependent.set(fn, newData);
|
|---|
| 148 | return newData;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * Invalidates the cached state associated with this value.
|
|---|
| 153 | * @private
|
|---|
| 154 | * @returns {void}
|
|---|
| 155 | */
|
|---|
| 156 | _invalidateCache() {
|
|---|
| 157 | if (this._cache !== undefined) {
|
|---|
| 158 | this._cache.clear();
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | * Invalidate ordered cache.
|
|---|
| 164 | * @private
|
|---|
| 165 | * @returns {void}
|
|---|
| 166 | */
|
|---|
| 167 | _invalidateOrderedCache() {
|
|---|
| 168 | if (this._cacheOrderIndependent !== undefined) {
|
|---|
| 169 | this._cacheOrderIndependent.clear();
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 174 | * Returns the raw array.
|
|---|
| 175 | * @returns {T[]} the raw array
|
|---|
| 176 | */
|
|---|
| 177 | toJSON() {
|
|---|
| 178 | return [...this];
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | module.exports = SortableSet;
|
|---|