]> git.donarmstrong.com Git - lilypond.git/blob - flower/hash.cc
* Another grand 2003 update.
[lilypond.git] / flower / hash.cc
1 /*   
2   hash.cc --  implement various functions for hash tables.
3   
4   source file of the Flower Library
5   
6   (c)  1999--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include "string.hh"
10 #include "array.hh"
11 #include "dictionary.hh"
12
13
14 // Note: assumes long is at least 32 bits.
15 const unsigned long my_prime_list[] = 
16 {
17   5, 11, 23,                    // be a bit careful for short lists: we want reasonable mem usage.
18   53,         97,         193,       389,       769,
19   1543,       3079,       6151,      12289,     24593,
20   49157,      98317,      196613,    393241,    786433,
21   1572869,    3145739,    6291469,   12582917,  25165843,
22   50331653,   100663319,  201326611, 402653189u, 805306457u, 
23   1610612741u, 3221225473u, 4294967291u
24 };
25
26 unsigned long
27 prime_list (int idx)
28 {
29   return my_prime_list [idx];
30 }
31
32 unsigned int
33 string_hash (String s)
34 {
35   const char* str = s.to_str0 ();
36   unsigned int result = 0;
37   while (1) {
38     char c = *str++;
39     if (c == 0) break;
40       result += (result<<3) + c;
41   }
42   return result;
43 }
44
45
46 unsigned int
47 hash (unsigned int i)
48 {
49   return i;
50 }
51
52 unsigned int
53 int_hash (int i)
54 {
55   return (unsigned) i;
56 }
57