Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a hash table in a file?

How can I store a hash table with separate chaining in a file on disk?

Generating the data stored in the hash table at runtime is expensive, it would be faster to just load the HT from disk...if only I can figure out how to do it.

Edit: The lookups are done with the HT loaded in memory. I need to find a way to store the hashtable (in memory) to a file in some binary format. So that next time when the program runs it can just load the HT off disk into RAM.

I am using C++.

like image 979
Girish Avatar asked Feb 07 '09 18:02

Girish


People also ask

How do you store a hash table?

To store an element in the hash table you must insert it into a specific linked list. If there is any collision (i.e. two different elements have same hash value) then store both the elements in the same linked list. The cost of a lookup is that of scanning the entries of the selected linked list for the required key.

How are hash tables stored in memory?

The Key in Map is stored under given position of array (memory). The position is set by RunTime (not compiler), using algorithm that use transformed hashCode of object and array length. Time needed to retrieve element is O(1), that do not require any iteration.

Where is hash value stored?

A hash table stores key and value pairs in a list that is accessible through its index. Because key and value pairs are unlimited, the hash function will map the keys to the table size. A hash value then becomes the index for a specific element.

What is a good size for a hash table?

But a good general “rule of thumb” is: The hash table should be an array with length about 1.3 times the maximum number of keys that will actually be in the table, and. Size of hash table array should be a prime number.


2 Answers

What language are you using? The common method is to do some sort binary serialization.

Ok, I see you have edited to add the language. For C++ there a few options. I believe the Boost serialization mechanism is pretty good. In addition, the page for Boost's serialization library also describes alternatives. Here is the link:

http://www.boost.org/doc/libs/1_37_0/libs/serialization/doc/index.html

like image 52
BobbyShaftoe Avatar answered Oct 03 '22 20:10

BobbyShaftoe


Assuming C/C++: Use array indexes and fixed size structs instead of pointers and variable length allocations. You should be able to directly write() the data structures to file for later read()ing.

For anything higher-level: A lot of higher language APIs have serialization facilities. Java and Qt/C++ both have methods that sprint immediately to mind, so I know others do as well.

like image 32
Ryan Graham Avatar answered Oct 03 '22 18:10

Ryan Graham