2017-06-18 08:41:58 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
module.exports = class SortableSet extends Set {
|
|
|
|
|
|
|
|
constructor(initialIterable, defaultSort) {
|
|
|
|
super(initialIterable);
|
|
|
|
this._sortFn = defaultSort;
|
2017-06-19 20:20:10 +08:00
|
|
|
this._lastActiveSortFn = null;
|
2017-11-03 17:17:08 +08:00
|
|
|
this._cache = null;
|
|
|
|
this._cacheOrderIndependent = null;
|
2017-06-19 20:20:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} value - value to add to set
|
|
|
|
* @returns {SortableSet} - returns itself
|
|
|
|
*/
|
|
|
|
add(value) {
|
|
|
|
this._lastActiveSortFn = null;
|
2017-11-03 17:17:08 +08:00
|
|
|
this._cache = null;
|
|
|
|
this._cacheOrderIndependent = null;
|
2017-06-19 20:20:10 +08:00
|
|
|
super.add(value);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-09-22 20:07:28 +08:00
|
|
|
delete(value) {
|
2017-11-03 17:17:08 +08:00
|
|
|
this._cache = null;
|
|
|
|
this._cacheOrderIndependent = null;
|
2017-09-22 20:07:28 +08:00
|
|
|
return super.delete(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
2017-11-03 17:17:08 +08:00
|
|
|
this._cache = null;
|
|
|
|
this._cacheOrderIndependent = null;
|
2017-09-22 20:07:28 +08:00
|
|
|
return super.clear();
|
|
|
|
}
|
|
|
|
|
2017-06-18 08:41:58 +08:00
|
|
|
/**
|
|
|
|
* @param {Function} sortFn - function to sort the set
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
sortWith(sortFn) {
|
2017-06-20 20:50:32 +08:00
|
|
|
if(this.size === 0 || sortFn === this._lastActiveSortFn) {
|
2017-06-19 20:20:10 +08:00
|
|
|
// already sorted - nothing to do
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-18 08:41:58 +08:00
|
|
|
const sortedArray = Array.from(this).sort(sortFn);
|
2017-06-19 20:20:10 +08:00
|
|
|
super.clear();
|
2017-06-18 08:41:58 +08:00
|
|
|
for(let i = 0; i < sortedArray.length; i += 1) {
|
2017-09-22 23:23:49 +08:00
|
|
|
super.add(sortedArray[i]);
|
2017-06-18 08:41:58 +08:00
|
|
|
}
|
2017-06-19 20:20:10 +08:00
|
|
|
this._lastActiveSortFn = sortFn;
|
2017-11-03 17:17:08 +08:00
|
|
|
this._cache = null;
|
2017-06-18 08:41:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
sort() {
|
|
|
|
this.sortWith(this._sortFn);
|
|
|
|
}
|
2017-09-22 20:07:28 +08:00
|
|
|
|
|
|
|
/**
|
2017-11-03 17:17:08 +08:00
|
|
|
* @param {Function} fn - function to calculate value
|
|
|
|
* @returns {any} - returns result of fn(this), cached until set changes
|
2017-09-22 20:07:28 +08:00
|
|
|
*/
|
2017-11-06 19:15:23 +08:00
|
|
|
getFromCache(fn) {
|
2017-11-03 17:17:08 +08:00
|
|
|
if(this._cache === null) {
|
|
|
|
this._cache = new Map();
|
|
|
|
} else {
|
|
|
|
const data = this._cache.get(fn);
|
|
|
|
if(data !== undefined) {
|
|
|
|
return data;
|
|
|
|
}
|
2017-09-22 20:07:28 +08:00
|
|
|
}
|
2017-11-03 17:17:08 +08:00
|
|
|
const newData = fn(this);
|
|
|
|
this._cache.set(fn, newData);
|
|
|
|
return newData;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param {Function} fn - function to calculate value
|
|
|
|
* @returns {any} - returns result of fn(this), cached until set changes
|
|
|
|
*/
|
2017-11-06 19:15:23 +08:00
|
|
|
getFromUnorderedCache(fn) {
|
2017-11-03 17:17:08 +08:00
|
|
|
if(this._cacheOrderIndependent === null) {
|
|
|
|
this._cacheOrderIndependent = new Map();
|
|
|
|
} else {
|
|
|
|
const data = this._cacheOrderIndependent.get(fn);
|
|
|
|
if(data !== undefined) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const newData = fn(this);
|
|
|
|
this._cacheOrderIndependent.set(fn, newData);
|
|
|
|
return newData;
|
2017-09-22 20:07:28 +08:00
|
|
|
}
|
2017-06-18 08:41:58 +08:00
|
|
|
};
|