From 6bbd10c78408f9fcb275e0c679b68e3066a53afd Mon Sep 17 00:00:00 2001 From: Matt Austern Date: Sat, 19 Feb 2005 23:57:23 +0000 Subject: [PATCH] functional (tr1_hashtable_define_trivial_hash): Make hash::operator() a const member function for T a fundamental type * include/tr1/functional (tr1_hashtable_define_trivial_hash): Make hash::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 --- libstdc++-v3/ChangeLog | 14 +++ libstdc++-v3/include/tr1/functional | 2 +- libstdc++-v3/include/tr1/hashtable | 40 ++++----- .../tr1/6_containers/unordered/find/map1.cc | 71 ++++++++++++++++ .../6_containers/unordered/find/multimap1.cc | 85 +++++++++++++++++++ .../6_containers/unordered/find/multiset1.cc | 67 +++++++++++++++ .../tr1/6_containers/unordered/find/set1.cc | 66 ++++++++++++++ 7 files changed, 324 insertions(+), 21 deletions(-) create mode 100644 libstdc++-v3/testsuite/tr1/6_containers/unordered/find/map1.cc create mode 100644 libstdc++-v3/testsuite/tr1/6_containers/unordered/find/multimap1.cc create mode 100644 libstdc++-v3/testsuite/tr1/6_containers/unordered/find/multiset1.cc create mode 100644 libstdc++-v3/testsuite/tr1/6_containers/unordered/find/set1.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 84299a131b3..6625f296328 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,17 @@ +2005-02-19 Matt Austern + + * include/tr1/functional (tr1_hashtable_define_trivial_hash): Make + hash::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 PR libstdc++/20071 diff --git a/libstdc++-v3/include/tr1/functional b/libstdc++-v3/include/tr1/functional index d3d681cf01f..0323b962d3f 100644 --- a/libstdc++-v3/include/tr1/functional +++ b/libstdc++-v3/include/tr1/functional @@ -88,7 +88,7 @@ namespace tr1 #define tr1_hashtable_define_trivial_hash(T) \ template <> struct hash { \ - std::size_t operator()(T val) { return static_cast(val); } \ + std::size_t operator()(T val) const { return static_cast(val); } \ } \ tr1_hashtable_define_trivial_hash(bool); diff --git a/libstdc++-v3/include/tr1/hashtable b/libstdc++-v3/include/tr1/hashtable index fb53f70d269..28a71f9b1f7 100644 --- a/libstdc++-v3/include/tr1/hashtable +++ b/libstdc++-v3/include/tr1/hashtable @@ -251,7 +251,7 @@ struct identity { template 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* p, std::size_t N) { + std::size_t bucket_index (const hash_node* 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* n) + bool compare (const Key& k, hash_code_t, hash_node* n) const { return m_eq (k, m_extract(n->m_v)); } - void copy_code (hash_node*, const hash_node*) { } + void copy_code (hash_node*, const hash_node*) 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* p, std::size_t N) { + std::size_t bucket_index (const hash_node* 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* n) + bool compare (const Key& k, hash_code_t, hash_node* n) const { return m_eq (k, m_extract(n->m_v)); } - void copy_code (hash_node*, const hash_node*) { } + void copy_code (hash_node*, const hash_node*) 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* p, std::size_t N) { + std::size_t bucket_index (const hash_node* p, std::size_t N) const { return m_h2 (p->hash_code, N); } - bool compare (const Key& k, hash_code_t c, hash_node* n) + bool compare (const Key& k, hash_code_t c, hash_node* n) const { return c == n->hash_code && m_eq (k, m_extract(n->m_v)); } - void copy_code (hash_node* to, const hash_node* from) + void copy_code (hash_node* to, const hash_node* from) const { to->hash_code = from->hash_code; } void m_swap(hash_code_base& x) { @@ -1109,7 +1109,7 @@ typename hashtable::iterator hashtable::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::const_iterator hashtable::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::size_type hashtable::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::iterator, hashtable::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::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::const_iterator, hashtable::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::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 diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/map1.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/map1.cc new file mode 100644 index 00000000000..eb4385c7c33 --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/map1.cc @@ -0,0 +1,71 @@ +// { dg-do run } + +// 2005-2-18 Matt Austern +// +// 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 +#include +#include +#include +#include +#include "testsuite_hooks.h" + +bool test __attribute__((unused)) = true; + +void test01() +{ + typedef std::tr1::unordered_map Map; + typedef std::pair Pair; + + Map m; + VERIFY(m.empty()); + + std::pair 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 p = m.equal_range("grape"); + VERIFY(std::distance(p.first, p.second) == 1); + VERIFY(p.first == i2); + + std::pair 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; +} diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/multimap1.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/multimap1.cc new file mode 100644 index 00000000000..7adfaa1faf5 --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/multimap1.cc @@ -0,0 +1,85 @@ +// { dg-do run } + +// 2005-2-18 Matt Austern +// +// 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 +#include +#include +#include +#include +#include "testsuite_hooks.h" + +bool test __attribute__((unused)) = true; + +void test01() +{ + typedef std::tr1::unordered_multimap Map; + typedef std::pair 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 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 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 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; +} diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/multiset1.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/multiset1.cc new file mode 100644 index 00000000000..423667751a8 --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/multiset1.cc @@ -0,0 +1,67 @@ +// { dg-do run } + +// 2005-2-18 Matt Austern +// +// 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 +#include +#include +#include +#include "testsuite_hooks.h" + +bool test __attribute__((unused)) = true; + +void test01() +{ + typedef std::tr1::unordered_multiset 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 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; +} diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/set1.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/set1.cc new file mode 100644 index 00000000000..3baaee69f8d --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered/find/set1.cc @@ -0,0 +1,66 @@ +// { dg-do run } + +// 2005-2-18 Matt Austern +// +// 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 +#include +#include +#include +#include "testsuite_hooks.h" + +bool test __attribute__((unused)) = true; + +void test01() +{ + typedef std::tr1::unordered_set Set; + Set s; + VERIFY(s.empty()); + + std::pair 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 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 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; +}