Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.coding.basic.linklist;

/**
* 用双向链表实现LRU算法
* @author liuxin
*
*/
public class LRUPageFrame {

private static class Node {

Node prev;
Node next;
int pageNum;

Node() {
}
}

private int capacity;
private int currentSize;

private Node first;// 链表头
private Node last;// 链表尾


public LRUPageFrame(int capacity) {

this.capacity = capacity;
currentSize = 0;
}

/**
* 获取缓存中对象
*
* @param key
* @return
*/
public void access(int pageNum) {

// 判断缓存是否命中
Node n = find(pageNum);
if(n != null){
// 命中,则把节点提到表头
moveNodeToHead(n);
}else{
// 若未命中,新增节点放到表头,如果链表长度超过capacity,移除链表尾部节点
n = new Node();
n.pageNum = pageNum;

if(currentSize >= capacity){
removeLast();
}
addNodeToHead(n);
}
}



private void addNodeToHead(Node n) {
if(first == null){
first = n;
last = n;
}else{
n.next = first;
first.prev = n;
first = n;
}
currentSize++;
}

private void removeLast() {
Node prevN = last.prev;
prevN.next = null;
last.prev = null;
last = prevN;
currentSize--;
}

private void moveNodeToHead(Node n) {
if(n == first){
return ;
}else if(n == last){
Node preN = n.prev;
preN.next = null;
last.prev = null;
last = preN;
}else{
// 中间节点
Node preN = n.prev;
Node nextN = n.next;
preN.next = nextN;
nextN.prev = preN;
}
n.prev = null;
n.next = first;
first.prev = n;
first = n;
}

private Node find(int pageNum) {
Node n = first;
while(n != null){
if(n.pageNum == pageNum){
return n;
}
n = n.next;
}
return null;
}

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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.coding.basic.linklist;

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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.coding.basic.linklist;

import com.coding.basic.Iterator;
import com.coding.basic.List;

public class LinkedList implements List {

private Node head;

public void add(Object o){

}
public void add(int index , Object o){

}
public Object get(int index){
return null;
}
public Object remove(int index){
return null;
}

public int size(){
return -1;
}

public void addFirst(Object o){

}
public void addLast(Object o){

}
public Object removeFirst(){
return null;
}
public Object removeLast(){
return null;
}
public Iterator iterator(){
return null;
}


private static class Node{
Object data;
Node next;

}

/**
* 把该链表逆置
* 例如链表为 3->7->10 , 逆置后变为 10->7->3
*/
public void reverse(){

}

/**
* 删除一个单链表的前半部分
* 例如:list = 2->5->7->8 , 删除以后的值为 7->8
* 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10

*/
public void removeFirstHalf(){

}

/**
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始
* @param i
* @param length
*/
public void remove(int i, int length){

}
/**
* 假定当前链表和listB均包含已升序排列的整数
* 从当前链表中取出那些listB所指定的元素
* 例如当前链表 = 11->101->201->301->401->501->601->701
* listB = 1->3->4->6
* 返回的结果应该是[101,301,401,601]
* @param list
*/
public int[] getElements(LinkedList list){
return null;
}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 从当前链表中中删除在listB中出现的元素

* @param list
*/

public void subtract(LinkedList list){

}

/**
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
*/
public void removeDuplicateValues(){

}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素)
* @param min
* @param max
*/
public void removeRange(int min, int max){

}

/**
* 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同)
* 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列
* @param list
*/
public LinkedList intersection( LinkedList list){
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.coderising.jvm.loader;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



public class ClassFileLoader {

private List<String> clzPaths = new ArrayList<String>();

public byte[] readBinaryCode(String className) {
if(null == className || "".equals(className)){
return null;
}
className = className.replace(".", File.separator)+".class";
Iterator<String> it = clzPaths.iterator();
byte[] bytes = null;
while(it.hasNext() && bytes == null){
String filePath = it.next()+File.separator+className;
bytes = getClassFile(filePath);
}
return bytes;

}


private byte[] getClassFile(String filePath) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
InputStream is = new FileInputStream(new File(filePath));
byte[] buffer = new byte[1024];
int len = 0;
while((len=is.read(buffer)) > 0){
bos.write(buffer,0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
return bos.toByteArray();
}


public void addClassPath(String path) {
if(null != path && !"".equals(path)){
clzPaths.add(path);
}
}



public String getClassPath(){
StringBuilder sb = new StringBuilder();
Iterator<String> it = clzPaths.iterator();
while(it.hasNext()){
if(sb.length() > 0){
sb.append(";");
}
sb.append(it.next());
}
return sb.toString();
}





}
Loading