-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
155 lines (130 loc) · 3.13 KB
/
Queue.java
File metadata and controls
155 lines (130 loc) · 3.13 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* This is a linked based queue of class.
*
* @author wenfeng ren
* @version 11/02/2014
*/
public class Queue
{
private Node front = new Node(null);
private Node rear = new Node(null);
private int size;
private int maxSize;
// ----------------------------------------------------------
/**
* Create a new Queue object.
*
* @param maxSize
* the size
*/
public Queue(int maxSize)
{
this.maxSize = maxSize;
front.setNext(rear);
rear.setPrev(front);
size = 0;
}
/**
* remove buffer from pool.
*
* @return the removed buffer
*/
public Buffer dequeue()
{
// pool is empty
if (size == 0)
{
return null;
}
// otherwise remove buffer, there are at least one buffer
Buffer remove = front.next().getData();
front.setNext(front.next().next());
front.next().setPrev(front);
size--;
return remove;
}
/**
* add buffer method by LRU
*
* @param buff
* the buffer
* @return the removed buffer if pool is full
*/
public Buffer enqueue(Buffer buff)
{
Buffer removed = null;
// pool is full
if (size == maxSize)
{
// remove the LRU buffer
removed = dequeue();
}
// enqueue the buffer
Node newbuff = new Node(buff);
newbuff.setPrev(rear.prev());
rear.prev().setNext(newbuff);
rear.setPrev(newbuff);
newbuff.setNext(rear);
// update size
size++;
return removed;
}
/**
* search for this block see if the block is in the pool. If it is, remove
* it from the queue and return this block.
*
* @param block
* the block
* @return true if found in pool, false if didn't find.
*/
public Boolean search(int block)
{
if (size == 0)
{
return false;
}
Node current = front.next();
int index = 1;
while (index < size)
{
if (current.getData().block() == block)
{
// promote
current.next().setPrev(current.prev());
current.prev().setNext(current.next());
current.setPrev(rear.prev());
rear.prev().setNext(current);
rear.setPrev(current);
current.setNext(rear);
return true;
}
current = current.next();
index++;
}
// already at the rear, do not need to promote
return index == size && current.getData().block() == block;
}
/**
* rear buffer
*
* @return the rear buffer
*/
public Buffer rearBuffer()
{
if (size == 0)
{
return null;
}
return rear.prev().getData();
}
// ----------------------------------------------------------
/**
* get size of pool.
*
* @return size
*/
public int getSize()
{
return maxSize;
}
}