source: imaps-frontend/node_modules/webpack/lib/util/LazySet.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const makeSerializable = require("./makeSerializable.js");
9
10/**
11 * @template T
12 * @param {Set<T>} targetSet set where items should be added
13 * @param {Set<Iterable<T>>} toMerge iterables to be merged
14 * @returns {void}
15 */
16const merge = (targetSet, toMerge) => {
17 for (const set of toMerge) {
18 for (const item of set) {
19 targetSet.add(item);
20 }
21 }
22};
23
24/**
25 * @template T
26 * @param {Set<Iterable<T>>} targetSet set where iterables should be added
27 * @param {Array<LazySet<T>>} toDeepMerge lazy sets to be flattened
28 * @returns {void}
29 */
30const flatten = (targetSet, toDeepMerge) => {
31 for (const set of toDeepMerge) {
32 if (set._set.size > 0) targetSet.add(set._set);
33 if (set._needMerge) {
34 for (const mergedSet of set._toMerge) {
35 targetSet.add(mergedSet);
36 }
37 flatten(targetSet, set._toDeepMerge);
38 }
39 }
40};
41
42/**
43 * Like Set but with an addAll method to eventually add items from another iterable.
44 * Access methods make sure that all delayed operations are executed.
45 * Iteration methods deopts to normal Set performance until clear is called again (because of the chance of modifications during iteration).
46 * @template T
47 */
48class LazySet {
49 /**
50 * @param {Iterable<T>=} iterable init iterable
51 */
52 constructor(iterable) {
53 /** @type {Set<T>} */
54 this._set = new Set(iterable);
55 /** @type {Set<Iterable<T>>} */
56 this._toMerge = new Set();
57 /** @type {Array<LazySet<T>>} */
58 this._toDeepMerge = [];
59 this._needMerge = false;
60 this._deopt = false;
61 }
62
63 _flatten() {
64 flatten(this._toMerge, this._toDeepMerge);
65 this._toDeepMerge.length = 0;
66 }
67
68 _merge() {
69 this._flatten();
70 merge(this._set, this._toMerge);
71 this._toMerge.clear();
72 this._needMerge = false;
73 }
74
75 _isEmpty() {
76 return (
77 this._set.size === 0 &&
78 this._toMerge.size === 0 &&
79 this._toDeepMerge.length === 0
80 );
81 }
82
83 get size() {
84 if (this._needMerge) this._merge();
85 return this._set.size;
86 }
87
88 /**
89 * @param {T} item an item
90 * @returns {LazySet<T>} itself
91 */
92 add(item) {
93 this._set.add(item);
94 return this;
95 }
96
97 /**
98 * @param {Iterable<T> | LazySet<T>} iterable a immutable iterable or another immutable LazySet which will eventually be merged into the Set
99 * @returns {LazySet<T>} itself
100 */
101 addAll(iterable) {
102 if (this._deopt) {
103 const _set = this._set;
104 for (const item of iterable) {
105 _set.add(item);
106 }
107 } else {
108 if (iterable instanceof LazySet) {
109 if (iterable._isEmpty()) return this;
110 this._toDeepMerge.push(iterable);
111 this._needMerge = true;
112 if (this._toDeepMerge.length > 100000) {
113 this._flatten();
114 }
115 } else {
116 this._toMerge.add(iterable);
117 this._needMerge = true;
118 }
119 if (this._toMerge.size > 100000) this._merge();
120 }
121 return this;
122 }
123
124 clear() {
125 this._set.clear();
126 this._toMerge.clear();
127 this._toDeepMerge.length = 0;
128 this._needMerge = false;
129 this._deopt = false;
130 }
131
132 /**
133 * @param {T} value an item
134 * @returns {boolean} true, if the value was in the Set before
135 */
136 delete(value) {
137 if (this._needMerge) this._merge();
138 return this._set.delete(value);
139 }
140
141 /**
142 * @returns {IterableIterator<[T, T]>} entries
143 */
144 entries() {
145 this._deopt = true;
146 if (this._needMerge) this._merge();
147 return this._set.entries();
148 }
149
150 /**
151 * @param {function(T, T, Set<T>): void} callbackFn function called for each entry
152 * @param {any} thisArg this argument for the callbackFn
153 * @returns {void}
154 */
155 forEach(callbackFn, thisArg) {
156 this._deopt = true;
157 if (this._needMerge) this._merge();
158 // eslint-disable-next-line unicorn/no-array-for-each
159 this._set.forEach(callbackFn, thisArg);
160 }
161
162 /**
163 * @param {T} item an item
164 * @returns {boolean} true, when the item is in the Set
165 */
166 has(item) {
167 if (this._needMerge) this._merge();
168 return this._set.has(item);
169 }
170
171 /**
172 * @returns {IterableIterator<T>} keys
173 */
174 keys() {
175 this._deopt = true;
176 if (this._needMerge) this._merge();
177 return this._set.keys();
178 }
179
180 /**
181 * @returns {IterableIterator<T>} values
182 */
183 values() {
184 this._deopt = true;
185 if (this._needMerge) this._merge();
186 return this._set.values();
187 }
188
189 /**
190 * @returns {IterableIterator<T>} iterable iterator
191 */
192 [Symbol.iterator]() {
193 this._deopt = true;
194 if (this._needMerge) this._merge();
195 return this._set[Symbol.iterator]();
196 }
197
198 /* istanbul ignore next */
199 get [Symbol.toStringTag]() {
200 return "LazySet";
201 }
202
203 /**
204 * @param {import("../serialization/ObjectMiddleware").ObjectSerializerContext} context context
205 */
206 serialize({ write }) {
207 if (this._needMerge) this._merge();
208 write(this._set.size);
209 for (const item of this._set) write(item);
210 }
211
212 /**
213 * @template T
214 * @param {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} context context
215 * @returns {LazySet<T>} lazy set
216 */
217 static deserialize({ read }) {
218 const count = read();
219 const items = [];
220 for (let i = 0; i < count; i++) {
221 items.push(read());
222 }
223 return new LazySet(items);
224 }
225}
226
227makeSerializable(LazySet, "webpack/lib/util/LazySet");
228
229module.exports = LazySet;
Note: See TracBrowser for help on using the repository browser.