C:/Users/Mike/Documents/Visual Studio Projects/GameEngine/src/GameObjectType.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 "GameObjectType.h"
00022 
00023 namespace GameEngine {
00024 
00025         unsigned long GameObjectType::HashName(char const * pTypeName)
00026         {
00027                 // largest prime smaller than 65536
00028                 unsigned long BASE = 65521L;
00029 
00030                 // NMAX is the largest n such that
00031                 // 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
00032                 unsigned long NMAX = 5552;
00033 
00034                 #define DO1(buf, i) {s1 += tolower(buf[i]); s2 += s1;}
00035                 #define DO2(buf, i) DO1(buf, i); DO1(buf, i+1);
00036                 #define DO4(buf, i) DO2(buf, i); DO2(buf, i+2);
00037                 #define DO8(buf, i) DO4(buf, i); DO4(buf, i+4);
00038                 #define DO16(buf)   DO8(buf, 0); DO8(buf, 8);
00039 
00040                 if (pTypeName == NULL) return NULL;
00041 
00042                 unsigned long s1 = 0;
00043                 unsigned long s2 = 0;
00044 
00045                 for (size_t len = strlen(pTypeName); len > 0; ) {
00046                         unsigned long k = (len < NMAX) ? len : NMAX;
00047                         len -= k;
00048                         while (k >= 16) {
00049                                 DO16(pTypeName);
00050                                 pTypeName += 16;
00051                                 k -= 16;
00052                         }
00053                         if (k != 0) {
00054                                 do {
00055                                         s1 += *pTypeName++;
00056                                         s2 += s1;
00057                                 } while (--k);
00058 
00059                                 s1 %= BASE;
00060                                 s2 %= BASE;
00061                         }
00062                 }
00063 
00064                 return (s2 << 16) | s1;
00065 
00066                 #undef DO1
00067                 #undef DO2
00068                 #undef DO4
00069                 #undef DO8
00070                 #undef DO16
00071         }
00072 
00073 }

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