Skip to content

dependencies: update and clean (and auto-fix style issues) #1195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 121 additions & 121 deletions Cache/LFUCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,210 +45,210 @@ class FrequencyMap extends Map {
}

class LFUCache {
#capacity
#frequencyMap
#capacity
#frequencyMap

/**
/**
* @param {number} capacity - The range of LFUCache
* @returns {LFUCache} - sealed
*/
constructor (capacity) {
this.#capacity = capacity
this.#frequencyMap = new FrequencyMap()
this.misses = 0
this.hits = 0
this.cache = new Map()

return Object.seal(this)
}
constructor (capacity) {
this.#capacity = capacity
this.#frequencyMap = new FrequencyMap()
this.misses = 0
this.hits = 0
this.cache = new Map()

return Object.seal(this)
}

/**
/**
* Get the capacity of the LFUCache
* @returns {number}
*/
get capacity () {
return this.#capacity
}
get capacity () {
return this.#capacity
}

/**
/**
* Get the current size of LFUCache
* @returns {number}
*/
get size () {
return this.cache.size
}
get size () {
return this.cache.size
}

/**
/**
* Set the capacity of the LFUCache if you decrease the capacity its removed CacheNodes following the LFU - least frequency used
*/
set capacity (newCapacity) {
if (this.#capacity > newCapacity) {
let diff = this.#capacity - newCapacity // get the decrement number of capacity
set capacity (newCapacity) {
if (this.#capacity > newCapacity) {
let diff = this.#capacity - newCapacity // get the decrement number of capacity

while (diff--) {
this.#removeCacheNode()
}

this.cache.size === 0 && this.#frequencyMap.clear()
while (diff--) {
this.#removeCacheNode()
}

this.#capacity = newCapacity
this.cache.size === 0 && this.#frequencyMap.clear()
}

get info () {
return Object.freeze({
misses: this.misses,
hits: this.hits,
capacity: this.capacity,
currentSize: this.size,
leastFrequency: this.leastFrequency
})
}
this.#capacity = newCapacity
}

get leastFrequency () {
const freqCacheIterator = this.#frequencyMap.keys()
let leastFrequency = freqCacheIterator.next().value || null
get info () {
return Object.freeze({
misses: this.misses,
hits: this.hits,
capacity: this.capacity,
currentSize: this.size,
leastFrequency: this.leastFrequency
})
}

// select the non-empty frequency Set
while (this.#frequencyMap.get(leastFrequency)?.size === 0) {
leastFrequency = freqCacheIterator.next().value
}
get leastFrequency () {
const freqCacheIterator = this.#frequencyMap.keys()
let leastFrequency = freqCacheIterator.next().value || null

return leastFrequency
// select the non-empty frequency Set
while (this.#frequencyMap.get(leastFrequency)?.size === 0) {
leastFrequency = freqCacheIterator.next().value
}

#removeCacheNode () {
const leastFreqSet = this.#frequencyMap.get(this.leastFrequency)
// Select the least recently used node from the least Frequency set
const LFUNode = leastFreqSet.values().next().value
return leastFrequency
}

leastFreqSet.delete(LFUNode)
this.cache.delete(LFUNode.key)
}
#removeCacheNode () {
const leastFreqSet = this.#frequencyMap.get(this.leastFrequency)
// Select the least recently used node from the least Frequency set
const LFUNode = leastFreqSet.values().next().value

leastFreqSet.delete(LFUNode)
this.cache.delete(LFUNode.key)
}

/**
/**
* if key exist then return true otherwise false
* @param {any} key
* @returns {boolean}
*/
has (key) {
key = String(key) // converted to string
has (key) {
key = String(key) // converted to string

return this.cache.has(key)
}
return this.cache.has(key)
}

/**
/**
* @method get
* @description - This method return the value of key & refresh the frequencyMap by the oldNode
* @param {string} key
* @returns {any}
*/
get (key) {
key = String(key) // converted to string
get (key) {
key = String(key) // converted to string

if (this.cache.has(key)) {
const oldNode = this.cache.get(key)
this.#frequencyMap.refresh(oldNode)
if (this.cache.has(key)) {
const oldNode = this.cache.get(key)
this.#frequencyMap.refresh(oldNode)

this.hits++
this.hits++

return oldNode.value
}

this.misses++
return null
return oldNode.value
}

/**
this.misses++
return null
}

/**
* @method set
* @description - This method stored the value by key & add frequency if it doesn't exist
* @param {string} key
* @param {any} value
* @param {number} frequency
* @returns {LFUCache}
*/
set (key, value, frequency = 1) {
key = String(key) // converted to string
set (key, value, frequency = 1) {
key = String(key) // converted to string

if (this.#capacity === 0) {
throw new RangeError('LFUCache ERROR: The Capacity is 0')
}
if (this.#capacity === 0) {
throw new RangeError('LFUCache ERROR: The Capacity is 0')
}

if (this.cache.has(key)) {
const node = this.cache.get(key)
node.value = value
if (this.cache.has(key)) {
const node = this.cache.get(key)
node.value = value

this.#frequencyMap.refresh(node)
this.#frequencyMap.refresh(node)

return this
}
return this
}

// if the cache size is full, then it's delete the Least Frequency Used node
if (this.#capacity === this.cache.size) {
this.#removeCacheNode()
}
// if the cache size is full, then it's delete the Least Frequency Used node
if (this.#capacity === this.cache.size) {
this.#removeCacheNode()
}

const newNode = new CacheNode(key, value, frequency)
const newNode = new CacheNode(key, value, frequency)

this.cache.set(key, newNode)
this.#frequencyMap.insert(newNode)
this.cache.set(key, newNode)
this.#frequencyMap.insert(newNode)

return this
}
return this
}

/**
/**
* @method parse
* @description - This method receive a valid LFUCache JSON & run JSON.prase() method and merge with existing LFUCache
* @param {JSON} json
* @returns {LFUCache} - merged
*/
parse (json) {
const { misses, hits, cache } = JSON.parse(json)
parse (json) {
const { misses, hits, cache } = JSON.parse(json)

this.misses += misses ?? 0
this.hits += hits ?? 0
this.misses += misses ?? 0
this.hits += hits ?? 0

for (const key in cache) {
const { value, frequency } = cache[key]
this.set(key, value, frequency)
}

return this
for (const key in cache) {
const { value, frequency } = cache[key]
this.set(key, value, frequency)
}

/**
return this
}

/**
* @method clear
* @description - This method cleared the whole LFUCache
* @returns {LFUCache}
*/
clear () {
this.cache.clear()
this.#frequencyMap.clear()
clear () {
this.cache.clear()
this.#frequencyMap.clear()

return this
}
return this
}

/**
/**
* @method toString
* @description - This method generate a JSON format of LFUCache & return it.
* @param {number} indent
* @returns {string} - JSON
*/
toString (indent) {
const replacer = (_, value) => {
if (value instanceof Set) {
return [...value]
}

if (value instanceof Map) {
return Object.fromEntries(value)
}
toString (indent) {
const replacer = (_, value) => {
if (value instanceof Set) {
return [...value]
}

return value
if (value instanceof Map) {
return Object.fromEntries(value)
}

return JSON.stringify(this, replacer, indent)
return value
}

return JSON.stringify(this, replacer, indent)
}
}

export default LFUCache
7 changes: 6 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* [AllCombinationsOfSizeK](Backtracking/AllCombinationsOfSizeK.js)
* [GeneratePermutations](Backtracking/GeneratePermutations.js)
* [KnightTour](Backtracking/KnightTour.js)
* [NQueen](Backtracking/NQueen.js)
* [NQueens](Backtracking/NQueens.js)
* [RatInAMaze](Backtracking/RatInAMaze.js)
* [Sudoku](Backtracking/Sudoku.js)
* [SumOfSubset](Backtracking/SumOfSubset.js)
Expand Down Expand Up @@ -149,6 +149,7 @@
* [CollatzSequence](Maths/CollatzSequence.js)
* [Coordinate](Maths/Coordinate.js)
* [CoPrimeCheck](Maths/CoPrimeCheck.js)
* [CountNumbersDivisible](Maths/CountNumbersDivisible.js)
* [DecimalExpansion](Maths/DecimalExpansion.js)
* [DecimalIsolate](Maths/DecimalIsolate.js)
* [DegreeToRadian](Maths/DegreeToRadian.js)
Expand All @@ -172,6 +173,7 @@
* [IsDivisible](Maths/IsDivisible.js)
* [IsEven](Maths/IsEven.js)
* [IsOdd](Maths/IsOdd.js)
* [isPalindromeIntegerNumber](Maths/isPalindromeIntegerNumber.js)
* [IsPronic](Maths/IsPronic.js)
* [IsSquareFree](Maths/IsSquareFree.js)
* [JugglerSequence](Maths/JugglerSequence.js)
Expand Down Expand Up @@ -227,6 +229,7 @@
* [Problem008](Project-Euler/Problem008.js)
* [Problem009](Project-Euler/Problem009.js)
* [Problem010](Project-Euler/Problem010.js)
* [Problem011](Project-Euler/Problem011.js)
* [Problem012](Project-Euler/Problem012.js)
* [Problem013](Project-Euler/Problem013.js)
* [Problem014](Project-Euler/Problem014.js)
Expand All @@ -237,6 +240,7 @@
* [Problem020](Project-Euler/Problem020.js)
* [Problem023](Project-Euler/Problem023.js)
* [Problem025](Project-Euler/Problem025.js)
* [Problem044](Project-Euler/Problem044.js)
* **Recursive**
* [BinaryEquivalent](Recursive/BinaryEquivalent.js)
* [BinarySearch](Recursive/BinarySearch.js)
Expand Down Expand Up @@ -290,6 +294,7 @@
* [ShellSort](Sorts/ShellSort.js)
* [SimplifiedWiggleSort](Sorts/SimplifiedWiggleSort.js)
* [StoogeSort](Sorts/StoogeSort.js)
* [SwapSort](Sorts/SwapSort.js)
* [TimSort](Sorts/TimSort.js)
* [TopologicalSort](Sorts/TopologicalSort.js)
* **String**
Expand Down
2 changes: 1 addition & 1 deletion Maths/CollatzSequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export function collatz (n) {
steps.push(n)
}

return { result: n, steps: steps }
return { result: n, steps }
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy