C:/Users/Mike/Documents/Visual Studio Projects/GameEngine/src/EventType.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 Copyright (C) 2006 Michael Harnetty
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 -----------------------------------------------------------------------------
00019 */
00020 
00021 #include "EventType.h"
00022 
00023 namespace GameEngine {
00024         
00025         void* EventType::hash_name(const char* identStr)
00026         {
00027                 // Relatively simple hash of arbitrary text string into a
00028                 // 32-bit identifier Output value is
00029                 // input-valid-deterministic, but no guarantees are made
00030                 // about the uniqueness of the output per-input
00031                 //
00032                 // Input value is treated as lower-case to cut down on false
00033                 // separations cause by human mistypes. Sure, it could be
00034                 // construed as a programming error to mix up your cases, and
00035                 // it cuts down on permutations, but in Real World Usage
00036                 // making this text case-sensitive will likely just lead to
00037                 // Pain and Suffering.
00038                 //
00039                 // This code lossely based upon the adler32 checksum by Mark
00040                 // Adler and published as part of the zlib compression
00041                 // library sources.
00042 
00043                 // largest prime smaller than 65536
00044                 unsigned long BASE = 65521L;
00045 
00046                 // NMAX is the largest n such that 255n(n+1)/2 +
00047                 // (n+1)(BASE-1) <= 2^32-1
00048                 unsigned long NMAX = 5552;
00049 
00050                 #define DO1(buf,i)  {s1 += tolower(buf[i]); s2 += s1;}
00051                 #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
00052                 #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
00053                 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
00054                 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
00055 
00056                 if (identStr == NULL)
00057                         return NULL;
00058 
00059                 if ( strcmp( identStr, wildcardEventType ) == 0 )
00060                         return 0;
00061                 
00062                 unsigned long s1 = 0;
00063                 unsigned long s2 = 0;
00064 
00065                 for ( size_t len = strlen( identStr ); len > 0 ; ) {
00066                         unsigned long k = len < NMAX ? len : NMAX;
00067 
00068                         len -= k;
00069 
00070                         while (k >= 16) {
00071                                 DO16(identStr);
00072                                 identStr += 16;
00073                                 k -= 16;
00074                         }
00075                         
00076                         if (k != 0) {
00077                                 do {
00078                                         s1 += *identStr++;
00079                                         s2 += s1;
00080                                 } while (--k);
00081                         }
00082                         
00083                         s1 %= BASE;
00084                         s2 %= BASE;
00085                 }
00086 
00087                 #pragma warning(push)
00088                 #pragma warning(disable : 4312)
00089 
00090                 return reinterpret_cast<void *>( (s2 << 16) | s1 );
00091 
00092                 #pragma warning(pop)
00093                 #undef DO1
00094                 #undef DO2
00095                 #undef DO4
00096                 #undef DO8
00097                 #undef DO16
00098         }
00099 
00100 }

Generated on Thu Apr 19 09:47:02 2007 for GameEngine by  doxygen 1.5.2