|
1 | 1 | import ListCache from './.internal/ListCache.js'; |
2 | | -import stackClear from './.internal/stackClear.js'; |
3 | | -import stackDelete from './.internal/stackDelete.js'; |
4 | | -import stackGet from './.internal/stackGet.js'; |
5 | | -import stackHas from './.internal/stackHas.js'; |
6 | | -import stackSet from './.internal/stackSet.js'; |
7 | | - |
8 | | -/** |
9 | | - * Creates a stack cache object to store key-value pairs. |
10 | | - * |
11 | | - * @private |
12 | | - * @constructor |
13 | | - * @param {Array} [entries] The key-value pairs to cache. |
14 | | - */ |
| 2 | +import MapCache from './.internal/MapCache.js'; |
| 3 | + |
| 4 | +/** Used as the size to enable large array optimizations. */ |
| 5 | +const LARGE_ARRAY_SIZE = 200; |
| 6 | + |
15 | 7 | class Stack { |
| 8 | + |
| 9 | + /** |
| 10 | + * Creates a stack cache object to store key-value pairs. |
| 11 | + * |
| 12 | + * @private |
| 13 | + * @constructor |
| 14 | + * @param {Array} [entries] The key-value pairs to cache. |
| 15 | + */ |
16 | 16 | constructor(entries) { |
17 | 17 | const data = this.__data__ = new ListCache(entries); |
18 | 18 | this.size = data.size; |
19 | 19 | } |
20 | | -} |
21 | 20 |
|
22 | | -// Add methods to `Stack`. |
23 | | -Stack.prototype.clear = stackClear; |
24 | | -Stack.prototype['delete'] = stackDelete; |
25 | | -Stack.prototype.get = stackGet; |
26 | | -Stack.prototype.has = stackHas; |
27 | | -Stack.prototype.set = stackSet; |
| 21 | + /** |
| 22 | + * Removes all key-value entries from the stack. |
| 23 | + * |
| 24 | + * @memberOf Stack |
| 25 | + */ |
| 26 | + clear() { |
| 27 | + this.__data__ = new ListCache; |
| 28 | + this.size = 0; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Removes `key` and its value from the stack. |
| 33 | + * |
| 34 | + * @memberOf Stack |
| 35 | + * @param {string} key The key of the value to remove. |
| 36 | + * @returns {boolean} Returns `true` if the entry was removed, else `false`. |
| 37 | + */ |
| 38 | + delete(key) { |
| 39 | + const data = this.__data__; |
| 40 | + const result = data['delete'](key); |
| 41 | + |
| 42 | + this.size = data.size; |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the stack value for `key`. |
| 48 | + * |
| 49 | + * @memberOf Stack |
| 50 | + * @param {string} key The key of the value to get. |
| 51 | + * @returns {*} Returns the entry value. |
| 52 | + */ |
| 53 | + get(key) { |
| 54 | + return this.__data__.get(key); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Checks if a stack value for `key` exists. |
| 59 | + * |
| 60 | + * @memberOf Stack |
| 61 | + * @param {string} key The key of the entry to check. |
| 62 | + * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. |
| 63 | + */ |
| 64 | + has(key) { |
| 65 | + return this.__data__.has(key); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Sets the stack `key` to `value`. |
| 70 | + * |
| 71 | + * @memberOf Stack |
| 72 | + * @param {string} key The key of the value to set. |
| 73 | + * @param {*} value The value to set. |
| 74 | + * @returns {Object} Returns the stack cache instance. |
| 75 | + */ |
| 76 | + set(key, value) { |
| 77 | + let data = this.__data__; |
| 78 | + if (data instanceof ListCache) { |
| 79 | + const pairs = data.__data__; |
| 80 | + if (!Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) { |
| 81 | + pairs.push([key, value]); |
| 82 | + this.size = ++data.size; |
| 83 | + return this; |
| 84 | + } |
| 85 | + data = this.__data__ = new MapCache(pairs); |
| 86 | + } |
| 87 | + data.set(key, value); |
| 88 | + this.size = data.size; |
| 89 | + return this; |
| 90 | + } |
| 91 | +} |
28 | 92 |
|
29 | 93 | export default Stack; |
0 commit comments