libstdc++: [_Hashtable] Insert range of types convertible to value_type PR 105717
Fix insertion of range of instances convertible to value_type. libstdc++-v3/ChangeLog: PR libstdc++/105717 * include/bits/hashtable_policy.h (_ConvertToValueType): New. * include/bits/hashtable.h (_Hashtable<>::_M_insert_unique_aux): New. (_Hashtable<>::_M_insert(_Arg&&, const _NodeGenerator&, true_type)): Use latters. (_Hashtable<>::_M_insert(_Arg&&, const _NodeGenerator&, false_type)): Likewise. (_Hashtable(_InputIterator, _InputIterator, size_type, const _Hash&, const _Equal&, const allocator_type&, true_type)): Use this.insert range. (_Hashtable(_InputIterator, _InputIterator, size_type, const _Hash&, const _Equal&, const allocator_type&, false_type)): Use _M_insert. * testsuite/23_containers/unordered_map/cons/56112.cc: Check how many times conversion is done. * testsuite/23_containers/unordered_map/insert/105717.cc: New test. * testsuite/23_containers/unordered_set/insert/105717.cc: New test.
This commit is contained in:
parent
636b01ab49
commit
dc9b92facf
5 changed files with 227 additions and 16 deletions
|
@ -899,21 +899,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
|
|
||||||
template<typename _Arg, typename _NodeGenerator>
|
template<typename _Arg, typename _NodeGenerator>
|
||||||
std::pair<iterator, bool>
|
std::pair<iterator, bool>
|
||||||
_M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
|
_M_insert_unique_aux(_Arg&& __arg, const _NodeGenerator& __node_gen)
|
||||||
true_type /* __uks */)
|
|
||||||
{
|
{
|
||||||
return _M_insert_unique(
|
return _M_insert_unique(
|
||||||
_S_forward_key(_ExtractKey{}(std::forward<_Arg>(__arg))),
|
_S_forward_key(_ExtractKey{}(std::forward<_Arg>(__arg))),
|
||||||
std::forward<_Arg>(__arg), __node_gen);
|
std::forward<_Arg>(__arg), __node_gen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename _Arg, typename _NodeGenerator>
|
||||||
|
std::pair<iterator, bool>
|
||||||
|
_M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
|
||||||
|
true_type /* __uks */)
|
||||||
|
{
|
||||||
|
using __to_value
|
||||||
|
= __detail::_ConvertToValueType<_ExtractKey, value_type>;
|
||||||
|
return _M_insert_unique_aux(
|
||||||
|
__to_value{}(std::forward<_Arg>(__arg)), __node_gen);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename _Arg, typename _NodeGenerator>
|
template<typename _Arg, typename _NodeGenerator>
|
||||||
iterator
|
iterator
|
||||||
_M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
|
_M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
|
||||||
false_type __uks)
|
false_type __uks)
|
||||||
{
|
{
|
||||||
return _M_insert(cend(), std::forward<_Arg>(__arg), __node_gen,
|
using __to_value
|
||||||
__uks);
|
= __detail::_ConvertToValueType<_ExtractKey, value_type>;
|
||||||
|
return _M_insert(cend(),
|
||||||
|
__to_value{}(std::forward<_Arg>(__arg)), __node_gen, __uks);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert with hint, not used when keys are unique.
|
// Insert with hint, not used when keys are unique.
|
||||||
|
@ -1185,10 +1197,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
const _Hash& __h, const _Equal& __eq,
|
const _Hash& __h, const _Equal& __eq,
|
||||||
const allocator_type& __a, true_type /* __uks */)
|
const allocator_type& __a, true_type /* __uks */)
|
||||||
: _Hashtable(__bkt_count_hint, __h, __eq, __a)
|
: _Hashtable(__bkt_count_hint, __h, __eq, __a)
|
||||||
{
|
{ this->insert(__f, __l); }
|
||||||
for (; __f != __l; ++__f)
|
|
||||||
this->insert(*__f);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename _Key, typename _Value, typename _Alloc,
|
template<typename _Key, typename _Value, typename _Alloc,
|
||||||
typename _ExtractKey, typename _Equal,
|
typename _ExtractKey, typename _Equal,
|
||||||
|
@ -1200,7 +1209,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
_Hashtable(_InputIterator __f, _InputIterator __l,
|
_Hashtable(_InputIterator __f, _InputIterator __l,
|
||||||
size_type __bkt_count_hint,
|
size_type __bkt_count_hint,
|
||||||
const _Hash& __h, const _Equal& __eq,
|
const _Hash& __h, const _Equal& __eq,
|
||||||
const allocator_type& __a, false_type /* __uks */)
|
const allocator_type& __a, false_type __uks)
|
||||||
: _Hashtable(__h, __eq, __a)
|
: _Hashtable(__h, __eq, __a)
|
||||||
{
|
{
|
||||||
auto __nb_elems = __detail::__distance_fw(__f, __l);
|
auto __nb_elems = __detail::__distance_fw(__f, __l);
|
||||||
|
@ -1215,8 +1224,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
_M_bucket_count = __bkt_count;
|
_M_bucket_count = __bkt_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__alloc_node_gen_t __node_gen(*this);
|
||||||
for (; __f != __l; ++__f)
|
for (; __f != __l; ++__f)
|
||||||
this->insert(*__f);
|
_M_insert(*__f, __node_gen, __uks);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename _Key, typename _Value, typename _Alloc,
|
template<typename _Key, typename _Value, typename _Alloc,
|
||||||
|
|
|
@ -113,6 +113,40 @@ namespace __detail
|
||||||
{ return std::forward<_Tp>(__x).first; }
|
{ return std::forward<_Tp>(__x).first; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename _ExKey, typename _Value>
|
||||||
|
struct _ConvertToValueType;
|
||||||
|
|
||||||
|
template<typename _Value>
|
||||||
|
struct _ConvertToValueType<_Identity, _Value>
|
||||||
|
{
|
||||||
|
template<typename _Kt>
|
||||||
|
constexpr _Kt&&
|
||||||
|
operator()(_Kt&& __k) const noexcept
|
||||||
|
{ return std::forward<_Kt>(__k); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename _Value>
|
||||||
|
struct _ConvertToValueType<_Select1st, _Value>
|
||||||
|
{
|
||||||
|
constexpr _Value&&
|
||||||
|
operator()(_Value&& __x) const noexcept
|
||||||
|
{ return std::move(__x); }
|
||||||
|
|
||||||
|
constexpr const _Value&
|
||||||
|
operator()(const _Value& __x) const noexcept
|
||||||
|
{ return __x; }
|
||||||
|
|
||||||
|
template<typename _Kt, typename _Val>
|
||||||
|
constexpr std::pair<_Kt, _Val>&&
|
||||||
|
operator()(std::pair<_Kt, _Val>&& __x) const noexcept
|
||||||
|
{ return std::move(__x); }
|
||||||
|
|
||||||
|
template<typename _Kt, typename _Val>
|
||||||
|
constexpr const std::pair<_Kt, _Val>&
|
||||||
|
operator()(const std::pair<_Kt, _Val>& __x) const noexcept
|
||||||
|
{ return __x; }
|
||||||
|
};
|
||||||
|
|
||||||
template<typename _ExKey>
|
template<typename _ExKey>
|
||||||
struct _NodeBuilder;
|
struct _NodeBuilder;
|
||||||
|
|
||||||
|
|
|
@ -20,30 +20,51 @@
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <testsuite_hooks.h>
|
||||||
|
|
||||||
struct Key
|
struct Key
|
||||||
{
|
{
|
||||||
explicit Key(const int* p) : value(p) { }
|
explicit Key(const int* p) : value(p) { }
|
||||||
~Key() { value = nullptr; }
|
~Key() { value = nullptr; }
|
||||||
|
|
||||||
bool operator==(const Key& k) const { return *value == *k.value; }
|
bool operator==(const Key& k) const
|
||||||
|
{ return *value == *k.value; }
|
||||||
|
|
||||||
const int* value;
|
const int* value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct hash
|
struct hash
|
||||||
{
|
{
|
||||||
std::size_t operator()(const Key& k) const noexcept { return *k.value; }
|
std::size_t operator()(const Key& k) const noexcept
|
||||||
|
{ return *k.value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct S
|
struct S
|
||||||
{
|
{
|
||||||
|
static int _count;
|
||||||
|
|
||||||
int value;
|
int value;
|
||||||
operator std::pair<const Key, int>() const { return {Key(&value), value}; }
|
operator std::pair<const Key, int>() const
|
||||||
|
{
|
||||||
|
++_count;
|
||||||
|
return { Key(&value), value };
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int S::_count = 0;
|
||||||
|
|
||||||
|
void test01()
|
||||||
|
{
|
||||||
|
S s[1] = { {2} };
|
||||||
|
std::unordered_map<Key, int, hash> m(s, s + 1);
|
||||||
|
VERIFY( S::_count == 1 );
|
||||||
|
|
||||||
|
std::unordered_multimap<Key, int, hash> mm(s, s + 1);
|
||||||
|
VERIFY( S::_count == 2 );
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
S s[1] = { {2} };
|
test01();
|
||||||
std::unordered_map<Key, int, hash> m(s, s+1);
|
return 0;
|
||||||
std::unordered_multimap<Key, int, hash> mm(s, s+1);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
// { dg-do run { target c++11 } }
|
||||||
|
|
||||||
|
// Copyright (C) 2022 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 3, 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 COPYING3. If not see
|
||||||
|
// <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include <testsuite_hooks.h>
|
||||||
|
|
||||||
|
struct Key
|
||||||
|
{
|
||||||
|
explicit Key(const int* p) : value(p) { }
|
||||||
|
~Key() { value = nullptr; }
|
||||||
|
|
||||||
|
bool operator==(const Key& k) const
|
||||||
|
{ return *value == *k.value; }
|
||||||
|
|
||||||
|
const int* value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct hash
|
||||||
|
{
|
||||||
|
std::size_t operator()(const Key& k) const noexcept
|
||||||
|
{ return *k.value; }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct S
|
||||||
|
{
|
||||||
|
static int _count;
|
||||||
|
|
||||||
|
int value;
|
||||||
|
operator std::pair<const Key, int>() const
|
||||||
|
{
|
||||||
|
++_count;
|
||||||
|
return { Key(&value), value };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int S::_count = 0;
|
||||||
|
|
||||||
|
void test01()
|
||||||
|
{
|
||||||
|
S s[1] = { {2} };
|
||||||
|
std::unordered_map<Key, int, hash> m;
|
||||||
|
std::unordered_multimap<Key, int, hash> mm;
|
||||||
|
|
||||||
|
m.insert(s, s + 1);
|
||||||
|
VERIFY( S::_count == 1 );
|
||||||
|
|
||||||
|
mm.insert(s, s + 1);
|
||||||
|
VERIFY( S::_count == 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
test01();
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
// { dg-do run { target c++11 } }
|
||||||
|
|
||||||
|
// Copyright (C) 2022 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 3, 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 COPYING3. If not see
|
||||||
|
// <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include <testsuite_hooks.h>
|
||||||
|
|
||||||
|
struct Key
|
||||||
|
{
|
||||||
|
explicit Key(const int* p) : value(p) { }
|
||||||
|
~Key() { value = nullptr; }
|
||||||
|
|
||||||
|
bool operator==(const Key& k) const
|
||||||
|
{ return *value == *k.value; }
|
||||||
|
|
||||||
|
const int* value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct hash
|
||||||
|
{
|
||||||
|
std::size_t operator()(const Key& k) const noexcept
|
||||||
|
{ return *k.value; }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct S
|
||||||
|
{
|
||||||
|
static int _count;
|
||||||
|
|
||||||
|
int value;
|
||||||
|
operator Key() const
|
||||||
|
{
|
||||||
|
++_count;
|
||||||
|
return Key(&value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int S::_count = 0;
|
||||||
|
|
||||||
|
void test01()
|
||||||
|
{
|
||||||
|
S a[1] = { {2} };
|
||||||
|
std::unordered_set<Key, hash> s;
|
||||||
|
std::unordered_multiset<Key, hash> ms;
|
||||||
|
|
||||||
|
s.insert(a, a + 1);
|
||||||
|
VERIFY( S::_count == 1 );
|
||||||
|
|
||||||
|
ms.insert(a, a + 1);
|
||||||
|
VERIFY( S::_count == 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
test01();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue