Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

direct/src/dcparser/hashGenerator.cxx

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

Generated on Fri May 2 01:37:08 2003 for Direct by doxygen1.3