1 // std::hash and std::tr1::hash definitions -*- C++ -*-
3 // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
34 #ifdef __GXX_EXPERIMENTAL_CXX0X__
36 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
37 # define _GLIBCXX_END_NAMESPACE_TR1
39 #include <tr1/functional>
40 # define _GLIBCXX_BEGIN_NAMESPACE_TR1 namespace tr1 {
41 # define _GLIBCXX_END_NAMESPACE_TR1 }
46 _GLIBCXX_BEGIN_NAMESPACE_TR1
48 // For long double, careful with random padding bits (e.g., on x86,
49 // 10 bytes -> 12 bytes) and resort to frexp.
52 hash<long double>::operator()(long double __val) const
57 __val = std::frexp(__val, &__exponent);
58 __val = __val < 0.0l ? -(__val + 0.5l) : __val;
60 const long double __mult =
61 __gnu_cxx::__numeric_traits<size_t>::__max + 1.0l;
64 // Try to use all the bits of the mantissa (really necessary only
65 // on 32-bit targets, at least for 80-bit floating point formats).
66 const size_t __hibits = (size_t)__val;
67 __val = (__val - (long double)__hibits) * __mult;
69 const size_t __coeff =
70 __gnu_cxx::__numeric_traits<size_t>::__max / __LDBL_MAX_EXP__;
72 __result = __hibits + (size_t)__val + __coeff * __exponent;
77 #ifndef _GLIBCXX_LONG_DOUBLE_COMPAT_IMPL
80 hash<string>::operator()(string __s) const
81 { return _Fnv_hash<>::hash(__s.data(), __s.length()); }
85 hash<const string&>::operator()(const string& __s) const
86 { return _Fnv_hash<>::hash(__s.data(), __s.length()); }
88 #ifdef _GLIBCXX_USE_WCHAR_T
91 hash<wstring>::operator()(wstring __s) const
93 const char* __p = reinterpret_cast<const char*>(__s.data());
94 return _Fnv_hash<>::hash(__p, __s.length() * sizeof(wchar_t));
99 hash<const wstring&>::operator()(const wstring& __s) const
101 const char* __p = reinterpret_cast<const char*>(__s.data());
102 return _Fnv_hash<>::hash(__p, __s.length() * sizeof(wchar_t));
107 _GLIBCXX_END_NAMESPACE_TR1