/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef _DECAF_UTIL_HASHCODE_H_ #define _DECAF_UTIL_HASHCODE_H_ #include #include #include #include #include #include namespace decaf { namespace util { template struct HashCodeUnaryBase { public: typedef T argument_type; typedef int result_type; virtual ~HashCodeUnaryBase() {} }; /** * Base HashCode template, specializations are created from this to account for * the various native types. * * @since 1.0 */ template struct HashCode : HashCodeUnaryBase { public: int operator()(const T& arg) const { return arg.getHashCode(); } }; template struct HashCode : HashCodeUnaryBase { int operator()(const T& arg) const { return arg.getHashCode(); } }; template struct HashCode : public HashCodeUnaryBase { int operator()(const T* arg) const { if (arg != NULL) { return arg->getHashCode(); } return 0; } }; template struct HashCode : public HashCodeUnaryBase { int operator()(const T* arg) const { if (arg != NULL) { return arg->getHashCode(); } return 0; } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(bool arg) const { return arg ? 1231 : 1237; } }; #if defined(HAVE_WCHAR_T) template<> struct HashCode : public HashCodeUnaryBase { int operator()(unsigned char arg) const { return (int) arg; } }; #endif template<> struct HashCode : public HashCodeUnaryBase { int operator()(char arg) const { return (int) arg; } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(wchar_t arg) const { return (int) arg; } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(unsigned short arg) const { return (int) arg; } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(short arg) const { return (int) arg; } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(unsigned int arg) const { return (int) arg; } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(int arg) const { return arg; } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(unsigned long long arg) const { return (int) (arg ^ (arg >> 32)); } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(long long arg) const { return (int) (arg ^ ((unsigned long long) arg >> 32)); } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(float arg) const { return decaf::lang::Float::floatToIntBits(arg); } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(double arg) const { long long value = decaf::lang::Double::doubleToLongBits(arg); return (int) (value ^ ((unsigned long long) value >> 32)); } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(const std::string& arg) const { int h = 0; if (h == 0 && arg.length() > 0) { std::string::const_iterator iter = arg.begin(); for (; iter != arg.end(); ++iter) { h = 31 * h + (*iter); } } return h; } }; template<> struct HashCode : public HashCodeUnaryBase { int operator()(const std::string& arg) const { int h = 0; if (h == 0 && arg.length() > 0) { std::string::const_iterator iter = arg.begin(); for (; iter != arg.end(); ++iter) { h = 31 * h + (*iter); } } return h; } }; template struct HashCode< decaf::lang::Pointer > : public HashCodeUnaryBase > { int operator()(decaf::lang::Pointer arg) const { if (arg != NULL) { return HashCode()(*arg); } return 0; } }; }} #endif /* _DECAF_UTIL_HASHCODE_H_ */