I have a publisher-subscriber relationship I want to model. Publishers can have multiple subscribers, but each subscriber can only get data from a single publisher. I thought a map of vectors would be good for this:
std::map<publisher, std::vector<subscriber>>
Publisher lookup, insertion, and deletion is fast, subscriber insertion is fast, and getting all subscribers for a publisher is easy. But subscriber lookup and deletion from the map is cumbersome. It requires iterating all publishers until that subscriber is found. And I would still like an easy way of iterating through all subscribers, ideally without a double loop.
I'd like a container with these properties, where each operation is a single function call, or loop, where appropriate:
Is there a ready-made container that can do this or will I have to make a custom one?
I would suggest:
struct SubscriptionRecords {
std::unordered_map<publisher, std::unordered_set<subscriber>>
subscribers;
std::unordered_map<subscriber, publisher>
subscriptions;
};
Then some example methods:
void add_subscription(publisher p, subscriber s) {
auto res = self->subscriptions.insert(s);
assert(res->second); // At most one subscription.
self->subscribers[p].insert(s);
}
void remove_subscriber(subscriber s) {
auto sp = self->subscriptions.find(s);
if (sp != self->subscriptions.end()) {
self->subscribers[*sp].erase(s);
self->subscriptions.erase(sp);
}
}
and similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With