Skip to content

Commit a9c482e

Browse files
committed
Rename
1 parent 15d53ff commit a9c482e

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/test/kotlin/com/igorwojda/cache/lru/Solution.kt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,61 @@ package com.igorwojda.cache.lru
44
// Time Complexity: O(1)
55
private object Solution1 {
66
class LRUCache(private val capacity: Int) {
7-
private val map = mutableMapOf<Int, Node>()
7+
private val map = mutableMapOf<Int, CacheItem>()
88

9-
private var head: Node? = null
10-
private var tail: Node? = null
9+
private var head: CacheItem? = null
10+
private var tail: CacheItem? = null
1111

1212
val size get() = map.size
1313

1414
fun put(key: Int, value: String) {
1515
// Check if node exits
16-
val existingNode = map[key]
16+
val existingCacheItem = map[key]
1717

18-
if (existingNode == null) {
18+
if (existingCacheItem == null) {
1919
// Check Map capacity
2020
if (map.size >= capacity) {
2121
val removedNode = removeHead()
2222
map.remove(removedNode?.key)
2323
}
2424

2525
// Add a new node
26-
val newNode = Node(key, value)
26+
val newCacheItem = CacheItem(key, value)
2727

28-
map[key] = newNode
29-
addTail(newNode)
28+
map[key] = newCacheItem
29+
addTail(newCacheItem)
3030
} else {
31-
existingNode.value = value
32-
moveToTail(existingNode)
31+
existingCacheItem.value = value
32+
moveToTail(existingCacheItem)
3333
}
3434
}
3535

36-
private fun addTail(node: Node) {
36+
private fun addTail(cacheItem: CacheItem) {
3737
// If list is empty
3838
if (head == null) {
39-
head = node
39+
head = cacheItem
4040
} else {
41-
node.prev = tail
42-
tail?.next = node
41+
cacheItem.prev = tail
42+
tail?.next = cacheItem
4343
}
4444

45-
tail = node
45+
tail = cacheItem
4646
}
4747

48-
private fun removeHead(): Node? {
48+
private fun removeHead(): CacheItem? {
4949
// Head exists
5050
if (head != null) {
5151
// Store current head to return
52-
val node = head
52+
val cacheItem = head
5353

5454
// Remove head
5555
head = head?.next
5656
head?.prev = null
5757

5858
// Remove tail if head is tail
59-
if (node == tail) tail = null
59+
if (cacheItem == tail) tail = null
6060

61-
return node
61+
return cacheItem
6262
}
6363

6464
return null
@@ -77,27 +77,27 @@ private object Solution1 {
7777
return node?.value
7878
}
7979

80-
private fun moveToTail(node: Node) {
80+
private fun moveToTail(cacheItem: CacheItem) {
8181
// Check if node is tail
82-
if (node != tail) {
82+
if (cacheItem != tail) {
8383
// Remove node from list
84-
if (node == head) {
85-
head = node.next
84+
if (cacheItem == head) {
85+
head = cacheItem.next
8686
} else {
87-
node.prev?.next = node.next
88-
node.next?.prev = node.prev
87+
cacheItem.prev?.next = cacheItem.next
88+
cacheItem.next?.prev = cacheItem.prev
8989
}
9090

9191
// Add node to tail
92-
addTail(node)
92+
addTail(cacheItem)
9393
}
9494
}
9595

96-
private data class Node(
96+
private data class CacheItem(
9797
val key: Int,
9898
var value: String,
99-
var prev: Node? = null,
100-
var next: Node? = null,
99+
var prev: CacheItem? = null,
100+
var next: CacheItem? = null,
101101
)
102102
}
103103
}

0 commit comments

Comments
 (0)