1 /* $Id: HashCode.c,v 1.3 2004/12/15 11:27:03 mva Exp $ */
2 /* Hash functions for basic types.
3 Copyright (C) 2003 Michael van Acken
4
5 This module is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
9
10 This module is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with OOC. If not, write to the Free Software Foundation,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #include "HashCode.d"
21
22 #define SIZE_OF_HASH(x) (sizeof(x) == sizeof(HashCode__Hash))
23 #define AS_HASH(x) ((HashCode__Hash)(x))
24 #define COMBINE(x,y) ((x)^(y))
25
HashCode__Boolean(OOC_CHAR8 x)26 HashCode__Hash HashCode__Boolean(OOC_CHAR8 x) {
27 return (x != 0)+1;
28 }
29
HashCode__Real(OOC_REAL32 x)30 HashCode__Hash HashCode__Real(OOC_REAL32 x) {
31 union {
32 OOC_REAL32 real;
33 HashCode__Hash hash;
34 } p;
35
36 p.real = x;
37 return p.hash;
38 }
39
HashCode__LongReal(OOC_REAL64 x)40 HashCode__Hash HashCode__LongReal(OOC_REAL64 x) {
41 union {
42 OOC_REAL64 real;
43 HashCode__Hash hash[2];
44 } p;
45
46 p.real = x;
47 return COMBINE(p.hash[0], p.hash[1]);
48 }
49
HashCode__Set(OOC_UINT32 x)50 HashCode__Hash HashCode__Set(OOC_UINT32 x) {
51 return AS_HASH(x);
52 }
53
HashCode__Ptr(void * x)54 HashCode__Hash HashCode__Ptr(void* x) {
55 if (SIZE_OF_HASH(x)) {
56 return AS_HASH(x); /* this causes a warning on 64 bit systems */
57 } else {
58 union {
59 void* ptr;
60 HashCode__Hash hash[2];
61 } p;
62
63 p.ptr = x;
64 return COMBINE(p.hash[0], p.hash[1]);
65 }
66 }
67
HashCode__CharRegion(const OOC_CHAR8 * data,OOC_LEN data_0d,OOC_INT32 start,OOC_INT32 end)68 HashCode__Hash HashCode__CharRegion(const OOC_CHAR8 *data, OOC_LEN data_0d,
69 OOC_INT32 start, OOC_INT32 end) {
70 /* taken from Python's dist/src/Objects/stringobject.c */
71 register OOC_CHAR8 *p, *s;
72 register HashCode__Hash x;
73
74 s = (OOC_CHAR8 *)(data+end);
75 p = (OOC_CHAR8 *)(data+start);
76 if (p == s) {
77 return 0;
78 } else {
79 x = *p << 7;
80 while (p != s)
81 x = (1000003*x) ^ *p++;
82 return x ^ (end-start);
83 }
84 }
85
HashCode__LongCharRegion(const OOC_CHAR16 * data,OOC_LEN data_0d,OOC_INT32 start,OOC_INT32 end)86 HashCode__Hash HashCode__LongCharRegion(const OOC_CHAR16 *data,OOC_LEN data_0d,
87 OOC_INT32 start, OOC_INT32 end) {
88 /* taken from Python's dist/src/Objects/stringobject.c */
89 register OOC_CHAR16 *p, *s;
90 register HashCode__Hash x;
91
92 s = (OOC_CHAR16 *)(data+end);
93 p = (OOC_CHAR16 *)(data+start);
94 if (p == s) {
95 return 0;
96 } else {
97 x = *p << 7;
98 while (p != s)
99 x = (1000003*x) ^ *p++;
100 return x ^ (end-start);
101 }
102 }
103
HashCode__UCS4CharRegion(const OOC_CHAR32 * data,OOC_LEN data_0d,OOC_INT32 start,OOC_INT32 end)104 HashCode__Hash HashCode__UCS4CharRegion(const OOC_CHAR32 *data,OOC_LEN data_0d,
105 OOC_INT32 start, OOC_INT32 end) {
106 /* taken from Python's dist/src/Objects/stringobject.c */
107 register OOC_CHAR32 *p, *s;
108 register HashCode__Hash x;
109
110 s = (OOC_CHAR32 *)(data+end);
111 p = (OOC_CHAR32 *)(data+start);
112 if (p == s) {
113 return 0;
114 } else {
115 x = *p << 7;
116 while (p != s)
117 x = (1000003*x) ^ *p++;
118 return x ^ (end-start);
119 }
120 }
121
HashCode__Append(HashCode__Hash x,HashCode__Hash * hash)122 void HashCode__Append(HashCode__Hash x, HashCode__Hash *hash) {
123 *hash ^= x;
124 }
125
OOC_HashCode_init()126 void OOC_HashCode_init() {
127 }
128
OOC_HashCode_destroy(void)129 void OOC_HashCode_destroy(void) {
130 }
131