OSDN Git Service

* include/tr1/functional (hash): New function object.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1 / functional
1 // TR1 functional header -*- C++ -*-
2
3 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
21 /** @file 
22  *  This is a TR1 C++ Library header. 
23  */
24
25 #ifndef _TR1_FUNCTIONAL
26 #define _TR1_FUNCTIONAL 1
27
28 #include "../functional"
29 #include <string>               // for std::tr1::hash
30
31 namespace std
32 {
33 namespace tr1
34 {
35   template<typename _Tp>
36     class reference_wrapper
37     {
38       _Tp* _M_data;
39     public:
40       typedef _Tp type;
41       explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
42       { }
43     
44       reference_wrapper(const reference_wrapper<_Tp>& __inref):
45       _M_data(__inref._M_data)
46       { }
47
48       reference_wrapper& 
49       operator=(const reference_wrapper<_Tp>& __inref)
50       {
51         _M_data = __inref._M_data;
52         return *this;
53       }
54       
55       operator _Tp&() const
56       { return this->get(); }
57     
58       _Tp&
59       get() const
60       { return *_M_data; }
61     };
62   
63   // Denotes a reference should be taken to a variable.
64   template<typename _Tp>
65     reference_wrapper<_Tp>
66     ref(_Tp& __t)
67     { return reference_wrapper<_Tp>(__t); }
68   
69   // Denotes a const reference should be taken to a variable.
70   template<typename _Tp>
71     reference_wrapper<const _Tp>
72     cref(const _Tp& __t)
73     { return reference_wrapper<const _Tp>(__t); }
74
75   template<typename _Tp>
76     reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t)
77     { return ref(__t.get()); }
78
79   template<typename _Tp>
80     reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t)
81     { return cref(__t.get()); }
82
83
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.
86
87   template <typename T> struct hash;
88
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); } \
92     }                                                                       \
93
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);
105
106   tr1_hashtable_define_trivial_hash(float);
107   tr1_hashtable_define_trivial_hash(double);
108   tr1_hashtable_define_trivial_hash(long double);
109
110   #undef tr1_hashtable_define_trivial_hash
111
112   template <typename T>
113     struct hash<T*> {
114       std::size_t operator()(T* p) const {
115         return reinterpret_cast<std::size_t>(p);
116       }
117     };
118
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).
121
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.
125
126   template <>
127     struct hash<std::string>
128     {
129       std::size_t operator()(const std::string& s) const
130       {
131         std::size_t result = 0;
132         for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
133           result = (result * 131) + *i;
134         return result;
135       }
136     };
137
138   template <>
139     struct hash<std::wstring>
140     {
141       std::size_t operator()(const std::wstring& s) const
142       {
143         std::size_t result = 0;
144         for (std::wstring::const_iterator i = s.begin(); i != s.end(); ++i)
145           result = (result * 131) + *i;
146         return result;
147       }
148     };
149
150 }
151 }
152
153 #endif