S
Sebastian Karlsson
I'm creating an audio player and want its clients to be able to play a
certain sound in a certain category using an identifier of some sort.
The identifier corresponding to a certain sound will be linked to a
file using a xml file, and as a consequence I'm thinking the best
design is probably using a string as an identifier. For performance
reasons as well as to satisfy my curiosity of the subject I'm thinking
this might be an ideal candidate for string hashing. Now I have some
questions regarding this which I'm having troubles finding an answer
to:
I don't want the client of my player to really have to familiarize
himself to much with my string hashing. Simplified I'm thinking
something along the lines of audioplayer.Play( "boom" ) where Play is
Play( StringHash p_Identifier ). Since construction of the hash is
pretty expensive I'm thinking of having a private static hash table in
StringHash which would mantain the actual hashes, and then use the
memory address of passed argument as the key. Then I would just do a
fast lookup from the argument to see if there already is a hash that
is computed for that particular string. I'm not exactly an expert on
the particulars of string literals but from my experiments and from
what I've read their memory address should remain constant throughout
all the calls. Is this true in theory? Or atleast in practice?
If above is true there's still a very large problem remaining, users
passing strings that aren't string literals, for example
std::string::c_str() comes to mind. These aren't guaranteed to be
constant between calls to a StringHash constructor so I can't use the
memory address as a key to the value. I could create a constructor of
StringHash which accepts a std::string&, however that certainly
doesn't guarantee that the user doesn't use c_str(), and std::string
isn't really the only possible problem anyway. What really would help
me here would be if there's any neat way to guarantee that the client
only uses string literals for my StringHash( const char* )
constructor. Is there any?
Any feedback appreciated!
certain sound in a certain category using an identifier of some sort.
The identifier corresponding to a certain sound will be linked to a
file using a xml file, and as a consequence I'm thinking the best
design is probably using a string as an identifier. For performance
reasons as well as to satisfy my curiosity of the subject I'm thinking
this might be an ideal candidate for string hashing. Now I have some
questions regarding this which I'm having troubles finding an answer
to:
I don't want the client of my player to really have to familiarize
himself to much with my string hashing. Simplified I'm thinking
something along the lines of audioplayer.Play( "boom" ) where Play is
Play( StringHash p_Identifier ). Since construction of the hash is
pretty expensive I'm thinking of having a private static hash table in
StringHash which would mantain the actual hashes, and then use the
memory address of passed argument as the key. Then I would just do a
fast lookup from the argument to see if there already is a hash that
is computed for that particular string. I'm not exactly an expert on
the particulars of string literals but from my experiments and from
what I've read their memory address should remain constant throughout
all the calls. Is this true in theory? Or atleast in practice?
If above is true there's still a very large problem remaining, users
passing strings that aren't string literals, for example
std::string::c_str() comes to mind. These aren't guaranteed to be
constant between calls to a StringHash constructor so I can't use the
memory address as a key to the value. I could create a constructor of
StringHash which accepts a std::string&, however that certainly
doesn't guarantee that the user doesn't use c_str(), and std::string
isn't really the only possible problem anyway. What really would help
me here would be if there's any neat way to guarantee that the client
only uses string literals for my StringHash( const char* )
constructor. Is there any?
Any feedback appreciated!