hashtable.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. Hashtable = function() {
  2. this._hash = new Object();
  3. this.onPropertyChanged = function(added, updated, deleted) { }
  4. this.add = function(key, value) {
  5. if (typeof (key) != "undefined") {
  6. if (this.contains(key) == false) {
  7. this._hash[key] = typeof (value) == "undefined" ? null : value;
  8. this.onPropertyChanged([{ 'key': key, 'value': value}], [], []);
  9. return true;
  10. } else {
  11. this.setvalue(key,value);
  12. }
  13. } else {
  14. return false;
  15. }
  16. }
  17. this.remove = function(key) {
  18. var deleted = [{ 'key': key, 'value': this._hash[key]}];
  19. delete this._hash[key];
  20. this.onPropertyChanged([], [], deleted);
  21. }
  22. this.count = function() { var i = 0; for (var k in this._hash) { i++; } return i; }
  23. this.items = function(key) { return this._hash[key]; }
  24. this.contains = function(key) { return typeof (this._hash[key]) != "undefined"; }
  25. this.clear = function(lazy) {
  26. var deleted = [];
  27. if (lazy == undefined || !lazy)
  28. deleted = this.allitems();
  29. for (var k in this._hash) {
  30. delete this._hash[k];
  31. }
  32. if (lazy == undefined || !lazy)
  33. this.onPropertyChanged([], [], deleted);
  34. }
  35. this.values = function() {
  36. var list = new Array();
  37. for (var k in this._hash)
  38. list[list.length] = this._hash[k];
  39. return list;
  40. }
  41. this.keys = function() {
  42. var list = new Array();
  43. for (var k in this._hash)
  44. list[list.length] = k;
  45. return list;
  46. }
  47. this.setvalue = function(key, value) { this._hash[key] = value; this.onPropertyChanged(this); }
  48. this.allitems = function() {
  49. var list = new Array();
  50. for (var k in this._hash) {
  51. list[list.length] = { key: k, value: this._hash[k] };
  52. }
  53. return list;
  54. }
  55. }