Panda3D
 All Classes Functions Variables Enumerations
hashGenerator.cxx
1 // Filename: hashGenerator.cxx
2 // Created by: drose (22Mar01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "hashGenerator.h"
16 #include "primeNumberGenerator.h"
17 
18 // We multiply each consecutive integer by the next prime number and
19 // add it to the total. This will generate pretty evenly-distributed
20 // hash numbers for an arbitrary sequence of ints.
21 
22 // We do recycle the prime number table at some point, just to keep it
23 // from growing insanely large, however (and to avoid wasting time
24 // computing large prime numbers unnecessarily), and we also truncate
25 // the result to the low-order 32 bits.
26 
27 static const int max_prime_numbers = 10000;
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: HashGenerator::Constructor
31 // Access: Public
32 // Description:
33 ////////////////////////////////////////////////////////////////////
34 HashGenerator::
35 HashGenerator() {
36  _hash = 0;
37  _index = 0;
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: HashGenerator::add_int
42 // Access: Public
43 // Description: Adds another integer to the hash so far.
44 ////////////////////////////////////////////////////////////////////
45 void HashGenerator::
46 add_int(int num) {
47  nassertv(_index >= 0 && _index < max_prime_numbers);
48  _hash += _primes[_index] * num;
49  _index = (_index + 1) % max_prime_numbers;
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: HashGenerator::add_string
54 // Access: Public
55 // Description: Adds a string to the hash, by breaking it down into a
56 // sequence of integers.
57 ////////////////////////////////////////////////////////////////////
58 void HashGenerator::
59 add_string(const string &str) {
60  add_int(str.length());
61  string::const_iterator si;
62  for (si = str.begin(); si != str.end(); ++si) {
63  add_int(*si);
64  }
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: HashGenerator::get_hash
69 // Access: Public
70 // Description: Returns the hash number generated.
71 ////////////////////////////////////////////////////////////////////
72 unsigned long HashGenerator::
73 get_hash() const {
74  return (unsigned long)(_hash & 0xffffffff);
75 }
void add_string(const string &str)
Adds a string to the hash, by breaking it down into a sequence of integers.
void add_int(int num)
Adds another integer to the hash so far.
unsigned long get_hash() const
Returns the hash number generated.