mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-05 04:58:57 +00:00
Support binary search for struct in cpp (#4245)
* Support binary search for struct in cpp CreateVectorOfSortedStruct is provided for convenience. * fix continuous-integration error * add generated files * compile Ability.cs in csharp test * compile Ability.cs in csharp * modify according to code review
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
89041a1686
commit
a5cc2092a6
@@ -134,12 +134,21 @@ flatbuffers::unique_ptr_t CreateFlatBufferTest(std::string &buffer) {
|
||||
// Create an array of sorted tables, can be used with binary search when read:
|
||||
auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3);
|
||||
|
||||
// Create an array of sorted structs,
|
||||
// can be used with binary search when read:
|
||||
std::vector<Ability> abilities;
|
||||
abilities.push_back(Ability(4, 40));
|
||||
abilities.push_back(Ability(3, 30));
|
||||
abilities.push_back(Ability(2, 20));
|
||||
abilities.push_back(Ability(1, 10));
|
||||
auto vecofstructs = builder.CreateVectorOfSortedStructs(&abilities);
|
||||
|
||||
// shortcut for creating monster with all fields set:
|
||||
auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
|
||||
Any_Monster, mlocs[1].Union(), // Store a union.
|
||||
testv, vecofstrings, vecoftables, 0, 0, 0, false,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 3.14159f, 3.0f, 0.0f,
|
||||
vecofstrings2);
|
||||
vecofstrings2, vecofstructs);
|
||||
|
||||
FinishMonsterBuffer(builder, mloc);
|
||||
|
||||
@@ -249,6 +258,18 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
|
||||
TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
|
||||
|
||||
// Test accessing a vector of sorted structs
|
||||
auto vecofstructs = monster->testarrayofsortedstruct();
|
||||
if (vecofstructs) { // not filled in monster_test.bfbs
|
||||
for (size_t i = 0; i < vecofstructs->size()-1; i++) {
|
||||
auto left = vecofstructs->Get(i);
|
||||
auto right = vecofstructs->Get(i+1);
|
||||
TEST_EQ(true, (left->KeyCompareLessThan(right)));
|
||||
}
|
||||
TEST_NOTNULL(vecofstructs->LookupByKey(3));
|
||||
TEST_EQ(static_cast<const Ability*>(nullptr), vecofstructs->LookupByKey(5));
|
||||
}
|
||||
|
||||
// Since Flatbuffers uses explicit mechanisms to override the default
|
||||
// compiler alignment, double check that the compiler indeed obeys them:
|
||||
// (Test consists of a short and byte):
|
||||
|
||||
Reference in New Issue
Block a user