[GEM-cvs] SF.net SVN: pd-gem:[3463] trunk/Gem/src/Pixes/pix_share_write.cpp
Brought to you by:
zmoelnig
From: <zmo...@us...> - 2010-09-06 10:58:39
|
Revision: 3463 http://pd-gem.svn.sourceforge.net/pd-gem/?rev=3463&view=rev Author: zmoelnig Date: 2010-09-06 10:58:33 +0000 (Mon, 06 Sep 2010) Log Message: ----------- hopefully better hashing algorithm Modified Paths: -------------- trunk/Gem/src/Pixes/pix_share_write.cpp Modified: trunk/Gem/src/Pixes/pix_share_write.cpp =================================================================== --- trunk/Gem/src/Pixes/pix_share_write.cpp 2010-09-05 17:32:16 UTC (rev 3462) +++ trunk/Gem/src/Pixes/pix_share_write.cpp 2010-09-06 10:58:33 UTC (rev 3463) @@ -13,9 +13,43 @@ #include "Base/GemException.h" #include <errno.h> +#include <stdio.h> CPPEXTERN_NEW_WITH_GIMME(pix_share_write) +#if 0 + ; +#endif + +int hash_str2us(std::string s) { + /* + def self.rs( str, len=str.length ) + a,b = 63689,378551 + hash = 0 + len.times{ |i| + hash = hash*a + str[i] + a *= b + } + hash & SIGNEDSHORT + end + */ + + int result=0; + int a=63689; + int b=378551; + + int i=0; + + if(s.length()<1)return -1; + + for(i=0; i<s.length(); i++) { + result=result*a+s[i]; + a *= b; + } + + return ((unsigned short)(result) & 0x7FFFFFFF); +} + ///////////////////////////////////////////////////////// // Constructor // @@ -105,22 +139,12 @@ if(shm_id>0)freeShm(); #endif if(A_FLOAT==argv->a_type){ - fake=atom_getint(argv); + char buf[MAXPDSTRING]; + snprintf(buf, MAXPDSTRING-1, "%g", atom_getfloat(argv)); + buf[MAXPDSTRING-1]=0; + fake = hash_str2us(buf); } else if(A_SYMBOL==argv->a_type){ - char*s=atom_getsymbol(argv)->s_name; - char c=0; - while((c=*s++)) - { - int x=0; - if(c>96)x=c-96; - else if(c>64)x=c-64; - else if(c>48)x=c-48; - if(x<0)x=0; - if(x>26)x=26; - - fake*=26; - fake+=x; - } + fake = hash_str2us(atom_getsymbol(argv)->s_name); } if(fake<=0)return 8; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |