From patchwork Fri Jun 11 16:00:25 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 55341 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id CE00E1007D1 for ; Sat, 12 Jun 2010 02:00:41 +1000 (EST) Received: (qmail 13925 invoked by alias); 11 Jun 2010 16:00:39 -0000 Received: (qmail 13908 invoked by uid 22791); 11 Jun 2010 16:00:38 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from vsmtp12.tin.it (HELO vsmtp12.tin.it) (212.216.176.206) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 11 Jun 2010 16:00:31 +0000 Received: from [192.168.0.4] (79.17.189.140) by vsmtp12.tin.it (8.5.113) id 4BC85B9705E95B1E; Fri, 11 Jun 2010 18:00:28 +0200 Message-ID: <4C125D99.4090109@oracle.com> Date: Fri, 11 Jun 2010 18:00:25 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100317 SUSE/3.0.4-1.1.1 Thunderbird/3.0.4 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: libstdc++ , Jonathan Wakely Subject: [v3] Add hash and hash X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi all, hi Jon, I'm about to commit this patch adding two missing std::hash specializations... Jon, skimming through unique_ptr.h I don't really understand why we are using std::tuple instead of the *much* simpler std::pair. Can you? I think that otherwise we should consider changing that, even if that means breaking the "ABI" for people already experimenting with the C++0x unique_ptr, because in the future it will be more and more difficult... If we can avoid using tuple in unique_ptr, I think can be made *much* smaller (it used to be very small), because I'm pretty sure we don't need the entire , only some bits, like functional_hash, and very little else. Tested x86_64-linux. Paolo. ///////////////////// 2010-06-11 Paolo Carlini * include/bits/shared_ptr.h (hash): Add. * include/bits/shared_ptr_base.h (hash<__shared_ptr>): Likewise. * include/bits/unique_ptr.h (hash): Likewise. * testsuite/20_util/shared_ptr/hash/1.cc: New. * testsuite/20_util/unique_ptr/hash/1.cc: Likewise. Index: include/bits/shared_ptr.h =================================================================== --- include/bits/shared_ptr.h (revision 160601) +++ include/bits/shared_ptr.h (working copy) @@ -514,6 +514,16 @@ std::forward<_Args>(__args)...); } + /// std::hash specialization for shared_ptr. + template + struct hash> + : public std::unary_function, size_t> + { + size_t + operator()(const shared_ptr<_Tp>& __s) const + { return std::hash<_Tp*>()(__s.get()); } + }; + // @} group pointer_abstractions _GLIBCXX_END_NAMESPACE Index: include/bits/unique_ptr.h =================================================================== --- include/bits/unique_ptr.h (revision 160601) +++ include/bits/unique_ptr.h (working copy) @@ -233,7 +233,7 @@ // [unique.ptr.runtime] // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 740 - omit specialization for array objects with a compile time length - template + template class unique_ptr<_Tp[], _Tp_Deleter> { typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type; @@ -444,6 +444,19 @@ const unique_ptr<_Up, _Up_Deleter>& __y) { return !(__x.get() < __y.get()); } + /// std::hash specialization for unique_ptr. + template + struct hash> + : public std::unary_function, size_t> + { + size_t + operator()(const unique_ptr<_Tp, _Tp_Deleter>& __u) const + { + typedef unique_ptr<_Tp, _Tp_Deleter> _UP; + return std::hash()(__u.get()); + } + }; + // @} group pointer_abstractions _GLIBCXX_END_NAMESPACE Index: include/bits/shared_ptr_base.h =================================================================== --- include/bits/shared_ptr_base.h (revision 160601) +++ include/bits/shared_ptr_base.h (working copy) @@ -1164,6 +1164,16 @@ std::forward<_Args>(__args)...); } + /// std::hash specialization for __shared_ptr. + template + struct hash<__shared_ptr<_Tp, _Lp>> + : public std::unary_function<__shared_ptr<_Tp, _Lp>, size_t> + { + size_t + operator()(const __shared_ptr<_Tp, _Lp>& __s) const + { return std::hash<_Tp*>()(__s.get()); } + }; + _GLIBCXX_END_NAMESPACE #endif // _SHARED_PTR_BASE_H Index: testsuite/20_util/unique_ptr/hash/1.cc =================================================================== --- testsuite/20_util/unique_ptr/hash/1.cc (revision 0) +++ testsuite/20_util/unique_ptr/hash/1.cc (revision 0) @@ -0,0 +1,48 @@ +// { dg-options "-std=gnu++0x" } + +// 2010-06-11 Paolo Carlini + +// Copyright (C) 2010 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + struct T { }; + + std::unique_ptr u0(new T); + std::hash> hu0; + std::hash::pointer> hp0; + + VERIFY( hu0(u0) == hp0(u0.get()) ); + + std::unique_ptr u1(new T[10]); + std::hash> hu1; + std::hash::pointer> hp1; + + VERIFY( hu1(u1) == hp1(u1.get()) ); +} + +int main() +{ + test01(); + return 0; +} Index: testsuite/20_util/shared_ptr/hash/1.cc =================================================================== --- testsuite/20_util/shared_ptr/hash/1.cc (revision 0) +++ testsuite/20_util/shared_ptr/hash/1.cc (revision 0) @@ -0,0 +1,48 @@ +// { dg-options "-std=gnu++0x" } + +// 2010-06-11 Paolo Carlini + +// Copyright (C) 2010 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + struct T { }; + + std::shared_ptr s0(new T); + std::hash> hs0; + std::hash hp0; + + VERIFY( hs0(s0) == hp0(s0.get()) ); + + std::__shared_ptr s1(new T); + std::hash> hs1; + std::hash hp1; + + VERIFY( hs1(s1) == hp1(s1.get()) ); +} + +int main() +{ + test01(); + return 0; +}