webpack/lib/util/StackedSetMap.js

121 lines
2.4 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
*/
"use strict";
const TOMBSTONE = {};
const UNDEFINED_MARKER = {};
2017-08-11 18:30:33 +08:00
class StackedSetMap {
constructor(parentStack) {
2017-08-11 18:30:33 +08:00
this.stack = parentStack === undefined ? [] : parentStack.slice();
this.map = new Map();
this.stack.push(this.map);
}
add(item) {
this.map.set(item, true);
}
set(item, value) {
this.map.set(item, value === undefined ? UNDEFINED_MARKER : value);
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
}
has(item) {
const topValue = this.map.get(item);
2018-02-25 09:00:20 +08:00
if (topValue !== undefined) return topValue !== TOMBSTONE;
if (this.stack.length > 1) {
for (var 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
}
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) {
for (var 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();
2017-11-19 16:06:40 +08:00
return Array.from(this.map.entries(), pair => pair[0]);
}
2017-11-19 16:06:40 +08:00
asSet() {
return new Set(this.asArray());
}
asPairArray() {
this._compress();
2018-02-25 09:00:20 +08:00
return Array.from(
this.map.entries(),
2018-04-06 01:44:15 +08:00
pair =>
/** @type {[TODO, TODO]} */ (pair[1] === UNDEFINED_MARKER
2018-04-06 01:44:15 +08:00
? [pair[0], undefined]
: pair)
2018-02-25 09:00:20 +08:00
);
}
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;