Skip to content

Instantly share code, notes, and snippets.

@junwen12221
Created March 10, 2017 13:50
Show Gist options
  • Save junwen12221/b7fd2058b7e267517561de914bd8e353 to your computer and use it in GitHub Desktop.
Save junwen12221/b7fd2058b7e267517561de914bd8e353 to your computer and use it in GitHub Desktop.
package org.javolution.io;
import java.nio.ByteBuffer;
/**
* Created by Administrator on 2017/3/10 0010.
* 导入库
* javolution
*/
public class StructTest {
public static class MyStruct extends Struct {
public final Struct.Signed32 leftAddress = new Signed32();
public final Struct.Signed32 op = new Signed32();
public final Struct.Signed32 rightAddress = new Signed32();
public final Struct.Signed32 data = new Signed32();
}
static final int lenght = 8192;
public static void main(String[] args) throws Exception {
//int max = 536870912; //512mb
MyStruct struct = new MyStruct();
int size = struct.size();
System.out.println("MyStruct结构体大小:" + size + "bytes");
ByteBuffer memory = ByteBuffer.allocate(lenght);
struct.setByteBuffer(memory, 0);
//+:+ data:1
//top
int left = size - 1;
int right = size + size - 1;
System.out.println(left);
System.out.println(right);
struct.op.set('+');
struct.leftAddress.set(size - 1);
struct.rightAddress.set(size + size - 1);
System.out.println(struct.getByteBufferPosition());
System.out.println(struct);
//left
struct.setByteBufferPosition(size - 1);
struct.op.set(1);
struct.data.set(1);
System.out.println(struct.getByteBufferPosition());
System.out.println(struct);
//right
struct.setByteBufferPosition(size + size - 1);
struct.op.set(1);
struct.data.set(2);
System.out.println(struct);
System.out.println(struct.getByteBufferPosition());
System.out.println(struct);
//计算
struct.setByteBufferPosition(0);
System.out.println("结果:" + eval(struct, 0));
}
public static final int eval(MyStruct struct, int start) throws Exception {
struct.setByteBufferPosition(start);
switch (struct.op.get()) {
case '+': {
int leftAddress = struct.leftAddress.get();
int left = eval(struct, leftAddress);
struct.setByteBufferPosition(start);
int rightAdddress = struct.rightAddress.get();
int right = eval(struct, rightAdddress);
return left + right;
}
case '-': {
int leftAddress = struct.leftAddress.get();
int left = eval(struct, leftAddress);
struct.setByteBufferPosition(start);
int rightAdddress = struct.rightAddress.get();
int right = eval(struct, rightAdddress);
return left - right;
}
case 1:
return struct.data.get();
default:
break;
}
throw new Exception("异常");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment