Panda3D
|
00001 // Filename: hashGenerator.cxx 00002 // Created by: drose (22Mar01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "hashGenerator.h" 00016 #include "primeNumberGenerator.h" 00017 00018 // We multiply each consecutive integer by the next prime number and 00019 // add it to the total. This will generate pretty evenly-distributed 00020 // hash numbers for an arbitrary sequence of ints. 00021 00022 // We do recycle the prime number table at some point, just to keep it 00023 // from growing insanely large, however (and to avoid wasting time 00024 // computing large prime numbers unnecessarily), and we also truncate 00025 // the result to the low-order 32 bits. 00026 00027 static const int max_prime_numbers = 10000; 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: HashGenerator::Constructor 00031 // Access: Public 00032 // Description: 00033 //////////////////////////////////////////////////////////////////// 00034 HashGenerator:: 00035 HashGenerator() { 00036 _hash = 0; 00037 _index = 0; 00038 } 00039 00040 //////////////////////////////////////////////////////////////////// 00041 // Function: HashGenerator::add_int 00042 // Access: Public 00043 // Description: Adds another integer to the hash so far. 00044 //////////////////////////////////////////////////////////////////// 00045 void HashGenerator:: 00046 add_int(int num) { 00047 nassertv(_index >= 0 && _index < max_prime_numbers); 00048 _hash += _primes[_index] * num; 00049 _index = (_index + 1) % max_prime_numbers; 00050 } 00051 00052 //////////////////////////////////////////////////////////////////// 00053 // Function: HashGenerator::add_string 00054 // Access: Public 00055 // Description: Adds a string to the hash, by breaking it down into a 00056 // sequence of integers. 00057 //////////////////////////////////////////////////////////////////// 00058 void HashGenerator:: 00059 add_string(const string &str) { 00060 add_int(str.length()); 00061 string::const_iterator si; 00062 for (si = str.begin(); si != str.end(); ++si) { 00063 add_int(*si); 00064 } 00065 } 00066 00067 //////////////////////////////////////////////////////////////////// 00068 // Function: HashGenerator::get_hash 00069 // Access: Public 00070 // Description: Returns the hash number generated. 00071 //////////////////////////////////////////////////////////////////// 00072 unsigned long HashGenerator:: 00073 get_hash() const { 00074 return (unsigned long)(_hash & 0xffffffff); 00075 }