Comparing version 0.28.0 to 0.29.0
# Changelog | ||
## 0.29.0 | ||
* Adding `LRUCache.setpop` and `LRUMap.setpop` (@veggiesaurus). | ||
## 0.28.0 | ||
@@ -4,0 +8,0 @@ |
@@ -20,2 +20,3 @@ /** | ||
set(key: K, value: V): this; | ||
setpop(key: K, value: V): {evicted: boolean, key: K, value: V}; | ||
get(key: K): V | undefined; | ||
@@ -22,0 +23,0 @@ peek(key: K): V | undefined; |
@@ -143,2 +143,59 @@ /** | ||
/** | ||
* Method used to set the value for the given key in the cache | ||
* | ||
* @param {any} key - Key. | ||
* @param {any} value - Value. | ||
* @return {{evicted: boolean, key: any, value: any}} An object containing the | ||
* key and value of an item that was overwritten or evicted in the set | ||
* operation, as well as a boolean indicating whether it was evicted due to | ||
* limited capacity. Return value is null if nothing was evicted or overwritten | ||
* during the set operation. | ||
*/ | ||
LRUCache.prototype.setpop = function(key, value) { | ||
var oldValue = null; | ||
var oldKey = null; | ||
// The key already exists, we just need to update the value and splay on top | ||
var pointer = this.items[key]; | ||
if (typeof pointer !== 'undefined') { | ||
this.splayOnTop(pointer); | ||
oldValue = this.V[pointer]; | ||
this.V[pointer] = value; | ||
return {evicted: false, key: key, value: oldValue}; | ||
} | ||
// The cache is not yet full | ||
if (this.size < this.capacity) { | ||
pointer = this.size++; | ||
} | ||
// Cache is full, we need to drop the last value | ||
else { | ||
pointer = this.tail; | ||
this.tail = this.backward[pointer]; | ||
oldValue = this.V[pointer]; | ||
oldKey = this.K[pointer]; | ||
delete this.items[this.K[pointer]]; | ||
} | ||
// Storing key & value | ||
this.items[key] = pointer; | ||
this.K[pointer] = key; | ||
this.V[pointer] = value; | ||
// Moving the item at the front of the list | ||
this.forward[pointer] = this.head; | ||
this.backward[this.head] = pointer; | ||
this.head = pointer; | ||
// Return object if eviction took place, otherwise return null | ||
if (oldKey) { | ||
return {evicted: true, key: oldKey, value: oldValue}; | ||
} | ||
else { | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Method used to check whether the key exists in the cache. | ||
@@ -145,0 +202,0 @@ * |
@@ -20,2 +20,3 @@ /** | ||
set(key: K, value: V): this; | ||
setpop(key: K, value: V): {evicted: boolean, key: K, value: V}; | ||
get(key: K): V | undefined; | ||
@@ -22,0 +23,0 @@ peek(key: K): V | undefined; |
@@ -103,2 +103,59 @@ /** | ||
/** | ||
* Method used to set the value for the given key in the cache. | ||
* | ||
* @param {any} key - Key. | ||
* @param {any} value - Value. | ||
* @return {{evicted: boolean, key: any, value: any}} An object containing the | ||
* key and value of an item that was overwritten or evicted in the set | ||
* operation, as well as a boolean indicating whether it was evicted due to | ||
* limited capacity. Return value is null if nothing was evicted or overwritten | ||
* during the set operation. | ||
*/ | ||
LRUMap.prototype.setpop = function(key, value) { | ||
var oldValue = null; | ||
var oldKey = null; | ||
// The key already exists, we just need to update the value and splay on top | ||
var pointer = this.items.get(key); | ||
if (typeof pointer !== 'undefined') { | ||
this.splayOnTop(pointer); | ||
oldValue = this.V[pointer]; | ||
this.V[pointer] = value; | ||
return {evicted: false, key: key, value: oldValue}; | ||
} | ||
// The cache is not yet full | ||
if (this.size < this.capacity) { | ||
pointer = this.size++; | ||
} | ||
// Cache is full, we need to drop the last value | ||
else { | ||
pointer = this.tail; | ||
this.tail = this.backward[pointer]; | ||
oldValue = this.V[pointer]; | ||
oldKey = this.K[pointer]; | ||
this.items.delete(this.K[pointer]); | ||
} | ||
// Storing key & value | ||
this.items.set(key, pointer); | ||
this.K[pointer] = key; | ||
this.V[pointer] = value; | ||
// Moving the item at the front of the list | ||
this.forward[pointer] = this.head; | ||
this.backward[this.head] = pointer; | ||
this.head = pointer; | ||
// Return object if eviction took place, otherwise return null | ||
if (oldKey) { | ||
return {evicted: true, key: oldKey, value: oldValue}; | ||
} | ||
else { | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Method used to check whether the key exists in the cache. | ||
@@ -105,0 +162,0 @@ * |
{ | ||
"name": "mnemonist", | ||
"version": "0.28.0", | ||
"version": "0.29.0", | ||
"description": "Curated collection of data structures for the JavaScript language.", | ||
@@ -74,11 +74,11 @@ "scripts": { | ||
"asciitree": "^1.0.2", | ||
"damerau-levenshtein": "^1.0.3", | ||
"eslint": "^5.12.1", | ||
"leven": "^2.0.0", | ||
"damerau-levenshtein": "^1.0.5", | ||
"eslint": "^6.0.1", | ||
"leven": "^3.1.0", | ||
"lodash": "^4.17.11", | ||
"matcha": "^0.7.0", | ||
"mocha": "^5.2.0", | ||
"mocha": "^6.1.4", | ||
"pandemonium": "^1.2.1", | ||
"seedrandom": "^2.4.4", | ||
"typescript": "^3.2.4" | ||
"seedrandom": "^3.0.1", | ||
"typescript": "^3.5.2" | ||
}, | ||
@@ -85,0 +85,0 @@ "eslintConfig": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
316217
11396