forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 8
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
6 changed files
with
614 additions
and
0 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
group02/562768642/src/com/github/orajavac/coding2017/basic/linklist/LRUPageFrame.java
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,133 @@ | ||
package com.github.orajavac.coding2017.basic.linklist; | ||
|
||
public class LRUPageFrame { | ||
private static class Node { | ||
|
||
Node prev; | ||
Node next; | ||
int pageNum=10000; | ||
String flag; | ||
|
||
Node() { | ||
} | ||
} | ||
|
||
private int capacity; | ||
private int length; | ||
|
||
|
||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
|
||
public LRUPageFrame(int capacity) { | ||
|
||
this.capacity = capacity; | ||
|
||
first = new Node(); | ||
last = new Node(); | ||
last.flag = "last"; //用来标识最后一个节点是链表尾,在这里链表尾并不算做内存页 | ||
first.flag = "first"; //用来标识链表头,在这里链表头并不算做内存页 | ||
first.next = last; | ||
last.prev=first; | ||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @param key | ||
* @return | ||
*/ | ||
public void access(int pageNum) { | ||
if(lookup(pageNum)){ | ||
; | ||
}else{ | ||
if (length<capacity){ | ||
addFirst(pageNum); | ||
}else{ | ||
remove(pageNum); //删除尾元素 | ||
addFirst(pageNum); | ||
} | ||
} | ||
} | ||
|
||
public boolean lookup(int pageNum){ | ||
Node nodef = first; | ||
Node nodel = last; | ||
|
||
//判断要找的元素是否在第一个节点里 | ||
if (nodef.next.pageNum == pageNum){ | ||
return true; | ||
} | ||
|
||
//判断要找的元素是否在最后一个节点里 | ||
if (nodel.prev.pageNum == pageNum){ | ||
remove(pageNum); //删除尾元素 | ||
addFirst(pageNum); //把访问pageNum添加到第一个节点里 | ||
return true; | ||
} | ||
|
||
while(nodef.next!=null&&nodef.next.pageNum != 999){ | ||
if (nodef.next.pageNum == pageNum){ | ||
nodef.next = nodef.next.next; //先删除元素 | ||
nodef.next.prev = nodef; | ||
addFirst(pageNum); //再把访问添加元素 | ||
return true; | ||
} | ||
nodef = nodef.next; | ||
} | ||
return false; | ||
} | ||
|
||
public void remove(int pageNum){ | ||
length--; | ||
Node n = last.prev; | ||
last.prev = n.prev; | ||
n.prev.next = last; | ||
} | ||
|
||
public void addFirst(int pageNum){ | ||
Node s = first.next; | ||
Node n = new Node(); | ||
n.pageNum=pageNum; | ||
n.next=s; | ||
s.prev=n; | ||
first.next=n; | ||
length++; | ||
} | ||
|
||
public String toString(){ | ||
StringBuilder buffer = new StringBuilder(); | ||
Node node = first; | ||
while(node.next.flag != "last"){ | ||
buffer.append(node.next.pageNum); | ||
|
||
node = node.next; | ||
|
||
if(node.next.flag != "last"){ | ||
buffer.append(","); | ||
} | ||
|
||
} | ||
return buffer.toString(); | ||
} | ||
|
||
/** | ||
* 测试双向链表逆序输出 | ||
* @return | ||
*/ | ||
public String lastToString(){ | ||
StringBuilder buffer = new StringBuilder(); | ||
Node node = last; | ||
while(node.prev!=null&&node.prev.flag != "first"){ | ||
buffer.append(node.prev.pageNum); | ||
|
||
node = node.prev; | ||
|
||
if(node.prev!=null&&node.prev.flag != "first"){ | ||
buffer.append(","); | ||
} | ||
} | ||
return buffer.toString(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
group02/562768642/src/com/github/orajavac/coding2017/basic/linklist/LRUPageFrameTest.java
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,29 @@ | ||
package com.github.orajavac.coding2017.basic.linklist; | ||
|
||
import org.junit.Test; | ||
|
||
import junit.framework.Assert; | ||
|
||
public class LRUPageFrameTest { | ||
|
||
@Test | ||
public void testAccess() { | ||
LRUPageFrame frame = new LRUPageFrame(3); | ||
frame.access(7); | ||
frame.access(0); | ||
frame.access(1); | ||
Assert.assertEquals("1,0,7", frame.toString()); | ||
frame.access(2); | ||
Assert.assertEquals("2,1,0", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,2,1", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,2,1", frame.toString()); | ||
frame.access(3); | ||
Assert.assertEquals("3,0,2", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,3,2", frame.toString()); | ||
frame.access(4); | ||
Assert.assertEquals("4,0,3", frame.toString()); | ||
} | ||
} |
Oops, something went wrong.