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