LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 /* A hash table. | 5 /* A hash table. |
6 Example, hashing nul-terminated char*s: | 6 Example, hashing nul-terminated char*s: |
7 hash_hash_t str_hash (void *v) { | 7 hash_hash_t str_hash (void *v) { |
8 char *s; | 8 char *s; |
9 hash_hash_t hash = 0; | 9 hash_hash_t hash = 0; |
10 for (s = *(char **)v; *s != 0; s++) { | 10 for (s = *(char **)v; *s != 0; s++) { |
11 hash = (hash ^ *s) * 2654435769U; | 11 hash = (hash ^ *s) * 2654435769U; |
12 } | 12 } |
13 return (hash); | 13 return (hash); |
14 } | 14 } |
15 int str_eq (void *a, void *b) { | 15 int str_eq (void *a, void *b) { |
16 return (strcmp (*(char **)a, *(char **)b) == 0); | 16 return (strcmp (*(char **)a, *(char **)b) == 0); |
17 } | 17 } |
18 void str_del (void *arg, void *data) { | 18 void str_del (void *arg, void *data) { |
19 *(char **)arg = *(char **)data; | 19 *(char **)arg = *(char **)data; |
20 } | 20 } |
21 | 21 |
22 » struct Hmap *h = hash_new (sizeof (char *), &str_hash, &str_eq, &str_del
, 3, 12, 15); | 22 » struct hash *h = hash_new (sizeof (char *), &str_hash, &str_eq, &str_del
, 3, 12, 15); |
23 ... 3=> 2**3 entries initial size | 23 ... 3=> 2**3 entries initial size |
24 ... 12=> 2**12 entries before sprouting sub-tables | 24 ... 12=> 2**12 entries before sprouting sub-tables |
25 ... 15=> number of adjacent probes to attempt before growing | 25 ... 15=> number of adjacent probes to attempt before growing |
26 | 26 |
27 Example lookup: | 27 Example lookup: |
28 char *key = "foobar"; | 28 char *key = "foobar"; |
29 char **result_ptr; | 29 char **result_ptr; |
30 if (hash_lookup (h, &key, (void **) &result_ptr)) { | 30 if (hash_lookup (h, &key, (void **) &result_ptr)) { |
31 printf ("found in table: %s\n", *result_ptr); | 31 printf ("found in table: %s\n", *result_ptr); |
32 } else { | 32 } else { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // void hash_iter_init (struct hash *h, struct hash_iter *it); | 150 // void hash_iter_init (struct hash *h, struct hash_iter *it); |
151 | 151 |
152 /* Return the next used entry in the table which which *it was initialized. */ | 152 /* Return the next used entry in the table which which *it was initialized. */ |
153 // void *hash_next (struct hash_iter *it); | 153 // void *hash_next (struct hash_iter *it); |
154 | 154 |
155 /*---- test interface ----*/ | 155 /*---- test interface ----*/ |
156 /* Call (*data_visit) (arg, level, data) for every data entry in the table, | 156 /* Call (*data_visit) (arg, level, data) for every data entry in the table, |
157 whether used or not. "level" is the subtable level, 0 means first level. */ | 157 whether used or not. "level" is the subtable level, 0 means first level. */ |
158 /* TESTING ONLY: DO NOT USE THIS ROUTINE IN NORMAL CODE */ | 158 /* TESTING ONLY: DO NOT USE THIS ROUTINE IN NORMAL CODE */ |
159 // void hash_visit (struct hash *h, void (*data_visit) (void *arg, int32 level,
void *data), void *arg); | 159 // void hash_visit (struct hash *h, void (*data_visit) (void *arg, int32 level,
void *data), void *arg); |
LEFT | RIGHT |