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

* Update npm dependencies (and auto-fix style issues)

* Updated Documentation in README.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Roland Hummel
2022-10-16 12:39:56 +02:00
committed by GitHub
parent 18baef8e7e
commit 4ce3dbe6b4
5 changed files with 4278 additions and 9274 deletions

View File

@ -45,121 +45,121 @@ class FrequencyMap extends Map {
} }
class LFUCache { class LFUCache {
#capacity #capacity
#frequencyMap #frequencyMap
/** /**
* @param {number} capacity - The range of LFUCache * @param {number} capacity - The range of LFUCache
* @returns {LFUCache} - sealed * @returns {LFUCache} - sealed
*/ */
constructor (capacity) { constructor (capacity) {
this.#capacity = capacity this.#capacity = capacity
this.#frequencyMap = new FrequencyMap() this.#frequencyMap = new FrequencyMap()
this.misses = 0 this.misses = 0
this.hits = 0 this.hits = 0
this.cache = new Map() this.cache = new Map()
return Object.seal(this) return Object.seal(this)
} }
/** /**
* Get the capacity of the LFUCache * Get the capacity of the LFUCache
* @returns {number} * @returns {number}
*/ */
get capacity () { get capacity () {
return this.#capacity return this.#capacity
} }
/** /**
* Get the current size of LFUCache * Get the current size of LFUCache
* @returns {number} * @returns {number}
*/ */
get size () { get size () {
return this.cache.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 the capacity of the LFUCache if you decrease the capacity its removed CacheNodes following the LFU - least frequency used
*/ */
set capacity (newCapacity) { set capacity (newCapacity) {
if (this.#capacity > newCapacity) { if (this.#capacity > newCapacity) {
let diff = this.#capacity - newCapacity // get the decrement number of capacity let diff = this.#capacity - newCapacity // get the decrement number of capacity
while (diff--) { while (diff--) {
this.#removeCacheNode() this.#removeCacheNode()
}
this.cache.size === 0 && this.#frequencyMap.clear()
} }
this.#capacity = newCapacity this.cache.size === 0 && this.#frequencyMap.clear()
} }
get info () { this.#capacity = newCapacity
return Object.freeze({ }
misses: this.misses,
hits: this.hits, get info () {
capacity: this.capacity, return Object.freeze({
currentSize: this.size, misses: this.misses,
leastFrequency: this.leastFrequency hits: this.hits,
}) capacity: this.capacity,
currentSize: this.size,
leastFrequency: this.leastFrequency
})
}
get leastFrequency () {
const freqCacheIterator = this.#frequencyMap.keys()
let leastFrequency = freqCacheIterator.next().value || null
// select the non-empty frequency Set
while (this.#frequencyMap.get(leastFrequency)?.size === 0) {
leastFrequency = freqCacheIterator.next().value
} }
get leastFrequency () { return leastFrequency
const freqCacheIterator = this.#frequencyMap.keys() }
let leastFrequency = freqCacheIterator.next().value || null
// select the non-empty frequency Set #removeCacheNode () {
while (this.#frequencyMap.get(leastFrequency)?.size === 0) { const leastFreqSet = this.#frequencyMap.get(this.leastFrequency)
leastFrequency = freqCacheIterator.next().value // 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 * if key exist then return true otherwise false
* @param {any} key * @param {any} key
* @returns {boolean} * @returns {boolean}
*/ */
has (key) { has (key) {
key = String(key) // converted to string key = String(key) // converted to string
return this.cache.has(key) return this.cache.has(key)
} }
/** /**
* @method get * @method get
* @description - This method return the value of key & refresh the frequencyMap by the oldNode * @description - This method return the value of key & refresh the frequencyMap by the oldNode
* @param {string} key * @param {string} key
* @returns {any} * @returns {any}
*/ */
get (key) { get (key) {
key = String(key) // converted to string key = String(key) // converted to string
if (this.cache.has(key)) { if (this.cache.has(key)) {
const oldNode = this.cache.get(key) const oldNode = this.cache.get(key)
this.#frequencyMap.refresh(oldNode) this.#frequencyMap.refresh(oldNode)
this.hits++ this.hits++
return oldNode.value return oldNode.value
}
this.misses++
return null
} }
/** this.misses++
return null
}
/**
* @method set * @method set
* @description - This method stored the value by key & add frequency if it doesn't exist * @description - This method stored the value by key & add frequency if it doesn't exist
* @param {string} key * @param {string} key
@ -167,88 +167,88 @@ class LFUCache {
* @param {number} frequency * @param {number} frequency
* @returns {LFUCache} * @returns {LFUCache}
*/ */
set (key, value, frequency = 1) { set (key, value, frequency = 1) {
key = String(key) // converted to string key = String(key) // converted to string
if (this.#capacity === 0) { if (this.#capacity === 0) {
throw new RangeError('LFUCache ERROR: The Capacity is 0') throw new RangeError('LFUCache ERROR: The Capacity is 0')
} }
if (this.cache.has(key)) { if (this.cache.has(key)) {
const node = this.cache.get(key) const node = this.cache.get(key)
node.value = value node.value = value
this.#frequencyMap.refresh(node) this.#frequencyMap.refresh(node)
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()
}
const newNode = new CacheNode(key, value, frequency)
this.cache.set(key, newNode)
this.#frequencyMap.insert(newNode)
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()
}
const newNode = new CacheNode(key, value, frequency)
this.cache.set(key, newNode)
this.#frequencyMap.insert(newNode)
return this
}
/**
* @method parse * @method parse
* @description - This method receive a valid LFUCache JSON & run JSON.prase() method and merge with existing LFUCache * @description - This method receive a valid LFUCache JSON & run JSON.prase() method and merge with existing LFUCache
* @param {JSON} json * @param {JSON} json
* @returns {LFUCache} - merged * @returns {LFUCache} - merged
*/ */
parse (json) { parse (json) {
const { misses, hits, cache } = JSON.parse(json) const { misses, hits, cache } = JSON.parse(json)
this.misses += misses ?? 0 this.misses += misses ?? 0
this.hits += hits ?? 0 this.hits += hits ?? 0
for (const key in cache) { for (const key in cache) {
const { value, frequency } = cache[key] const { value, frequency } = cache[key]
this.set(key, value, frequency) this.set(key, value, frequency)
}
return this
} }
/** return this
}
/**
* @method clear * @method clear
* @description - This method cleared the whole LFUCache * @description - This method cleared the whole LFUCache
* @returns {LFUCache} * @returns {LFUCache}
*/ */
clear () { clear () {
this.cache.clear() this.cache.clear()
this.#frequencyMap.clear() this.#frequencyMap.clear()
return this return this
} }
/** /**
* @method toString * @method toString
* @description - This method generate a JSON format of LFUCache & return it. * @description - This method generate a JSON format of LFUCache & return it.
* @param {number} indent * @param {number} indent
* @returns {string} - JSON * @returns {string} - JSON
*/ */
toString (indent) { toString (indent) {
const replacer = (_, value) => { const replacer = (_, value) => {
if (value instanceof Set) { if (value instanceof Set) {
return [...value] return [...value]
}
if (value instanceof Map) {
return Object.fromEntries(value)
}
return value
} }
return JSON.stringify(this, replacer, indent) if (value instanceof Map) {
return Object.fromEntries(value)
}
return value
} }
return JSON.stringify(this, replacer, indent)
}
} }
export default LFUCache export default LFUCache

View File

@ -2,7 +2,7 @@
* [AllCombinationsOfSizeK](Backtracking/AllCombinationsOfSizeK.js) * [AllCombinationsOfSizeK](Backtracking/AllCombinationsOfSizeK.js)
* [GeneratePermutations](Backtracking/GeneratePermutations.js) * [GeneratePermutations](Backtracking/GeneratePermutations.js)
* [KnightTour](Backtracking/KnightTour.js) * [KnightTour](Backtracking/KnightTour.js)
* [NQueen](Backtracking/NQueen.js) * [NQueens](Backtracking/NQueens.js)
* [RatInAMaze](Backtracking/RatInAMaze.js) * [RatInAMaze](Backtracking/RatInAMaze.js)
* [Sudoku](Backtracking/Sudoku.js) * [Sudoku](Backtracking/Sudoku.js)
* [SumOfSubset](Backtracking/SumOfSubset.js) * [SumOfSubset](Backtracking/SumOfSubset.js)
@ -149,6 +149,7 @@
* [CollatzSequence](Maths/CollatzSequence.js) * [CollatzSequence](Maths/CollatzSequence.js)
* [Coordinate](Maths/Coordinate.js) * [Coordinate](Maths/Coordinate.js)
* [CoPrimeCheck](Maths/CoPrimeCheck.js) * [CoPrimeCheck](Maths/CoPrimeCheck.js)
* [CountNumbersDivisible](Maths/CountNumbersDivisible.js)
* [DecimalExpansion](Maths/DecimalExpansion.js) * [DecimalExpansion](Maths/DecimalExpansion.js)
* [DecimalIsolate](Maths/DecimalIsolate.js) * [DecimalIsolate](Maths/DecimalIsolate.js)
* [DegreeToRadian](Maths/DegreeToRadian.js) * [DegreeToRadian](Maths/DegreeToRadian.js)
@ -172,6 +173,7 @@
* [IsDivisible](Maths/IsDivisible.js) * [IsDivisible](Maths/IsDivisible.js)
* [IsEven](Maths/IsEven.js) * [IsEven](Maths/IsEven.js)
* [IsOdd](Maths/IsOdd.js) * [IsOdd](Maths/IsOdd.js)
* [isPalindromeIntegerNumber](Maths/isPalindromeIntegerNumber.js)
* [IsPronic](Maths/IsPronic.js) * [IsPronic](Maths/IsPronic.js)
* [IsSquareFree](Maths/IsSquareFree.js) * [IsSquareFree](Maths/IsSquareFree.js)
* [JugglerSequence](Maths/JugglerSequence.js) * [JugglerSequence](Maths/JugglerSequence.js)
@ -227,6 +229,7 @@
* [Problem008](Project-Euler/Problem008.js) * [Problem008](Project-Euler/Problem008.js)
* [Problem009](Project-Euler/Problem009.js) * [Problem009](Project-Euler/Problem009.js)
* [Problem010](Project-Euler/Problem010.js) * [Problem010](Project-Euler/Problem010.js)
* [Problem011](Project-Euler/Problem011.js)
* [Problem012](Project-Euler/Problem012.js) * [Problem012](Project-Euler/Problem012.js)
* [Problem013](Project-Euler/Problem013.js) * [Problem013](Project-Euler/Problem013.js)
* [Problem014](Project-Euler/Problem014.js) * [Problem014](Project-Euler/Problem014.js)
@ -237,6 +240,7 @@
* [Problem020](Project-Euler/Problem020.js) * [Problem020](Project-Euler/Problem020.js)
* [Problem023](Project-Euler/Problem023.js) * [Problem023](Project-Euler/Problem023.js)
* [Problem025](Project-Euler/Problem025.js) * [Problem025](Project-Euler/Problem025.js)
* [Problem044](Project-Euler/Problem044.js)
* **Recursive** * **Recursive**
* [BinaryEquivalent](Recursive/BinaryEquivalent.js) * [BinaryEquivalent](Recursive/BinaryEquivalent.js)
* [BinarySearch](Recursive/BinarySearch.js) * [BinarySearch](Recursive/BinarySearch.js)
@ -290,6 +294,7 @@
* [ShellSort](Sorts/ShellSort.js) * [ShellSort](Sorts/ShellSort.js)
* [SimplifiedWiggleSort](Sorts/SimplifiedWiggleSort.js) * [SimplifiedWiggleSort](Sorts/SimplifiedWiggleSort.js)
* [StoogeSort](Sorts/StoogeSort.js) * [StoogeSort](Sorts/StoogeSort.js)
* [SwapSort](Sorts/SwapSort.js)
* [TimSort](Sorts/TimSort.js) * [TimSort](Sorts/TimSort.js)
* [TopologicalSort](Sorts/TopologicalSort.js) * [TopologicalSort](Sorts/TopologicalSort.js)
* **String** * **String**

View File

@ -26,5 +26,5 @@ export function collatz (n) {
steps.push(n) steps.push(n)
} }
return { result: n, steps: steps } return { result: n, steps }
} }

13273
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,18 +11,18 @@
"author": "TheAlgorithms", "author": "TheAlgorithms",
"license": "GPL-3.0", "license": "GPL-3.0",
"dependencies": { "dependencies": {
"@babel/core": "^7.11.6", "@babel/core": "^7.19.3",
"@babel/plugin-transform-runtime": "^7.11.5", "@babel/plugin-transform-runtime": "^7.19.1",
"@babel/preset-env": "^7.11.5" "@babel/preset-env": "^7.19.4"
}, },
"devDependencies": { "devDependencies": {
"@babel/eslint-parser": "^7.17.0", "@babel/eslint-parser": "^7.19.1",
"@types/jest": "^27.4.1", "@types/jest": "^29.1.2",
"babel-jest": "^26.3.0", "babel-jest": "^29.2.0",
"globby": "^12.0.2", "globby": "^13.1.2",
"husky": "^7.0.4", "husky": "^8.0.1",
"jest": "^26.6.3", "jest": "^29.2.0",
"standard": "^16.0.4" "standard": "^17.0.0"
}, },
"engines": { "engines": {
"node": ">=16.6.0" "node": ">=16.6.0"