@@ -4,61 +4,61 @@ package com.igorwojda.cache.lru
4
4
// Time Complexity: O(1)
5
5
private object Solution1 {
6
6
class LRUCache (private val capacity : Int ) {
7
- private val map = mutableMapOf<Int , Node >()
7
+ private val map = mutableMapOf<Int , CacheItem >()
8
8
9
- private var head: Node ? = null
10
- private var tail: Node ? = null
9
+ private var head: CacheItem ? = null
10
+ private var tail: CacheItem ? = null
11
11
12
12
val size get() = map.size
13
13
14
14
fun put (key : Int , value : String ) {
15
15
// Check if node exits
16
- val existingNode = map[key]
16
+ val existingCacheItem = map[key]
17
17
18
- if (existingNode == null ) {
18
+ if (existingCacheItem == null ) {
19
19
// Check Map capacity
20
20
if (map.size >= capacity) {
21
21
val removedNode = removeHead()
22
22
map.remove(removedNode?.key)
23
23
}
24
24
25
25
// Add a new node
26
- val newNode = Node (key, value)
26
+ val newCacheItem = CacheItem (key, value)
27
27
28
- map[key] = newNode
29
- addTail(newNode )
28
+ map[key] = newCacheItem
29
+ addTail(newCacheItem )
30
30
} else {
31
- existingNode .value = value
32
- moveToTail(existingNode )
31
+ existingCacheItem .value = value
32
+ moveToTail(existingCacheItem )
33
33
}
34
34
}
35
35
36
- private fun addTail (node : Node ) {
36
+ private fun addTail (cacheItem : CacheItem ) {
37
37
// If list is empty
38
38
if (head == null ) {
39
- head = node
39
+ head = cacheItem
40
40
} else {
41
- node .prev = tail
42
- tail?.next = node
41
+ cacheItem .prev = tail
42
+ tail?.next = cacheItem
43
43
}
44
44
45
- tail = node
45
+ tail = cacheItem
46
46
}
47
47
48
- private fun removeHead (): Node ? {
48
+ private fun removeHead (): CacheItem ? {
49
49
// Head exists
50
50
if (head != null ) {
51
51
// Store current head to return
52
- val node = head
52
+ val cacheItem = head
53
53
54
54
// Remove head
55
55
head = head?.next
56
56
head?.prev = null
57
57
58
58
// Remove tail if head is tail
59
- if (node == tail) tail = null
59
+ if (cacheItem == tail) tail = null
60
60
61
- return node
61
+ return cacheItem
62
62
}
63
63
64
64
return null
@@ -77,27 +77,27 @@ private object Solution1 {
77
77
return node?.value
78
78
}
79
79
80
- private fun moveToTail (node : Node ) {
80
+ private fun moveToTail (cacheItem : CacheItem ) {
81
81
// Check if node is tail
82
- if (node != tail) {
82
+ if (cacheItem != tail) {
83
83
// Remove node from list
84
- if (node == head) {
85
- head = node .next
84
+ if (cacheItem == head) {
85
+ head = cacheItem .next
86
86
} 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
89
89
}
90
90
91
91
// Add node to tail
92
- addTail(node )
92
+ addTail(cacheItem )
93
93
}
94
94
}
95
95
96
- private data class Node (
96
+ private data class CacheItem (
97
97
val key : Int ,
98
98
var value : String ,
99
- var prev : Node ? = null ,
100
- var next : Node ? = null ,
99
+ var prev : CacheItem ? = null ,
100
+ var next : CacheItem ? = null ,
101
101
)
102
102
}
103
103
}
0 commit comments