Thread Tools Display Modes
11-20-05, 06:41 AM   #1
Elkano
A Flamescale Wyrmkin
 
Elkano's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 131
hashing strings to int

I'm looking for a function to hash strings (in my case player names) to intiger values from a specified range. The hash function should be sth like md5 wrt the hashing so that similar names won't normaly have close int values asigned (changing one char in the name should alter at least 1/4 of the ints bits, distributed over all bits). I'm going to use the int value as a base for a color so eg I'l have 24 values for the RGB channels I would hash to 24*24*24 = 13824 and then use mod/div to get the values for RGB.

Does anybody know of a good lua implementation of such a hash algorithem?
  Reply With Quote
11-20-05, 07:28 PM   #2
Littlejohn
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 90
Wow, that's an interesting way to assign colors. Lua hashes strings already as part of its shared/constant string implementation (i.e. "ab" is the same object as "a".."b"). I don't think you can get at the internal hash calculation.

Another way to assign colors is to simply stash people into a table by name, and then use an iterator to walk the names in hash order. Keep track of an index counter and you can optimally assign colors to use all of the RGB space. Every time you add a player though, the previous colors would be changed -- not sure if that's a feature for you.
  Reply With Quote
11-20-05, 07:33 PM   #3
noraj
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jul 2005
Posts: 102
lol blizz is gonna nerf it if you succeed....i think...if yer trying to get some way to uniquely identify mobs)

But omg that would be awesome
  Reply With Quote
11-21-05, 02:18 AM   #4
Elkano
A Flamescale Wyrmkin
 
Elkano's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 131
@noraj: that's not what I'm trying to do, sorry ^^ I think it wouldn't even be possible that way because the mobs all have the same name so hashing wouldn't change anything.

@littlejohn: that's a bit like the way I'm doing it now: if I get a name I dont know I randomly pick RGB values und store them in a table using the name as a key. But this gives me another color for the same name afer a reload of the UI. But I don't want to keep track of the random colors over sessions because I don't think it's needed. So finding a good funktion to assign the colors would be a much cleaner way.
Here is the addon I'm talking about: http://www.curse-gaming.com/mod.php?addid=1786
  Reply With Quote
12-08-05, 09:03 PM   #5
Aquendyn
A Deviate Faerie Dragon
Join Date: Sep 2005
Posts: 12
i just uploaded a md5 addon for 1.9. it's not my code, but i did fix it up a bit to work with WoW.
http://www.wowinterface.com/download...php?s=&id=4385
  Reply With Quote
12-09-05, 04:21 AM   #6
Elkano
A Flamescale Wyrmkin
 
Elkano's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 131
Thanks a lot, this will safe me a lot of work since I intended to implement md5 myself after 1.9 is released (haven't had te disk space to install 0.9 :/ )

As long as I use values based on 2 taking the lower value bits from the md5 hash should hold the thinks I wanted the hash function to have since I'm not changing the hash's bits (using values that are not a 2^x would do so since I would have to reasign those values that don't fit :/ ) but I think taht's a small price to pay
  Reply With Quote
12-09-05, 07:21 PM   #7
tardmrr
Lua Ninja
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 133
Elkano, if you are trying to color people's names in chat like many chat programs do, I think you will find that hashing is overkill (and possibly resource unfriendly). Randomize for each name once as your encounter it and store the result.
  Reply With Quote
12-10-05, 04:45 AM   #8
Elkano
A Flamescale Wyrmkin
 
Elkano's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 131
Well, the current version is doing exactly that for whispers:
If you send/recive a whisper from a 'new' person (based on session) it will asign a new color randomly and stor it in a table. The new version will do the same but instead of picling the color randomly it will use the names hash once. This shouldn't take to many resources compared to the random version but will keep the colors over sessions without having a öarg savedvariable with people you'll likely never talk to again and the colors will also be better spread when comparing similar names.

coloring chat channels would be an option but I don't see hat much use in it since there are a lot mor people in the channels than there are sending you whispers, so the colors would rather help you distinguish insted of help to identify.

For party/raid I've implemented a system assigning the class colors (also with caching) so there is an other coloring system in use ^^

Last edited by Elkano : 12-10-05 at 04:50 AM.
  Reply With Quote
12-10-05, 08:17 PM   #9
Aquendyn
A Deviate Faerie Dragon
Join Date: Sep 2005
Posts: 12
a 1600-letter string takes about 8ms to hash. 400 letters about 2ms. everything used by the hashing function is garbage-collectible.

to rephrase what Elkano just said. someone unimportant talks to you again, but you didn't save his info. however, because his hash will always be the same, you might recognize him just from the color. if it was random, the color will be different, making it harder to recognize him. you might save his color for this session, but you don't really care for him enough to save it into SavedVars.

the digest (hash) is 32 hex digits. you can use 8 letters each for red, green, blue, and alpha. like this:
PHP Code:
function htocol(hash)
    -- 
color values between 0 and 1
    local c
={};
    
local m=md5.ff; --local m=tonumber('ffffffff',16);
    
c.red=tonumber(strsub(hash,1,8),16)/m;
    
c.green=tonumber(strsub(hash,9,16),16)/m;
    
c.blue=tonumber(strsub(hash,17,24),16)/m;
    
c.alpha=tonumber(strsub(hash,25,32),16)/m;
    return 
c;
end 
  Reply With Quote
04-06-06, 07:19 PM   #10
Guillotine
A Cobalt Mageweaver
 
Guillotine's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 206
Any chance you are getting anywhere on this elkano? Your addon still seems to use math.random rather than the hash.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » hashing strings to int


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off