mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-09 06:30:54 +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
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user