digitalmars.D - string hashing
I needed a way to hash a string, and found that the TypeInfo classes have a getHash() function. However, calling the function results in an access violation being thrown. This seems to happen for all types, not just strings. char[] name = "Jack"; uint nameHash = typeid(char[]).getHash(name); // throws here It's worrying that the built in method doesn't work. If it's not meant to be accessed by user code, it should be private. Otherwise, shouldn't it be fixed? It would be good to use in generic code such as templates. John.
Aug 24 2005
Ah, the getHash() function works if I pass it the address of an object. This seems inconsistent with the way things are generally expressed in D. Perhaps Phobos should have a template that makes hashing for non-object types less error-prone. template toHash(T) { uint toHash(T value) { return typeid(T).getHash(&value); } } "John C" <johnch_atms hotmail.com> wrote in message news:dej22p$1dih$1 digitaldaemon.com...I needed a way to hash a string, and found that the TypeInfo classes have a getHash() function. However, calling the function results in an access violation being thrown. This seems to happen for all types, not just strings. char[] name = "Jack"; uint nameHash = typeid(char[]).getHash(name); // throws here It's worrying that the built in method doesn't work. If it's not meant to be accessed by user code, it should be private. Otherwise, shouldn't it be fixed? It would be good to use in generic code such as templates. John.
Aug 24 2005
"John C" <johnch_atms hotmail.com> wrote in message news:dej2ne$1e4s$1 digitaldaemon.com...Ah, the getHash() function works if I pass it the address of an object.Thisseems inconsistent with the way things are generally expressed in D.PerhapsPhobos should have a template that makes hashing for non-object types less error-prone. template toHash(T) { uint toHash(T value) { return typeid(T).getHash(&value); } }It's that way because then it works completely generically without using templates (makes for more compact code). Your template solution on top of it is good.
Aug 24 2005