Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2465 Discussions

accessing same data in the concurrent hash map using two different keys

Yumo_J_
Beginner
541 Views

We need to access same data from concurrent hash map using two different keys (say using user id OR using user name).

Currently we are achieving it by inserting data twice, each with different key. It is working, but is there a better way?

Thanks 

0 Kudos
5 Replies
Yumo_J_
Beginner
541 Views

can anyone or intel support help please. I am not liking idea or keeping duplicate entries in hashmap.

Thanks

 

0 Kudos
Yumo_J_
Beginner
541 Views

anyone? intel support??

 

0 Kudos
Alexei_K_Intel
Employee
541 Views
  • Is it possible that you have only user id or user name?
  • Is it possible that the same user have multiple ids or multiple ids are assigned to the same user?

​If the both statement are negative than you can insert structures of <id, name> and define your own HashCompare that implements equal with the following behavior:

  • returns true for <id, 0> and <id, X>
  • returns true for <0, name> and <X, name>
  • returns true for <id, name> and <id, name>
  • returns false for any other case

Where X any value, 0 is impossible value.

Than if you need to find something by id, find <id,0>. If by name, find <0, name>.

Regards,
Alex

0 Kudos
Yumo_J_
Beginner
541 Views

Alex,

Thanks for the suggestion. Each user has an unique id and a name. 

However, what about hash() function? the hash for <id, name> will be different from  <id, 0> or <0, name> hash and in this case equal() will not be called at all. Pls correct me if wrong but I assume equal() only gets called for hash collisions (same hash).  

Thanks

0 Kudos
Alexei_K_Intel
Employee
541 Views

You are right. I though about iterating over the container but it is not concurrent and it will be a linear search (that is not what is expected). Looks like you really need two maps.

0 Kudos
Reply