[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 10 | exports.PartiallyOrderedSet = exports.CircularDependencyFoundException = exports.DependencyNotFoundException = void 0;
|
---|
| 11 | const exception_1 = require("../exception");
|
---|
| 12 | class DependencyNotFoundException extends exception_1.BaseException {
|
---|
| 13 | constructor() {
|
---|
| 14 | super('One of the dependencies is not part of the set.');
|
---|
| 15 | }
|
---|
| 16 | }
|
---|
| 17 | exports.DependencyNotFoundException = DependencyNotFoundException;
|
---|
| 18 | class CircularDependencyFoundException extends exception_1.BaseException {
|
---|
| 19 | constructor() {
|
---|
| 20 | super('Circular dependencies found.');
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 | exports.CircularDependencyFoundException = CircularDependencyFoundException;
|
---|
| 24 | class PartiallyOrderedSet {
|
---|
| 25 | constructor() {
|
---|
| 26 | this._items = new Map();
|
---|
| 27 | }
|
---|
| 28 | _checkCircularDependencies(item, deps) {
|
---|
| 29 | if (deps.has(item)) {
|
---|
| 30 | throw new CircularDependencyFoundException();
|
---|
| 31 | }
|
---|
| 32 | deps.forEach((dep) => this._checkCircularDependencies(item, this._items.get(dep) || new Set()));
|
---|
| 33 | }
|
---|
| 34 | clear() {
|
---|
| 35 | this._items.clear();
|
---|
| 36 | }
|
---|
| 37 | has(item) {
|
---|
| 38 | return this._items.has(item);
|
---|
| 39 | }
|
---|
| 40 | get size() {
|
---|
| 41 | return this._items.size;
|
---|
| 42 | }
|
---|
| 43 | forEach(callbackfn, thisArg) {
|
---|
| 44 | for (const x of this) {
|
---|
| 45 | callbackfn.call(thisArg, x, x, this);
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | /**
|
---|
| 49 | * Returns an iterable of [v,v] pairs for every value `v` in the set.
|
---|
| 50 | */
|
---|
| 51 | *entries() {
|
---|
| 52 | for (const item of this) {
|
---|
| 53 | yield [item, item];
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | /**
|
---|
| 57 | * Despite its name, returns an iterable of the values in the set,
|
---|
| 58 | */
|
---|
| 59 | keys() {
|
---|
| 60 | return this.values();
|
---|
| 61 | }
|
---|
| 62 | /**
|
---|
| 63 | * Returns an iterable of values in the set.
|
---|
| 64 | */
|
---|
| 65 | values() {
|
---|
| 66 | return this[Symbol.iterator]();
|
---|
| 67 | }
|
---|
| 68 | add(item, deps = new Set()) {
|
---|
| 69 | if (Array.isArray(deps)) {
|
---|
| 70 | deps = new Set(deps);
|
---|
| 71 | }
|
---|
| 72 | // Verify item is not already in the set.
|
---|
| 73 | if (this._items.has(item)) {
|
---|
| 74 | const itemDeps = this._items.get(item) || new Set();
|
---|
| 75 | // If the dependency list is equal, just return, otherwise remove and keep going.
|
---|
| 76 | let equal = true;
|
---|
| 77 | for (const dep of deps) {
|
---|
| 78 | if (!itemDeps.has(dep)) {
|
---|
| 79 | equal = false;
|
---|
| 80 | break;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | if (equal) {
|
---|
| 84 | for (const dep of itemDeps) {
|
---|
| 85 | if (!deps.has(dep)) {
|
---|
| 86 | equal = false;
|
---|
| 87 | break;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | if (equal) {
|
---|
| 92 | return this;
|
---|
| 93 | }
|
---|
| 94 | else {
|
---|
| 95 | this._items.delete(item);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | // Verify all dependencies are part of the Set.
|
---|
| 99 | for (const dep of deps) {
|
---|
| 100 | if (!this._items.has(dep)) {
|
---|
| 101 | throw new DependencyNotFoundException();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | // Verify there's no dependency cycle.
|
---|
| 105 | this._checkCircularDependencies(item, deps);
|
---|
| 106 | this._items.set(item, new Set(deps));
|
---|
| 107 | return this;
|
---|
| 108 | }
|
---|
| 109 | delete(item) {
|
---|
| 110 | if (!this._items.has(item)) {
|
---|
| 111 | return false;
|
---|
| 112 | }
|
---|
| 113 | // Remove it from all dependencies if force == true.
|
---|
| 114 | this._items.forEach((value) => value.delete(item));
|
---|
| 115 | return this._items.delete(item);
|
---|
| 116 | }
|
---|
| 117 | *[Symbol.iterator]() {
|
---|
| 118 | const copy = new Map(this._items);
|
---|
| 119 | for (const [key, value] of copy.entries()) {
|
---|
| 120 | copy.set(key, new Set(value));
|
---|
| 121 | }
|
---|
| 122 | while (copy.size > 0) {
|
---|
| 123 | const run = [];
|
---|
| 124 | // Take the first item without dependencies.
|
---|
| 125 | for (const [item, deps] of copy.entries()) {
|
---|
| 126 | if (deps.size == 0) {
|
---|
| 127 | run.push(item);
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | for (const item of run) {
|
---|
| 131 | copy.forEach((s) => s.delete(item));
|
---|
| 132 | copy.delete(item);
|
---|
| 133 | yield item;
|
---|
| 134 | }
|
---|
| 135 | if (run.length == 0) {
|
---|
| 136 | // uh oh...
|
---|
| 137 | throw new CircularDependencyFoundException();
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | get [Symbol.toStringTag]() {
|
---|
| 142 | return 'Set';
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | exports.PartiallyOrderedSet = PartiallyOrderedSet;
|
---|