mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +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
@@ -1943,6 +1943,24 @@ class CppGenerator : public BaseGenerator {
|
||||
code_ += " }";
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a comparison function for this field if it is a key.
|
||||
if (field.key) {
|
||||
code_ += " bool KeyCompareLessThan(const {{STRUCT_NAME}} *o) const {";
|
||||
code_ += " return {{FIELD_NAME}}() < o->{{FIELD_NAME}}();";
|
||||
code_ += " }";
|
||||
auto type = GenTypeBasic(field.value.type, false);
|
||||
if (parser_.opts.scoped_enums && field.value.type.enum_def &&
|
||||
IsScalar(field.value.type.base_type)) {
|
||||
type = GenTypeGet(field.value.type, " ", "const ", " *", true);
|
||||
}
|
||||
|
||||
code_.SetValue("KEY_TYPE", type);
|
||||
code_ += " int KeyCompareWithValue({{KEY_TYPE}} val) const {";
|
||||
code_ += " const auto key = {{FIELD_NAME}}();";
|
||||
code_ += " return static_cast<int>(key > val) - static_cast<int>(key < val);";
|
||||
code_ += " }";
|
||||
}
|
||||
}
|
||||
code_ += "};";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user