This commit is contained in:
TGIshib 2016-08-03 13:29:50 +03:00
parent 867dfc5957
commit dc7f5bc0d8
9 changed files with 241 additions and 12 deletions

View File

@ -367,6 +367,39 @@ public class FlatBufferBuilder {
}
/// @endcond
/**
* Create a vector of tables.
*
* @param offsets Offsets of the tables.
* @return Returns offset of the vector.
*/
public int createVectorOfTables(int[] offsets) {
notNested();
startVector(4, offsets.length, 4);
for(int i = offsets.length - 1; i >= 0; i--) addOffset(offsets[i]);
return endVector();
}
/**
* Create a vector of sorted by the key tables.
*
* @param type Type of the tables.
* @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;
}
/**
* Encode the string `s` in the buffer using UTF-8. If {@code s} is
* already a {@link CharBuffer}, this method is allocation free.

View File

@ -56,9 +56,11 @@ 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) {
int vtable = bb_pos - bb.getInt(bb_pos);
return vtable_offset < bb.getShort(vtable) ? bb.getShort(vtable + vtable_offset) : 0;
protected int __offset(int vtable_offset) { return __offset(vtable_offset, bb_pos - bb.getInt(bb_pos), bb, false); }
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;
}
/**
@ -83,11 +85,15 @@ 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);

View File

@ -295,6 +295,18 @@ namespace FlatBuffers
PutInt(_vectorNumElems);
return new VectorOffset(Offset);
}
/// <summary>
/// Creates a vector of tables.
/// </summary>
/// <param name="offsets">Offsets of the tables.</param>
public VectorOffset CreateVectorOfTables<T>(Offset<T>[] offsets) where T : class
{
NotNested();
StartVector(sizeof(int), offsets.Length, sizeof(int));
for (int i = offsets.Length - 1; i >= 0; i--) AddOffset(offsets[i].Value);
return EndVector();
}
/// @cond FLATBUFFERS_INTENRAL
public void Nested(int obj)

View File

@ -31,10 +31,12 @@ namespace FlatBuffers
// Look up a field in the vtable, return an offset into the object, or 0 if the field is not
// present.
protected int __offset(int vtableOffset)
protected int __offset(int vtableOffset) { return __offset(vtableOffset, bb_pos - bb.GetInt(bb_pos), bb, false); }
protected static int __offset(int vtableOffset, int vtable, ByteBuffer _bb, bool invoked_static)
{
int vtable = bb_pos - bb.GetInt(bb_pos);
return vtableOffset < bb.GetShort(vtable) ? (int)bb.GetShort(vtable + vtableOffset) : 0;
if (!invoked_static) return vtableOffset < _bb.GetShort(vtable) ? (int)_bb.GetShort(vtable + vtableOffset) : 0;
else return (int)_bb.GetShort(vtable + vtableOffset - _bb.GetInt(vtable)) + vtable;
}
// Retrieve the relative offset stored at "offset"
@ -44,12 +46,14 @@ namespace FlatBuffers
}
// Create a .NET String from UTF-8 data stored inside the flatbuffer.
protected string __string(int offset)
protected string __string(int offset) { return __string(offset, bb); }
protected static string __string(int offset, ByteBuffer _bb)
{
offset += bb.GetInt(offset);
var len = bb.GetInt(offset);
offset += _bb.GetInt(offset);
var len = _bb.GetInt(offset);
var startPos = offset + sizeof(int);
return Encoding.UTF8.GetString(bb.Data, startPos , len);
return Encoding.UTF8.GetString(_bb.Data, startPos, len);
}
// Get the length of a vector whose offset is stored at "offset" in this object.

View File

@ -960,6 +960,7 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) {
}
}
code += "\n";
flatbuffers::FieldDef *key_field = nullptr;
if (struct_def.fixed) {
// create a struct constructor function
code += " public static " + GenOffsetType(struct_def) + " ";
@ -1048,6 +1049,7 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
if (field.key) key_field = &field;
code += " public static void " + FunctionStart('A') + "dd";
code += MakeCamel(field.name);
code += "(FlatBufferBuilder builder, ";
@ -1130,6 +1132,71 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) {
code += "); }\n";
}
}
if (struct_def.has_key) {
bool is_string = key_field->value.type.base_type == BASE_TYPE_STRING;
std::string key_name = lang_.language == IDLOptions::kCSharp ? MakeCamel(key_field->name) : key_field->name + "()";
std::string key_type = GenTypeGet(key_field->value.type);
std::string key_offset = NumToString(key_field->value.offset);
std::string key_getter = is_string ? GenGetter(key_field->value.type) : GenGetter(key_field->value.type).substr(2);
std::string data_buffer = lang_.language == IDLOptions::kCSharp ? "builder.DataBuffer" : "bb";
code += "\n public static int " + FunctionStart('K') + "eysCompare(" + key_type + " o1, " + key_type + " o2) ";
if (lang_.language == IDLOptions::kJava && !is_string) {
code += "{\n if (o1 < o2) return -1;\n";
code += " else if (o1 == o2) return 0;\n";
code += " else return 1;\n }\n";
}
else {
code += "{ return o1." + FunctionStart('C') + "ompareTo(o2); }\n";
}
code += "\n public int " + FunctionStart('K') + "eyCompareWithValue(" + key_type + " val) ";
if (lang_.language == IDLOptions::kJava && !is_string) {
code += "{\n if (" + key_name + " < val) return -1;\n";
code += " else if (" + key_name + " == val) return 0;\n";
code += " else return 1;\n }\n";
}
else {
code += "{ return " + key_name + "." + FunctionStart('C') + "ompareTo(val); }\n";
}
code += "\n public static ";
code += (lang_.language == IDLOptions::kCSharp ? "VectorOffset " : "int ") + FunctionStart('C') + "reateMySortedTableVector(FlatBufferBuilder builder, ";
code += (lang_.language == IDLOptions::kCSharp ? " Offset<" + struct_def.name + ">" : "ByteBuffer bb, int") + "[] off" + (lang_.language == IDLOptions::kCSharp ? "sets" : "") + ") {\n";
if (lang_.language == IDLOptions::kJava) {
code += " Integer[] offsets = new Integer[off.length];\n";
code += " for (int i = 0; i < off.length; i++) offsets[i] = off[i];\n";
}
code += " Array";
code += (lang_.language == IDLOptions::kCSharp ? ".S" : "s.s");
code += "ort(offsets, (";
code += (lang_.language == IDLOptions::kCSharp ? "Offset<" + struct_def.name + "> o1, Offset<" + struct_def.name + "> o2) => " : "Integer o1, Integer o2) -> ");
code += FunctionStart('K') + "eysCompare(";
code += (is_string ? key_getter : data_buffer + key_getter) + "(__offset(" + key_offset + ", " + data_buffer + (lang_.language == IDLOptions::kCSharp ? ".Length - o1.Value" : ".array().length - o1");
code += ", " + data_buffer + ", true)" + (is_string ? ", " + data_buffer : "") + "),\n ";
code += (is_string ? key_getter : data_buffer + key_getter) + "(__offset(" + key_offset + ", " + data_buffer + (lang_.language == IDLOptions::kCSharp ? ".Length - o2.Value" : ".array().length - o2");
code += ", " + data_buffer + ", true)" + (is_string ? ", " + data_buffer : "") + ")));\n";
if (lang_.language == IDLOptions::kJava) code += " for (int i = 0; i < off.length; i++) off[i] = offsets[i];\n";
code += " return builder." + FunctionStart('C') + "reateVectorOfTables(off" + (lang_.language == IDLOptions::kCSharp ? "sets" : "") + ");\n }\n";
code += "\n public static " + struct_def.name + " " + FunctionStart('L') + "ookupByKey(" + struct_def.name;
code += "[] tables, " + key_type + " key) {\n";
code += " int span = tables." + FunctionStart('L') + "ength, start = 0;\n";
code += " while (span != 0) {\n";
code += " int middle = span / 2;\n";
code += " " + struct_def.name + " table = tables[start + middle];\n";
code += " int comp = table." + FunctionStart('K') + "eyCompareWithValue(key);\n";
code += " if (comp > 0) span = middle;\n";
code += " else if (comp < 0) {\n";
code += " middle++;\n";
code += " start += middle;\n";
code += " span -= middle;\n";
code += " }\n";
code += " else return table;\n";
code += " }\n";
code += " return null;\n";
code += " }\n";
}
code += "}";
// Java does not need the closing semi-colon on class definitions.
code += (lang_.language != IDLOptions::kJava) ? ";" : "";

View File

@ -39,6 +39,19 @@ namespace FlatBuffers.Test
// better for performance.
var fbb = new FlatBufferBuilder(1);
StringOffset[] names = { fbb.CreateString("Frodo"), fbb.CreateString("Barney"), fbb.CreateString("Wilma") };
Offset<Monster>[] off = new Offset<Monster>[3];
Monster.StartMonster(fbb);
Monster.AddName(fbb, names[0]);
off[0] = Monster.EndMonster(fbb);
Monster.StartMonster(fbb);
Monster.AddName(fbb, names[1]);
off[1] = Monster.EndMonster(fbb);
Monster.StartMonster(fbb);
Monster.AddName(fbb, names[2]);
off[2] = Monster.EndMonster(fbb);
var sortMons = Monster.CreateMySortedTableVector(fbb, off);
// We set up the same values as monsterdata.json:
var str = fbb.CreateString("MyMonster");
@ -79,6 +92,7 @@ namespace FlatBuffers.Test
Monster.AddTest4(fbb, test4);
Monster.AddTestarrayofstring(fbb, testArrayOfString);
Monster.AddTestbool(fbb, false);
Monster.AddTestarrayoftables(fbb, sortMons);
var mon = Monster.EndMonster(fbb);
Monster.FinishMonsterBuffer(fbb, mon);
@ -102,6 +116,17 @@ namespace FlatBuffers.Test
// the mana field should retain its default value
Assert.AreEqual(monster.MutateMana((short)10), false);
Assert.AreEqual(monster.Mana, (short)150);
// Accessing a vector of sorted by the key tables
Monster[] monsters = { monster.GetTestarrayoftables(0), monster.GetTestarrayoftables(1), monster.GetTestarrayoftables(2) };
Assert.AreEqual(monster.GetTestarrayoftables(0).Name, "Barney");
Assert.AreEqual(monster.GetTestarrayoftables(1).Name, "Frodo");
Assert.AreEqual(monster.GetTestarrayoftables(2).Name, "Wilma");
// Example of searching for a table by the key
Assert.IsTrue(Monster.LookupByKey(monsters, "Frodo") != null);
Assert.IsTrue(Monster.LookupByKey(monsters, "Barney") != null);
Assert.IsTrue(Monster.LookupByKey(monsters, "Wilma") != null);
// testType is an existing field and mutating it should succeed
Assert.AreEqual(monster.TestType, Any.Monster);

View File

@ -51,6 +51,19 @@ class JavaTest {
// better for performance.
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
int[] names = {fbb.createString("Frodo"), fbb.createString("Barney"), fbb.createString("Wilma")};
int[] off = new int[3];
Monster.startMonster(fbb);
Monster.addName(fbb, names[0]);
off[0] = Monster.endMonster(fbb);
Monster.startMonster(fbb);
Monster.addName(fbb, names[1]);
off[1] = Monster.endMonster(fbb);
Monster.startMonster(fbb);
Monster.addName(fbb, names[2]);
off[2] = Monster.endMonster(fbb);
int sortMons = fbb.createSortedTableVector(Monster.class, off);
// We set up the same values as monsterdata.json:
int str = fbb.createString("MyMonster");
@ -84,6 +97,7 @@ class JavaTest {
Monster.addTestarrayofstring(fbb, testArrayOfString);
Monster.addTestbool(fbb, false);
Monster.addTesthashu32Fnv1(fbb, Integer.MAX_VALUE + 1L);
Monster.addTestarrayoftables(fbb, sortMons);
int mon = Monster.endMonster(fbb);
Monster.finishMonsterBuffer(fbb, mon);
@ -121,6 +135,17 @@ class JavaTest {
// the mana field should retain its default value
TestEq(monster.mutateMana((short)10), false);
TestEq(monster.mana(), (short)150);
// Accessing a vector of sorted by the key tables
Monster[] monsters = { monster.testarrayoftables(0), monster.testarrayoftables(1), monster.testarrayoftables(2) };
TestEq(monster.testarrayoftables(0).name(), "Barney");
TestEq(monster.testarrayoftables(1).name(), "Frodo");
TestEq(monster.testarrayoftables(2).name(), "Wilma");
// Example of searching for a table by the key
TestEq(Monster.lookupByKey(monsters, "Frodo").name(), "Frodo");
TestEq(Monster.lookupByKey(monsters, "Barney").name(), "Barney");
TestEq(Monster.lookupByKey(monsters, "Wilma").name(), "Wilma");
// testType is an existing field and mutating it should succeed
TestEq(monster.testType(), (byte)Any.Monster);

View File

@ -129,6 +129,33 @@ public sealed class Monster : Table {
return new Offset<Monster>(o);
}
public static void FinishMonsterBuffer(FlatBufferBuilder builder, Offset<Monster> offset) { builder.Finish(offset.Value, "MONS"); }
public static int KeysCompare(string o1, string o2) { return o1.CompareTo(o2); }
public int KeyCompareWithValue(string val) { return Name.CompareTo(val); }
public static VectorOffset CreateMySortedTableVector(FlatBufferBuilder builder, Offset<Monster>[] offsets) {
Array.Sort(offsets, (Offset<Monster> o1, Offset<Monster> o2) => KeysCompare(__string(__offset(10, builder.DataBuffer.Length - o1.Value, builder.DataBuffer, true), builder.DataBuffer),
__string(__offset(10, builder.DataBuffer.Length - o2.Value, builder.DataBuffer, true), builder.DataBuffer)));
return builder.CreateVectorOfTables(offsets);
}
public static Monster LookupByKey(Monster[] tables, string key) {
int span = tables.Length, start = 0;
while (span != 0) {
int middle = span / 2;
Monster table = tables[start + middle];
int comp = table.KeyCompareWithValue(key);
if (comp > 0) span = middle;
else if (comp < 0) {
middle++;
start += middle;
span -= middle;
}
else return table;
}
return null;
}
};

View File

@ -135,5 +135,35 @@ public final class Monster extends Table {
return o;
}
public static void finishMonsterBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset, "MONS"); }
public static int keysCompare(String o1, String o2) { return o1.compareTo(o2); }
public int keyCompareWithValue(String val) { return name().compareTo(val); }
public static int createMySortedTableVector(FlatBufferBuilder builder, ByteBuffer bb, int[] off) {
Integer[] offsets = new Integer[off.length];
for (int i = 0; i < off.length; i++) offsets[i] = off[i];
Arrays.sort(offsets, (Integer o1, Integer o2) -> keysCompare(__string(__offset(10, bb.array().length - o1, bb, true), bb),
__string(__offset(10, bb.array().length - o2, bb, true), bb)));
for (int i = 0; i < off.length; i++) off[i] = offsets[i];
return builder.createVectorOfTables(off);
}
public static Monster lookupByKey(Monster[] tables, String key) {
int span = tables.length, start = 0;
while (span != 0) {
int middle = span / 2;
Monster table = tables[start + middle];
int comp = table.keyCompareWithValue(key);
if (comp > 0) span = middle;
else if (comp < 0) {
middle++;
start += middle;
span -= middle;
}
else return table;
}
return null;
}
}