bimap: one to many

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

bimap: one to many

Boost - Users mailing list
Hello
I want a collection to store a relationship N to M like so:

Item of type A                 Item of type B
A1                                    B1,B2,B3
A2                                    B4
A3                                    B1

and I would like a convenient way to get from a A instance to the list of B instances, and vice versa, from a given B instance to the list of A instances.

At runtime, the A objects are created first, then a list of B objects is created.
Later, B objects get modified asynchronously and that modification need to update the relevant A instances

what container to use?

boost::bimap< unordered_set<A*> ,  unordered_set<B*> >

?

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: bimap: one to many

Boost - Users mailing list
On 13/03/2020 09:53, MM wrote:

> I want a collection to store a relationship N to M like so:
>
> Item of type A                 Item of type B
> A1                                    B1,B2,B3
> A2                                    B4
> A3                                    B1
>
> and I would like a convenient way to get from a A instance to the list
> of B instances, and vice versa, from a given B instance to the list of A
> instances.
>
> At runtime, the A objects are created first, then a list of B objects is
> created.
> Later, B objects get modified asynchronously and that modification need
> to update the relevant A instances
>
> what container to use?
>
> boost::bimap< unordered_set<A*> ,  unordered_set<B*> >
>
> ?

I don't think that'd work unless a B had some other means to find the
set that includes it.

A relatively simple method would be to have each B store a weak_ptr<A>
of its "owner".  Given that, there's a few different ways to model the
relationship:

   1. An external unordered_map<shared_ptr<A>, unordered_set<shared_ptr<B>>>
   2. An external unordered_set<shared_ptr<A>> and each A has a
unordered_set<shared_ptr<B>>
   3. One of the above but substitute vector for unordered_set if you do
care about the order.

(Which of these makes more sense depends on your intended ownership
relations and other factors.  Other things are also possible, of course.)

Given something like this, you can explore the whole graph from any one
node easily enough.
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: bimap: one to many

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
El 12/03/2020 a las 21:53, MM via Boost-users escribió:

> Hello
> I want a collection to store a relationship N to M like so:
>
> Item of type A                 Item of type B
> A1                                    B1,B2,B3
> A2                                    B4
> A3                                    B1
>
> and I would like a convenient way to get from a A instance to the list
> of B instances, and vice versa,
> from a given B instance to the list of A instances.
>
> At runtime, the A objects are created first, then a list of B objects
> is created.
> Later, B objects get modified asynchronously and that modification
> need to update the relevant
> A instances
>
> what container to use?
>
> boost::bimap< unordered_set<A*> , unordered_set<B*> >

As the relationship is N:M, you'll want something like

  boost::bimap< unordered_multiset_of<A*> , unordered_multiset_of<B*> >

Joaquín M López Muñoz

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users