Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / source-map / lib / source-map / array-set.js
1 /* -*- Mode: js; js-indent-level: 2; -*- */
2 /*
3  * Copyright 2011 Mozilla Foundation and contributors
4  * Licensed under the New BSD license. See LICENSE or:
5  * http://opensource.org/licenses/BSD-3-Clause
6  */
7 if (typeof define !== 'function') {
8     var define = require('amdefine')(module, require);
9 }
10 define(function (require, exports, module) {
11
12   var util = require('./util');
13
14   /**
15    * A data structure which is a combination of an array and a set. Adding a new
16    * member is O(1), testing for membership is O(1), and finding the index of an
17    * element is O(1). Removing elements from the set is not supported. Only
18    * strings are supported for membership.
19    */
20   function ArraySet() {
21     this._array = [];
22     this._set = {};
23   }
24
25   /**
26    * Static method for creating ArraySet instances from an existing array.
27    */
28   ArraySet.fromArray = function ArraySet_fromArray(aArray, aAllowDuplicates) {
29     var set = new ArraySet();
30     for (var i = 0, len = aArray.length; i < len; i++) {
31       set.add(aArray[i], aAllowDuplicates);
32     }
33     return set;
34   };
35
36   /**
37    * Return how many unique items are in this ArraySet. If duplicates have been
38    * added, than those do not count towards the size.
39    *
40    * @returns Number
41    */
42   ArraySet.prototype.size = function ArraySet_size() {
43     return Object.getOwnPropertyNames(this._set).length;
44   };
45
46   /**
47    * Add the given string to this set.
48    *
49    * @param String aStr
50    */
51   ArraySet.prototype.add = function ArraySet_add(aStr, aAllowDuplicates) {
52     var isDuplicate = this.has(aStr);
53     var idx = this._array.length;
54     if (!isDuplicate || aAllowDuplicates) {
55       this._array.push(aStr);
56     }
57     if (!isDuplicate) {
58       this._set[util.toSetString(aStr)] = idx;
59     }
60   };
61
62   /**
63    * Is the given string a member of this set?
64    *
65    * @param String aStr
66    */
67   ArraySet.prototype.has = function ArraySet_has(aStr) {
68     return Object.prototype.hasOwnProperty.call(this._set,
69                                                 util.toSetString(aStr));
70   };
71
72   /**
73    * What is the index of the given string in the array?
74    *
75    * @param String aStr
76    */
77   ArraySet.prototype.indexOf = function ArraySet_indexOf(aStr) {
78     if (this.has(aStr)) {
79       return this._set[util.toSetString(aStr)];
80     }
81     throw new Error('"' + aStr + '" is not in the set.');
82   };
83
84   /**
85    * What is the element at the given index?
86    *
87    * @param Number aIdx
88    */
89   ArraySet.prototype.at = function ArraySet_at(aIdx) {
90     if (aIdx >= 0 && aIdx < this._array.length) {
91       return this._array[aIdx];
92     }
93     throw new Error('No element indexed by ' + aIdx);
94   };
95
96   /**
97    * Returns the array representation of this set (which has the proper indices
98    * indicated by indexOf). Note that this is a copy of the internal array used
99    * for storing the members so that no one can mess with internal state.
100    */
101   ArraySet.prototype.toArray = function ArraySet_toArray() {
102     return this._array.slice();
103   };
104
105   exports.ArraySet = ArraySet;
106
107 });