add getters and setters for primitive lists. fixes #6

This commit is contained in:
David Renshaw 2014-09-25 18:07:40 -04:00
parent 5f44c60a5c
commit 983b4f4238
4 changed files with 207 additions and 16 deletions

View file

@ -47,6 +47,18 @@ object TestUtil {
builder.setEnumField(TestEnum.CORGE);
builder.initVoidList(6);
val boolList = builder.initBoolList(4);
boolList.set(0, true);
boolList.set(1, false);
boolList.set(2, false);
boolList.set(3, true);
val float64List = builder.initFloat64List(4);
float64List.set(0, 7777.75);
float64List.set(1, Double.PositiveInfinity);
float64List.set(2, Double.NegativeInfinity);
float64List.set(3, Double.NaN);
val textList = builder.initTextList(3);
textList.set(0, new Text.Reader("plugh"));
textList.set(1, new Text.Reader("xyzzy"));
@ -98,6 +110,18 @@ object TestUtil {
assert(builder.getVoidList().size() == 6);
val boolList = builder.getBoolList();
assert(boolList.get(0) == true);
assert(boolList.get(1) == false);
assert(boolList.get(2) == false);
assert(boolList.get(3) == true);
val float64List = builder.getFloat64List();
assert(float64List.get(0) == 7777.75);
assert(float64List.get(1) == Double.PositiveInfinity);
assert(float64List.get(2) == Double.NegativeInfinity);
assert(float64List.get(3) != float64List.get(3)); // NaN
val textList = builder.getTextList();
assert(textList.size() == 3);
assert(textList.get(0).toString() == "plugh");
@ -152,6 +176,18 @@ object TestUtil {
assert(reader.getVoidList().size() == 6);
val boolList = reader.getBoolList();
assert(boolList.get(0) == true);
assert(boolList.get(1) == false);
assert(boolList.get(2) == false);
assert(boolList.get(3) == true);
val float64List = reader.getFloat64List();
assert(float64List.get(0) == 7777.75);
assert(float64List.get(1) == Double.PositiveInfinity);
assert(float64List.get(2) == Double.NegativeInfinity);
assert(float64List.get(3) != float64List.get(3)); // NaN
val textList = reader.getTextList();
assert(textList.size() == 3);
assert(textList.get(0).toString() == "plugh");

View file

@ -23,6 +23,68 @@ public final class ListBuilder {
return this.elementCount;
}
public boolean getBooleanElement(int index) {
byte b = this.segment.buffer.get(this.ptr + index / 8);
return (b & (1 << (index % 8))) != 0;
}
public byte getByteElement(int index) {
return this.segment.buffer.get(this.ptr + index);
}
public short getShortElement(int index) {
return this.segment.buffer.getShort(this.ptr + index * 2);
}
public int getIntElement(int index) {
return this.segment.buffer.getInt(this.ptr + index * 4);
}
public long getLongElement(int index) {
return this.segment.buffer.getLong(this.ptr + index * 8);
}
public float getFloatElement(int index) {
return this.segment.buffer.getFloat(this.ptr + index * 4);
}
public double getDoubleElement(int index) {
return this.segment.buffer.getDouble(this.ptr + index * 8);
}
public void setBooleanElement(int index, boolean value) {
int bitOffset = index;
byte bitnum = (byte)(bitOffset % 8);
int position = this.ptr + (bitOffset / 8);
byte oldValue = this.segment.buffer.get(position);
this.segment.buffer.put(position,
(byte)((oldValue & (~(1 << bitnum))) | (( value ? 1 : 0) << bitnum)));
}
public void setByteElement(int index, byte value) {
this.segment.buffer.put(this.ptr + index, value);
}
public void setShortElement(int index, short value) {
this.segment.buffer.putShort(this.ptr + index * 2, value);
}
public void setIntElement(int index, int value) {
this.segment.buffer.putInt(this.ptr + index * 4, value);
}
public void setLongElement(int index, long value) {
this.segment.buffer.putLong(this.ptr + index * 8, value);
}
public void setFloatElement(int index, float value) {
this.segment.buffer.putFloat(this.ptr + index * 4, value);
}
public void setDoubleElement(int index, double value) {
this.segment.buffer.putDouble(this.ptr + index * 8, value);
}
public final StructBuilder getStructElement(int index) {
int indexBit = index * this.step;
int structData = this.ptr + indexBit / 8 ;

View file

@ -37,6 +37,35 @@ public final class ListReader {
return this.elementCount;
}
public boolean getBooleanElement(int index) {
byte b = this.segment.buffer.get(this.ptr + index / 8);
return (b & (1 << (index % 8))) != 0;
}
public byte getByteElement(int index) {
return this.segment.buffer.get(this.ptr + index);
}
public short getShortElement(int index) {
return this.segment.buffer.getShort(this.ptr + index * 2);
}
public int getIntElement(int index) {
return this.segment.buffer.getInt(this.ptr + index * 4);
}
public long getLongElement(int index) {
return this.segment.buffer.getLong(this.ptr + index * 8);
}
public float getFloatElement(int index) {
return this.segment.buffer.getFloat(this.ptr + index * 4);
}
public double getDoubleElement(int index) {
return this.segment.buffer.getDouble(this.ptr + index * 8);
}
public StructReader getStructElement(int index) {
// TODO check nesting limit

View file

@ -46,7 +46,7 @@ public class PrimitiveList {
}
public boolean get(int index) {
throw new Error();
return this.reader.getBooleanElement(index);
}
}
@ -61,12 +61,16 @@ public class PrimitiveList {
return this.builder.size();
}
public boolean get(int index) {
return this.builder.getBooleanElement(index);
}
public void set(int index, boolean value) {
this.builder.setBooleanElement(index, value);
}
}
}
public static class Byte {
public static final class Reader {
public final ListReader reader;
@ -80,7 +84,7 @@ public class PrimitiveList {
}
public byte get(int index) {
throw new Error();
return this.reader.getByteElement(index);
}
}
@ -91,6 +95,17 @@ public class PrimitiveList {
this.builder = builder;
}
public int size() {
return this.builder.size();
}
public byte get(int index) {
return this.builder.getByteElement(index);
}
public void set(int index, byte value) {
this.builder.setByteElement(index, value);
}
}
}
@ -108,7 +123,7 @@ public class PrimitiveList {
}
public short get(int index) {
throw new Error();
return this.reader.getShortElement(index);
}
}
@ -119,6 +134,17 @@ public class PrimitiveList {
this.builder = builder;
}
public int size() {
return this.builder.size();
}
public short get(int index) {
return this.builder.getShortElement(index);
}
public void set(int index, short value) {
this.builder.setShortElement(index, value);
}
}
}
@ -136,7 +162,7 @@ public class PrimitiveList {
}
public int get(int index) {
throw new Error();
return this.reader.getIntElement(index);
}
}
@ -147,8 +173,18 @@ public class PrimitiveList {
this.builder = builder;
}
}
public int size() {
return this.builder.size();
}
public int get(int index) {
return this.builder.getIntElement(index);
}
public void set(int index, int value) {
this.builder.setIntElement(index, value);
}
}
}
public static class Float {
@ -164,7 +200,7 @@ public class PrimitiveList {
}
public float get(int index) {
throw new Error();
return this.reader.getFloatElement(index);
}
}
@ -175,8 +211,18 @@ public class PrimitiveList {
this.builder = builder;
}
}
public int size() {
return this.builder.size();
}
public float get(int index) {
return this.builder.getFloatElement(index);
}
public void set(int index, float value) {
this.builder.setFloatElement(index, value);
}
}
}
@ -193,7 +239,7 @@ public class PrimitiveList {
}
public long get(int index) {
throw new Error();
return this.reader.getLongElement(index);
}
}
@ -204,8 +250,18 @@ public class PrimitiveList {
this.builder = builder;
}
}
public int size() {
return this.builder.size();
}
public long get(int index) {
return this.builder.getLongElement(index);
}
public void set(int index, long value) {
this.builder.setLongElement(index, value);
}
}
}
public static class Double {
@ -221,7 +277,7 @@ public class PrimitiveList {
}
public double get(int index) {
throw new Error();
return this.reader.getDoubleElement(index);
}
}
@ -232,9 +288,17 @@ public class PrimitiveList {
this.builder = builder;
}
public int size() {
return this.builder.size();
}
public double get(int index) {
return this.builder.getDoubleElement(index);
}
public void set(int index, double value) {
this.builder.setDoubleElement(index, value);
}
}
}
}