mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
Merge pull request #3978 from TGIshib/key
Find by key on C# and Java (2)
This commit is contained in:
@@ -389,6 +389,31 @@ public class FlatBufferBuilder {
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user