react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / source-map / lib / source-map / array-set.js
80559 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2011 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*/6if (typeof define !== 'function') {7var define = require('amdefine')(module, require);8}9define(function (require, exports, module) {1011var util = require('./util');1213/**14* A data structure which is a combination of an array and a set. Adding a new15* member is O(1), testing for membership is O(1), and finding the index of an16* element is O(1). Removing elements from the set is not supported. Only17* strings are supported for membership.18*/19function ArraySet() {20this._array = [];21this._set = {};22}2324/**25* Static method for creating ArraySet instances from an existing array.26*/27ArraySet.fromArray = function ArraySet_fromArray(aArray, aAllowDuplicates) {28var set = new ArraySet();29for (var i = 0, len = aArray.length; i < len; i++) {30set.add(aArray[i], aAllowDuplicates);31}32return set;33};3435/**36* Add the given string to this set.37*38* @param String aStr39*/40ArraySet.prototype.add = function ArraySet_add(aStr, aAllowDuplicates) {41var isDuplicate = this.has(aStr);42var idx = this._array.length;43if (!isDuplicate || aAllowDuplicates) {44this._array.push(aStr);45}46if (!isDuplicate) {47this._set[util.toSetString(aStr)] = idx;48}49};5051/**52* Is the given string a member of this set?53*54* @param String aStr55*/56ArraySet.prototype.has = function ArraySet_has(aStr) {57return Object.prototype.hasOwnProperty.call(this._set,58util.toSetString(aStr));59};6061/**62* What is the index of the given string in the array?63*64* @param String aStr65*/66ArraySet.prototype.indexOf = function ArraySet_indexOf(aStr) {67if (this.has(aStr)) {68return this._set[util.toSetString(aStr)];69}70throw new Error('"' + aStr + '" is not in the set.');71};7273/**74* What is the element at the given index?75*76* @param Number aIdx77*/78ArraySet.prototype.at = function ArraySet_at(aIdx) {79if (aIdx >= 0 && aIdx < this._array.length) {80return this._array[aIdx];81}82throw new Error('No element indexed by ' + aIdx);83};8485/**86* Returns the array representation of this set (which has the proper indices87* indicated by indexOf). Note that this is a copy of the internal array used88* for storing the members so that no one can mess with internal state.89*/90ArraySet.prototype.toArray = function ArraySet_toArray() {91return this._array.slice();92};9394exports.ArraySet = ArraySet;9596});979899