Data structure for topic list of clients
I need a data structure with O(1) add, find, and delete operations for a topic subscribed by a list of clients.
Some of the functions it needs to support are: isTopicExists, isClientExists, getClientsForTopic, addClientForTopic, removeClientForTopic, and getTopicsForClient.
Given a topic name, a client id that we can assume to be unique, and client pointe开发者_StackOverflow社区r, what is the best data structure to use? What implementations are available?
A hash map may seem not a bad idea. Its expected complexity is O(1) but a pessimistic scenario with many collisions can get you to O(n) depending on how chaining is implemented. A logarithmic search will be hard to beat here, so I would go for a self-balancing binary search tree, even std::map (red-black tree in most STL implementations). The only way of making it more efficient is using a vector (an array), but only as long as your IDs are small or offseted but close to each other. You can't beat maths here.
精彩评论