| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- "use strict";
- /**
- * Somewhat mimics the ValueSet in mongo (std::set<Value>)
- * @class valueSet
- * @namespace mungedb-aggregate.pipeline.expressions
- * @module mungedb-aggregate
- * @constructor
- */
- var ValueSet = module.exports = function ValueSet(vals) {
- this.set = {};
- if (vals instanceof Array)
- this.insertRange(vals);
- }, klass = ValueSet, proto = klass.prototype;
- proto._getKey = JSON.stringify;
- proto.hasKey = function hasKey(key) {
- return key in this.set;
- };
- //SKIPPED: proto.count -- use hasKey instead
- proto.has = function has(val) {
- return this._getKey(val) in this.set;
- };
- proto.insert = function insert(val) {
- var valKey = this._getKey(val);
- if (!this.hasKey(valKey)) {
- this.set[valKey] = val;
- return valKey;
- }
- return undefined;
- };
- proto.insertRange = function insertRange(vals) {
- var results = [];
- for (var i = 0, l = vals.length; i < l; i++)
- results.push(this.insert(vals[i]));
- return results;
- };
- proto.equals = function equals(other) {
- for (var key in this.set) {
- if (!other.hasKey(key))
- return false;
- }
- for (var otherKey in other.set) {
- if (!this.hasKey(otherKey))
- return false;
- }
- return true;
- };
- proto.values = function values() {
- var vals = [];
- for (var key in this.set)
- vals.push(this.set[key]);
- return vals;
- };
- proto.size = function values() {
- var n = 0;
- for (var key in this.set) //jshint ignore:line
- n++;
- return n;
- };
- proto.swap = function swap(other) {
- var tmp = this.set;
- this.set = other.set;
- other.set = tmp;
- };
- proto.eraseKey = function eraseKey(key) {
- delete this.set[key];
- };
- proto.erase = function erase(val) {
- var key = this._getKey(val);
- this.eraseKey(key);
- };
- proto.empty = function empty() {
- for (var key in this.set) //jshint ignore:line
- return false;
- return true;
- };
|