@@ -45,210 +45,210 @@ class FrequencyMap extends Map {
4545}
4646
4747class LFUCache {
48- #capacity
49- #frequencyMap
48+ #capacity
49+ #frequencyMap
5050
51- /**
51+ /**
5252 * @param {number } capacity - The range of LFUCache
5353 * @returns {LFUCache } - sealed
5454 */
55- constructor ( capacity ) {
56- this . #capacity = capacity
57- this . #frequencyMap = new FrequencyMap ( )
58- this . misses = 0
59- this . hits = 0
60- this . cache = new Map ( )
61-
62- return Object . seal ( this )
63- }
55+ constructor ( capacity ) {
56+ this . #capacity = capacity
57+ this . #frequencyMap = new FrequencyMap ( )
58+ this . misses = 0
59+ this . hits = 0
60+ this . cache = new Map ( )
61+
62+ return Object . seal ( this )
63+ }
6464
65- /**
65+ /**
6666 * Get the capacity of the LFUCache
6767 * @returns {number }
6868 */
69- get capacity ( ) {
70- return this . #capacity
71- }
69+ get capacity ( ) {
70+ return this . #capacity
71+ }
7272
73- /**
73+ /**
7474 * Get the current size of LFUCache
7575 * @returns {number }
7676 */
77- get size ( ) {
78- return this . cache . size
79- }
77+ get size ( ) {
78+ return this . cache . size
79+ }
8080
81- /**
81+ /**
8282 * Set the capacity of the LFUCache if you decrease the capacity its removed CacheNodes following the LFU - least frequency used
8383 */
84- set capacity ( newCapacity ) {
85- if ( this . #capacity > newCapacity ) {
86- let diff = this . #capacity - newCapacity // get the decrement number of capacity
84+ set capacity ( newCapacity ) {
85+ if ( this . #capacity > newCapacity ) {
86+ let diff = this . #capacity - newCapacity // get the decrement number of capacity
8787
88- while ( diff -- ) {
89- this . #removeCacheNode( )
90- }
91-
92- this . cache . size === 0 && this . #frequencyMap. clear ( )
88+ while ( diff -- ) {
89+ this . #removeCacheNode( )
9390 }
9491
95- this . #capacity = newCapacity
92+ this . cache . size === 0 && this . #frequencyMap . clear ( )
9693 }
9794
98- get info ( ) {
99- return Object . freeze ( {
100- misses : this . misses ,
101- hits : this . hits ,
102- capacity : this . capacity ,
103- currentSize : this . size ,
104- leastFrequency : this . leastFrequency
105- } )
106- }
95+ this . #capacity = newCapacity
96+ }
10797
108- get leastFrequency ( ) {
109- const freqCacheIterator = this . #frequencyMap. keys ( )
110- let leastFrequency = freqCacheIterator . next ( ) . value || null
98+ get info ( ) {
99+ return Object . freeze ( {
100+ misses : this . misses ,
101+ hits : this . hits ,
102+ capacity : this . capacity ,
103+ currentSize : this . size ,
104+ leastFrequency : this . leastFrequency
105+ } )
106+ }
111107
112- // select the non-empty frequency Set
113- while ( this . #frequencyMap. get ( leastFrequency ) ?. size === 0 ) {
114- leastFrequency = freqCacheIterator . next ( ) . value
115- }
108+ get leastFrequency ( ) {
109+ const freqCacheIterator = this . #frequencyMap. keys ( )
110+ let leastFrequency = freqCacheIterator . next ( ) . value || null
116111
117- return leastFrequency
112+ // select the non-empty frequency Set
113+ while ( this . #frequencyMap. get ( leastFrequency ) ?. size === 0 ) {
114+ leastFrequency = freqCacheIterator . next ( ) . value
118115 }
119116
120- #removeCacheNode ( ) {
121- const leastFreqSet = this . #frequencyMap. get ( this . leastFrequency )
122- // Select the least recently used node from the least Frequency set
123- const LFUNode = leastFreqSet . values ( ) . next ( ) . value
117+ return leastFrequency
118+ }
124119
125- leastFreqSet . delete ( LFUNode )
126- this . cache . delete ( LFUNode . key )
127- }
120+ #removeCacheNode ( ) {
121+ const leastFreqSet = this . #frequencyMap. get ( this . leastFrequency )
122+ // Select the least recently used node from the least Frequency set
123+ const LFUNode = leastFreqSet . values ( ) . next ( ) . value
124+
125+ leastFreqSet . delete ( LFUNode )
126+ this . cache . delete ( LFUNode . key )
127+ }
128128
129- /**
129+ /**
130130 * if key exist then return true otherwise false
131131 * @param {any } key
132132 * @returns {boolean }
133133 */
134- has ( key ) {
135- key = String ( key ) // converted to string
134+ has ( key ) {
135+ key = String ( key ) // converted to string
136136
137- return this . cache . has ( key )
138- }
137+ return this . cache . has ( key )
138+ }
139139
140- /**
140+ /**
141141 * @method get
142142 * @description - This method return the value of key & refresh the frequencyMap by the oldNode
143143 * @param {string } key
144144 * @returns {any }
145145 */
146- get ( key ) {
147- key = String ( key ) // converted to string
146+ get ( key ) {
147+ key = String ( key ) // converted to string
148148
149- if ( this . cache . has ( key ) ) {
150- const oldNode = this . cache . get ( key )
151- this . #frequencyMap. refresh ( oldNode )
149+ if ( this . cache . has ( key ) ) {
150+ const oldNode = this . cache . get ( key )
151+ this . #frequencyMap. refresh ( oldNode )
152152
153- this . hits ++
153+ this . hits ++
154154
155- return oldNode . value
156- }
157-
158- this . misses ++
159- return null
155+ return oldNode . value
160156 }
161157
162- /**
158+ this . misses ++
159+ return null
160+ }
161+
162+ /**
163163 * @method set
164164 * @description - This method stored the value by key & add frequency if it doesn't exist
165165 * @param {string } key
166166 * @param {any } value
167167 * @param {number } frequency
168168 * @returns {LFUCache }
169169 */
170- set ( key , value , frequency = 1 ) {
171- key = String ( key ) // converted to string
170+ set ( key , value , frequency = 1 ) {
171+ key = String ( key ) // converted to string
172172
173- if ( this . #capacity === 0 ) {
174- throw new RangeError ( 'LFUCache ERROR: The Capacity is 0' )
175- }
173+ if ( this . #capacity === 0 ) {
174+ throw new RangeError ( 'LFUCache ERROR: The Capacity is 0' )
175+ }
176176
177- if ( this . cache . has ( key ) ) {
178- const node = this . cache . get ( key )
179- node . value = value
177+ if ( this . cache . has ( key ) ) {
178+ const node = this . cache . get ( key )
179+ node . value = value
180180
181- this . #frequencyMap. refresh ( node )
181+ this . #frequencyMap. refresh ( node )
182182
183- return this
184- }
183+ return this
184+ }
185185
186- // if the cache size is full, then it's delete the Least Frequency Used node
187- if ( this . #capacity === this . cache . size ) {
188- this . #removeCacheNode( )
189- }
186+ // if the cache size is full, then it's delete the Least Frequency Used node
187+ if ( this . #capacity === this . cache . size ) {
188+ this . #removeCacheNode( )
189+ }
190190
191- const newNode = new CacheNode ( key , value , frequency )
191+ const newNode = new CacheNode ( key , value , frequency )
192192
193- this . cache . set ( key , newNode )
194- this . #frequencyMap. insert ( newNode )
193+ this . cache . set ( key , newNode )
194+ this . #frequencyMap. insert ( newNode )
195195
196- return this
197- }
196+ return this
197+ }
198198
199- /**
199+ /**
200200 * @method parse
201201 * @description - This method receive a valid LFUCache JSON & run JSON.prase() method and merge with existing LFUCache
202202 * @param {JSON } json
203203 * @returns {LFUCache } - merged
204204 */
205- parse ( json ) {
206- const { misses, hits, cache } = JSON . parse ( json )
205+ parse ( json ) {
206+ const { misses, hits, cache } = JSON . parse ( json )
207207
208- this . misses += misses ?? 0
209- this . hits += hits ?? 0
208+ this . misses += misses ?? 0
209+ this . hits += hits ?? 0
210210
211- for ( const key in cache ) {
212- const { value, frequency } = cache [ key ]
213- this . set ( key , value , frequency )
214- }
215-
216- return this
211+ for ( const key in cache ) {
212+ const { value, frequency } = cache [ key ]
213+ this . set ( key , value , frequency )
217214 }
218215
219- /**
216+ return this
217+ }
218+
219+ /**
220220 * @method clear
221221 * @description - This method cleared the whole LFUCache
222222 * @returns {LFUCache }
223223 */
224- clear ( ) {
225- this . cache . clear ( )
226- this . #frequencyMap. clear ( )
224+ clear ( ) {
225+ this . cache . clear ( )
226+ this . #frequencyMap. clear ( )
227227
228- return this
229- }
228+ return this
229+ }
230230
231- /**
231+ /**
232232 * @method toString
233233 * @description - This method generate a JSON format of LFUCache & return it.
234234 * @param {number } indent
235235 * @returns {string } - JSON
236236 */
237- toString ( indent ) {
238- const replacer = ( _ , value ) => {
239- if ( value instanceof Set ) {
240- return [ ...value ]
241- }
242-
243- if ( value instanceof Map ) {
244- return Object . fromEntries ( value )
245- }
237+ toString ( indent ) {
238+ const replacer = ( _ , value ) => {
239+ if ( value instanceof Set ) {
240+ return [ ...value ]
241+ }
246242
247- return value
243+ if ( value instanceof Map ) {
244+ return Object . fromEntries ( value )
248245 }
249246
250- return JSON . stringify ( this , replacer , indent )
247+ return value
251248 }
249+
250+ return JSON . stringify ( this , replacer , indent )
251+ }
252252}
253253
254254export default LFUCache
0 commit comments