libstdc++
hash_eq_fn.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file hash_eq_fn.hpp
00038  * Contains 2 eqivalence functions, one employing a hash value,
00039  * and one ignoring it.
00040  */
00041 
00042 #ifndef PB_DS_HASH_EQ_FN_HPP
00043 #define PB_DS_HASH_EQ_FN_HPP
00044 
00045 #include <utility>
00046 #include <debug/debug.h>
00047 
00048 namespace __gnu_pbds
00049 {
00050   namespace detail
00051   {
00052     /// Primary template.
00053     template<typename Key, typename Eq_Fn, typename _Alloc, bool Store_Hash>
00054       struct hash_eq_fn;
00055 
00056     /// Specialization 1 - The client requests that hash values not be stored.
00057     template<typename Key, typename Eq_Fn, typename _Alloc>
00058     struct hash_eq_fn<Key, Eq_Fn, _Alloc, false> : public Eq_Fn
00059     {
00060       typedef Eq_Fn                        eq_fn_base;
00061       typedef typename _Alloc::template rebind<Key>::other key_allocator;
00062       typedef typename key_allocator::const_reference      key_const_reference;
00063 
00064       hash_eq_fn() { }
00065 
00066       hash_eq_fn(const Eq_Fn& r_eq_fn) : Eq_Fn(r_eq_fn) { }
00067 
00068       bool
00069       operator()(key_const_reference r_lhs_key, 
00070          key_const_reference r_rhs_key) const
00071       { return eq_fn_base::operator()(r_lhs_key, r_rhs_key); }
00072 
00073       void
00074       swap(const hash_eq_fn& other)
00075       { std::swap((Eq_Fn&)(*this), (Eq_Fn&)other); }
00076     };
00077 
00078 
00079     /// Specialization 2 - The client requests that hash values be stored.
00080     template<typename Key, class Eq_Fn, class _Alloc>
00081     struct hash_eq_fn<Key, Eq_Fn, _Alloc, true> : public Eq_Fn
00082     {
00083       typedef typename _Alloc::size_type           size_type;
00084       typedef Eq_Fn                        eq_fn_base;
00085       typedef typename _Alloc::template rebind<Key>::other key_allocator;
00086       typedef typename key_allocator::const_reference      key_const_reference;
00087 
00088       hash_eq_fn() { }
00089 
00090       hash_eq_fn(const Eq_Fn& r_eq_fn) : Eq_Fn(r_eq_fn) { }
00091 
00092       bool
00093       operator()(key_const_reference r_lhs_key, size_type lhs_hash, 
00094          key_const_reference r_rhs_key, size_type rhs_hash) const
00095       {
00096     _GLIBCXX_DEBUG_ASSERT(!eq_fn_base::operator()(r_lhs_key, r_rhs_key) 
00097                   || lhs_hash == rhs_hash);
00098 
00099     return (lhs_hash == rhs_hash && 
00100         eq_fn_base::operator()(r_lhs_key, r_rhs_key));
00101       }
00102 
00103       void
00104       swap(const hash_eq_fn& other)
00105       { std::swap((Eq_Fn&)(*this), (Eq_Fn&)(other)); }
00106     };
00107   } // namespace detail
00108 } // namespace __gnu_pbds
00109 
00110 #endif