Jump to content
  • Advertisement
Sign in to follow this  
Ranger_One

C++ Atom/String Identity class?

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, does anyone know of an example of said class type out there? Do I need to write one from scratch and post it up here? Basically I'm just talking about a huge index of unique strings with reference ids for each.

Share this post


Link to post
Share on other sites
Advertisement
Wouldn't something like the following do? (sorry if there are errors, it's a 2 min impl.)
#include <map>
#include <string>

class StringIdentity
{
typedef std::map<std::string, int> string_id_map_t;

string_id_map_t registry;
int nextId;

public:

StringIdentity() : nextId (0) {}

bool isRegistered(std::string key)
{
return registry.find(key) != registry.end();
}

int getId(std::string key)
{
int index;

string_id_map_t::iterator i = registry.find(key);

if (i == registry.end())
{
index = ++nextId;
registry.insert(string_id_map_t::value_type(key, nextId));
}
else
{
index = i->second;
}
return index;
}
};
edit: fixed a couple of errors caused by me using Java too much

[Edited by - lucky_monkey on August 16, 2005 3:08:05 AM]

Share this post


Link to post
Share on other sites
Yea, something simple like that would work. I was more wondering if an optimized class was floating about somewhere, perhaps using a hash table instead of the slower std::map option. It is not a big deal to make one, but it would be interesting to see how others would do it.

I was thinking off hand of an Identity class with static members for the table itself, and then just creating Identitys with a custom constructor. Of course testing for invalid identities would mean you'd have to have some kind of error condition, but it seemed an elegant enough plan.

Share this post


Link to post
Share on other sites
If you want a hash table solution, your compiler may ship with a hash_map class. For example, in MSVC 2003 or later it's in the stdext namespace in the hash_map header.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ranger_One
Yea, something simple like that would work. I was more wondering if an optimized class was floating about somewhere, perhaps using a hash table instead of the slower std::map option. It is not a big deal to make one, but it would be interesting to see how others would do it.
I used std::map to keep the implementation details of the class familiar, clear, and concise (as much as C++ can be either of the last two). I'm sure there are some very optimised hash map implementations out there [smile].

Since I'm lazy I'd just follow SiCrane's suggestion and use stdext::hash_map (or keep using std::map until profiling made it clear that std::map was a bottleneck...)
Quote:
I was thinking off hand of an Identity class with static members for the table itself, and then just creating Identitys with a custom constructor. Of course testing for invalid identities would mean you'd have to have some kind of error condition, but it seemed an elegant enough plan.
I'm not a big fan of static, but it's your program [smile].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!