-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from JaneZhou91/master
the fourth homework
- Loading branch information
Showing
4 changed files
with
422 additions
and
0 deletions.
There are no files selected for viewing
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,142 @@ | ||
package LRU; | ||
|
||
/** | ||
* 使用双向链表实现LRU算法 | ||
* @author 12946 | ||
* | ||
*/ | ||
|
||
public class LRUPageFrame { | ||
|
||
private static class Node { | ||
|
||
Node prev = null; | ||
Node next = null; | ||
int pageNum; | ||
|
||
Node(Object data) { | ||
if(data != null){ | ||
this.pageNum = (Integer)data; | ||
} | ||
} | ||
} | ||
|
||
private int capacity;//链表总元素个数 | ||
private int currentSize;//链表当前元素个数 | ||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
|
||
public LRUPageFrame(int capacity) { | ||
|
||
first = new Node(null);//栈底,元素最先放入的地方 | ||
last = new Node(null);//栈顶 | ||
|
||
this.currentSize = 0; | ||
this.capacity = capacity; | ||
|
||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @param key | ||
* @return | ||
*/ | ||
public void access(int pageNum) { | ||
Node newNode = new Node(pageNum); | ||
|
||
if(currentSize == capacity){ | ||
Node tempNode = null; | ||
if(getIndex(pageNum) == -1){//不包含 | ||
tempNode = first.next; | ||
}else{//包含 | ||
tempNode = getNode(pageNum); | ||
} | ||
(tempNode.prev).next = tempNode.next; | ||
(tempNode.next).prev = tempNode.prev; | ||
|
||
|
||
newNode.next = last; | ||
newNode.prev = last.prev; | ||
last.prev = newNode; | ||
newNode.prev.next = newNode; | ||
} | ||
|
||
if(currentSize < capacity){ | ||
Node point = first; | ||
for(int i = 0; i < currentSize; i++){ | ||
point = point.next; | ||
} | ||
point.next = newNode; | ||
newNode.prev = point; | ||
|
||
newNode.next = last; | ||
last.prev = newNode; | ||
|
||
currentSize += 1; | ||
} | ||
|
||
|
||
} | ||
|
||
public Node getNode(int data){ | ||
|
||
Node point = first.next; | ||
for(int i = 0; i < capacity; i++){ | ||
if(point.pageNum == data){ | ||
return point; | ||
} | ||
point = point.next; | ||
} | ||
|
||
return null; | ||
|
||
} | ||
|
||
|
||
public int getIndex(int data){ | ||
Node point = first.next; | ||
for(int i = 0; i < capacity; i++){ | ||
if(point.pageNum == data){ | ||
return i; | ||
} | ||
point = point.next; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
// 原toString | ||
// public String toString(){ | ||
// StringBuilder buffer = new StringBuilder(); | ||
// Node node = first; | ||
// while(node != null){ | ||
// buffer.append(node.pageNum); | ||
// | ||
// node = node.next; | ||
// if(node != null){ | ||
// buffer.append(","); | ||
// } | ||
// } | ||
// return buffer.toString(); | ||
// } | ||
|
||
public String toString(){ | ||
StringBuilder sb = new StringBuilder(); | ||
Node point = last.prev; | ||
while(point != first){ | ||
sb.append(point.pageNum); | ||
if(point.prev != first){ | ||
sb.append(","); | ||
} | ||
point = point.prev; | ||
} | ||
|
||
return sb.toString(); | ||
|
||
} | ||
|
||
|
||
|
||
} |
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,140 @@ | ||
package jvm.loader; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
|
||
import jvm.clz.ClassFile; | ||
|
||
|
||
|
||
public class ClassFileLoader { | ||
|
||
private List<String> clzPaths = new ArrayList<String>(); | ||
|
||
public byte[] readBinaryCode(String className) { | ||
|
||
className = className.replace('.', File.separatorChar) + ".class"; | ||
|
||
for(String path : clzPaths){ | ||
String clzFileName = path + File.separatorChar + className; | ||
byte[] codes = loadClassFile(clzFileName); | ||
if(codes != null){ | ||
return codes; | ||
} | ||
} | ||
|
||
return null; | ||
|
||
} | ||
|
||
private byte[] loadClassFile(String clzFileName) { | ||
|
||
try { | ||
InputStream input = new FileInputStream(new File(clzFileName)); | ||
return IOUtils.toByteArray(input); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
|
||
public void addClassPath(String path) { | ||
if(this.clzPaths.contains(path)){ | ||
return; | ||
} | ||
|
||
this.clzPaths.add(path); | ||
|
||
} | ||
|
||
|
||
|
||
public String getClassPath(){ | ||
return StringUtils.join(this.clzPaths,";"); | ||
} | ||
|
||
public ClassFile loadClass(String className) { | ||
// byte[] codes = this.readBinaryCode(className); | ||
// ClassFileParser parser = new ClassFileParser(); | ||
// return parser.parse(codes); | ||
return null; | ||
|
||
} | ||
|
||
|
||
|
||
// ------------------------------backup------------------------ | ||
public String getClassPath_V1(){ | ||
|
||
StringBuffer buffer = new StringBuffer(); | ||
for(int i=0;i<this.clzPaths.size();i++){ | ||
buffer.append(this.clzPaths.get(i)); | ||
if(i<this.clzPaths.size()-1){ | ||
buffer.append(";"); | ||
} | ||
} | ||
return buffer.toString(); | ||
} | ||
|
||
private byte[] loadClassFile_V1(String clzFileName) { | ||
|
||
BufferedInputStream bis = null; | ||
|
||
try { | ||
|
||
File f = new File(clzFileName); | ||
|
||
|
||
bis = new BufferedInputStream(new FileInputStream(f)); | ||
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
|
||
|
||
byte[] buffer = new byte[1024]; | ||
int length = -1; | ||
|
||
while((length = bis.read(buffer)) != -1){ | ||
bos.write(buffer, 0, length); | ||
} | ||
|
||
byte [] codes = bos.toByteArray(); | ||
|
||
return codes; | ||
|
||
} catch(IOException e){ | ||
e.printStackTrace(); | ||
|
||
} finally{ | ||
if(bis != null){ | ||
try { | ||
bis.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
return null; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
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,34 @@ | ||
package LRU; | ||
|
||
import org.junit.Assert; | ||
|
||
import org.junit.Test; | ||
|
||
|
||
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()); | ||
frame.access(5); | ||
Assert.assertEquals("5,4,0", frame.toString()); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.