functional (tr1_hashtable_define_trivial_hash): Make hash<T>::operator() a const member function for T a fundamental type

* include/tr1/functional (tr1_hashtable_define_trivial_hash): Make
hash<T>::operator() a const member function for T a fundamental type
* include/tr1/hashtable (extract1st::operator()): Declare const.
(hash_code_base): Declare all member functions const
(hashtable::find): fix call to this->bucket_count()
(hashtable::count): Likewise.
(hashtable::equal_range): m_incr_bucket applies to iterator, not node.
* testsuite/tr1/6_containers/unordered/find/set1.cc: New test.
* testsuite/tr1/6_containers/unordered/find/map1.cc: New test.
* testsuite/tr1/6_containers/unordered/find/multimap1.cc: New test.
* testsuite/tr1/6_containers/unordered/find/multiset1.cc: New test.

From-SVN: r95293
This commit is contained in:
Matt Austern 2005-02-19 23:57:23 +00:00 committed by Matt Austern
parent 72aff31a37
commit 6bbd10c784
7 changed files with 324 additions and 21 deletions

View File

@ -1,3 +1,17 @@
2005-02-19 Matt Austern <austern@gmail.com>
* include/tr1/functional (tr1_hashtable_define_trivial_hash): Make
hash<T>::operator() a const member function for T a fundamental type
* include/tr1/hashtable (extract1st::operator()): Declare const.
(hash_code_base): Declare all member functions const
(hashtable::find): fix call to this->bucket_count()
(hashtable::count): Likewise.
(hashtable::equal_range): m_incr_bucket applies to iterator, not node.
* testsuite/tr1/6_containers/unordered/find/set1.cc: New test.
* testsuite/tr1/6_containers/unordered/find/map1.cc: New test.
* testsuite/tr1/6_containers/unordered/find/multimap1.cc: New test.
* testsuite/tr1/6_containers/unordered/find/multiset1.cc: New test.
2005-02-19 Hans-Peter Nilsson <hp@axis.com>
PR libstdc++/20071

View File

@ -88,7 +88,7 @@ namespace tr1
#define tr1_hashtable_define_trivial_hash(T) \
template <> struct hash<T> { \
std::size_t operator()(T val) { return static_cast<std::size_t>(val); } \
std::size_t operator()(T val) const { return static_cast<std::size_t>(val); } \
} \
tr1_hashtable_define_trivial_hash(bool);

View File

@ -251,7 +251,7 @@ struct identity {
template <typename Pair>
struct extract1st {
typename Pair::first_type operator()(const Pair& p) { return p.first; }
typename Pair::first_type operator()(const Pair& p) const { return p.first; }
};
// Default range hashing function: use division to fold a large number
@ -519,17 +519,17 @@ protected:
: m_extract(ex), m_eq(eq), m_ranged_hash(h) { }
typedef void* hash_code_t;
hash_code_t m_hash_code (const Key& k) { return 0; }
hash_code_t m_hash_code (const Key& k) const { return 0; }
std::size_t bucket_index (const Key& k, hash_code_t, std::size_t N) const
{ return m_ranged_hash (k, N); }
std::size_t bucket_index (const hash_node<Value, false>* p, std::size_t N) {
std::size_t bucket_index (const hash_node<Value, false>* p, std::size_t N) const {
return m_ranged_hash (m_extract (p->m_v), N);
}
bool compare (const Key& k, hash_code_t, hash_node<Value, false>* n)
bool compare (const Key& k, hash_code_t, hash_node<Value, false>* n) const
{ return m_eq (k, m_extract(n->m_v)); }
void copy_code (hash_node<Value, false>*, const hash_node<Value, false>*) { }
void copy_code (hash_node<Value, false>*, const hash_node<Value, false>*) const { }
void m_swap(hash_code_base& x) {
m_extract.m_swap(x);
@ -576,17 +576,17 @@ protected:
: m_extract(ex), m_eq(eq), m_h1(h1), m_h2(h2) { }
typedef std::size_t hash_code_t;
hash_code_t m_hash_code (const Key& k) { return m_h1(k); }
hash_code_t m_hash_code (const Key& k) const { return m_h1(k); }
std::size_t bucket_index (const Key&, hash_code_t c, std::size_t N) const
{ return m_h2 (c, N); }
std::size_t bucket_index (const hash_node<Value, false>* p, std::size_t N) {
std::size_t bucket_index (const hash_node<Value, false>* p, std::size_t N) const {
return m_h2 (m_h1 (m_extract (p->m_v)), N);
}
bool compare (const Key& k, hash_code_t, hash_node<Value, false>* n)
bool compare (const Key& k, hash_code_t, hash_node<Value, false>* n) const
{ return m_eq (k, m_extract(n->m_v)); }
void copy_code (hash_node<Value, false>*, const hash_node<Value, false>*) { }
void copy_code (hash_node<Value, false>*, const hash_node<Value, false>*) const { }
void m_swap(hash_code_base& x) {
m_extract.m_swap(x);
@ -619,18 +619,18 @@ protected:
: m_extract(ex), m_eq(eq), m_h1(h1), m_h2(h2) { }
typedef std::size_t hash_code_t;
hash_code_t m_hash_code (const Key& k) { return m_h1(k); }
hash_code_t m_hash_code (const Key& k) const { return m_h1(k); }
std::size_t bucket_index (const Key&, hash_code_t c, std::size_t N) const
{ return m_h2 (c, N); }
std::size_t bucket_index (const hash_node<Value, true>* p, std::size_t N) {
std::size_t bucket_index (const hash_node<Value, true>* p, std::size_t N) const {
return m_h2 (p->hash_code, N);
}
bool compare (const Key& k, hash_code_t c, hash_node<Value, true>* n)
bool compare (const Key& k, hash_code_t c, hash_node<Value, true>* n) const
{ return c == n->hash_code && m_eq (k, m_extract(n->m_v)); }
void copy_code (hash_node<Value, true>* to, const hash_node<Value, true>* from)
void copy_code (hash_node<Value, true>* to, const hash_node<Value, true>* from) const
{ to->hash_code = from->hash_code; }
void m_swap(hash_code_base& x) {
@ -1109,7 +1109,7 @@ typename hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::iterator
hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::find (const key_type& k)
{
typename hashtable::hash_code_t code = this->m_hash_code (k);
std::size_t n = this->bucket_index (k, code, this->bucket_count);
std::size_t n = this->bucket_index (k, code, this->bucket_count());
node* p = find_node (m_buckets[n], k, code);
return p ? iterator(p, m_buckets + n) : this->end();
}
@ -1122,7 +1122,7 @@ typename hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::const_iterator
hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::find (const key_type& k) const
{
typename hashtable::hash_code_t code = this->m_hash_code (k);
std::size_t n = this->bucket_index (k, code, this->bucket_count);
std::size_t n = this->bucket_index (k, code, this->bucket_count());
node* p = find_node (m_buckets[n], k, code);
return p ? const_iterator(p, m_buckets + n) : this->end();
}
@ -1135,7 +1135,7 @@ typename hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::size_type
hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::count (const key_type& k) const
{
typename hashtable::hash_code_t code = this->m_hash_code (k);
std::size_t n = this->bucket_index (k, code, this->bucket_count);
std::size_t n = this->bucket_index (k, code, this->bucket_count());
size_t result = 0;
for (node* p = m_buckets[n]; p ; p = p->m_next)
if (this->compare (k, code, p))
@ -1152,7 +1152,7 @@ std::pair<typename hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::iterator,
hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::equal_range (const key_type& k)
{
typename hashtable::hash_code_t code = this->m_hash_code (k);
std::size_t n = this->bucket_index (k, code, this->bucket_count);
std::size_t n = this->bucket_index (k, code, this->bucket_count());
node** head = m_buckets + n;
node* p = find_node (*head, k, code);
@ -1164,7 +1164,7 @@ hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::equal_range (const key_type& k)
iterator first(p, head);
iterator last(p1, head);
if (!p1)
p1->m_incr_bucket();
last.m_incr_bucket();
return std::make_pair(first, last);
}
else
@ -1180,7 +1180,7 @@ std::pair<typename hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::const_iterator,
hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::equal_range (const key_type& k) const
{
typename hashtable::hash_code_t code = this->m_hash_code (k);
std::size_t n = this->bucket_index (k, code, this->bucket_count);
std::size_t n = this->bucket_index (k, code, this->bucket_count());
node** head = m_buckets + n;
node* p = find_node (*head, k, code);
@ -1192,7 +1192,7 @@ hashtable<K,V,A,Ex,Eq,H1,H2,H,RP,c,m,u>::equal_range (const key_type& k) const
const_iterator first(p, head);
const_iterator last(p1, head);
if (!p1)
p1->m_incr_bucket();
last.m_incr_bucket();
return std::make_pair(first, last);
}
else

View File

@ -0,0 +1,71 @@
// { dg-do run }
// 2005-2-18 Matt Austern <austern@apple.com>
//
// Copyright (C) 2005 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 6.3.4.4 unordered_map
// find, equal_range, count
#include <string>
#include <iterator>
#include <algorithm>
#include <utility>
#include <tr1/unordered_map>
#include "testsuite_hooks.h"
bool test __attribute__((unused)) = true;
void test01()
{
typedef std::tr1::unordered_map<std::string, int> Map;
typedef std::pair<const std::string, int> Pair;
Map m;
VERIFY(m.empty());
std::pair<Map::iterator, bool> tmp = m.insert(Pair("grape", 3));
Map::iterator i = tmp.first;
VERIFY(tmp.second);
Map::iterator i2 = m.find("grape");
VERIFY(i2 != m.end());
VERIFY(i2 == i);
VERIFY(i2->first == "grape");
VERIFY(i2->second == 3);
Map::iterator i3 = m.find("lime");
VERIFY(i3 == m.end());
std::pair<Map::iterator, Map::iterator> p = m.equal_range("grape");
VERIFY(std::distance(p.first, p.second) == 1);
VERIFY(p.first == i2);
std::pair<Map::iterator, Map::iterator> p2 = m.equal_range("lime");
VERIFY(p2.first == p2.second);
VERIFY(m.count("grape") == 1);
VERIFY(m.count("lime") == 0);
}
int main()
{
test01();
return 0;
}

View File

@ -0,0 +1,85 @@
// { dg-do run }
// 2005-2-18 Matt Austern <austern@apple.com>
//
// Copyright (C) 2005 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 6.3.4.6 unordered_multimap
// find, equal_range, count
#include <string>
#include <iterator>
#include <algorithm>
#include <utility>
#include <tr1/unordered_map>
#include "testsuite_hooks.h"
bool test __attribute__((unused)) = true;
void test01()
{
typedef std::tr1::unordered_multimap<std::string, int> Map;
typedef std::pair<const std::string, int> Pair;
Map m;
VERIFY(m.empty());
m.insert(Pair("grape", 3));
m.insert(Pair("durian", 8));
m.insert(Pair("grape", 7));
Map::iterator i1 = m.find("grape");
Map::iterator i2 = m.find("durian");
Map::iterator i3 = m.find("kiwi");
VERIFY(i1 != m.end());
VERIFY(i1->first == "grape");
VERIFY(i1->second == 3 || i2->second == 7);
VERIFY(i2 != m.end());
VERIFY(i2->first == "durian");
VERIFY(i2->second == 8);
VERIFY(i3 == m.end());
std::pair<Map::iterator, Map::iterator> p1 = m.equal_range("grape");
VERIFY(std::distance(p1.first, p1.second) == 2);
Map::iterator tmp = p1.first;
++tmp;
VERIFY(p1.first->first == "grape");
VERIFY(tmp->first == "grape");
VERIFY((p1.first->second == 3 && tmp->second == 7) ||
(p1.first->second == 7 && tmp->second == 3));
std::pair<Map::iterator, Map::iterator> p2 = m.equal_range("durian");
VERIFY(std::distance(p2.first, p2.second) == 1);
VERIFY(p2.first->first == "durian");
VERIFY(p2.first->second == 8);
std::pair<Map::iterator, Map::iterator> p3 = m.equal_range("kiwi");
VERIFY(p3.first == p3.second);
VERIFY(m.count("grape") == 2);
VERIFY(m.count("durian") == 1);
VERIFY(m.count("kiwi") == 0);
}
int main()
{
test01();
return 0;
}

View File

@ -0,0 +1,67 @@
// { dg-do run }
// 2005-2-18 Matt Austern <austern@apple.com>
//
// Copyright (C) 2005 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 6.3.4.5 unordered_set
// find, equal_range, count
#include <string>
#include <iterator>
#include <algorithm>
#include <tr1/unordered_set>
#include "testsuite_hooks.h"
bool test __attribute__((unused)) = true;
void test01()
{
typedef std::tr1::unordered_multiset<std::string> Set;
Set s;
VERIFY(s.empty());
s.insert("grape");
s.insert("banana");
s.insert("grape");
Set::iterator i2 = s.find("banana");
VERIFY(i2 != s.end());
VERIFY(*i2 == "banana");
std::pair<Set::iterator, Set::iterator> p = s.equal_range("grape");
VERIFY(std::distance(p.first, p.second) == 2);
Set::iterator i3 = p.first;
++i3;
VERIFY(*p.first == "grape");
VERIFY(*i3 == "grape");
Set::iterator i4 = s.find("lime");
VERIFY(i4 == s.end());
VERIFY(s.count("grape") == 2);
VERIFY(s.count("banana") == 1);
VERIFY(s.count("lime") == 0);
}
int main()
{
test01();
return 0;
}

View File

@ -0,0 +1,66 @@
// { dg-do run }
// 2005-2-18 Matt Austern <austern@apple.com>
//
// Copyright (C) 2005 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 6.3.4.3 unordered_set
// find, equal_range, count
#include <string>
#include <iterator>
#include <algorithm>
#include <tr1/unordered_set>
#include "testsuite_hooks.h"
bool test __attribute__((unused)) = true;
void test01()
{
typedef std::tr1::unordered_set<std::string> Set;
Set s;
VERIFY(s.empty());
std::pair<Set::iterator, bool> tmp = s.insert("grape");
Set::iterator i = tmp.first;
Set::iterator i2 = s.find("grape");
VERIFY(i2 != s.end());
VERIFY(i2 == i);
VERIFY(*i2 == "grape");
std::pair<Set::iterator, Set::iterator> p = s.equal_range("grape");
VERIFY(p.first == i2);
VERIFY(std::distance(p.first, p.second) == 1);
Set::iterator i3 = s.find("lime");
VERIFY(i3 == s.end());
std::pair<Set::iterator, Set::iterator> p2 = s.equal_range("lime");
VERIFY(p2.first == p2.second);
VERIFY(s.count("grape") == 1);
VERIFY(s.count("lime") == 0);
}
int main()
{
test01();
return 0;
}