-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUcache.java
138 lines (130 loc) · 3.28 KB
/
LRUcache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.company;
import java.util.Arrays;
import java.util.*;
/**
* Created by Paul on 8/12/2015.
*/
public class Main {
public static void main(String[] args){
LRUcache<Integer> lru = new LRUcache<>();
Random rnd = new Random();
for(int i = 0; i < 1000; ++i){
lru.insert(rnd.nextInt(20));
if(Math.random() > 0.5){
lru.remove();
}
}
/*while(!lru.isEmpty()){
lru.remove();
}*/
for(int i : lru){
System.out.println(i);
}
System.out.println("------");
lru.insert(18);
for(int i : lru){
System.out.println(i);
}
}
}
class LRUcache<K> implements Iterable<K>{
private HashMap<K, Node> map;
@Override
public Iterator<K> iterator() {
return new LRUiter();
}
private class LRUiter implements Iterator<K>{
Node it = first;
@Override
public K next(){
K key = it.data;
it = it.next;
return key;
}
@Override
public boolean hasNext(){
return it != null;
}
}
private class Node{
Node prev, next;
K data;
public Node(K data){
this.data = data;
}
}
private Node first, last;
public LRUcache(){
map = new HashMap<>();
}
public void insert(K key){
if(!map.containsKey(key)) {
Node node = new Node(key);
if (isEmpty()) {
first = last = node;
} else {
node.next = first;
first.prev = node;
first = node;
}
map.put(key, node);
} else{
Node node = map.get(key);
if(node != first && node != last) {
Node prev = node.prev;
Node next = node.next;
if (prev != null) {
prev.next = next;
}
if (next != null) {
next.prev = prev;
}
node.next = first;
first.prev = node;
first = node;
} else if(node != first && node == last){
last = last.prev;
last.next = null;
node.prev = null;
node.next = first;
first.prev = node;
first= node;
}
}
}
public K remove(){
if(isEmpty()) return null;
K ret;
if(first == last){
Node node = last;
first = last = null;
K key = node.data;
map.remove(key);
ret = key;
} else{
Node node = last;
last = last.prev;
last.next = null;
node.prev = null;
K key = node.data;
map.remove(key);
ret = key;
}
return ret;
}
public boolean isEmpty(){
return map.isEmpty();
}
public int size(){
return map.size();
}
public int countSize(){
Node it = first;
int count = 0;
for(;it != null; it = it.next){
System.out.println("Key: " + it.data);
count++;
}
return count;
}
}