LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need help/advice on system (http://www.linuxquestions.org/questions/programming-9/need-help-advice-on-system-341057/)

nodger 07-07-2005 05:28 PM

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

keefaz 07-07-2005 05:59 PM

Are you aware of UNIX db api ? Try man db, just to see
It can works with flat-file format

ta0kira 07-08-2005 06:29 AM

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.

nodger 07-09-2005 03:32 PM

thanks, ta0kira, I'll look into that route


All times are GMT -5. The time now is 11:43 PM.