11573Srgrimes// -*- C++ -*-
21573Srgrimes
31573Srgrimes// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
41573Srgrimes//
51573Srgrimes// This file is part of the GNU ISO C++ Library.  This library is free
61573Srgrimes// software; you can redistribute it and/or modify it under the terms
71573Srgrimes// of the GNU General Public License as published by the Free Software
81573Srgrimes// Foundation; either version 2, or (at your option) any later
91573Srgrimes// version.
101573Srgrimes
111573Srgrimes// This library is distributed in the hope that it will be useful, but
121573Srgrimes// WITHOUT ANY WARRANTY; without even the implied warranty of
131573Srgrimes// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
141573Srgrimes// General Public License for more details.
151573Srgrimes
16251069Semaste// You should have received a copy of the GNU General Public License
171573Srgrimes// along with this library; see the file COPYING.  If not, write to
181573Srgrimes// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
191573Srgrimes// MA 02111-1307, USA.
201573Srgrimes
211573Srgrimes// As a special exception, you may use this file as part of a free
221573Srgrimes// software library without restriction.  Specifically, if other files
231573Srgrimes// instantiate templates or use macros or inline functions from this
241573Srgrimes// file, or you compile this file and link it with other files to
251573Srgrimes// produce an executable, this file does not by itself cause the
261573Srgrimes// resulting executable to be covered by the GNU General Public
271573Srgrimes// License.  This exception does not however invalidate any other
281573Srgrimes// reasons why the executable file might be covered by the GNU General
291573Srgrimes// Public License.
301573Srgrimes
311573Srgrimes// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
321573Srgrimes
331573Srgrimes// Permission to use, copy, modify, sell, and distribute this software
341573Srgrimes// is hereby granted without fee, provided that the above copyright
351573Srgrimes// notice appears in all copies, and that both that copyright notice
3686170Sobrien// and this permission notice appear in supporting documentation. None
3786170Sobrien// of the above authors, nor IBM Haifa Research Laboratories, make any
381573Srgrimes// representation about the suitability of this software for any
39121531Speter// purpose. It is provided "as is" without express or implied
40121531Speter// warranty.
411573Srgrimes
421573Srgrimes/**
431573Srgrimes * @file hash_eq_fn.hpp
441573Srgrimes * Contains 2 eqivalence functions, one employing a hash value,
451573Srgrimes *    and one ignoring it.
461573Srgrimes */
471573Srgrimes
481573Srgrimes#ifndef PB_DS_HASH_EQ_FN_HPP
491573Srgrimes#define PB_DS_HASH_EQ_FN_HPP
501573Srgrimes
511573Srgrimes#include <utility>
521573Srgrimes#include <debug/debug.h>
531573Srgrimes
541573Srgrimesnamespace pb_ds
55102809Srobert{
56102809Srobert  namespace detail
57102809Srobert  {
58102809Srobert    template<typename Key, class Eq_Fn, class Allocator, bool Store_Hash>
591573Srgrimes    struct hash_eq_fn;
60102809Srobert
611573Srgrimes#define PB_DS_CLASS_T_DEC \
62102809Srobert    template<typename Key, class Eq_Fn, class Allocator>
63102809Srobert
64102809Srobert#define PB_DS_CLASS_C_DEC \
651573Srgrimes    hash_eq_fn<Key, Eq_Fn, Allocator, false>
66102809Srobert
67102809Srobert    /**
681573Srgrimes     * Specialization 1- The client requests that hash values not be stored.
69102809Srobert     **/
701573Srgrimes    template<typename Key, class Eq_Fn, class Allocator>
711573Srgrimes    struct hash_eq_fn<Key, Eq_Fn, Allocator, false> : public Eq_Fn
7292889Sobrien    {
7392889Sobrien      typedef Eq_Fn eq_fn_base;
7492889Sobrien
751573Srgrimes      typedef typename Allocator::template rebind<Key>::other key_allocator;
761573Srgrimes
771573Srgrimes      typedef typename key_allocator::const_reference const_key_reference;
781573Srgrimes
791573Srgrimes      hash_eq_fn();
801573Srgrimes
811573Srgrimes      hash_eq_fn(const Eq_Fn& r_eq_fn);
821573Srgrimes
831573Srgrimes      inline bool
841573Srgrimes      operator()(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const;
851573Srgrimes
861573Srgrimes      inline void
871573Srgrimes      swap(const PB_DS_CLASS_C_DEC& other);
881573Srgrimes    };
89121531Speter
90121531Speter    PB_DS_CLASS_T_DEC
911573Srgrimes    PB_DS_CLASS_C_DEC::
921573Srgrimes    hash_eq_fn()
931573Srgrimes    { }
941573Srgrimes
95121531Speter    PB_DS_CLASS_T_DEC
961573Srgrimes    inline void
971573Srgrimes    PB_DS_CLASS_C_DEC::
981573Srgrimes    swap(const PB_DS_CLASS_C_DEC& other)
991573Srgrimes    { std::swap((Eq_Fn& )(*this), (Eq_Fn& )other); }
1001573Srgrimes
1011573Srgrimes    PB_DS_CLASS_T_DEC
1021573Srgrimes    PB_DS_CLASS_C_DEC::
1031573Srgrimes    hash_eq_fn(const Eq_Fn& r_eq_fn) :
1041573Srgrimes      Eq_Fn(r_eq_fn)
1051573Srgrimes    { }
1061573Srgrimes
1071573Srgrimes    PB_DS_CLASS_T_DEC
1081573Srgrimes    inline bool
1091573Srgrimes    PB_DS_CLASS_C_DEC::
1101573Srgrimes    operator()(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const
1111573Srgrimes    { return (eq_fn_base::operator()(r_lhs_key, r_rhs_key)); }
1121573Srgrimes
1131573Srgrimes#undef PB_DS_CLASS_T_DEC
1141573Srgrimes#undef PB_DS_CLASS_C_DEC
1151573Srgrimes
1161573Srgrimes#define PB_DS_CLASS_T_DEC \
117121531Speter    template<typename Key, class Eq_Fn, class Allocator>
118121531Speter
119121531Speter#define PB_DS_CLASS_C_DEC \
1201573Srgrimes    hash_eq_fn<Key, Eq_Fn, Allocator, true>
1211573Srgrimes
1221573Srgrimes    /**
1231573Srgrimes     * Specialization 2- The client requests that hash values be stored.
1241573Srgrimes     **/
1251573Srgrimes    template<typename Key, class Eq_Fn, class Allocator>
1261573Srgrimes    struct hash_eq_fn<Key, Eq_Fn, Allocator, true> :
1271573Srgrimes      public Eq_Fn
1281573Srgrimes    {
1291573Srgrimes      typedef typename Allocator::size_type size_type;
1301573Srgrimes
1311573Srgrimes      typedef Eq_Fn eq_fn_base;
1321573Srgrimes
1331573Srgrimes      typedef typename Allocator::template rebind<Key>::other key_allocator;
1341573Srgrimes
1351573Srgrimes      typedef typename key_allocator::const_reference const_key_reference;
1361573Srgrimes
1371573Srgrimes      hash_eq_fn();
138
139      hash_eq_fn(const Eq_Fn& r_eq_fn);
140
141      inline bool
142      operator()(const_key_reference r_lhs_key, size_type lhs_hash,
143		 const_key_reference r_rhs_key, size_type rhs_hash) const;
144
145      inline void
146      swap(const PB_DS_CLASS_C_DEC& other);
147    };
148
149    PB_DS_CLASS_T_DEC
150    PB_DS_CLASS_C_DEC::
151    hash_eq_fn()
152    { }
153
154    PB_DS_CLASS_T_DEC
155    PB_DS_CLASS_C_DEC::
156    hash_eq_fn(const Eq_Fn& r_eq_fn) :
157      Eq_Fn(r_eq_fn)
158    { }
159
160    PB_DS_CLASS_T_DEC
161    inline bool
162    PB_DS_CLASS_C_DEC::
163    operator()(const_key_reference r_lhs_key, size_type lhs_hash,
164	       const_key_reference r_rhs_key, size_type rhs_hash) const
165    {
166      _GLIBCXX_DEBUG_ASSERT(!eq_fn_base::operator()(r_lhs_key, r_rhs_key)
167		            || lhs_hash == rhs_hash);
168
169      return (lhs_hash == rhs_hash &&
170	      eq_fn_base::operator()(r_lhs_key, r_rhs_key));
171    }
172
173    PB_DS_CLASS_T_DEC
174    inline void
175    PB_DS_CLASS_C_DEC::
176    swap(const PB_DS_CLASS_C_DEC& other)
177    { std::swap((Eq_Fn& )(*this), (Eq_Fn& )(other)); }
178
179#undef PB_DS_CLASS_T_DEC
180#undef PB_DS_CLASS_C_DEC
181
182  } // namespace detail
183} // namespace pb_ds
184
185#endif
186