Skip to content

Commit ab84d70

Browse files
authored
Merge pull request onlyliuxin#32 from xukaide77/master
jvm5
2 parents 7f79869 + 809a389 commit ab84d70

25 files changed

+1038
-14
lines changed
12.5 KB
Binary file not shown.

group19/527220084/lite-struts-tdd.zip

284 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.xukai.coderising.queue;
2+
3+
/**
4+
* 用数组实现循环队列
5+
* @author liuxin
6+
*
7+
* @param <E>
8+
*/
9+
public class CircleQueue <E> {
10+
11+
private final static int DEFAULT_SIZE = 10;
12+
13+
private int count = 0;
14+
15+
//用数组来保存循环队列的元素
16+
private Object[] elementData = new Object[DEFAULT_SIZE] ;
17+
18+
//队头
19+
private int front = 0;
20+
//队尾
21+
private int rear = 0;
22+
23+
public boolean isEmpty() {
24+
return count == 0;
25+
26+
}
27+
28+
public int size() {
29+
return count;
30+
}
31+
32+
33+
34+
public void enQueue(E data) {
35+
if (count == DEFAULT_SIZE ) {
36+
throw new RuntimeException();
37+
}
38+
elementData[front] = data;
39+
front = (front + 1) % DEFAULT_SIZE;
40+
count++;
41+
}
42+
43+
public E deQueue() {
44+
if (count == 0) {
45+
return null;
46+
}
47+
count--;
48+
E e = (E)elementData[rear];
49+
rear = (rear + 1) % DEFAULT_SIZE;
50+
return e;
51+
}
52+
53+
public static void main(String[] args) {
54+
CircleQueue<Integer> queue = new CircleQueue<>();
55+
queue.enQueue(1);
56+
queue.enQueue(2);
57+
queue.enQueue(3);
58+
queue.enQueue(4);
59+
queue.enQueue(5);
60+
queue.enQueue(6);
61+
System.out.println(queue.deQueue());
62+
System.out.println(queue.deQueue());
63+
System.out.println(queue.deQueue());
64+
System.out.println(queue.deQueue());System.out.println(queue.deQueue());System.out.println(queue.deQueue());
65+
System.out.println(queue.deQueue());
66+
queue.enQueue(7);
67+
queue.enQueue(8);
68+
queue.enQueue(9);
69+
queue.enQueue(0);
70+
queue.enQueue(1);
71+
System.out.println(queue.deQueue());
72+
System.out.println(queue.deQueue());
73+
System.out.println(queue.deQueue());
74+
System.out.println(queue.deQueue());
75+
System.out.println(queue.deQueue());
76+
System.out.println(queue.deQueue());
77+
78+
}
79+
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.xukai.coderising.queue;
2+
3+
import com.google.common.base.Preconditions;
4+
5+
import java.util.ArrayDeque;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* 用Queue来实现Josephus问题
11+
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来
12+
* 该方法返回一个List, 包含了被杀死人的次序
13+
* @author liuxin
14+
*
15+
*/
16+
public class Josephus {
17+
18+
19+
public static List<Integer> execute(int n, int m){
20+
Preconditions.checkArgument(n > 0);
21+
ArrayList<Integer> deadList = new ArrayList<>();
22+
ArrayDeque<Integer> deque1 = new ArrayDeque<>();
23+
ArrayDeque<Integer> deque2 = new ArrayDeque<>();
24+
for (int i = 0; i < n; i++) {
25+
deque1.offer(i);
26+
}
27+
int count = 0;
28+
while(n >= m){
29+
if (deque1.isEmpty()) {
30+
while (!deque2.isEmpty()){
31+
deque1.offer(deque2.poll());
32+
}
33+
}
34+
count++;
35+
if (count == m) {
36+
deadList.add(deque1.poll());
37+
count = 0;
38+
n--;
39+
continue;
40+
}
41+
deque2.offer(deque1.poll());
42+
43+
}
44+
45+
return deadList;
46+
}
47+
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.xukai.coderising.queue;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
9+
10+
public class JosephusTest {
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
}
15+
16+
@After
17+
public void tearDown() throws Exception {
18+
}
19+
20+
@Test
21+
public void testExecute() {
22+
23+
Assert.assertEquals("[1, 3, 5, 0, 4, 2]", Josephus.execute(7, 2).toString());
24+
25+
26+
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.xukai.coderising.queue;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 用两个栈来实现一个队列
7+
* @author liuxin
8+
*
9+
* @param <E>
10+
*/
11+
public class QueueWithTwoStacks<E> {
12+
private Stack<E> stack1;
13+
private Stack<E> stack2;
14+
15+
16+
public QueueWithTwoStacks() {
17+
stack1 = new Stack<E>();
18+
stack2 = new Stack<E>();
19+
}
20+
21+
22+
23+
24+
public boolean isEmpty() {
25+
return stack1.isEmpty();
26+
}
27+
28+
29+
30+
public int size() {
31+
return stack1.size();
32+
}
33+
34+
35+
36+
public void enQueue(E item) {
37+
stack1.push(item);
38+
}
39+
40+
public E deQueue() {
41+
if (stack1.isEmpty()) {
42+
return null;
43+
}
44+
while (!stack1.isEmpty()) {
45+
stack2.push(stack1.pop());
46+
}
47+
E e = stack2.pop();
48+
while (!stack2.isEmpty()) {
49+
stack1.push(stack2.pop());
50+
}
51+
return e;
52+
}
53+
54+
public static void main(String[] args) {
55+
QueueWithTwoStacks queue = new QueueWithTwoStacks();
56+
queue.enQueue(1);
57+
queue.enQueue(2);
58+
queue.enQueue(3);
59+
queue.enQueue(4);
60+
queue.enQueue(5);
61+
queue.enQueue(6);
62+
System.out.println(queue.deQueue());
63+
System.out.println(queue.deQueue());
64+
System.out.println(queue.deQueue());
65+
System.out.println(queue.deQueue());System.out.println(queue.deQueue());System.out.println(queue.deQueue());
66+
System.out.println(queue.deQueue());
67+
queue.enQueue(7);
68+
queue.enQueue(8);
69+
queue.enQueue(9);
70+
queue.enQueue(0);
71+
queue.enQueue(1);
72+
System.out.println(queue.deQueue());
73+
System.out.println(queue.deQueue());
74+
System.out.println(queue.deQueue());
75+
System.out.println(queue.deQueue());
76+
System.out.println(queue.deQueue());
77+
System.out.println(queue.deQueue());
78+
79+
80+
}
81+
82+
}
83+

group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/jvm/cmd/BiPushCmd.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
import org.xukai.jvm.clz.ClassFile;
55
import org.xukai.jvm.constant.ConstantPool;
6+
import org.xukai.jvm.engine.ExecutionResult;
7+
import org.xukai.jvm.engine.Heap;
8+
import org.xukai.jvm.engine.JavaObject;
9+
import org.xukai.jvm.engine.StackFrame;
610

711
public class BiPushCmd extends OneOperandCmd {
812

@@ -16,7 +20,12 @@ public String toString(ConstantPool pool) {
1620

1721
return this.getOffset()+": "+ this.getOpCode()+" " + this.getReadableCodeText() + " " + this.getOperand();
1822
}
19-
20-
23+
24+
@Override
25+
public void execute(StackFrame frame, ExecutionResult result) {
26+
JavaObject javaObject = Heap.getInstance().newInt(getOperand());
27+
frame.getOprandStack().push(javaObject);
28+
}
29+
2130

2231
}

group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/jvm/cmd/ByteCodeCommand.java

+39-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.xukai.jvm.clz.ClassFile;
55
import org.xukai.jvm.constant.ConstantInfo;
66
import org.xukai.jvm.constant.ConstantPool;
7+
import org.xukai.jvm.engine.ExecutionResult;
8+
import org.xukai.jvm.engine.StackFrame;
79

810
import java.util.HashMap;
911
import java.util.Map;
@@ -14,7 +16,42 @@ public abstract class ByteCodeCommand {
1416
String opCode;
1517
ClassFile clzFile;
1618
private int offset;
17-
19+
20+
public static final String aconst_null = "01";
21+
public static final String new_object = "BB";
22+
public static final String lstore = "37";
23+
public static final String invokespecial = "B7";
24+
public static final String invokevirtual = "B6";
25+
public static final String getfield = "B4";
26+
public static final String putfield = "B5";
27+
public static final String getstatic = "B2";
28+
public static final String ldc = "12";
29+
public static final String dup = "59";
30+
public static final String bipush = "10";
31+
public static final String aload_0 = "2A";
32+
public static final String aload_1 = "2B";
33+
public static final String aload_2 = "2C";
34+
public static final String iload = "15";
35+
public static final String iload_1 = "1B";
36+
public static final String iload_2 = "1C";
37+
public static final String iload_3 = "1D";
38+
public static final String fload_3 = "25";
39+
40+
public static final String voidreturn = "B1";
41+
public static final String ireturn = "AC";
42+
public static final String freturn = "AE";
43+
44+
public static final String astore_1 = "4C";
45+
public static final String if_icmp_ge = "A2";
46+
public static final String if_icmple = "A4";
47+
public static final String goto_no_condition = "A7";
48+
public static final String iconst_0 = "03";
49+
public static final String iconst_1 = "04";
50+
public static final String istore_1 = "3C";
51+
public static final String istore_2 = "3D";
52+
public static final String iadd = "60";
53+
public static final String iinc = "84";
54+
1855
private static Map<String,String> codeMap = new HashMap<String,String>();
1956

2057
static{
@@ -125,5 +162,5 @@ public String getReadableCodeText(){
125162
return txt;
126163
}
127164

128-
//public abstract void execute(StackFrame frame,FrameResult result);
165+
public abstract void execute(StackFrame frame, ExecutionResult result);
129166
}

group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/jvm/cmd/GetFieldCmd.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
import org.xukai.jvm.clz.ClassFile;
55
import org.xukai.jvm.constant.ConstantPool;
6+
import org.xukai.jvm.constant.FieldRefInfo;
7+
import org.xukai.jvm.engine.ExecutionResult;
8+
import org.xukai.jvm.engine.JavaObject;
9+
import org.xukai.jvm.engine.StackFrame;
610

711
public class GetFieldCmd extends TwoOperandCmd {
812

@@ -13,10 +17,22 @@ public GetFieldCmd(ClassFile clzFile, String opCode) {
1317
@Override
1418
public String toString(ConstantPool pool) {
1519

16-
return super.getOperandAsField(pool);
20+
return super.getOperandAsField(clzFile.getConstantPool());
1721
}
1822

19-
23+
@Override
24+
public void execute(StackFrame frame, ExecutionResult result) {
25+
26+
FieldRefInfo fieldRef = (FieldRefInfo)this.getConstantInfo(this.getIndex());
27+
String fieldName = fieldRef.getFieldName();
28+
JavaObject jo = frame.getOprandStack().pop();
29+
JavaObject fieldValue = jo.getFieldValue(fieldName);
30+
31+
frame.getOprandStack().push(fieldValue);
32+
33+
34+
35+
}
2036

2137

2238
}

group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/jvm/cmd/GetStaticFieldCmd.java

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import org.xukai.jvm.clz.ClassFile;
55
import org.xukai.jvm.constant.ConstantPool;
6+
import org.xukai.jvm.engine.ExecutionResult;
7+
import org.xukai.jvm.engine.StackFrame;
68

79
public class GetStaticFieldCmd extends TwoOperandCmd {
810

@@ -17,4 +19,9 @@ public String toString(ConstantPool pool) {
1719
return super.getOperandAsField(pool);
1820
}
1921

22+
@Override
23+
public void execute(StackFrame frame, ExecutionResult result) {
24+
25+
}
26+
2027
}

0 commit comments

Comments
 (0)