472,371 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 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 1387
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.