1 /*
2 *
3 * Copyright (c) 2003 The Regents of the University of California. All
4 * rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31
32 /*
33 * Plese see comments in the hash.h for the source of this hash facility
34 */
35
36 #include "common.h"
37 #include "hash.h"
38
39 /* original function was for java, and used '>>>' in couple of places to stop
40 * the sign-extension of integers. since we're dealing with unsigned integers
41 * this does not affect us, so we simply use '>>' (well, that and the fact that
42 * C does not have '>>>' operator) */
43 unsigned long int
44 twang_hash(unsigned long int key)
45 {
46 key += ~(key << 15);
47 key ^= (key >> 10);
48 key += (key << 3);
49 key ^= (key >> 6);
50 key += ~(key << 11);
51 key ^= (key >> 16);
52
53 return key;
54 }
55
56 guint
57 hash_u32(gconstpointer key)
58 {
59 assert(key);
60 return (guint) twang_hash(* ((unsigned long *)key));
61 }
62
63 guint
64 hash_udp(gconstpointer key)
65 {
66 struct udp_info *u = (struct udp_info *) key;
67 uint32_t newkey = 0;
68
69 assert(u);
70
71 /* multiplication is fast. we dont care about overflows */
72 newkey = (uint32_t)(u->ip.s_addr) * u->port;
73
74 return (guint) twang_hash(newkey);
75 }
76
77 guint
78 hash_geoinfo(gconstpointer key)
79 {
80 geo_t *g = (geo_t *) key;
81 uint32_t newkey = 0;
82
83 assert(g);
84
85 // newkey = (g->x << 16) | g->y;
86
87 /** XXX: Is this change OK??? ***/
88 newkey = (uint32_t)((g->x * (1 << 16)) + g->y);
89
90 return (guint) twang_hash(newkey);
91 }
92
93 gboolean
94 hash_compare_u32(gconstpointer a, gconstpointer b)
95 {
96 assert(a);
97 assert(b);
98
99 return (*((ulong *)a) == *((ulong *)b));
100 }
101
102 gboolean
103 hash_compare_geoinfo(gconstpointer a, gconstpointer b)
104 {
105 geo_t *ga = (geo_t *) a;
106 geo_t *gb = (geo_t *) b;
107
108 assert(ga);
109 assert(gb);
110
111 return ((ga->x == gb->x) && (ga->y == gb->y));
112 }
113
114 gboolean
115 hash_compare_udp(gconstpointer a, gconstpointer b)
116 {
117 struct udp_info *ua = (struct udp_info *) a;
118 struct udp_info *ub = (struct udp_info *) b;
119
120 assert(ua);
121 assert(ub);
122
123 return ((ua->ip.s_addr == ub->ip.s_addr) && (ua->port == ub->port));
124 }
125
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.