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 | * @param {Iterable<T>=} initialIterable The initial iterable value
|
---|
20 | * @typedef {function(T, T): number} SortFunction
|
---|
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 | function(T, T): number}}
|
---|
33 | */
|
---|
34 | this._lastActiveSortFn = NONE;
|
---|
35 | /**
|
---|
36 | * @private
|
---|
37 | * @type {Map<Function, any> | undefined}
|
---|
38 | */
|
---|
39 | this._cache = undefined;
|
---|
40 | /**
|
---|
41 | * @private
|
---|
42 | * @type {Map<Function, any> | undefined}
|
---|
43 | */
|
---|
44 | this._cacheOrderIndependent = undefined;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @param {T} value value to add to set
|
---|
49 | * @returns {this} returns itself
|
---|
50 | */
|
---|
51 | add(value) {
|
---|
52 | this._lastActiveSortFn = NONE;
|
---|
53 | this._invalidateCache();
|
---|
54 | this._invalidateOrderedCache();
|
---|
55 | super.add(value);
|
---|
56 | return this;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * @param {T} value value to delete
|
---|
61 | * @returns {boolean} true if value existed in set, false otherwise
|
---|
62 | */
|
---|
63 | delete(value) {
|
---|
64 | this._invalidateCache();
|
---|
65 | this._invalidateOrderedCache();
|
---|
66 | return super.delete(value);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @returns {void}
|
---|
71 | */
|
---|
72 | clear() {
|
---|
73 | this._invalidateCache();
|
---|
74 | this._invalidateOrderedCache();
|
---|
75 | return super.clear();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Sort with a comparer function
|
---|
80 | * @param {SortFunction<T> | undefined} sortFn Sorting comparer function
|
---|
81 | * @returns {void}
|
---|
82 | */
|
---|
83 | sortWith(sortFn) {
|
---|
84 | if (this.size <= 1 || sortFn === this._lastActiveSortFn) {
|
---|
85 | // already sorted - nothing to do
|
---|
86 | return;
|
---|
87 | }
|
---|
88 |
|
---|
89 | const sortedArray = Array.from(this).sort(sortFn);
|
---|
90 | super.clear();
|
---|
91 | for (let i = 0; i < sortedArray.length; i += 1) {
|
---|
92 | super.add(sortedArray[i]);
|
---|
93 | }
|
---|
94 | this._lastActiveSortFn = sortFn;
|
---|
95 | this._invalidateCache();
|
---|
96 | }
|
---|
97 |
|
---|
98 | sort() {
|
---|
99 | this.sortWith(this._sortFn);
|
---|
100 | return this;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Get data from cache
|
---|
105 | * @template R
|
---|
106 | * @param {function(SortableSet<T>): R} fn function to calculate value
|
---|
107 | * @returns {R} returns result of fn(this), cached until set changes
|
---|
108 | */
|
---|
109 | getFromCache(fn) {
|
---|
110 | if (this._cache === undefined) {
|
---|
111 | this._cache = new Map();
|
---|
112 | } else {
|
---|
113 | const result = this._cache.get(fn);
|
---|
114 | const data = /** @type {R} */ (result);
|
---|
115 | if (data !== undefined) {
|
---|
116 | return data;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | const newData = fn(this);
|
---|
120 | this._cache.set(fn, newData);
|
---|
121 | return newData;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Get data from cache (ignoring sorting)
|
---|
126 | * @template R
|
---|
127 | * @param {function(SortableSet<T>): R} fn function to calculate value
|
---|
128 | * @returns {R} returns result of fn(this), cached until set changes
|
---|
129 | */
|
---|
130 | getFromUnorderedCache(fn) {
|
---|
131 | if (this._cacheOrderIndependent === undefined) {
|
---|
132 | this._cacheOrderIndependent = new Map();
|
---|
133 | } else {
|
---|
134 | const result = this._cacheOrderIndependent.get(fn);
|
---|
135 | const data = /** @type {R} */ (result);
|
---|
136 | if (data !== undefined) {
|
---|
137 | return data;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | const newData = fn(this);
|
---|
141 | this._cacheOrderIndependent.set(fn, newData);
|
---|
142 | return newData;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * @private
|
---|
147 | * @returns {void}
|
---|
148 | */
|
---|
149 | _invalidateCache() {
|
---|
150 | if (this._cache !== undefined) {
|
---|
151 | this._cache.clear();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * @private
|
---|
157 | * @returns {void}
|
---|
158 | */
|
---|
159 | _invalidateOrderedCache() {
|
---|
160 | if (this._cacheOrderIndependent !== undefined) {
|
---|
161 | this._cacheOrderIndependent.clear();
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * @returns {T[]} the raw array
|
---|
167 | */
|
---|
168 | toJSON() {
|
---|
169 | return Array.from(this);
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | module.exports = SortableSet;
|
---|