469,950 Members | 2,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Saving a map

Hi, I have a map and I want to save it to disk. What is the most efficient
method of doing this?
As i understand it, maps are stored as a binary tree. Is it balanced or is
this implementation dependant?
My first thoughts were to iterate through all the entries and just output
the string and number to a text file, but when its read in again, if it's
balanced then its going to have to do an awful lot of rebalancing as items
are re-added!
Regards

Michael

typedef unsigned long int BigNumber;
map<string,BigNumber> wordList;

Jul 22 '05 #1
3 1305
Ian
Michael wrote:
Hi, I have a map and I want to save it to disk. What is the most efficient
method of doing this?
As i understand it, maps are stored as a binary tree. Is it balanced or is
this implementation dependant?
My first thoughts were to iterate through all the entries and just output
the string and number to a text file, but when its read in again, if it's
balanced then its going to have to do an awful lot of rebalancing as items
are re-added!


So what? I'm sure your processor is faster than your drive.

If you iterate through the map, the contents will be in sorted order.

Do the simplest thing that can possibly work.

Ian
Jul 22 '05 #2
As Ian says, do it simple first. Don't prematurely optimize - you don't
even know (do you?) if the rebalancing on loading (from sorted keys)
will be a factor. As Ian says, disk IO will probably be much slower
than your CPU... If you don't *know* it is a problem, don't try to fix
it. Profile first.

If load time does become a factor, you could try something like
iterating through the map, saving off your string keys to a vector.
Perform a random shuffle on the vector, then iterate through it, saving
the string and the looked-up value from the map. However, with the
extra expense of copying N strings, shuffling N strings, then doing N
lookups on the map to get the keys... I'd have a hard time believing
that could be more efficient than letting the tree rebalance itself.

Jul 22 '05 #3
If there is a problem with speed, I would fix it when loading the data
instead. Reading the data as one big chunk, accepting them as being sorted.
Then do an algorithm that would help with the balancing e.g. setting the top
of the tree first, and then throw in the rest, but do not overengineer....

"Michael" <sl***********@hotmail.com> wrote in message
news:co**********@sparta.btinternet.com...
Hi, I have a map and I want to save it to disk. What is the most efficient
method of doing this?
As i understand it, maps are stored as a binary tree. Is it balanced or is
this implementation dependant?
My first thoughts were to iterate through all the entries and just output
the string and number to a text file, but when its read in again, if it's
balanced then its going to have to do an awful lot of rebalancing as items
are re-added!
Regards

Michael

typedef unsigned long int BigNumber;
map<string,BigNumber> wordList;

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by G-Factor | last post: by
1 post views Thread by Mahesh Devjibhai Dhola [MVP] | last post: by
2 posts views Thread by manning_news | last post: by
14 posts views Thread by Amitabh Deepak | last post: by
2 posts views Thread by =?Utf-8?B?bWFydGluMQ==?= | last post: by
27 posts views Thread by RobG | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.