forked from dracome/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
34472e3
commit c9152aa
Showing
7 changed files
with
370 additions
and
6 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
group10/904627477/src/com/coding/basic/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,135 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
* 用双向链表实现LRU算法 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class LRUPageFrame { | ||
|
||
private static class Node { | ||
|
||
Node prev; | ||
Node next; | ||
int pageNum; | ||
|
||
Node() { | ||
} | ||
} | ||
|
||
private int capacity; | ||
|
||
|
||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
|
||
public LRUPageFrame(int capacity) { | ||
|
||
this.capacity = capacity; | ||
|
||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @param key | ||
* @return | ||
*/ | ||
public void access(int pageNum) { | ||
Node node = findPageNumNode(pageNum); | ||
if(node==null){ | ||
if(size()>=capacity){ | ||
removeLast(); | ||
} | ||
push(pageNum); | ||
}else{ | ||
moveToFirst(node); | ||
} | ||
} | ||
|
||
public Node findPageNumNode(int pageNum) { | ||
Node node = first; | ||
while(node!=null){ | ||
if(node.pageNum==pageNum){ | ||
break; | ||
}else{ | ||
node = node.next; | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
public void moveToFirst(Node node) { | ||
if(first==null||node==null||first.pageNum==node.pageNum){ | ||
return; | ||
} | ||
if(node.pageNum == last.pageNum){ | ||
node.prev.next = null; | ||
last = node.prev; | ||
}else{ | ||
Node tPrev = node.prev; | ||
Node tNext = node.next; | ||
tPrev.next = tNext; | ||
tNext.prev = tPrev; | ||
} | ||
node.prev = null; | ||
node.next = first; | ||
first = node; | ||
} | ||
|
||
public void push(int pageNum) { | ||
if(size()==0){ | ||
first = new Node(); | ||
first.pageNum = pageNum; | ||
last = first; | ||
return ; | ||
} | ||
Node node; | ||
node = new Node(); | ||
node.pageNum = pageNum; | ||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
} | ||
|
||
public void removeLast() { | ||
if(last==null){ | ||
return ; | ||
} | ||
if(size()==1){ | ||
first = null; | ||
last = null; | ||
return ; | ||
} | ||
Node temp = last.prev; | ||
last.prev = null; | ||
last = temp; | ||
last.next = null; | ||
} | ||
|
||
public int size() { | ||
int size = 0; | ||
Node temp = first; | ||
while (temp!=null) { | ||
size++; | ||
temp = temp.next; | ||
} | ||
return size; | ||
} | ||
|
||
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(); | ||
} | ||
|
||
} |
6 changes: 0 additions & 6 deletions
6
group10/904627477/src/com/coding/download/FileDownloader.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
88 changes: 88 additions & 0 deletions
88
group10/904627477/src/com/coding/test/ClassFileloaderTest.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,88 @@ | ||
package com.coding.test; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.conding.jvm.loader.ClassFileLoader; | ||
|
||
public class ClassFileloaderTest { | ||
|
||
|
||
static String path1 = "D:\\workspace\\MyGithub\\coding2017\\group10\\904627477\\target\\classes"; | ||
static String path2 = "C:\temp"; | ||
|
||
|
||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testClassPath(){ | ||
|
||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
loader.addClassPath(path2); | ||
|
||
String clzPath = loader.getClassPath(); | ||
|
||
Assert.assertEquals(path1+";"+path2,clzPath); | ||
|
||
} | ||
|
||
@Test | ||
public void testClassFileLength() { | ||
|
||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
|
||
String className = "com.coding.test.EmployeeV1"; | ||
|
||
byte[] byteCodes = loader.readBinaryCode(className); | ||
|
||
// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 | ||
Assert.assertEquals(1040, byteCodes.length); | ||
|
||
} | ||
|
||
|
||
@Test | ||
public void testMagicNumber(){ | ||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
String className = "com.coding.test.EmployeeV1"; | ||
byte[] byteCodes = loader.readBinaryCode(className); | ||
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; | ||
|
||
|
||
String acctualValue = this.byteToHexString(codes); | ||
|
||
Assert.assertEquals("cafebabe", acctualValue); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
private String byteToHexString(byte[] codes ){ | ||
StringBuffer buffer = new StringBuffer(); | ||
for(int i=0;i<codes.length;i++){ | ||
byte b = codes[i]; | ||
int value = b & 0xFF; | ||
String strHex = Integer.toHexString(value); | ||
if(strHex.length()< 2){ | ||
strHex = "0" + strHex; | ||
} | ||
buffer.append(strHex); | ||
} | ||
return buffer.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,29 @@ | ||
package com.coding.test; | ||
@SuppressWarnings("unused") | ||
public class EmployeeV1 { | ||
|
||
|
||
|
||
private String name; | ||
private int age; | ||
|
||
public EmployeeV1(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
public void setAge(int age){ | ||
this.age = age; | ||
} | ||
public void sayHello() { | ||
System.out.println("Hello , this is class Employee "); | ||
} | ||
public static void main(String[] args){ | ||
EmployeeV1 p = new EmployeeV1("Andy",29); | ||
p.sayHello(); | ||
|
||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
group10/904627477/src/com/coding/test/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,32 @@ | ||
package com.coding.test; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.coding.basic.LRUPageFrame; | ||
|
||
|
||
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()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.