Panda3D
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 std::string &str) {
52  add_int(str.length());
53  std::string::const_iterator si;
54  for (si = str.begin(); si != str.end(); ++si) {
55  add_int(*si);
56  }
57 }
58 
59 /**
60  * Adds a blob to the hash, by breaking it down into a sequence of integers.
61  */
62 void HashGenerator::
63 add_blob(const vector_uchar &bytes) {
64  add_int(bytes.size());
65  vector_uchar::const_iterator bi;
66  for (bi = bytes.begin(); bi != bytes.end(); ++bi) {
67  add_int(*bi);
68  }
69 }
70 
71 /**
72  * Returns the hash number generated.
73  */
74 unsigned long HashGenerator::
75 get_hash() const {
76  return (unsigned long)(_hash & 0xffffffff);
77 }
void add_int(int num)
Adds another integer to the hash so far.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_string(const std::string &str)
Adds a string to the hash, by breaking it down into a sequence of integers.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_blob(const vector_uchar &bytes)
Adds a blob to the hash, by breaking it down into a sequence of integers.
unsigned long get_hash() const
Returns the hash number generated.