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