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,50 @@
package com.github.orajavac.coding2017.basic.queue;

public class CircleQueue <E>{
private final static int DEFAULT_SIZE = 8;

//用数组来保存循环队列的元素
private Object[] elementData = new Object[DEFAULT_SIZE] ;

//队头
private int front = 0;
//队尾
private int rear = 0;

public boolean isEmpty() {
if (front==0&&rear==0){
return true;
}
return false;

}

public int size() {
return DEFAULT_SIZE;
}



public void enQueue(E data) {
if (rear==DEFAULT_SIZE&&elementData[0]==null){
rear = 0;
elementData[rear]=data;
}else if (elementData[rear]==null){
elementData[rear]=data;
}else{
throw new RuntimeException("队列已满");
}
rear++;
}

@SuppressWarnings("unchecked")
public E deQueue() {
Object o = elementData[front];
elementData[front] = null;
front++;
if (front == DEFAULT_SIZE){
front = 0;
}
return (E)o;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.orajavac.coding2017.basic.queue;

import java.util.ArrayList;
import java.util.List;

/**
* 用Queue来实现Josephus问题
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来
* 该方法返回一个List, 包含了被杀死人的次序
* @author liuxin
*
*/
public class Josephus {

public static List<Integer> execute(int n, int m){
CircleQueue<Integer> q = new CircleQueue<Integer>();
List<Integer> l = new ArrayList<Integer>();
for (int i=1;i<=n;i++){
q.enQueue(i);
}
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
Integer c = q.deQueue();
if (j==m-1){
l.add(c);
}else{
q.enQueue(c);
}
}
}
return l;
}

public static void main(String[] args){
List<Integer> l = execute(8,4);
for (int i=0;i<l.size();i++){
System.out.print(l.get(i)+" ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.orajavac.coding2017.basic.queue;

import java.util.Stack;

public class QueueWithTwoStacks<E> {
private Stack<E> stack1;
private Stack<E> stack2;


public QueueWithTwoStacks() {
stack1 = new Stack<E>();
stack2 = new Stack<E>();
}

public boolean isEmpty() {
return stack1.size()==0;
}



public int size() {
return stack1.size();
}



public void enQueue(E item) {
int len1 = stack1.size();
for (int i=0;i<len1;i++){
stack2.push(stack1.pop());
}
stack1.push(item);
int len2 = stack2.size();
for (int i=0;i<len2;i++){
stack1.push(stack2.pop());
}
}

public E deQueue() {
return stack1.size()!=0?stack1.pop():null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.orajavac.coding2017.jvm.attr;

import com.github.orajavac.coding2017.jvm.clz.ClassFile;
import com.github.orajavac.coding2017.jvm.cmd.ByteCodeCommand;
import com.github.orajavac.coding2017.jvm.constant.ConstantPool;
import com.github.orajavac.coding2017.jvm.loader.ByteCodeIterator;

public class CodeAttr extends AttributeInfo{
Expand All @@ -13,10 +15,10 @@ public String getCode() {
return code;
}

//private ByteCodeCommand[] cmds ;
//public ByteCodeCommand[] getCmds() {
// return cmds;
//}
private ByteCodeCommand[] cmds ;
public ByteCodeCommand[] getCmds() {
return cmds;
}
private LineNumberTable lineNumTable;
private LocalVariableTable localVarTable;
private StackMapTable stackMapTable;
Expand Down Expand Up @@ -70,6 +72,19 @@ public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){
}
return null;
}

public String toString(ConstantPool pool){
StringBuilder buffer = new StringBuilder();
//buffer.append("Code:").append(code).append("\n");
for(int i=0;i<cmds.length;i++){
buffer.append(cmds[i].toString(pool)).append("\n");
}
buffer.append("\n");
buffer.append(this.lineNumTable.toString());
buffer.append(this.localVarTable.toString(pool));
return buffer.toString();
}

private void setStackMapTable(StackMapTable t) {
this.stackMapTable = t;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import com.github.orajavac.coding2017.jvm.constant.ConstantPool;
import com.github.orajavac.coding2017.jvm.loader.ByteCodeIterator;

public class LocalVariableTable extends AttributeInfo{
Expand All @@ -28,6 +29,20 @@ public static LocalVariableTable parse(ByteCodeIterator iter){
}
return table;
}

public String toString(ConstantPool pool){
StringBuilder buffer = new StringBuilder();
buffer.append("Local Variable Table:\n");
for(LocalVariableItem item : items){
buffer.append("startPC:"+item.getStartPC()).append(",");
buffer.append("name:"+pool.getUTF8String(item.getNameIndex())).append(",");
buffer.append("desc:"+pool.getUTF8String(item.getDescIndex())).append(",");
buffer.append("slotIndex:"+ item.getIndex()).append("\n");
}
buffer.append("\n");
return buffer.toString();
}

private void addLocalVariableItem(LocalVariableItem item) {
this.items.add(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public void print(){

}

private String getClassName(){
public String getClassName(){
int thisClassIndex = this.clzIndex.getThisClassIndex();
ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex);
return thisClass.getClassName();
}
private String getSuperClassName(){
public String getSuperClassName(){
ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex());
return superClass.getClassName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.orajavac.coding2017.jvm.cmd;

import com.github.orajavac.coding2017.jvm.clz.ClassFile;
import com.github.orajavac.coding2017.jvm.constant.ConstantInfo;
import com.github.orajavac.coding2017.jvm.constant.ConstantPool;


public class BiPushCmd extends OneOperandCmd {

public BiPushCmd(ClassFile clzFile,String opCode) {
super(clzFile,opCode);

}

@Override
public String toString(ConstantPool pool) {

return this.getOffset()+": "+ this.getOpCode()+" " + this.getReadableCodeText() + " " + this.getOperand();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.github.orajavac.coding2017.jvm.cmd;

import java.util.HashMap;
import java.util.Map;

import com.github.orajavac.coding2017.jvm.clz.ClassFile;
import com.github.orajavac.coding2017.jvm.constant.ConstantInfo;
import com.github.orajavac.coding2017.jvm.constant.ConstantPool;


public abstract class ByteCodeCommand {

String opCode;
ClassFile clzFile;
private int offset;

private static Map<String,String> codeMap = new HashMap<String,String>();

static{
codeMap.put("01", "aconst_null");

codeMap.put("BB", "new");
codeMap.put("37", "lstore");
codeMap.put("B7", "invokespecial");
codeMap.put("B6", "invokevirtual");
codeMap.put("B4", "getfield");
codeMap.put("B5", "putfield");
codeMap.put("B2", "getstatic");

codeMap.put("2A", "aload_0");
codeMap.put("2B", "aload_1");
codeMap.put("2C", "aload_2");

codeMap.put("10", "bipush");
codeMap.put("15", "iload");
codeMap.put("1A", "iload_0");
codeMap.put("1B", "iload_1");
codeMap.put("1C", "iload_2");
codeMap.put("1D", "iload_3");

codeMap.put("25", "fload_3");

codeMap.put("1E", "lload_0");

codeMap.put("24", "fload_2");
codeMap.put("4C", "astore_1");

codeMap.put("A2", "if_icmp_ge");
codeMap.put("A4", "if_icmple");

codeMap.put("A7", "goto");

codeMap.put("B1", "return");
codeMap.put("AC", "ireturn");
codeMap.put("AE", "freturn");

codeMap.put("03", "iconst_0");
codeMap.put("04", "iconst_1");

codeMap.put("3C", "istore_1");
codeMap.put("3D", "istore_2");

codeMap.put("59", "dup");

codeMap.put("60", "iadd");
codeMap.put("84", "iinc");

codeMap.put("12", "ldc");
}





protected ByteCodeCommand(ClassFile clzFile, String opCode){
this.clzFile = clzFile;
this.opCode = opCode;
}

protected ClassFile getClassFile() {
return clzFile;
}

public int getOffset() {
return offset;
}

public void setOffset(int offset) {
this.offset = offset;
}
protected ConstantInfo getConstantInfo(int index){
return this.getClassFile().getConstantPool().getConstantInfo(index);
}

protected ConstantPool getConstantPool(){
return this.getClassFile().getConstantPool();
}



public String getOpCode() {
return opCode;
}

public abstract int getLength();




public String toString(){

StringBuffer buffer = new StringBuffer();
buffer.append(this.opCode);

return buffer.toString();
}
public abstract String toString(ConstantPool pool);

public String getReadableCodeText(){
String txt = codeMap.get(opCode);
if(txt == null){
return opCode;
}
return txt;
}

//public abstract void execute(StackFrame frame,FrameResult result);
}
Loading