]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
99dee823 | 3 | // Copyright (C) 2005-2021 Free Software Foundation, Inc. |
4569a895 AT |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the terms | |
7 | // of the GNU General Public License as published by the Free Software | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
4569a895 AT |
9 | // version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, but | |
12 | // WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
4569a895 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
4569a895 AT |
24 | |
25 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
26 | ||
27 | // Permission to use, copy, modify, sell, and distribute this software | |
28 | // is hereby granted without fee, provided that the above copyright | |
29 | // notice appears in all copies, and that both that copyright notice | |
30 | // and this permission notice appear in supporting documentation. None | |
31 | // of the above authors, nor IBM Haifa Research Laboratories, make any | |
32 | // representation about the suitability of this software for any | |
33 | // purpose. It is provided "as is" without express or implied | |
34 | // warranty. | |
35 | ||
36 | /** | |
37 | * @file hash_eq_fn.hpp | |
ec541f1b | 38 | * Contains 2 equivalence functions, one employing a hash value, |
a345e45d | 39 | * and one ignoring it. |
4569a895 AT |
40 | */ |
41 | ||
42 | #ifndef PB_DS_HASH_EQ_FN_HPP | |
43 | #define PB_DS_HASH_EQ_FN_HPP | |
44 | ||
45 | #include <utility> | |
ec541f1b | 46 | #include <ext/pb_ds/detail/types_traits.hpp> |
47bea7b8 | 47 | #include <debug/debug.h> |
4569a895 | 48 | |
5e11f978 | 49 | namespace __gnu_pbds |
4569a895 AT |
50 | { |
51 | namespace detail | |
52 | { | |
30a96b3b | 53 | /// Primary template. |
a345e45d BK |
54 | template<typename Key, typename Eq_Fn, typename _Alloc, bool Store_Hash> |
55 | struct hash_eq_fn; | |
4569a895 | 56 | |
a345e45d BK |
57 | /// Specialization 1 - The client requests that hash values not be stored. |
58 | template<typename Key, typename Eq_Fn, typename _Alloc> | |
59 | struct hash_eq_fn<Key, Eq_Fn, _Alloc, false> : public Eq_Fn | |
4569a895 | 60 | { |
a345e45d | 61 | typedef Eq_Fn eq_fn_base; |
ec541f1b JW |
62 | typedef typename rebind_traits<_Alloc, Key>::const_reference |
63 | key_const_reference; | |
4569a895 | 64 | |
a345e45d | 65 | hash_eq_fn() { } |
4569a895 | 66 | |
a345e45d | 67 | hash_eq_fn(const Eq_Fn& r_eq_fn) : Eq_Fn(r_eq_fn) { } |
4569a895 | 68 | |
a345e45d BK |
69 | bool |
70 | operator()(key_const_reference r_lhs_key, | |
71 | key_const_reference r_rhs_key) const | |
72 | { return eq_fn_base::operator()(r_lhs_key, r_rhs_key); } | |
4569a895 | 73 | |
a345e45d BK |
74 | void |
75 | swap(const hash_eq_fn& other) | |
76 | { std::swap((Eq_Fn&)(*this), (Eq_Fn&)other); } | |
4569a895 AT |
77 | }; |
78 | ||
4569a895 | 79 | |
a345e45d BK |
80 | /// Specialization 2 - The client requests that hash values be stored. |
81 | template<typename Key, class Eq_Fn, class _Alloc> | |
82 | struct hash_eq_fn<Key, Eq_Fn, _Alloc, true> : public Eq_Fn | |
83 | { | |
84 | typedef typename _Alloc::size_type size_type; | |
85 | typedef Eq_Fn eq_fn_base; | |
ec541f1b JW |
86 | typedef typename rebind_traits<_Alloc, Key>::const_reference |
87 | key_const_reference; | |
4569a895 | 88 | |
a345e45d | 89 | hash_eq_fn() { } |
4569a895 | 90 | |
a345e45d | 91 | hash_eq_fn(const Eq_Fn& r_eq_fn) : Eq_Fn(r_eq_fn) { } |
4569a895 | 92 | |
a345e45d BK |
93 | bool |
94 | operator()(key_const_reference r_lhs_key, size_type lhs_hash, | |
95 | key_const_reference r_rhs_key, size_type rhs_hash) const | |
96 | { | |
97 | _GLIBCXX_DEBUG_ASSERT(!eq_fn_base::operator()(r_lhs_key, r_rhs_key) | |
98 | || lhs_hash == rhs_hash); | |
4569a895 | 99 | |
a345e45d BK |
100 | return (lhs_hash == rhs_hash && |
101 | eq_fn_base::operator()(r_lhs_key, r_rhs_key)); | |
102 | } | |
4569a895 | 103 | |
a345e45d BK |
104 | void |
105 | swap(const hash_eq_fn& other) | |
106 | { std::swap((Eq_Fn&)(*this), (Eq_Fn&)(other)); } | |
4569a895 | 107 | }; |
4569a895 | 108 | } // namespace detail |
5e11f978 | 109 | } // namespace __gnu_pbds |
4569a895 | 110 | |
47bea7b8 | 111 | #endif |