webpack/lib/util/StackedSetMap.js

175 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-08-11 18:30:33 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
2017-08-11 18:30:33 +08:00
"use strict";
2018-11-26 04:12:28 +08:00
const TOMBSTONE = Symbol("tombstone");
const UNDEFINED_MARKER = Symbol("undefined");
/**
* @template T
* @typedef {T | true | undefined} Cell<T>
*/
/**
* @template T
* @typedef {T | true | typeof TOMBSTONE | typeof UNDEFINED_MARKER} InternalCell<T>
*/
/**
* @template K
* @template V
* @param {[K, InternalCell<V>]} pair the internal cell
* @returns {[K, Cell<V>]} its safe representation
*/
const extractPair = pair => {
const key = pair[0];
const val = pair[1];
if (val === UNDEFINED_MARKER || val === TOMBSTONE) {
return [key, undefined];
} else {
2018-11-26 23:26:18 +08:00
return /** @type {[K, Cell<V>]} */ (pair);
2018-11-26 04:12:28 +08:00
}
};
2018-11-26 04:12:28 +08:00
/**
* @template K
* @template V
*/
2017-08-11 18:30:33 +08:00
class StackedSetMap {
2018-11-26 04:12:28 +08:00
/**
* @param {Map<K, InternalCell<V>>[]=} parentStack an optional parent
2018-11-26 04:12:28 +08:00
*/
constructor(parentStack) {
2018-11-26 04:12:28 +08:00
/** @type {Map<K, InternalCell<V>>} */
2017-08-11 18:30:33 +08:00
this.map = new Map();
2018-11-26 04:12:28 +08:00
/** @type {Map<K, InternalCell<V>>[]} */
this.stack = parentStack === undefined ? [] : parentStack.slice();
2017-08-11 18:30:33 +08:00
this.stack.push(this.map);
}
2018-11-26 04:12:28 +08:00
/**
* @param {K} item the item to add
* @returns {void}
*/
2017-08-11 18:30:33 +08:00
add(item) {
this.map.set(item, true);
}
2018-11-26 04:12:28 +08:00
/**
* @param {K} item the key of the element to add
* @param {V} value the value of the element to add
* @returns {void}
*/
2017-08-11 18:30:33 +08:00
set(item, value) {
this.map.set(item, value === undefined ? UNDEFINED_MARKER : value);
2017-08-11 18:30:33 +08:00
}
2018-11-26 04:12:28 +08:00
/**
* @param {K} item the item to delete
* @returns {void}
*/
2017-08-11 18:30:33 +08:00
delete(item) {
if (this.stack.length > 1) {
this.map.set(item, TOMBSTONE);
} else {
this.map.delete(item);
}
2017-08-11 18:30:33 +08:00
}
2018-11-26 04:12:28 +08:00
/**
* @param {K} item the item to test
* @returns {boolean} true if the item exists in this set
*/
2017-08-11 18:30:33 +08:00
has(item) {
const topValue = this.map.get(item);
2018-11-26 04:12:28 +08:00
if (topValue !== undefined) {
return topValue !== TOMBSTONE;
}
2018-02-25 09:00:20 +08:00
if (this.stack.length > 1) {
2018-11-26 04:12:28 +08:00
for (let i = this.stack.length - 2; i >= 0; i--) {
const value = this.stack[i].get(item);
2018-02-25 09:00:20 +08:00
if (value !== undefined) {
this.map.set(item, value);
return value !== TOMBSTONE;
}
}
this.map.set(item, TOMBSTONE);
}
return false;
2017-08-11 18:30:33 +08:00
}
2018-11-26 04:12:28 +08:00
/**
* @param {K} item the key of the element to return
* @returns {Cell<V>} the value of the element
*/
2017-08-11 18:30:33 +08:00
get(item) {
const topValue = this.map.get(item);
if (topValue !== undefined) {
2018-02-25 09:00:20 +08:00
return topValue === TOMBSTONE || topValue === UNDEFINED_MARKER
? undefined
: topValue;
}
2018-02-25 09:00:20 +08:00
if (this.stack.length > 1) {
2018-11-26 04:12:28 +08:00
for (let i = this.stack.length - 2; i >= 0; i--) {
const value = this.stack[i].get(item);
2018-02-25 09:00:20 +08:00
if (value !== undefined) {
this.map.set(item, value);
2018-02-25 09:00:20 +08:00
return value === TOMBSTONE || value === UNDEFINED_MARKER
? undefined
: value;
}
2017-08-11 18:30:33 +08:00
}
this.map.set(item, TOMBSTONE);
2017-08-11 18:30:33 +08:00
}
return undefined;
2017-08-11 18:30:33 +08:00
}
_compress() {
2018-02-25 09:00:20 +08:00
if (this.stack.length === 1) return;
2017-08-11 18:30:33 +08:00
this.map = new Map();
2018-02-25 09:00:20 +08:00
for (const data of this.stack) {
for (const pair of data) {
if (pair[1] === TOMBSTONE) {
this.map.delete(pair[0]);
} else {
this.map.set(pair[0], pair[1]);
}
2017-08-11 18:30:33 +08:00
}
}
this.stack = [this.map];
}
2017-11-19 16:06:40 +08:00
asArray() {
2017-08-11 18:30:33 +08:00
this._compress();
2018-11-26 04:12:28 +08:00
return Array.from(this.map.keys());
}
2017-11-19 16:06:40 +08:00
asSet() {
2018-11-26 23:26:18 +08:00
this._compress();
return new Set(this.map.keys());
2017-11-19 16:06:40 +08:00
}
asPairArray() {
this._compress();
2018-11-26 04:12:28 +08:00
return Array.from(this.map.entries(), extractPair);
}
asMap() {
2017-11-19 16:06:40 +08:00
return new Map(this.asPairArray());
2017-08-11 18:30:33 +08:00
}
2017-11-19 16:06:40 +08:00
get size() {
this._compress();
2017-11-19 16:06:40 +08:00
return this.map.size;
}
2017-08-11 18:30:33 +08:00
createChild() {
return new StackedSetMap(this.stack);
2017-08-11 18:30:33 +08:00
}
}
module.exports = StackedSetMap;