Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
411 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
LRU Cache build using hashmap and doubly-linked-list | ||
capacity = n | ||
Operations | ||
put(key, value) O(1) | ||
get(key) O(1) | ||
*/ | ||
|
||
class Node { | ||
constructor (key, value) { | ||
this.key = key | ||
this.value = value | ||
this.prev = null | ||
this.next = null | ||
} | ||
} | ||
|
||
class LRUCacheHDLL { | ||
constructor (capacity) { | ||
this.capacity = capacity | ||
this.head = null | ||
this.tail = null | ||
this.size = 0 | ||
this.map = new Map() | ||
} | ||
|
||
put (key, value) { | ||
let node | ||
if (this.map.has(key)) { | ||
node = this.map.get(key) | ||
node.value = value | ||
this._moveToTail(node) | ||
} else { | ||
node = new Node(key, value) | ||
this.map.set(key, node) | ||
|
||
if (this.size + 1 > this.capacity) { | ||
this._deleteNodeAtTail() | ||
} else { | ||
this.size++ | ||
} | ||
|
||
this._insertNodeAtTail(node) | ||
} | ||
} | ||
|
||
get (key) { | ||
if (this.map.has(key)) { | ||
const node = this.map.get(key) | ||
this._moveToTail(node) | ||
return node.value | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
_insertNodeAtTail (node) { | ||
if (this.head === null) { | ||
this.head = node | ||
this.tail = node | ||
} else { | ||
this.tail.next = node | ||
node.next = null | ||
node.prev = this.tail | ||
this.tail = node | ||
} | ||
} | ||
|
||
_moveToTail (node) { | ||
if (this.tail !== node) { | ||
if (node === this.head) { | ||
this.head = this.head.next | ||
this.head.prev = null | ||
} else { | ||
const prev = node.prev | ||
const next = node.next | ||
|
||
prev.next = next | ||
next.prev = prev | ||
} | ||
|
||
this.tail.next = node | ||
node.next = null | ||
node.prev = this.tail | ||
this.tail = node | ||
} | ||
} | ||
|
||
_deleteNodeAtTail () { | ||
const temp = this.head | ||
this.head = this.head.next | ||
if (this.head) { this.head.prev = null } | ||
|
||
temp.next = null | ||
temp.prev = null | ||
this.map.delete(temp.key) | ||
} | ||
} | ||
|
||
module.exports = LRUCacheHDLL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const LRUCache = require('./LRUCacheHDLL') | ||
|
||
test('get existing key', () => { | ||
const lru = new LRUCache(1) | ||
lru.put('key', 'value') | ||
expect(lru.get('key')).toBe('value') | ||
}) | ||
|
||
test('get non-existing key', () => { | ||
const lru = new LRUCache(1) | ||
expect(lru.get('key')).toBe(-1) | ||
}) | ||
|
||
test('put value of existing key', () => { | ||
const lru = new LRUCache(1) | ||
lru.put('key', 'value') | ||
lru.put('key', 'value2') | ||
|
||
expect(lru.get('key')).toBe('value2') | ||
}) | ||
|
||
test('put value of non existing key', () => { | ||
const lru = new LRUCache(1) | ||
lru.put('key', 'value') | ||
lru.put('key2', 'value2') | ||
|
||
expect(lru.get('key')).toBe(-1) | ||
expect(lru.get('key2')).toBe('value2') | ||
}) | ||
|
||
test('all cases', () => { | ||
const lru = new LRUCache(2) | ||
lru.put(1, 1) | ||
lru.put(2, 2) | ||
expect(lru.get(1)).toBe(1) | ||
lru.put(3, 3) | ||
expect(lru.get(2)).toBe(-1) | ||
lru.put(4, 4) | ||
expect(lru.get(1)).toBe(-1) | ||
expect(lru.get(3)).toBe(3) | ||
expect(lru.get(4)).toBe(4) | ||
}) |
Oops, something went wrong.