LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-07-2005, 05:28 PM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
Need help/advice on system


Heres what I need to do:

I need to implement a system for storing strings, and I need to be able to easily add and remove strings from it as fast and efficently as posssible. Also each string will have an associated id (unsigned long), and I need to be able to find a string by its id aswell. I can't use databases, so it all has to be done with files.

I hope Im explaining this well. Basically the interface would consist of 4 functions:

Code:
void AddString(string)
void RemoveString(id)
string findStringById(id)
id findIdByString(string)
Im having problems figuring out how the data would be organised. Any advise would be appreciated
 
Old 07-07-2005, 05:59 PM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 66
Are you aware of UNIX db api ? Try man db, just to see
It can works with flat-file format
 
Old 07-08-2005, 06:29 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you are using C++ and want to stay standard and portable, I'd suggest std::map and std::string, then make a wrapper class for the map:
Code:
//Off the top of my head; compiled, but not linked or run
#include <string>
#include <map>

class StringData
{
public:
	typedef unsigned long       id;
	typedef std::string         data;
	typedef std::map <id, data> storage;

		void
	AddString(data dData, id iId) //<- need to give the id also
	{ MapData.insert( MapData.end(), storage::value_type(iId, dData) ); }

		void
	RemoveString(id iId)
	{ MapData.erase(iId); }

		data
	findStringById(id iId)
	{ return MapData[iId]; }

		id
	findIdByString(data sString)
	{
	//This isn't like normal iteration because of no < or - operators
	storage::iterator DataPoint = MapData.begin();
	id Curr = 0;

	while (Curr < MapData.size())
	 {
	if (DataPoint->second == sString) return DataPoint->first;

	else
	  {
	DataPoint++;
	Curr++;
	  }
	 }

	return 0; //Default return
	}

private:
		storage
	MapData;
};
It was easier to just write something than try to explain what I meant. This isn't perfect, unless I got really lucky, but hopefully this gives you some ideas. If you go this route, you should wrap the size function and some of the other basic map functions. Take a look here:
http://www.sgi.com/tech/stl/table_of_contents.html
ta0kira

PS I just realized you might be talking about a file, therefore you could use the above plus an interface class to read/write the file. That I'm not sure about though.
 
Old 07-09-2005, 03:32 PM   #4
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
thanks, ta0kira, I'll look into that route
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File System Advice AxXium Linux From Scratch 3 08-02-2005 11:30 AM
Advice on building a new system downfall Linux - Hardware 2 04-19-2005 01:06 AM
Building AMD 64 system. I need advice. DeadPenguin Linux - Hardware 5 12-29-2004 07:12 AM
file system advice sought jagmandan Linux - General 2 07-01-2004 08:37 PM
Advice for trying Linux on old system bkonrad Linux - Newbie 1 02-10-2004 02:38 PM


All times are GMT -5. The time now is 07:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration