hash_eq_fn.hpp revision 169691
164562Sgshapiro// -*- C++ -*-
264562Sgshapiro
364562Sgshapiro// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
464562Sgshapiro//
564562Sgshapiro// This file is part of the GNU ISO C++ Library.  This library is free
664562Sgshapiro// software; you can redistribute it and/or modify it under the terms
764562Sgshapiro// of the GNU General Public License as published by the Free Software
864562Sgshapiro// Foundation; either version 2, or (at your option) any later
964562Sgshapiro// version.
1064562Sgshapiro
1164562Sgshapiro// This library is distributed in the hope that it will be useful, but
12132943Sgshapiro// WITHOUT ANY WARRANTY; without even the implied warranty of
13132943Sgshapiro// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1464562Sgshapiro// General Public License for more details.
1590792Sgshapiro
1690792Sgshapiro// You should have received a copy of the GNU General Public License
1790792Sgshapiro// along with this library; see the file COPYING.  If not, write to
1864562Sgshapiro// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
1990792Sgshapiro// MA 02111-1307, USA.
2090792Sgshapiro
2190792Sgshapiro// As a special exception, you may use this file as part of a free
2290792Sgshapiro// software library without restriction.  Specifically, if other files
23132943Sgshapiro// instantiate templates or use macros or inline functions from this
24132943Sgshapiro// file, or you compile this file and link it with other files to
2590792Sgshapiro// produce an executable, this file does not by itself cause the
26132943Sgshapiro// resulting executable to be covered by the GNU General Public
27132943Sgshapiro// License.  This exception does not however invalidate any other
28132943Sgshapiro// reasons why the executable file might be covered by the GNU General
2990792Sgshapiro// Public License.
30132943Sgshapiro
31132943Sgshapiro// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
3290792Sgshapiro
33132943Sgshapiro// Permission to use, copy, modify, sell, and distribute this software
34132943Sgshapiro// is hereby granted without fee, provided that the above copyright
35132943Sgshapiro// notice appears in all copies, and that both that copyright notice
3664562Sgshapiro// and this permission notice appear in supporting documentation. None
3764562Sgshapiro// of the above authors, nor IBM Haifa Research Laboratories, make any
3864562Sgshapiro// representation about the suitability of this software for any
3964562Sgshapiro// purpose. It is provided "as is" without express or implied
4064562Sgshapiro// warranty.
4164562Sgshapiro
4264562Sgshapiro/**
4364562Sgshapiro * @file hash_eq_fn.hpp
44110560Sgshapiro * Contains 2 eqivalence functions, one employing a hash value,
4564562Sgshapiro *    and one ignoring it.
4664562Sgshapiro */
4764562Sgshapiro
4864562Sgshapiro#ifndef PB_DS_HASH_EQ_FN_HPP
4964562Sgshapiro#define PB_DS_HASH_EQ_FN_HPP
5090792Sgshapiro
5190792Sgshapiro#include <utility>
5264562Sgshapiro#include <debug/debug.h>
5364562Sgshapiro
5464562Sgshapironamespace pb_ds
5564562Sgshapiro{
5664562Sgshapiro  namespace detail
5764562Sgshapiro  {
5864562Sgshapiro    template<typename Key, class Eq_Fn, class Allocator, bool Store_Hash>
5973188Sgshapiro    struct hash_eq_fn;
6073188Sgshapiro
6173188Sgshapiro#define PB_DS_CLASS_T_DEC \
6273188Sgshapiro    template<typename Key, class Eq_Fn, class Allocator>
6364562Sgshapiro
6473188Sgshapiro#define PB_DS_CLASS_C_DEC \
6564562Sgshapiro    hash_eq_fn<Key, Eq_Fn, Allocator, false>
6664562Sgshapiro
6764562Sgshapiro    /**
6864562Sgshapiro     * Specialization 1- The client requests that hash values not be stored.
6964562Sgshapiro     **/
7064562Sgshapiro    template<typename Key, class Eq_Fn, class Allocator>
7164562Sgshapiro    struct hash_eq_fn<Key, Eq_Fn, Allocator, false> : public Eq_Fn
7264562Sgshapiro    {
7364562Sgshapiro      typedef Eq_Fn eq_fn_base;
7480785Sgshapiro
7564562Sgshapiro      typedef typename Allocator::template rebind<Key>::other key_allocator;
7664562Sgshapiro
7764562Sgshapiro      typedef typename key_allocator::const_reference const_key_reference;
7864562Sgshapiro
7964562Sgshapiro      hash_eq_fn();
8064562Sgshapiro
8180785Sgshapiro      hash_eq_fn(const Eq_Fn& r_eq_fn);
8264562Sgshapiro
8364562Sgshapiro      inline bool
8464562Sgshapiro      operator()(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const;
8564562Sgshapiro
8664562Sgshapiro      inline void
8764562Sgshapiro      swap(const PB_DS_CLASS_C_DEC& other);
8864562Sgshapiro    };
8964562Sgshapiro
9064562Sgshapiro    PB_DS_CLASS_T_DEC
9166494Sgshapiro    PB_DS_CLASS_C_DEC::
9290792Sgshapiro    hash_eq_fn()
9366494Sgshapiro    { }
9464562Sgshapiro
9580785Sgshapiro    PB_DS_CLASS_T_DEC
9664562Sgshapiro    inline void
9764562Sgshapiro    PB_DS_CLASS_C_DEC::
9890792Sgshapiro    swap(const PB_DS_CLASS_C_DEC& other)
9980785Sgshapiro    { std::swap((Eq_Fn& )(*this), (Eq_Fn& )other); }
10080785Sgshapiro
10180785Sgshapiro    PB_DS_CLASS_T_DEC
10280785Sgshapiro    PB_DS_CLASS_C_DEC::
10380785Sgshapiro    hash_eq_fn(const Eq_Fn& r_eq_fn) :
10464562Sgshapiro      Eq_Fn(r_eq_fn)
10564562Sgshapiro    { }
10680785Sgshapiro
10780785Sgshapiro    PB_DS_CLASS_T_DEC
10864562Sgshapiro    inline bool
10990792Sgshapiro    PB_DS_CLASS_C_DEC::
11064562Sgshapiro    operator()(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const
11164562Sgshapiro    { return (eq_fn_base::operator()(r_lhs_key, r_rhs_key)); }
11264562Sgshapiro
11366494Sgshapiro#undef PB_DS_CLASS_T_DEC
11477349Sgshapiro#undef PB_DS_CLASS_C_DEC
11577349Sgshapiro
11664562Sgshapiro#define PB_DS_CLASS_T_DEC \
11766494Sgshapiro    template<typename Key, class Eq_Fn, class Allocator>
11866494Sgshapiro
11966494Sgshapiro#define PB_DS_CLASS_C_DEC \
12066494Sgshapiro    hash_eq_fn<Key, Eq_Fn, Allocator, true>
12166494Sgshapiro
12266494Sgshapiro    /**
12366494Sgshapiro     * Specialization 2- The client requests that hash values be stored.
12466494Sgshapiro     **/
12566494Sgshapiro    template<typename Key, class Eq_Fn, class Allocator>
12664562Sgshapiro    struct hash_eq_fn<Key, Eq_Fn, Allocator, true> :
12764562Sgshapiro      public Eq_Fn
12864562Sgshapiro    {
12964562Sgshapiro      typedef typename Allocator::size_type size_type;
13064562Sgshapiro
13164562Sgshapiro      typedef Eq_Fn eq_fn_base;
13264562Sgshapiro
13364562Sgshapiro      typedef typename Allocator::template rebind<Key>::other key_allocator;
13464562Sgshapiro
13564562Sgshapiro      typedef typename key_allocator::const_reference const_key_reference;
13664562Sgshapiro
13764562Sgshapiro      hash_eq_fn();
13864562Sgshapiro
13964562Sgshapiro      hash_eq_fn(const Eq_Fn& r_eq_fn);
14064562Sgshapiro
14164562Sgshapiro      inline bool
14264562Sgshapiro      operator()(const_key_reference r_lhs_key, size_type lhs_hash,
14364562Sgshapiro		 const_key_reference r_rhs_key, size_type rhs_hash) const;
14464562Sgshapiro
14564562Sgshapiro      inline void
14664562Sgshapiro      swap(const PB_DS_CLASS_C_DEC& other);
14764562Sgshapiro    };
14864562Sgshapiro
14964562Sgshapiro    PB_DS_CLASS_T_DEC
15064562Sgshapiro    PB_DS_CLASS_C_DEC::
15164562Sgshapiro    hash_eq_fn()
15264562Sgshapiro    { }
15364562Sgshapiro
15464562Sgshapiro    PB_DS_CLASS_T_DEC
15564562Sgshapiro    PB_DS_CLASS_C_DEC::
15664562Sgshapiro    hash_eq_fn(const Eq_Fn& r_eq_fn) :
15764562Sgshapiro      Eq_Fn(r_eq_fn)
15864562Sgshapiro    { }
15964562Sgshapiro
16064562Sgshapiro    PB_DS_CLASS_T_DEC
16164562Sgshapiro    inline bool
16264562Sgshapiro    PB_DS_CLASS_C_DEC::
16364562Sgshapiro    operator()(const_key_reference r_lhs_key, size_type lhs_hash,
16466494Sgshapiro	       const_key_reference r_rhs_key, size_type rhs_hash) const
16564562Sgshapiro    {
16664562Sgshapiro      _GLIBCXX_DEBUG_ASSERT(!eq_fn_base::operator()(r_lhs_key, r_rhs_key)
16764562Sgshapiro		            || lhs_hash == rhs_hash);
16864562Sgshapiro
16964562Sgshapiro      return (lhs_hash == rhs_hash &&
17064562Sgshapiro	      eq_fn_base::operator()(r_lhs_key, r_rhs_key));
17164562Sgshapiro    }
17264562Sgshapiro
17364562Sgshapiro    PB_DS_CLASS_T_DEC
17464562Sgshapiro    inline void
17564562Sgshapiro    PB_DS_CLASS_C_DEC::
17664562Sgshapiro    swap(const PB_DS_CLASS_C_DEC& other)
17764562Sgshapiro    { std::swap((Eq_Fn& )(*this), (Eq_Fn& )(other)); }
17864562Sgshapiro
17964562Sgshapiro#undef PB_DS_CLASS_T_DEC
18064562Sgshapiro#undef PB_DS_CLASS_C_DEC
18164562Sgshapiro
18264562Sgshapiro  } // namespace detail
18364562Sgshapiro} // namespace pb_ds
18464562Sgshapiro
18564562Sgshapiro#endif
18664562Sgshapiro