array-set.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. var util = require('./util');
  12. /**
  13. * A data structure which is a combination of an array and a set. Adding a new
  14. * member is O(1), testing for membership is O(1), and finding the index of an
  15. * element is O(1). Removing elements from the set is not supported. Only
  16. * strings are supported for membership.
  17. */
  18. function ArraySet() {
  19. this._array = [];
  20. this._set = {};
  21. }
  22. /**
  23. * Static method for creating ArraySet instances from an existing array.
  24. */
  25. ArraySet.fromArray = function ArraySet_fromArray(aArray, aAllowDuplicates) {
  26. var set = new ArraySet();
  27. for (var i = 0, len = aArray.length; i < len; i++) {
  28. set.add(aArray[i], aAllowDuplicates);
  29. }
  30. return set;
  31. };
  32. /**
  33. * Add the given string to this set.
  34. *
  35. * @param String aStr
  36. */
  37. ArraySet.prototype.add = function ArraySet_add(aStr, aAllowDuplicates) {
  38. var isDuplicate = this.has(aStr);
  39. var idx = this._array.length;
  40. if (!isDuplicate || aAllowDuplicates) {
  41. this._array.push(aStr);
  42. }
  43. if (!isDuplicate) {
  44. this._set[util.toSetString(aStr)] = idx;
  45. }
  46. };
  47. /**
  48. * Is the given string a member of this set?
  49. *
  50. * @param String aStr
  51. */
  52. ArraySet.prototype.has = function ArraySet_has(aStr) {
  53. return Object.prototype.hasOwnProperty.call(this._set,
  54. util.toSetString(aStr));
  55. };
  56. /**
  57. * What is the index of the given string in the array?
  58. *
  59. * @param String aStr
  60. */
  61. ArraySet.prototype.indexOf = function ArraySet_indexOf(aStr) {
  62. if (this.has(aStr)) {
  63. return this._set[util.toSetString(aStr)];
  64. }
  65. throw new Error('"' + aStr + '" is not in the set.');
  66. };
  67. /**
  68. * What is the element at the given index?
  69. *
  70. * @param Number aIdx
  71. */
  72. ArraySet.prototype.at = function ArraySet_at(aIdx) {
  73. if (aIdx >= 0 && aIdx < this._array.length) {
  74. return this._array[aIdx];
  75. }
  76. throw new Error('No element indexed by ' + aIdx);
  77. };
  78. /**
  79. * Returns the array representation of this set (which has the proper indices
  80. * indicated by indexOf). Note that this is a copy of the internal array used
  81. * for storing the members so that no one can mess with internal state.
  82. */
  83. ArraySet.prototype.toArray = function ArraySet_toArray() {
  84. return this._array.slice();
  85. };
  86. exports.ArraySet = ArraySet;
  87. });