开发者

Best way to store and hash <int, int> key (C++)

开发者 https://www.devze.com 2022-12-09 21:58 出处:网络
My goal is to create an efficient structure to store the most relevant entries of a matrix that would (in a world without memory limitations) be approximately 10^5 x 10^5 and filled with doubles. The

My goal is to create an efficient structure to store the most relevant entries of a matrix that would (in a world without memory limitations) be approximately 10^5 x 10^5 and filled with doubles. The matrix is symmetric, so it actually would contain only (10^10)/2 values.

I need to access entries many, many times in my simulation, so fast retrieval is critical.

To keep the structure manageable, I will delete members that are unlikely to be used开发者_如何学C. If the index is (int_x1, int_x2), I will often want to delete all pairs containing, e.g., x1.

What is the best structure or set of structures for this task? What is a good hash for two ints?

For portability, I would like to avoid Boost. I am currently using TR1's unordered_map elsewhere in the program. I was thinking of using unordered_map again with the key pair, but I'm not sure how I would be able to delete entries efficiently this way, and I don't know what a good hash function would look like.

I'm a beginning programmer, so please state the obvious.


If the data is going to be pretty sparse, you could use an array of hash tables.

hash_map<int,double> matrix[] = new hash_map<int,double>[10000];
for (int i = 0; i < 10000; i++) matrix[i] = new hash_map<int,double>();

Then to look up a value (x,y), you index the array with x and look up y in the hash table.

A few things to watch out for:

  • Deleting can get pretty expensive, as you have to iterate through a lot of the hash tables.
  • Total storage can grow as you delete/insert, you should trim() your hash_maps occasionally.
  • it should be easy to take advantage of symmetry.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号