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
4 changes: 3 additions & 1 deletion group24/1148285693/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ logs
.directory
.DS_Store

Test.java

Test.java
example
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package me.lzb.algorithm;

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

private static class Node {

Node prev;
Node next;
int pageNum;

Node(Node prev, Node next, int pageNum) {
this.prev = prev;
this.next = next;
this.pageNum = pageNum;
}
}

private int capacity;


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


public LRUPageFrame(int capacity) {
if(capacity < 1){
// throw new Exception("capacity boom");
}
this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @param pageNum
* @return
*/
public void access(int pageNum) {
if(capacity == 1){
first = last = new Node(null, null, pageNum);
return;
}


if(first == null){
first = last = new Node(null, null, pageNum);
return;
}

if(first.pageNum == pageNum){
return;
}

Node tmp = first;
int size = 0;
for (int i = 0; i < capacity; i++) {
size = size + 1;
//如果发现一样的,把这个挪到最前面
if(tmp.pageNum == pageNum){
moveToFirst(tmp);
return;
}
//链表已经循环结束,但是个数还没满,更新last
if(tmp.next == null){
last = tmp;
break;
}
tmp = tmp.next;
}


//没有相同的,在最顶端插入
Node f = new Node(null, first, pageNum);
addAsFirst(f);

//已经放满,更新last
if(size >= capacity){
removeLastOne();
}

}

/**
* 删除最后一个节点
*/
private void removeLastOne(){
last = last.prev;
//使GC ROOT 不可达
last.next.prev = null;
last.next = null;
}

/**
* 把某节点移动到最顶部
* @param tmp 在链表中的任意节点
*/
private void moveToFirst(Node tmp){
if(tmp == first){
return;
}

if (tmp.next != null){
tmp.next.prev = tmp.prev;
tmp.prev.next = tmp.next;
}else {
tmp.prev.next = null;
//当这个节点是last的时候,更新last
last = tmp.prev;
}

tmp.next = first;
tmp.prev = null;

first.prev = tmp;
first = tmp;
}

/**
* 在顶部增加一个节点
* @param node node
*/
private void addAsFirst(Node node){
first.prev = node;
first = node;
}



/**
* ASC
* @return
*/
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();
}


/**
* DESC
* @return
*/
public String toStringDESC(){

StringBuilder buffer = new StringBuilder();
Node node = last;
while(node != null){
buffer.append(node.pageNum);

node = node.prev;
if(node != null){
buffer.append(",");
}
}
return buffer.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0312.basic;
package me.lzb.datastructure;

/**
* 简易ArrayList
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0319.array;
package me.lzb.datastructure;

public class ArrayUtil {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package me.lzb.homework0312.basic;
package me.lzb.datastructure;

/**
* 左边比父节点小,右边比父节点大
* Created by LZB on 2017/3/11.
*/
public class BinaryTreeNode {

private int data;
private BinaryTreeNode left;
private BinaryTreeNode right;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0312.basic;
package me.lzb.datastructure;

/**
* Created by LZB on 2017/3/11.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0312.basic;
package me.lzb.datastructure;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0312.basic;
package me.lzb.datastructure;

/**
* list接口
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0312.basic;
package me.lzb.datastructure;

/**
* 先进先出
Expand All @@ -10,21 +10,21 @@ public class Queue {
public void enQueue(Object o){
elementData.add(o);
}

public Object deQueue() throws IndexOutOfBoundsException{
if(isEmpty()){
throw new IndexOutOfBoundsException("index boom");
}
return elementData.remove(elementData.size() - 1);
}

public boolean isEmpty(){
if(elementData.size() <= 0){
return true;
}
return false;
}

public int size(){
return elementData.size();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0312.basic;
package me.lzb.datastructure;

/**
* 先进后出
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.lzb.homework0326.download;
package me.lzb.download;

import me.lzb.homework0326.download.api.Connection;
import me.lzb.download.api.Connection;

import java.io.RandomAccessFile;
import java.util.concurrent.CyclicBarrier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package me.lzb.homework0326.download;
package me.lzb.download;

import me.lzb.homework0326.download.api.Connection;
import me.lzb.homework0326.download.api.ConnectionManager;
import me.lzb.homework0326.download.api.DownloadListener;
import me.lzb.download.api.Connection;
import me.lzb.download.api.ConnectionManager;
import me.lzb.download.api.DownloadListener;

import java.io.IOException;
import java.io.RandomAccessFile;
Expand Down Expand Up @@ -69,9 +69,7 @@ public void run() {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0326.download.api;
package me.lzb.download.api;

import java.io.IOException;

Expand All @@ -15,7 +15,7 @@ public interface Connection {
* @return
*/
int getContentLength();

/**
* 关闭连接
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0326.download.api;
package me.lzb.download.api;

public class ConnectionException extends Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0326.download.api;
package me.lzb.download.api;

public interface ConnectionManager {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.lzb.homework0326.download.api;
package me.lzb.download.api;

public interface DownloadListener {
void notifyFinished();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.lzb.homework0326.download.impl;
package me.lzb.download.impl;

import me.lzb.homework0326.download.api.Connection;
import me.lzb.homework0326.download.api.ConnectionException;
import me.lzb.download.api.Connection;
import me.lzb.download.api.ConnectionException;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand Down Expand Up @@ -35,7 +35,9 @@ public ConnectionImpl(String url) throws ConnectionException{
@Override
public byte[] read(int startPos, int endPos) throws IOException {

httpget.removeHeaders("Range");
httpget.addHeader("Range", "bytes=" + startPos + "-" + endPos);

CloseableHttpResponse response = httpClient.execute(httpget);
InputStream inputStream = response.getEntity().getContent();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.lzb.download.impl;


import me.lzb.download.api.Connection;
import me.lzb.download.api.ConnectionException;
import me.lzb.download.api.ConnectionManager;

public class ConnectionManagerImpl implements ConnectionManager {


@Override
public Connection open(String url) throws ConnectionException {
return new ConnectionImpl(url);
}


}

This file was deleted.

This file was deleted.

Loading