1
+ // https://github.com/somdipdey/JavaScript-implementation-of-java.util.TreeMap-Class/blob/master/treeMap.js
2
+ module . exports = class TreeMap {
3
+ constructor ( ) {
4
+ this . dict = { }
5
+ }
6
+
7
+ /*
8
+ * <summary>
9
+ * Value get(Object key)
10
+ * This method returns the value to which the specified key is mapped,
11
+ * or null if this map contains no mapping for the key.
12
+ * </summary>
13
+ */
14
+ get ( key ) {
15
+ return this . dict [ key ] ;
16
+ } ;
17
+
18
+ /*
19
+ * <summary>
20
+ * boolean containsKey(Object key)
21
+ * This method returns true
22
+ * if this map contains a mapping for the specified key.
23
+ * </summary>
24
+ */
25
+ containsKey ( key ) {
26
+ if ( this . get ( key ) !== undefined ) {
27
+ return true ;
28
+ } else {
29
+ return false ;
30
+ }
31
+ } ;
32
+
33
+ /*
34
+ * <summary>
35
+ * Value put(K key, V value)
36
+ * This method associates the specified value with the specified key in this map.
37
+ * </summary>
38
+ */
39
+ put ( key , value ) {
40
+ this . dict [ key ] = value ;
41
+ if ( isNumber ( key ) )
42
+ {
43
+ if ( allKeysAreNumeral ( this . dict ) ) {
44
+ this . dict = sortOnKeys ( this . dict ) ;
45
+ }
46
+ }
47
+ } ;
48
+
49
+ /*
50
+ * <summary>
51
+ * Value remove(Object key)
52
+ * This method removes the mapping for this key from this TreeMap if present.
53
+ * </summary>
54
+ */
55
+ remove ( key ) {
56
+ 'use strict' ;
57
+ delete this . dict [ key ] ;
58
+ } ;
59
+
60
+ /*
61
+ * <summary>
62
+ * void clear()
63
+ * This method removes all of the mappings from this map.
64
+ * </summary>
65
+ */
66
+ clear ( ) {
67
+ this . dict = { } ;
68
+ } ;
69
+
70
+
71
+ /*
72
+ * <summary>
73
+ * treeMap.foreach(V value)
74
+ * This method returns each value for each keys in the TreeMap.
75
+ * </summary>
76
+ */
77
+ forEach ( callback ) {
78
+ var len = this . size ( ) ;
79
+ for ( i = 0 ; i < len ; i ++ ) {
80
+ var item = this . get ( Object . keys ( this . dict ) [ i ] ) ;
81
+ callback ( item ) ;
82
+ }
83
+ }
84
+
85
+
86
+ /*
87
+ * <summary>
88
+ * int size()
89
+ * This method returns the number of key-value mappings in this map.
90
+ * </summary>
91
+ */
92
+ size ( ) {
93
+ return Object . keys ( this . dict ) . length ;
94
+ } ;
95
+
96
+ /*
97
+ * <summary>
98
+ * boolean isEmpty()
99
+ * This method returns a boolean
100
+ * determining whether the TreeMap is empty or not.
101
+ * </summary>
102
+ */
103
+ isEmpty ( ) {
104
+ return Object . keys ( this . dict ) . length == 0 ;
105
+ } ;
106
+
107
+
108
+ /*
109
+ * <summary>
110
+ * Key floorKey(K key)
111
+ * This method returns the greatest key less than or equal
112
+ * to the given key, or null if there is no such key.
113
+ * </summary>
114
+ */
115
+ floorKey ( key ) {
116
+ if ( ! isNumber ( key ) )
117
+ throw "Invalid Operation: key has to be an integer value" ;
118
+
119
+ if ( this . containsKey ( key ) )
120
+ return this . get ( key ) ;
121
+
122
+ return this . floorKey ( key - 1 ) ;
123
+ }
124
+
125
+
126
+ /*
127
+ * <summary>
128
+ * Key ceilingKey(K key)
129
+ * This method returns the least key greater than or equal
130
+ * to the given key, or null if there is no such key.
131
+ * </summary>
132
+ */
133
+ ceilingKey ( key ) {
134
+ if ( ! isNumber ( key ) )
135
+ throw "Invalid Operation: key has to be an integer value" ;
136
+
137
+ if ( this . containsKey ( key ) )
138
+ return this . get ( key ) ;
139
+
140
+ return this . floorKey ( key + 1 ) ;
141
+ }
142
+
143
+
144
+ /*
145
+ * <summary>
146
+ * Object clone()
147
+ * This method returns a shallow copy of this TreeMap instance.
148
+ * </summary>
149
+ */
150
+ clone ( ) {
151
+ return this . dict ;
152
+ }
153
+
154
+
155
+ /*
156
+ * <summary>
157
+ * boolean containsValue(Object value)
158
+ * This method returns true if this map maps one or more keys to the specified value.
159
+ * </summary>
160
+ */
161
+ containsValue ( value ) {
162
+ var len = this . size ( ) ;
163
+ for ( i = 0 ; i < len ; i ++ ) {
164
+ var item = this . get ( Object . keys ( this . dict ) [ i ] ) ;
165
+ if ( value === item )
166
+ return true ;
167
+ }
168
+
169
+ return false ;
170
+ }
171
+
172
+
173
+ /*
174
+ * <summary>
175
+ * Set<K> keySet()
176
+ * This method returns a Set view of the keys contained in this map.
177
+ * </summary>
178
+ */
179
+ keySet ( ) {
180
+ var set = [ ] ;
181
+ var len = this . size ( ) ;
182
+ for ( i = 0 ; i < len ; i ++ ) {
183
+ set . push ( Object . keys ( this . dict ) [ i ] ) ;
184
+ }
185
+
186
+ return set ;
187
+ }
188
+
189
+
190
+ /*
191
+ * <summary>
192
+ * Key firstKey()
193
+ * This method returns the first (lowest) key currently in this map.
194
+ * </summary>
195
+ */
196
+ firstKey ( ) {
197
+ return Object . keys ( this . dict ) [ 0 ] ;
198
+ }
199
+
200
+
201
+ /*
202
+ * <summary>
203
+ * Key lastKey()
204
+ * This method returns the last (highest) key currently in this map.
205
+ * </summary>
206
+ */
207
+ lastKey ( ) {
208
+ var len = this . size ( ) ;
209
+ return Object . keys ( this . dict ) [ len - 1 ] ;
210
+ }
211
+
212
+ }
213
+
214
+ // some more functions which might be required to complete the operations of TreeMap -->
215
+
216
+ // Checks if the input is a number or not
217
+ function isNumber ( input ) {
218
+ return ! isNaN ( input ) ;
219
+ }
220
+
221
+ // Sorts a JavaScript dictionary by key
222
+ function sortOnKeys ( dict ) {
223
+
224
+ var sorted = [ ] ;
225
+ for ( var key in dict ) {
226
+ sorted [ sorted . length ] = key ;
227
+ }
228
+ sorted . sort ( ) ;
229
+
230
+ var tempDict = { } ;
231
+ for ( var i = 0 ; i < sorted . length ; i ++ ) {
232
+ tempDict [ sorted [ i ] ] = dict [ sorted [ i ] ] ;
233
+ }
234
+
235
+ return tempDict ;
236
+ }
237
+
238
+ // Checks if all the keys in the JavaScript dictionary are numeral.
239
+ // If Yes, then it returns true or else it returns false
240
+ function allKeysAreNumeral ( dict ) {
241
+ for ( var key in dict ) {
242
+ if ( ! isNumber ( key ) )
243
+ return false ;
244
+ }
245
+
246
+ return true ;
247
+ }
0 commit comments