activemq-cpp-3.9.5
HashCode.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef _DECAF_UTIL_HASHCODE_H_
19#define _DECAF_UTIL_HASHCODE_H_
20
21#include <decaf/util/Config.h>
22
23#include <decaf/lang/Pointer.h>
24#include <decaf/lang/Float.h>
25#include <decaf/lang/Double.h>
26
27#include <string>
28#include <functional>
29
30namespace decaf {
31namespace util {
32
33 template<typename T>
35 public:
36
37 typedef T argument_type;
38 typedef int result_type;
39
40 virtual ~HashCodeUnaryBase() {}
41 };
42
49 template<typename T>
50 struct HashCode : HashCodeUnaryBase<const T&> {
51 public:
52 int operator()(const T& arg) const {
53 return arg.getHashCode();
54 }
55 };
56
57 template<typename T>
58 struct HashCode<const T> : HashCodeUnaryBase<const T&> {
59 int operator()(const T& arg) const {
60 return arg.getHashCode();
61 }
62 };
63
64 template<typename T>
66 int operator()(const T* arg) const {
67 if (arg != NULL) {
68 return arg->getHashCode();
69 }
70 return 0;
71 }
72 };
73
74 template<typename T>
76 int operator()(const T* arg) const {
77 if (arg != NULL) {
78 return arg->getHashCode();
79 }
80 return 0;
81 }
82 };
83
84 template<>
85 struct HashCode<bool> : public HashCodeUnaryBase<bool> {
86 int operator()(bool arg) const {
87 return arg ? 1231 : 1237;
88 }
89 };
90
91 #if defined(HAVE_WCHAR_T)
92 template<>
93 struct HashCode<unsigned char> : public HashCodeUnaryBase<unsigned char> {
94 int operator()(unsigned char arg) const {
95 return (int) arg;
96 }
97 };
98 #endif
99
100 template<>
101 struct HashCode<char> : public HashCodeUnaryBase<char> {
102 int operator()(char arg) const {
103 return (int) arg;
104 }
105 };
106
107 template<>
108 struct HashCode<wchar_t> : public HashCodeUnaryBase<wchar_t> {
109 int operator()(wchar_t arg) const {
110 return (int) arg;
111 }
112 };
113
114 template<>
115 struct HashCode<unsigned short> : public HashCodeUnaryBase<unsigned short> {
116 int operator()(unsigned short arg) const {
117 return (int) arg;
118 }
119 };
120
121 template<>
122 struct HashCode<short> : public HashCodeUnaryBase<short> {
123 int operator()(short arg) const {
124 return (int) arg;
125 }
126 };
127
128 template<>
129 struct HashCode<unsigned int> : public HashCodeUnaryBase<unsigned int> {
130 int operator()(unsigned int arg) const {
131 return (int) arg;
132 }
133 };
134
135 template<>
136 struct HashCode<int> : public HashCodeUnaryBase<int> {
137 int operator()(int arg) const {
138 return arg;
139 }
140 };
141
142 template<>
143 struct HashCode<unsigned long long> : public HashCodeUnaryBase<unsigned long long> {
144 int operator()(unsigned long long arg) const {
145 return (int) (arg ^ (arg >> 32));
146 }
147 };
148
149 template<>
150 struct HashCode<long long> : public HashCodeUnaryBase<long long> {
151 int operator()(long long arg) const {
152 return (int) ((unsigned long long) arg ^ ((unsigned long long) arg >> 32));
153 }
154 };
155
156 template<>
157 struct HashCode<float> : public HashCodeUnaryBase<float> {
158 int operator()(float arg) const {
160 }
161 };
162
163 template<>
164 struct HashCode<double> : public HashCodeUnaryBase<double> {
165 int operator()(double arg) const {
166 long long value = decaf::lang::Double::doubleToLongBits(arg);
167 return (int) ((unsigned long long) value ^ ((unsigned long long) value >> 32));
168 }
169 };
170
171 template<>
172 struct HashCode<std::string> : public HashCodeUnaryBase<const std::string&> {
173 int operator()(const std::string& arg) const {
174 int h = 0;
175 if (h == 0 && arg.length() > 0) {
176 std::string::const_iterator iter = arg.begin();
177 for (; iter != arg.end(); ++iter) {
178 h = 31 * h + (*iter);
179 }
180 }
181 return h;
182 }
183 };
184
185 template<>
186 struct HashCode<const std::string> : public HashCodeUnaryBase<const std::string&> {
187 int operator()(const std::string& arg) const {
188 int h = 0;
189 if (h == 0 && arg.length() > 0) {
190 std::string::const_iterator iter = arg.begin();
191 for (; iter != arg.end(); ++iter) {
192 h = 31 * h + (*iter);
193 }
194 }
195 return h;
196 }
197 };
198
199 template<typename T>
200 struct HashCode< decaf::lang::Pointer<T> > : public HashCodeUnaryBase<decaf::lang::Pointer<T> > {
202 if (arg != NULL) {
203 return HashCode<const T>()(*arg);
204 }
205 return 0;
206 }
207 };
208
209}}
210
211#endif /* _DECAF_UTIL_HASHCODE_H_ */
static long long doubleToLongBits(double value)
Returns a representation of the specified floating-point value according to the IEEE 754 floating-poi...
static int floatToIntBits(float value)
Returns a representation of the specified floating-point value according to the IEEE 754 floating-poi...
Decaf's implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition Pointer.h:53
#define NULL
Definition Config.h:33
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25
Definition ArrayPointer.h:432
int operator()(const T *arg) const
Definition HashCode.h:66
int operator()(bool arg) const
Definition HashCode.h:86
int operator()(char arg) const
Definition HashCode.h:102
int operator()(const T &arg) const
Definition HashCode.h:59
int operator()(const T *arg) const
Definition HashCode.h:76
int operator()(const std::string &arg) const
Definition HashCode.h:187
int operator()(decaf::lang::Pointer< T > arg) const
Definition HashCode.h:201
int operator()(double arg) const
Definition HashCode.h:165
int operator()(float arg) const
Definition HashCode.h:158
int operator()(int arg) const
Definition HashCode.h:137
int operator()(long long arg) const
Definition HashCode.h:151
int operator()(short arg) const
Definition HashCode.h:123
int operator()(const std::string &arg) const
Definition HashCode.h:173
int operator()(unsigned int arg) const
Definition HashCode.h:130
int operator()(unsigned long long arg) const
Definition HashCode.h:144
int operator()(unsigned short arg) const
Definition HashCode.h:116
int operator()(wchar_t arg) const
Definition HashCode.h:109
Base HashCode template, specializations are created from this to account for the various native types...
Definition HashCode.h:50
int operator()(const T &arg) const
Definition HashCode.h:52
Definition HashCode.h:34
virtual ~HashCodeUnaryBase()
Definition HashCode.h:40
int result_type
Definition HashCode.h:38
T argument_type
Definition HashCode.h:37
#define const
Definition zconf.h:198