Extend flat_buffer to support unordered_set (de)serialize (#6681)

* Extend flat_buffer to support unordered_set (de)serialize

Extend flat_buffer to support unordered_set (de)serialize

Description

Extend flat_buffer to support unordered_set (de)serialize

Testing

Added unit test in flat_buffers.cpp to validate the functionality:
1. Empty unordered_set (de)serialize
1. Non-Empty unordered_set (de)serialize
This commit is contained in:
Ata E Husain Bohra 2022-03-25 14:29:43 -07:00 committed by GitHub
parent 90983990df
commit 86e201001e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 0 deletions

View File

@ -19,6 +19,7 @@
*/
#include "flow/flat_buffers.h"
#include "flow/FileIdentifier.h"
#include "flow/UnitTest.h"
#include "flow/Arena.h"
#include "flow/serialize.h"
@ -26,6 +27,7 @@
#include <algorithm>
#include <iomanip>
#include <unordered_set>
#include <variant>
namespace detail {
@ -361,6 +363,7 @@ struct string_serialized_traits<Void> : std::true_type {
namespace unit_tests {
struct Y1 {
constexpr static FileIdentifier file_identifier = 338229;
int a;
template <class Archiver>
@ -369,6 +372,14 @@ struct Y1 {
}
};
struct Y1Hasher {
std::size_t operator()(const Y1& y) const noexcept { return std::hash<int>()(y.a); }
};
struct Y1Equal {
bool operator()(const Y1& l, const Y1& r) const { return l.a == r.a; }
};
struct Y2 {
int a;
std::variant<int> b;
@ -563,4 +574,43 @@ TEST_CASE("/flow/FlatBuffers/EmptyPreSerVectorRefs") {
return Void();
}
TEST_CASE("/flow/FlatBuffers/EmptyUnorderedSet") {
int kSize = deterministicRandom()->randomInt(0, 100);
Standalone<StringRef> msg =
ObjectWriter::toValue(std::vector<std::unordered_set<Y1, Y1Hasher, Y1Equal>>(kSize), Unversioned());
ObjectReader rd(msg.begin(), Unversioned());
std::vector<std::unordered_set<Y1, Y1Hasher, Y1Equal>> xs;
rd.deserialize(xs);
ASSERT(xs.size() == kSize);
for (const auto& x : xs) {
ASSERT(x.size() == 0);
}
return Void();
}
TEST_CASE("/flow/FlatBuffers/NonEmptyUnorderedSet") {
int kSize = deterministicRandom()->randomInt(0, 100);
std::vector<std::unordered_set<Y1, Y1Hasher, Y1Equal>> src;
std::unordered_set<Y1, Y1Hasher, Y1Equal> s;
for (int i = 0; i < kSize; i++) {
Y1 y;
y.a = i;
s.insert(y);
}
src.push_back(s);
Standalone<StringRef> msg = ObjectWriter::toValue(src, Unversioned());
ObjectReader rd(msg.begin(), Unversioned());
std::vector<std::unordered_set<Y1, Y1Hasher, Y1Equal>> xs;
rd.deserialize(xs);
ASSERT(xs.size() == 1);
ASSERT(xs[0].size() == kSize);
for (int i = 0; i < kSize; i++) {
Y1 y;
y.a = i;
ASSERT(xs[0].find(y) != xs[0].end());
}
return Void();
}
} // namespace unit_tests

View File

@ -35,6 +35,7 @@
#include <cstring>
#include <array>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include "flow/FileIdentifier.h"
#include "flow/ObjectSerializerTraits.h"
@ -250,6 +251,31 @@ struct vector_like_traits<std::set<Key, Compare, Allocator>> : std::true_type {
return v.begin();
}
};
template <class Key, class Hash, class KeyEqual, class Allocator>
struct vector_like_traits<std::unordered_set<Key, Hash, KeyEqual, Allocator>> : std::true_type {
using Vec = std::unordered_set<Key, Hash, KeyEqual, Allocator>;
using value_type = Key;
using iterator = typename Vec::const_iterator;
using insert_iterator = std::insert_iterator<Vec>;
template <class Context>
static size_t num_entries(const Vec& v, Context&) {
return v.size();
}
template <class Context>
static void reserve(Vec& v, size_t size, Context&) {
v.reserve(size);
}
template <class Context>
static insert_iterator insert(Vec& v, Context&) {
return std::inserter(v, v.end());
}
template <class Context>
static iterator begin(const Vec& v, Context&) {
return v.begin();
}
};
template <>
struct dynamic_size_traits<std::string> : std::true_type {

View File

@ -20,6 +20,7 @@
#ifndef FLOW_SERIALIZE_H
#define FLOW_SERIALIZE_H
#include <unordered_set>
#pragma once
#include <stdint.h>
@ -172,6 +173,13 @@ template <class T, class Allocator>
struct CompositionDepthFor<std::vector<T, Allocator>> : std::integral_constant<int, CompositionDepthFor<T>::value + 1> {
};
template <class Key, class Hash, class KeyEqual, class Allocator>
struct FileIdentifierFor<std::unordered_set<Key, Hash, KeyEqual, Allocator>> : ComposedIdentifierExternal<Key, 6> {};
template <class Key, class Hash, class KeyEqual, class Allocator>
struct CompositionDepthFor<std::unordered_set<Key, Hash, KeyEqual, Allocator>>
: std::integral_constant<int, CompositionDepthFor<Key>::value + 1> {};
template <class Archive, class T>
inline void save(Archive& ar, const std::vector<T>& value) {
ar << (int)value.size();