Can I serialize a map (essentially a vector of structs) in C++, and use it for lookup in Java?

869 views
Skip to first unread message

Bo Liu

unread,
Apr 14, 2016, 1:17:06 AM4/14/16
to FlatBuffers
Hi There,

I have a map<int64_t, int16_t> in C++, and want to serialize it into a sorted flatbuffer vector for binary search.
Is it possible to serialize it in C++, and use it for lookup in Java?
How about python?

Thanks,
Bo

mikkelfj

unread,
Apr 14, 2016, 6:52:04 AM4/14/16
to FlatBuffers
Any language API that does not support a find method in its api will be able to do efficient lookups in user code with a few lines of binary search. The main challenge is that the key lookup is in a separate vector from data array if use a map, but you just use the same index the data and key array.

mikkelfj

unread,
Apr 14, 2016, 6:57:53 AM4/14/16
to FlatBuffers
Or, it is normally done in vectors of structs where one element is the key, the other is data. Same principle. But in you case you would benefit from storing data in a separate vector to avoid padding 6 bytes for each element, but then I don't think the C++ API will help you create the sorting. In the end, all you need is to do is to store the already sorted data from your map.

Sorry for loosing half the words below.

Bo Liu

unread,
Apr 14, 2016, 3:06:07 PM4/14/16
to FlatBuffers
Good point!
I can serialize it into two separate vectors in C++, and do binary search manually in other languages.
I am assuming vector has efficient .size() and get(idx) api in all languages.

Wouter van Oortmerssen

unread,
Apr 18, 2016, 7:53:49 PM4/18/16
to Bo Liu, FlatBuffers
Yes, that can work too. Though really the best would be to port Lookup from C++.

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages