hash_eq_fn.hpp revision 169691
1127342Smlaier// -*- C++ -*-
2127342Smlaier
3127342Smlaier// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4127342Smlaier//
5127342Smlaier// This file is part of the GNU ISO C++ Library.  This library is free
6127342Smlaier// software; you can redistribute it and/or modify it under the terms
7135306Skeramida// of the GNU General Public License as published by the Free Software
8127342Smlaier// Foundation; either version 2, or (at your option) any later
9136224Smtm// version.
10127342Smlaier
11127342Smlaier// This library is distributed in the hope that it will be useful, but
12127342Smlaier// WITHOUT ANY WARRANTY; without even the implied warranty of
13127342Smlaier// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14127342Smlaier// General Public License for more details.
15127342Smlaier
16127342Smlaier// You should have received a copy of the GNU General Public License
17127342Smlaier// along with this library; see the file COPYING.  If not, write to
18127342Smlaier// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19127342Smlaier// MA 02111-1307, USA.
20136942Spjd
21136942Spjd// As a special exception, you may use this file as part of a free
22127342Smlaier// software library without restriction.  Specifically, if other files
23127342Smlaier// instantiate templates or use macros or inline functions from this
24127342Smlaier// file, or you compile this file and link it with other files to
25127342Smlaier// produce an executable, this file does not by itself cause the
26127342Smlaier// resulting executable to be covered by the GNU General Public
27127342Smlaier// License.  This exception does not however invalidate any other
28136942Spjd// reasons why the executable file might be covered by the GNU General
29127342Smlaier// Public License.
30127342Smlaier
31127342Smlaier// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32127342Smlaier
33127342Smlaier// Permission to use, copy, modify, sell, and distribute this software
34127342Smlaier// is hereby granted without fee, provided that the above copyright
35127342Smlaier// notice appears in all copies, and that both that copyright notice
36127342Smlaier// and this permission notice appear in supporting documentation. None
37127342Smlaier// of the above authors, nor IBM Haifa Research Laboratories, make any
38127342Smlaier// representation about the suitability of this software for any
39127342Smlaier// purpose. It is provided "as is" without express or implied
40127342Smlaier// warranty.
41127342Smlaier
42136942Spjd/**
43127342Smlaier * @file hash_eq_fn.hpp
44127342Smlaier * Contains 2 eqivalence functions, one employing a hash value,
45127342Smlaier *    and one ignoring it.
46127342Smlaier */
47127342Smlaier
48127342Smlaier#ifndef PB_DS_HASH_EQ_FN_HPP
49127342Smlaier#define PB_DS_HASH_EQ_FN_HPP
50127342Smlaier
51127342Smlaier#include <utility>
52136942Spjd#include <debug/debug.h>
53130954Smlaier
54130954Smlaiernamespace pb_ds
55130954Smlaier{
56127342Smlaier  namespace detail
57127342Smlaier  {
58127342Smlaier    template<typename Key, class Eq_Fn, class Allocator, bool Store_Hash>
59127342Smlaier    struct hash_eq_fn;
60127342Smlaier
61127342Smlaier#define PB_DS_CLASS_T_DEC \
62127342Smlaier    template<typename Key, class Eq_Fn, class Allocator>
63127342Smlaier
64127342Smlaier#define PB_DS_CLASS_C_DEC \
65127342Smlaier    hash_eq_fn<Key, Eq_Fn, Allocator, false>
66136942Spjd
67136942Spjd    /**
68136942Spjd     * Specialization 1- The client requests that hash values not be stored.
69136942Spjd     **/
70136942Spjd    template<typename Key, class Eq_Fn, class Allocator>
71136942Spjd    struct hash_eq_fn<Key, Eq_Fn, Allocator, false> : public Eq_Fn
72136942Spjd    {
73127342Smlaier      typedef Eq_Fn eq_fn_base;
74127342Smlaier
75127342Smlaier      typedef typename Allocator::template rebind<Key>::other key_allocator;
76127342Smlaier
77136942Spjd      typedef typename key_allocator::const_reference const_key_reference;
78127342Smlaier
79136942Spjd      hash_eq_fn();
80127342Smlaier
81127342Smlaier      hash_eq_fn(const Eq_Fn& r_eq_fn);
82127342Smlaier
83127342Smlaier      inline bool
84127342Smlaier      operator()(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const;
85127342Smlaier
86127342Smlaier      inline void
87127342Smlaier      swap(const PB_DS_CLASS_C_DEC& other);
88127342Smlaier    };
89127342Smlaier
90127342Smlaier    PB_DS_CLASS_T_DEC
91127342Smlaier    PB_DS_CLASS_C_DEC::
92127342Smlaier    hash_eq_fn()
93127342Smlaier    { }
94127342Smlaier
95127342Smlaier    PB_DS_CLASS_T_DEC
96127342Smlaier    inline void
97    PB_DS_CLASS_C_DEC::
98    swap(const PB_DS_CLASS_C_DEC& other)
99    { std::swap((Eq_Fn& )(*this), (Eq_Fn& )other); }
100
101    PB_DS_CLASS_T_DEC
102    PB_DS_CLASS_C_DEC::
103    hash_eq_fn(const Eq_Fn& r_eq_fn) :
104      Eq_Fn(r_eq_fn)
105    { }
106
107    PB_DS_CLASS_T_DEC
108    inline bool
109    PB_DS_CLASS_C_DEC::
110    operator()(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const
111    { return (eq_fn_base::operator()(r_lhs_key, r_rhs_key)); }
112
113#undef PB_DS_CLASS_T_DEC
114#undef PB_DS_CLASS_C_DEC
115
116#define PB_DS_CLASS_T_DEC \
117    template<typename Key, class Eq_Fn, class Allocator>
118
119#define PB_DS_CLASS_C_DEC \
120    hash_eq_fn<Key, Eq_Fn, Allocator, true>
121
122    /**
123     * Specialization 2- The client requests that hash values be stored.
124     **/
125    template<typename Key, class Eq_Fn, class Allocator>
126    struct hash_eq_fn<Key, Eq_Fn, Allocator, true> :
127      public Eq_Fn
128    {
129      typedef typename Allocator::size_type size_type;
130
131      typedef Eq_Fn eq_fn_base;
132
133      typedef typename Allocator::template rebind<Key>::other key_allocator;
134
135      typedef typename key_allocator::const_reference const_key_reference;
136
137      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