JsHashMap.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /////////////////////////////////////////////
  2. ///// Js Object //////
  3. ///////////////////////////////////////////
  4. // Author: NHM TAnveer Hossain Khan (Hasan)
  5. // http://hasan.we4tech.com
  6. // mail:admin at we4tech.com
  7. // hashmap internal data object
  8. JsObject=function(key, value) {
  9. this._key=key;
  10. this._value=value;
  11. }
  12. // set some methods for JsObject
  13. JsObject.prototype.getKey=function() {
  14. return this._key;
  15. }
  16. // get value
  17. JsObject.prototype.getValue=function() {
  18. return this._value;
  19. }
  20. /////////////////////////////////////////////
  21. //// Iterator ///////
  22. ///////////////////////////////////////////
  23. JsIterator=function(array) {
  24. // set internal array
  25. this._array=array;
  26. // create inernal index counter
  27. this._counter=0;
  28. // set _hasNext value
  29. if(array.length>0)
  30. this._hasNext=true;
  31. else
  32. this._hasNext=false;
  33. }
  34. // return boolean value
  35. JsIterator.prototype.hasNext=function() {
  36. return this._hasNext;
  37. }
  38. // return object in next method
  39. JsIterator.prototype.next=function() {
  40. if(this._array.length>this._counter)
  41. {
  42. // get object
  43. var rtnObj=this._array[this._counter];
  44. // increment counter value;
  45. this._counter++;
  46. // check is has next true of flase
  47. if(this._array.length>this._counter)
  48. this._hasNext=true;
  49. else
  50. this._hasNext=false;
  51. // return data
  52. return rtnObj;
  53. }
  54. else
  55. {
  56. this._hasNext=false;
  57. }
  58. }
  59. // remove object
  60. JsIterator.prototype.remove=function() {
  61. this._array.splice(this._counter,1);
  62. if(this._array.length > this._counter)
  63. this._hasNext=false;
  64. }
  65. /////////////////////////////////////////////
  66. //// HashMap Object ///////
  67. ///////////////////////////////////////////
  68. // create JsHashMap class object
  69. JsHashMap=function() {
  70. // init. internal array
  71. this._array=new Array();
  72. // set internal counter value as 0
  73. // this counter will keep track the current index
  74. // of array
  75. this._counter=0;
  76. }
  77. // create add method
  78. // put key and value
  79. JsHashMap.prototype.put=function(key, value) {
  80. // add new value
  81. var newJsObj=new JsObject(key, value);
  82. // add in internal array
  83. this._array[this._counter]=newJsObj;
  84. // increment the internal index counter
  85. this._counter++;
  86. }
  87. // retrive data based on iterator
  88. JsHashMap.prototype.iterator=function() {
  89. // create iterator
  90. var it=new JsIterator(this._array);
  91. // return iterator
  92. return it;
  93. }
  94. // retrive data based on keyword
  95. JsHashMap.prototype.get=function(key) {
  96. // create iterator object
  97. var it=this.iterator();
  98. // iterate untile get success
  99. while(it.hasNext())
  100. {
  101. // fetch object
  102. var getObj=it.next();
  103. // check is found or not
  104. if(getObj.getKey()==key)
  105. return getObj.getValue();
  106. }
  107. }
  108. // remove key and object
  109. JsHashMap.prototype.remove=function(key) {
  110. // create iterator object
  111. var it=this.iterator();
  112. // iterate untile get success
  113. while(it.hasNext())
  114. {
  115. // fetch object
  116. var getObj=it.next();
  117. // check is found or not
  118. if(getObj.getKey()==key)
  119. it.remove();
  120. }
  121. }