summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorTGIshib <justzeddicus@gmail.com>2016-08-14 14:58:51 +0300
committerTGIshib <justzeddicus@gmail.com>2016-08-14 14:58:51 +0300
commit8fdced4e1141a98fb37e952058874423702347ca (patch)
tree5c9e89d07a239d901f424f008c71233940487a37 /java
parent9031597f490e64880deb99fa379fc9ef616375c5 (diff)
downloadflatbuffers-8fdced4e1141a98fb37e952058874423702347ca.tar.gz
flatbuffers-8fdced4e1141a98fb37e952058874423702347ca.tar.bz2
flatbuffers-8fdced4e1141a98fb37e952058874423702347ca.zip
Update
Diffstat (limited to 'java')
-rw-r--r--java/com/google/flatbuffers/FlatBufferBuilder.java18
-rw-r--r--java/com/google/flatbuffers/Table.java65
2 files changed, 60 insertions, 23 deletions
diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java
index b3615a05..787e4e6c 100644
--- a/java/com/google/flatbuffers/FlatBufferBuilder.java
+++ b/java/com/google/flatbuffers/FlatBufferBuilder.java
@@ -375,7 +375,7 @@ public class FlatBufferBuilder {
*/
public int createVectorOfTables(int[] offsets) {
notNested();
- startVector(4, offsets.length, 4);
+ startVector(Constants.SIZEOF_INT, offsets.length, Constants.SIZEOF_INT);
for(int i = offsets.length - 1; i >= 0; i--) addOffset(offsets[i]);
return endVector();
}
@@ -383,21 +383,13 @@ public class FlatBufferBuilder {
/**
* Create a vector of sorted by the key tables.
*
- * @param type Type of the tables.
+ * @param obj Instance of the table class.
* @param offsets Offsets of the tables.
* @return Returns offset of the sorted vector.
*/
- public <T> int createSortedTableVector(Class<T> type, int[] offsets) {
- try{
- return (int)type.getMethod("createMySortedTableVector", FlatBufferBuilder.class, ByteBuffer.class, int[].class).invoke(null, this, bb, offsets);
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (Exception e){
- e.printStackTrace();
- }
- return 0;
+ public <T extends Table> int createSortedTableVector(T obj, int[] offsets) {
+ obj.sortTables(offsets, bb);
+ return createVectorOfTables(offsets);
}
/**
diff --git a/java/com/google/flatbuffers/Table.java b/java/com/google/flatbuffers/Table.java
index 9235c165..c23ad6a5 100644
--- a/java/com/google/flatbuffers/Table.java
+++ b/java/com/google/flatbuffers/Table.java
@@ -56,11 +56,14 @@ public class Table {
* @param vtable_offset An `int` offset to the vtable in the Table's ByteBuffer.
* @return Returns an offset into the object, or `0` if the field is not present.
*/
- protected int __offset(int vtable_offset) { return __offset(vtable_offset, bb_pos - bb.getInt(bb_pos), bb, false); }
+ protected int __offset(int vtable_offset) {
+ int vtable = bb_pos - bb.getInt(bb_pos);
+ return vtable_offset < bb.getShort(vtable) ? bb.getShort(vtable + vtable_offset) : 0;
+ }
- protected static int __offset(int vtable_offset, int vtable, ByteBuffer _bb, boolean invoked_static) {
- if (!invoked_static) return vtable_offset < _bb.getShort(vtable) ? _bb.getShort(vtable + vtable_offset) : 0;
- else return _bb.getShort(vtable + vtable_offset - _bb.getInt(vtable)) + vtable;
+ protected static int __offset(int vtable_offset, int offset, ByteBuffer bb) {
+ int vtable = bb.array().length - offset;
+ return bb.getShort(vtable + vtable_offset - bb.getInt(vtable)) + vtable;
}
/**
@@ -85,15 +88,11 @@ public class Table {
* @return Returns a `String` from the data stored inside the FlatBuffer at `offset`.
*/
protected String __string(int offset) {
- return __string(offset, bb);
- }
-
- protected static String __string(int offset, ByteBuffer _bb) {
CharsetDecoder decoder = UTF8_DECODER.get();
decoder.reset();
- offset += _bb.getInt(offset);
- ByteBuffer src = _bb.duplicate().order(ByteOrder.LITTLE_ENDIAN);
+ offset += bb.getInt(offset);
+ ByteBuffer src = bb.duplicate().order(ByteOrder.LITTLE_ENDIAN);
int length = src.getInt(offset);
src.position(offset + SIZEOF_INT);
src.limit(offset + SIZEOF_INT + length);
@@ -194,6 +193,52 @@ public class Table {
}
return true;
}
+
+ /**
+ * Sort tables by the key.
+ *
+ * @param offsets An 'int' indexes of the tables into the _bb.
+ * @param bb A {@code ByteBuffer} to get the tables.
+ */
+ protected void sortTables(int[] offsets, ByteBuffer bb) {
+ Integer[] off = new Integer[offsets.length];
+ for (int i = 0; i < offsets.length; i++) off[i] = offsets[i];
+ Arrays.sort(off, (Integer o1, Integer o2) -> keysCompare(o1, o2, bb));
+ for (int i = 0; i < offsets.length; i++) offsets[i] = off[i];
+ }
+
+ /**
+ * Compare two tables by the key.
+ *
+ * @param o1 An 'Integer' index of the first key into the _bb.
+ * @param o2 An 'Integer' index of the second key into the _bb.
+ * @param bb A {@code ByteBuffer} to get the keys.
+ */
+ protected int keysCompare(Integer o1, Integer o2, ByteBuffer bb) { return 0; }
+
+ /**
+ * Compare two strings in the buffer.
+ *
+ * @param offset_1 An 'int' index of the first string into the bb.
+ * @param offset_2 An 'int' index of the second string into the bb.
+ * @param bb A {@code ByteBuffer} to get the strings.
+ */
+ protected static int compareStrings(int offset_1, int offset_2, ByteBuffer bb) {
+ offset_1 += bb.getInt(offset_1);
+ offset_2 += bb.getInt(offset_2);
+ int len_1 = bb.getInt(offset_1);
+ int len_2 = bb.getInt(offset_2);
+ int startPos_1 = offset_1 + SIZEOF_INT;
+ int startPos_2 = offset_2 + SIZEOF_INT;
+ int len = Math.min(len_1, len_2);
+ for(int i = 0; i < len; i++) {
+ if (bb.array()[i + startPos_1] != bb.array()[i + startPos_2])
+ return bb.array()[i + startPos_1] - bb.array()[i + startPos_2];
+ }
+ if (len_1 < len_2) return -1;
+ if (len_1 > len_2) return 1;
+ return 0;
+ }
}
/// @endcond