hashtable.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*******************************************************************************************
  2. * Object: Hashtable
  3. * Description: Implementation of hashtable
  4. * Author: Uzi Refaeli
  5. *******************************************************************************************/
  6. //======================================= Properties ========================================
  7. Hashtable.prototype.hash = null;
  8. Hashtable.prototype.keys = null;
  9. Hashtable.prototype.location = null;
  10. /**
  11. * Hashtable - Constructor
  12. * Create a new Hashtable object.
  13. */
  14. function Hashtable(){
  15. this.hash = new Array();
  16. this.keys = new Array();
  17. this.location = 0;
  18. }
  19. /**
  20. * put
  21. * Add new key
  22. * param: key - String, key name
  23. * param: value - Object, the object to insert
  24. */
  25. Hashtable.prototype.put = function (key, value){
  26. if (value == null)
  27. return;
  28. if (this.hash[key] == null)
  29. this.keys[this.keys.length] = key;
  30. this.hash[key] = value;
  31. }
  32. /**
  33. * get
  34. * Return an element
  35. * param: key - String, key name
  36. * Return: object - The requested object
  37. */
  38. Hashtable.prototype.get = function (key){
  39. return this.hash[key];
  40. }
  41. /**
  42. * remove
  43. * Remove an element
  44. * param: key - String, key name
  45. */
  46. Hashtable.prototype.remove = function (key){
  47. for (var i = 0; i < this.keys.length; i++){
  48. //did we found our key?
  49. if (key == this.keys[i]){
  50. //remove it from the hash
  51. this.hash[this.keys[i]] = null;
  52. //and throw away the key...
  53. this.keys.splice(i ,1);
  54. return;
  55. }
  56. }
  57. }
  58. /**
  59. * size
  60. * Return: Number of elements in the hashtable
  61. */
  62. Hashtable.prototype.size = function (){
  63. return this.keys.length;
  64. }
  65. /**
  66. * populateItems
  67. * Deprecated
  68. */
  69. Hashtable.prototype.populateItems = function (){}
  70. /**
  71. * next
  72. * Return: true if theres more items
  73. */
  74. Hashtable.prototype.next = function (){
  75. if (++this.location < this.keys.length)
  76. return true;
  77. else
  78. return false;
  79. }
  80. /**
  81. * moveFirst
  82. * Move to the first item.
  83. */
  84. Hashtable.prototype.moveFirst = function (){
  85. try {
  86. this.location = -1;
  87. } catch(e) {/*//do nothing here :-)*/}
  88. }
  89. /**
  90. * moveLast
  91. * Move to the last item.
  92. */
  93. Hashtable.prototype.moveLast = function (){
  94. try {
  95. this.location = this.keys.length - 1;
  96. } catch(e) {/*//do nothing here :-)*/}
  97. }
  98. /**
  99. * getKey
  100. * Return: The value of item in the hash
  101. */
  102. Hashtable.prototype.getKey = function (){
  103. try {
  104. return this.keys[this.location];
  105. } catch(e) {
  106. return null;
  107. }
  108. }
  109. /**
  110. * getValue
  111. * Return: The value of item in the hash
  112. */
  113. Hashtable.prototype.getValue = function (){
  114. try {
  115. return this.hash[this.keys[this.location]];
  116. } catch(e) {
  117. return null;
  118. }
  119. }
  120. /**
  121. * getKey
  122. * Return: The first key contains the given value, or null if not found
  123. */
  124. Hashtable.prototype.getKeyOfValue = function (value){
  125. for (var i = 0; i < this.keys.length; i++)
  126. if (this.hash[this.keys[i]] == value)
  127. return this.keys[i]
  128. return null;
  129. }
  130. /**
  131. * toString
  132. * Returns a string representation of this Hashtable object in the form of a set of entries,
  133. * enclosed in braces and separated by the ASCII characters ", " (comma and space).
  134. * Each entry is rendered as the key, an equals sign =, and the associated element,
  135. * where the toString method is used to convert the key and element to strings.
  136. * Return: a string representation of this hashtable.
  137. */
  138. Hashtable.prototype.toString = function (){
  139. try {
  140. var s = new Array(this.keys.length);
  141. s[s.length] = "{";
  142. for (var i = 0; i < this.keys.length; i++){
  143. s[s.length] = this.keys[i];
  144. s[s.length] = "=";
  145. var v = this.hash[this.keys[i]];
  146. if (v)
  147. s[s.length] = v.toString();
  148. else
  149. s[s.length] = "null";
  150. if (i != this.keys.length-1)
  151. s[s.length] = ", ";
  152. }
  153. } catch(e) {
  154. //do nothing here :-)
  155. }finally{
  156. s[s.length] = "}";
  157. }
  158. return s.join("");
  159. }
  160. /**
  161. * add
  162. * Concatanates hashtable to another hashtable.
  163. */
  164. Hashtable.prototype.add = function(ht){
  165. try {
  166. ht.moveFirst();
  167. while(ht.next()){
  168. var key = ht.getKey();
  169. //put the new value in both cases (exists or not).
  170. this.hash[key] = ht.getValue();
  171. //but if it is a new key also increase the key set
  172. if (this.get(key) != null){
  173. this.keys[this.keys.length] = key;
  174. }
  175. }
  176. } catch(e) {
  177. //do nothing here :-)
  178. } finally {
  179. return this;
  180. }
  181. };