home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / firefox / objectsafemap.js < prev    next >
Text File  |  2006-08-07  |  3KB  |  99 lines

  1. function G_ObjectSafeMap(opt_name) {
  2. this.debugZone = "objectsafemap";
  3. this.name_ = opt_name ? opt_name : "noname";
  4. this.keys_ = [];
  5. this.values_ = [];
  6. }
  7. G_ObjectSafeMap.prototype.indexOfKey_ = function(key) {
  8. for (var i = 0; i < this.keys_.length; i++)
  9. if (this.keys_[i] === key)
  10. return i;
  11. return -1;
  12. }
  13. G_ObjectSafeMap.prototype.insert = function(key, value) {
  14. if (key === null)
  15. throw new Error("Can't use null as a key");
  16. if (value === undefined)
  17. throw new Error("Can't store undefined values in this map");
  18. var i = this.indexOfKey_(key);
  19. if (i == -1) {
  20. this.keys_.push(key);
  21. this.values_.push(value);
  22. } else {
  23. this.keys_[i] = key;
  24. this.values_[i] = value;
  25. }
  26. G_Assert(this, this.keys_.length == this.values_.length,
  27. "Different number of keys than values!");
  28. }
  29. G_ObjectSafeMap.prototype.erase = function(key) {
  30. var keyLocation = this.indexOfKey_(key);
  31. var keyFound = keyLocation != -1;
  32. if (keyFound) {
  33. this.keys_.splice(keyLocation, 1);
  34. this.values_.splice(keyLocation, 1);
  35. }
  36. G_Assert(this, this.keys_.length == this.values_.length,
  37. "Different number of keys than values!");
  38. return keyFound;
  39. }
  40. G_ObjectSafeMap.prototype.find = function(key) {
  41. var keyLocation = this.indexOfKey_(key);
  42. return keyLocation == -1 ? undefined : this.values_[keyLocation];
  43. }
  44. G_ObjectSafeMap.prototype.replace = function(other) {
  45. this.keys_ = [];
  46. this.values_ = [];
  47. for (var i = 0; i < other.keys_.length; i++) {
  48. this.keys_.push(other.keys_[i]);
  49. this.values_.push(other.values_[i]);
  50. }
  51. G_Assert(this, this.keys_.length == this.values_.length,
  52. "Different number of keys than values!");
  53. }
  54. G_ObjectSafeMap.prototype.forEach = function(func) {
  55. if (typeof func != "function")
  56. throw new Error("argument to forEach is not a function, it's a(n) " +
  57. typeof func);
  58. for (var i = 0; i < this.keys_.length; i++)
  59. func(this.keys_[i], this.values_[i]);
  60. }
  61. G_ObjectSafeMap.prototype.size = function() {
  62. return this.keys_.length;
  63. }
  64. function TEST_G_ObjectSafeMap() {
  65. if (G_GDEBUG) {
  66. var z = "map UNITTEST";
  67. G_debugService.enableZone(z);
  68. G_Debug(z, "Starting");
  69. var m = new G_ObjectSafeMap();
  70. G_Assert(z, m.size() == 0, "Initial size not zero");
  71. var o1 = new Object;
  72. var v1 = "1";
  73. var o2 = new Object;
  74. var v2 = "1";
  75. G_Assert(z, m.find(o1) == undefined, "Found non-existent item");
  76. m.insert(o1, v1);
  77. m.insert(o2, v2);
  78. G_Assert(z, m.size() == 2, "Size not 2");
  79. G_Assert(z, m.find(o1) == "1", "Didn't find item 1");
  80. G_Assert(z, m.find(o2) == "1", "Didn't find item 1");
  81. m.insert(o1, "2");
  82. G_Assert(z, m.size() == 2, "Size not 2");
  83. G_Assert(z, m.find(o1) == "2", "Didn't find item 1");
  84. G_Assert(z, m.find(o2) == "1", "Didn't find item 1");
  85. m.erase(o1);
  86. G_Assert(z, m.size() == 1, "Size not 1");
  87. G_Assert(z, m.find(o1) == undefined, "Found item1");
  88. G_Assert(z, m.find(o2) == "1", "Didn't find item 2");
  89. m.erase(o1);
  90. G_Assert(z, m.size() == 1, "Size not 1");
  91. G_Assert(z, m.find(o1) == undefined, "Found item1");
  92. G_Assert(z, m.find(o2) == "1", "Didn't find item 2");
  93. m.erase(o2);
  94. G_Assert(z, m.size() == 0, "Size not 0");
  95. G_Assert(z, m.find(o2) == undefined, "Found item2");
  96. G_Debug(z, "PASSED");
  97. }
  98. }
  99.