diff options
author | Wouter van Oortmerssen <wvo@google.com> | 2016-08-26 12:03:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-26 12:03:28 -0700 |
commit | 49ee30a20752b4876a30bfded80c69f5babc9cb2 (patch) | |
tree | dfba3f53649d60d1672e5d0d4aef1b94d7ad0240 /java | |
parent | 223ebebbeb868ab80c93dca9679fbdf69a07ba55 (diff) | |
parent | 7c69c5dc3d635e29e3442339caa8eb06b8b9775c (diff) | |
download | flatbuffers-49ee30a20752b4876a30bfded80c69f5babc9cb2.tar.gz flatbuffers-49ee30a20752b4876a30bfded80c69f5babc9cb2.tar.bz2 flatbuffers-49ee30a20752b4876a30bfded80c69f5babc9cb2.zip |
Merge pull request #3978 from TGIshib/key
Find by key on C# and Java (2)
Diffstat (limited to 'java')
-rw-r--r-- | java/com/google/flatbuffers/FlatBufferBuilder.java | 25 | ||||
-rw-r--r-- | java/com/google/flatbuffers/Table.java | 75 |
2 files changed, 100 insertions, 0 deletions
diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index f69fbcf9..fd4b729e 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -390,6 +390,31 @@ public class FlatBufferBuilder { } /** + * Create a vector of tables. + * + * @param offsets Offsets of the tables. + * @return Returns offset of the vector. + */ + public int createVectorOfTables(int[] offsets) { + notNested(); + startVector(Constants.SIZEOF_INT, offsets.length, Constants.SIZEOF_INT); + for(int i = offsets.length - 1; i >= 0; i--) addOffset(offsets[i]); + return endVector(); + } + + /** + * Create a vector of sorted by the key tables. + * + * @param obj Instance of the table subclass. + * @param offsets Offsets of the tables. + * @return Returns offset of the sorted vector. + */ + public <T extends Table> int createSortedVectorOfTables(T obj, int[] offsets) { + obj.sortTables(offsets, bb); + return createVectorOfTables(offsets); + } + + /** * Encode the string `s` in the buffer using UTF-8. If {@code s} is * already a {@link CharBuffer}, this method is allocation free. * diff --git a/java/com/google/flatbuffers/Table.java b/java/com/google/flatbuffers/Table.java index 40876542..c9c65456 100644 --- a/java/com/google/flatbuffers/Table.java +++ b/java/com/google/flatbuffers/Table.java @@ -61,6 +61,11 @@ public class Table { return vtable_offset < bb.getShort(vtable) ? bb.getShort(vtable + vtable_offset) : 0; } + 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; + } + /** * Retrieve a relative offset. * @@ -70,6 +75,10 @@ public class Table { protected int __indirect(int offset) { return offset + bb.getInt(offset); } + + protected static int __indirect(int offset, ByteBuffer bb) { + return offset + bb.getInt(offset); + } /** * Create a Java `String` from UTF-8 data stored inside the FlatBuffer. @@ -188,6 +197,72 @@ 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); + byte[] bbArray = bb.array(); + for(int i = 0; i < len; i++) { + if (bbArray[i + startPos_1] != bbArray[i + startPos_2]) + return bbArray[i + startPos_1] - bbArray[i + startPos_2]; + } + return len_1 - len_2; + } + + /** + * Compare string from the buffer with the 'String' object. + * + * @param offset_1 An 'int' index of the first string into the bb. + * @param key Second string as a byte array. + * @param bb A {@code ByteBuffer} to get the first string. + */ + protected static int compareStrings(int offset_1, byte[] key, ByteBuffer bb) { + offset_1 += bb.getInt(offset_1); + int len_1 = bb.getInt(offset_1); + int len_2 = key.length; + int startPos_1 = offset_1 + Constants.SIZEOF_INT; + int len = Math.min(len_1, len_2); + byte[] bbArray = bb.array(); + for (int i = 0; i < len; i++) { + if (bbArray[i + startPos_1] != key[i]) + return bbArray[i + startPos_1] - key[i]; + } + return len_1 - len_2; + } } /// @endcond |