nested containers with boost?

Hi, I would like an arbitrarily nested data structure, e.g. similar to std::vector< std::list<int> >, giving access to the elements of any of its subcontainers, but, at the same time, being a model of Container - in particular, having an iterator over all the elements in the last nesting level (int's in the previous example). Space/time efficiency is very important in my application. My solution would be to build a container adaptor similar to // vector of lists of ... of int typedef my_nested_container<std::vector, std::list, ..., int> nc_type; nc_type nc; (with type lists?) with an iterator adaptor that allows me to write something like: for (nc_type::iterator i = nc.begin(); i != nc.end(); ++i) // do smth with integer *i The nested container should also allow the user to modify each subcontainer and to iterate over the elements of a subcontainer, possibly without (much) overhead, e.g. // Iteration over the elements of one subcontainer for (nc_type::inner_iterator j = nc.inner_begin(); j != nc.inner_end(); ++j) // ... I have started to implement a prototype from scratch, but I am wondering whether I am reinventing the wheel. I think I can use boost::iterator_facade for my_nested_container::iterator and/or boost::pointer_container to build the nested structure internally (to minimize reallocation overhead upon dynamical change of the structure). Is there any other boost library I could take advantage from? I find that operations on a nested container are not completely trivial to implement (or even to define properly, think of add/remove in subcontainers which may themselves be nested containers) given its recursive nature, so I would be very glad to know that something like that is available off-the-shelf. I have found myself in need for such a data structure more often than not. Nicola

On 4/4/07, nicola <vitacolo@dimi.uniud.it> wrote:
Hi, I would like an arbitrarily nested data structure, e.g. similar to std::vector< std::list<int> >, giving access to the elements of any of its subcontainers, but, at the same time, being a model of Container - in particular, having an iterator over all the elements in the last nesting level (int's in the previous example). Space/time efficiency is very important in my application.
Nicola
Depending on why you need to have nested containers in the first place, you might want to look at Boost MultiIndex. It might be able to replace your nested containers completely. Otherwise, I'd look at making custom iterators for visitation, but I'm not sure things like insert() will make sense in all situations. Tony

In article <97ffb310704041346h4bb0ec9cq6c1edc7b943e64dc@mail.gmail.com>, "Gottlob Frege" <gottlobfrege@gmail.com> wrote:
On 4/4/07, nicola <vitacolo@dimi.uniud.it> wrote:
Hi, I would like an arbitrarily nested data structure, e.g. similar to std::vector< std::list<int> >, giving access to the elements of any of its subcontainers, but, at the same time, being a model of Container - in particular, having an iterator over all the elements in the last nesting level (int's in the previous example). Space/time efficiency is very important in my application.
Nicola
Depending on why you need to have nested containers in the first place, you might want to look at Boost MultiIndex. It might be able to replace your nested containers completely.
Thanks, I will take a deeper look at MultiIndex. My goal is to manage a partition of a collection of elements. The requirements are fast insertion/deletion into each class of the partition and fast iteration over both the elements of a single class and over all the elements of the collection (that must be amenable to be treated as a Container). Besides, in some cases, it may be convenient to maintain the elements within a class (not the whole collection) sorted by some key. Nicola
participants (2)
-
Gottlob Frege
-
nicola