Can Boot MultiIndex help me?

Dear list, I needed a data structure such that: - a list of POD structs (with an Id and other fields) stays in contiguous memory. This is required because I need to pass this block of memory to a C function. - the order of this POD structs is irrelevant. - I need to access to each element from the Id quickly. So I implemented a class using std::map and std::vector; the vector contains the structs and the map associate between the ids and the vector position. The vector ensures C memory layout and the map gives the elements from the Id quickly. To delete an element I copy the last element in its position, update the map, and pop the vector back (of course there are some special cases, like deleting the last element... but you got the point) To add an element I push it back to the vector and update the map. It works fine, but a colleague mentioned that boost multiindex can do a similar job. I checked the documentation, but I am not sure I can build an index on a vector. Is that possible? Thanks. Your faithfully, Paolo

On Wed, 22 Jul 2015 15:31:00 +0200, Paolo Bolzoni
Dear list,
I needed a data structure such that: - a list of POD structs (with an Id and other fields) stays in contiguous memory. This is required because I need to pass this block of memory to a C function. - the order of this POD structs is irrelevant. - I need to access to each element from the Id quickly. [...]It works fine, but a colleague mentioned that boost multiindex can do a similar job. I checked the documentation, but I am not sure I can build an index on a vector. Is that possible? Thanks.
If you need to store the same elements in multiple containers, Boost.Multiindex is worth a look. However I'm afraid Boost.Multiindex doesn't meet your requirement of contiguous memory. While the random access index comes closest to a vector, the documentation states: "Random access indices do not provide memory contiguity" (see http://www.boost.org/doc/libs/1_58_0/libs/multi_index/doc/reference/rnd_indi...). Boris

No, it is not (unfortunately) As stated here: http://www.boost.org/doc/libs/1_58_0/libs/multi_index/doc/tutorial/indices.h... They do not provide memory contiguity, a property of std::vectors by which elements are stored adjacent to one another in a single block of memory. -----Original Message----- From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Paolo Bolzoni Sent: Wednesday, July 22, 2015 4:31 PM To: boost-users@lists.boost.org Subject: [Boost-users] Can Boot MultiIndex help me? Dear list, I needed a data structure such that: - a list of POD structs (with an Id and other fields) stays in contiguous memory. This is required because I need to pass this block of memory to a C function. - the order of this POD structs is irrelevant. - I need to access to each element from the Id quickly. So I implemented a class using std::map and std::vector; the vector contains the structs and the map associate between the ids and the vector position. The vector ensures C memory layout and the map gives the elements from the Id quickly. To delete an element I copy the last element in its position, update the map, and pop the vector back (of course there are some special cases, like deleting the last element... but you got the point) To add an element I push it back to the vector and update the map. It works fine, but a colleague mentioned that boost multiindex can do a similar job. I checked the documentation, but I am not sure I can build an index on a vector. Is that possible? Thanks. Your faithfully, Paolo _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users [Read More]http://feeds.feedburner.com/~r/sizmek-blog/~6/1 [http://www.sizmek.com/Sizmek.png]http://www.sizmek.com/
participants (3)
-
Boris Schäling
-
Ernest Zaslavsky
-
Paolo Bolzoni