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:
tianyapiaozi
2017-03-30 00:51:12 +08:00
committed by Wouter van Oortmerssen
parent 89041a1686
commit a5cc2092a6
20 changed files with 564 additions and 17 deletions

View File

@@ -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_ += "};";