/tags/jsdoc_toolkit-2.3.2/jsdoc-toolkit/app/frame/Hash.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 84 lines · 60 code · 11 blank · 13 comment · 18 complexity · 611fac9de45ba885e04052402b352072 MD5 · raw file

  1. /**
  2. @constructor
  3. @example
  4. var _index = new Hash();
  5. _index.set("a", "apple");
  6. _index.set("b", "blue");
  7. _index.set("c", "coffee");
  8. for (var p = _index.first(); p; p = _index.next()) {
  9. print(p.key+" is for "+p.value);
  10. }
  11. */
  12. var Hash = function() {
  13. this._map = {};
  14. this._keys = [];
  15. this._vals = [];
  16. this.reset();
  17. }
  18. Hash.prototype.set = function(k, v) {
  19. if (k != "") {
  20. this._keys.push(k);
  21. this._map["="+k] = this._vals.length;
  22. this._vals.push(v);
  23. }
  24. }
  25. Hash.prototype.replace = function(k, k2, v) {
  26. if (k == k2) return;
  27. var offset = this._map["="+k];
  28. this._keys[offset] = k2;
  29. if (typeof v != "undefined") this._vals[offset] = v;
  30. this._map["="+k2] = offset;
  31. delete(this._map["="+k]);
  32. }
  33. Hash.prototype.drop = function(k) {
  34. if (k != "") {
  35. var offset = this._map["="+k];
  36. this._keys.splice(offset, 1);
  37. this._vals.splice(offset, 1);
  38. delete(this._map["="+k]);
  39. for (var p in this._map) {
  40. if (this._map[p] >= offset) this._map[p]--;
  41. }
  42. if (this._cursor >= offset && this._cursor > 0) this._cursor--;
  43. }
  44. }
  45. Hash.prototype.get = function(k) {
  46. if (k != "") {
  47. return this._vals[this._map["="+k]];
  48. }
  49. }
  50. Hash.prototype.keys = function() {
  51. return this._keys;
  52. }
  53. Hash.prototype.hasKey = function(k) {
  54. if (k != "") {
  55. return (typeof this._map["="+k] != "undefined");
  56. }
  57. }
  58. Hash.prototype.values = function() {
  59. return this._vals;
  60. }
  61. Hash.prototype.reset = function() {
  62. this._cursor = 0;
  63. }
  64. Hash.prototype.first = function() {
  65. this.reset();
  66. return this.next();
  67. }
  68. Hash.prototype.next = function() {
  69. if (this._cursor++ < this._keys.length)
  70. return {key: this._keys[this._cursor-1], value: this._vals[this._cursor-1]};
  71. }