473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1462
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
1
by: Mahesh Devjibhai Dhola [MVP] | last post by:
Hi, User can adjust the size of columns of a table or the position of splitter in UI. We want these adjustments to be remembered when we restart the application. Currently, we are saving these...
2
by: manning_news | last post by:
Has anyone had a problem with Access 2003 not saving their coding? I've been using 2003 for a couple of months now and just this week noticed that some coding I'd done for a database was not there...
14
by: Amitabh Deepak | last post by:
Is there any way to check whether daylight saving is enabled on a linux machine?
2
by: Peder Y | last post by:
My code is something like this: --------------- Image img = Image.FromFile("somefile.bmp"); FileStream fStream = new FileStream("someBinaryFile.dat"); BinaryWriter bw = new...
4
by: John Kandell | last post by:
Hi, I posted this in the asp.net group, but didn't get a response. Maybe someone here can help me with this... --- Would someone be able to shed some light on what is the cost of saving a...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
2
by: =?Utf-8?B?bWFydGluMQ==?= | last post by:
Hi, All, My app picks ccurrent time on the PC to retrieve data from sql DB, the Sql data is always data with non-daylight saving. The app runs on PC with both daylight saving and non-daylight...
27
by: RobG | last post by:
I was investigating a function to determine whether daylight saving was being observed on a particular date (given the platform's regional settings) and came across a suggestion at merlyn.com to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.