mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-25 19:18:39 +00:00
Sorted Vector & binary search functionality.
Bug: 16659276 Tested: on Linux & Windows. Change-Id: Ie7a73810345fad4cf0a3ad03dfaa5464e3ed5ac8
This commit is contained in:
@@ -30,7 +30,7 @@ table Monster {
|
||||
pos:Vec3 (id: 0);
|
||||
hp:short = 100 (id: 2);
|
||||
mana:short = 150 (id: 1);
|
||||
name:string (id: 3, required);
|
||||
name:string (id: 3, required, key);
|
||||
color:Color = Blue (id: 6);
|
||||
inventory:[ubyte] (id: 5);
|
||||
friendly:bool = false (deprecated, priority: 1, id: 4);
|
||||
|
||||
@@ -125,6 +125,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
int16_t mana() const { return GetField<int16_t>(6, 150); }
|
||||
int16_t hp() const { return GetField<int16_t>(8, 100); }
|
||||
const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(10); }
|
||||
bool KeyCompareLessThan(const Monster *o) const { return *name() < *o->name(); }
|
||||
int KeyCompareWithValue(const char *val) const { return strcmp(name()->c_str(), val); }
|
||||
const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(14); }
|
||||
Color color() const { return static_cast<Color>(GetField<int8_t>(16, 8)); }
|
||||
Any test_type() const { return static_cast<Any>(GetField<uint8_t>(18, 0)); }
|
||||
|
||||
@@ -81,10 +81,19 @@ std::string CreateFlatBufferTest() {
|
||||
|
||||
// create monster with very few fields set:
|
||||
// (same functionality as CreateMonster below, but sets fields manually)
|
||||
flatbuffers::Offset<Monster> mlocs[3];
|
||||
auto fred = builder.CreateString("Fred");
|
||||
MonsterBuilder mb(builder);
|
||||
mb.add_name(fred);
|
||||
auto mloc2 = mb.Finish();
|
||||
auto barney = builder.CreateString("Barney");
|
||||
auto wilma = builder.CreateString("Wilma");
|
||||
MonsterBuilder mb1(builder);
|
||||
mb1.add_name(fred);
|
||||
mlocs[0] = mb1.Finish();
|
||||
MonsterBuilder mb2(builder);
|
||||
mb2.add_name(barney);
|
||||
mlocs[1] = mb2.Finish();
|
||||
MonsterBuilder mb3(builder);
|
||||
mb3.add_name(wilma);
|
||||
mlocs[2] = mb3.Finish();
|
||||
|
||||
// Create an array of strings:
|
||||
flatbuffers::Offset<flatbuffers::String> strings[2];
|
||||
@@ -92,12 +101,12 @@ std::string CreateFlatBufferTest() {
|
||||
strings[1] = builder.CreateString("fred");
|
||||
auto vecofstrings = builder.CreateVector(strings, 2);
|
||||
|
||||
// Create an array of tables:
|
||||
auto vecoftables = builder.CreateVector(&mloc2, 1);
|
||||
// Create an array of sorted tables, can be used with binary search when read:
|
||||
auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3);
|
||||
|
||||
// shortcut for creating monster with all fields set:
|
||||
auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
|
||||
Any_Monster, mloc2.Union(), // Store a union.
|
||||
Any_Monster, mlocs[1].Union(), // Store a union.
|
||||
testv, vecofstrings, vecoftables, 0);
|
||||
|
||||
FinishMonsterBuffer(builder, mloc);
|
||||
@@ -163,9 +172,15 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
||||
|
||||
// Example of accessing a vector of tables:
|
||||
auto vecoftables = monster->testarrayoftables();
|
||||
TEST_EQ(vecoftables->Length(), 1U);
|
||||
TEST_EQ(vecoftables->Length(), 3U);
|
||||
for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it)
|
||||
TEST_EQ(strcmp(it->name()->c_str(), "Fred"), 0);
|
||||
TEST_EQ(strlen(it->name()->c_str()) >= 4, true);
|
||||
TEST_EQ(strcmp(vecoftables->Get(0)->name()->c_str(), "Barney"), 0);
|
||||
TEST_EQ(strcmp(vecoftables->Get(1)->name()->c_str(), "Fred"), 0);
|
||||
TEST_EQ(strcmp(vecoftables->Get(2)->name()->c_str(), "Wilma"), 0);
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Barney"));
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
|
||||
|
||||
// Since Flatbuffers uses explicit mechanisms to override the default
|
||||
// compiler alignment, double check that the compiler indeed obeys them:
|
||||
|
||||
Reference in New Issue
Block a user