SetCache.js 1.1 KB
Newer Older
1
import MapCache from './MapCache.js';
徐立's avatar
徐立 committed
2 3

/** Used to stand-in for `undefined` hash values. */
4
const HASH_UNDEFINED = '__lodash_hash_undefined__';
徐立's avatar
徐立 committed
5 6

class SetCache {
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	/**
	 * Creates an array cache object to store unique values.
	 *
	 * @private
	 * @constructor
	 * @param {Array} [values] The values to cache.
	 */
	constructor(values) {
		let index = -1;
		const length = values == null ? 0 : values.length;

		this.__data__ = new MapCache();
		while (++index < length) {
			this.add(values[index]);
		}
	}

	/**
	 * Adds `value` to the array cache.
	 *
	 * @memberOf SetCache
	 * @alias push
	 * @param {*} value The value to cache.
	 * @returns {Object} Returns the cache instance.
	 */
	add(value) {
		this.__data__.set(value, HASH_UNDEFINED);
		return this;
	}

	/**
	 * Checks if `value` is in the array cache.
	 *
	 * @memberOf SetCache
	 * @param {*} value The value to search for.
	 * @returns {number} Returns `true` if `value` is found, else `false`.
	 */
	has(value) {
		return this.__data__.has(value);
	}
徐立's avatar
徐立 committed
47 48
}

49
SetCache.prototype.push = SetCache.prototype.add;
徐立's avatar
徐立 committed
50

51
export default SetCache;