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

@@ -478,7 +478,7 @@ protected:
VectorOfAny();
uoffset_t length_;
private:
VectorOfAny(const VectorOfAny&);
};
@@ -1204,6 +1204,44 @@ FLATBUFFERS_FINAL_CLASS
return CreateVectorOfStructs(data(v), v.size());
}
/// @cond FLATBUFFERS_INTERNAL
template<typename T>
struct StructKeyComparator {
bool operator()(const T &a, const T &b) const {
return a.KeyCompareLessThan(&b);
}
private:
StructKeyComparator& operator= (const StructKeyComparator&);
};
/// @endcond
/// @brief Serialize a `std::vector` of structs into a FlatBuffer `vector`
/// in sorted order.
/// @tparam T The data type of the `std::vector` struct elements.
/// @param[in]] v A const reference to the `std::vector` of structs to
/// serialize into the buffer as a `vector`.
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template<typename T> Offset<Vector<const T *>> CreateVectorOfSortedStructs(
std::vector<T> *v) {
return CreateVectorOfSortedStructs(data(*v), v->size());
}
/// @brief Serialize an array of structs into a FlatBuffer `vector` in sorted
/// order.
/// @tparam T The data type of the struct array elements.
/// @param[in] v A pointer to the array of type `T` to serialize into the
/// buffer as a `vector`.
/// @param[in] len The number of elements to serialize.
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template<typename T> Offset<Vector<const T *>> CreateVectorOfSortedStructs(
T *v, size_t len) {
std::sort(v, v + len, StructKeyComparator<T>());
return CreateVectorOfStructs(v, len);
}
/// @cond FLATBUFFERS_INTERNAL
template<typename T>
struct TableKeyComparator {