1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004, 2005 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 along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 * This is a TR1 C++ Library header.
25 #ifndef _TR1_FUNCTIONAL
26 #define _TR1_FUNCTIONAL 1
28 #include "../functional"
29 #include <string> // for std::tr1::hash
35 template<typename _Tp>
36 class reference_wrapper
41 explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
44 reference_wrapper(const reference_wrapper<_Tp>& __inref):
45 _M_data(__inref._M_data)
49 operator=(const reference_wrapper<_Tp>& __inref)
51 _M_data = __inref._M_data;
56 { return this->get(); }
63 // Denotes a reference should be taken to a variable.
64 template<typename _Tp>
65 reference_wrapper<_Tp>
67 { return reference_wrapper<_Tp>(__t); }
69 // Denotes a const reference should be taken to a variable.
70 template<typename _Tp>
71 reference_wrapper<const _Tp>
73 { return reference_wrapper<const _Tp>(__t); }
75 template<typename _Tp>
76 reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t)
77 { return ref(__t.get()); }
79 template<typename _Tp>
80 reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t)
81 { return cref(__t.get()); }
84 // Definition of default hash function std::tr1::hash<>. The types for
85 // which std::tr1::hash<T> is defined is in clause 6.3.3. of the PDTR.
87 template <typename T> struct hash;
89 #define tr1_hashtable_define_trivial_hash(T) \
90 template <> struct hash<T> { \
91 std::size_t operator()(T val) { return static_cast<std::size_t>(val); } \
94 tr1_hashtable_define_trivial_hash(bool);
95 tr1_hashtable_define_trivial_hash(char);
96 tr1_hashtable_define_trivial_hash(signed char);
97 tr1_hashtable_define_trivial_hash(unsigned char);
98 tr1_hashtable_define_trivial_hash(wchar_t);
99 tr1_hashtable_define_trivial_hash(short);
100 tr1_hashtable_define_trivial_hash(int);
101 tr1_hashtable_define_trivial_hash(long);
102 tr1_hashtable_define_trivial_hash(unsigned short);
103 tr1_hashtable_define_trivial_hash(unsigned int);
104 tr1_hashtable_define_trivial_hash(unsigned long);
106 tr1_hashtable_define_trivial_hash(float);
107 tr1_hashtable_define_trivial_hash(double);
108 tr1_hashtable_define_trivial_hash(long double);
110 #undef tr1_hashtable_define_trivial_hash
112 template <typename T>
114 std::size_t operator()(T* p) const {
115 return reinterpret_cast<std::size_t>(p);
119 // ??? We can probably find a better hash function than this (i.e. one
120 // that vectorizes better and that produces a more uniform distribution).
122 // XXX String hash probably shouldn't be an inline member function,
123 // since it's nontrivial. Once we have the framework for TR1 .cc
124 // files, this should go in one.
127 struct hash<std::string>
129 std::size_t operator()(const std::string& s) const
131 std::size_t result = 0;
132 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
133 result = (result * 131) + *i;
139 struct hash<std::wstring>
141 std::size_t operator()(const std::wstring& s) const
143 std::size_t result = 0;
144 for (std::wstring::const_iterator i = s.begin(); i != s.end(); ++i)
145 result = (result * 131) + *i;