mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-10 15:16:28 +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_ += "};";
|
||||
|
||||
|
||||
@@ -1291,7 +1291,9 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) {
|
||||
code += "); }\n";
|
||||
}
|
||||
}
|
||||
if (struct_def.has_key) {
|
||||
// Only generate key compare function for table,
|
||||
// because `key_field` is not set for struct
|
||||
if (struct_def.has_key && !struct_def.fixed) {
|
||||
if (lang_.language == IDLOptions::kJava) {
|
||||
code += "\n @Override\n protected int keysCompare(";
|
||||
code += "Integer o1, Integer o2, ByteBuffer _bb) {";
|
||||
|
||||
Reference in New Issue
Block a user