1 // { dg-do run { target c++11 } }
2 
3 // Copyright (C) 2011-2019 Free Software Foundation, Inc.
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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3.  If not see
18 // <http://www.gnu.org/licenses/>.
19 
20 // range insert
21 
22 #include <vector>
23 #include <unordered_set>
24 #include <testsuite_hooks.h>
25 
26 class PathPoint
27 {
28 public:
PathPoint(char t,const std::vector<double> & c)29   PathPoint(char t, const std::vector<double>& c)
30   : type(t), coords(c) { }
PathPoint(char t,std::vector<double> && c)31   PathPoint(char t, std::vector<double>&& c)
32   : type(t), coords(std::move(c)) { }
getType() const33   char getType() const { return type; }
getCoords() const34   const std::vector<double>& getCoords() const { return coords; }
35 private:
36   char type;
37   std::vector<double> coords;
38 };
39 
40 struct PathPointHasher
41 {
operator ()PathPointHasher42   std::size_t operator() (const PathPoint& __pp) const
43   { return __pp.getType(); }
44 };
45 
46 struct PathPointEqual
47 {
operator ()PathPointEqual48   bool operator() (const PathPoint& __lhs, const PathPoint& __rhs) const
49   { return __lhs.getType() == __rhs.getType(); }
50 };
51 
test01()52 void test01()
53 {
54   typedef std::unordered_set<PathPoint, PathPointHasher, PathPointEqual> Set;
55   Set s;
56 
57   std::vector<double> coord1 = { 0.0, 1.0, 2.0 };
58 
59   auto ret = s.emplace('a', coord1);
60   VERIFY( ret.second );
61   VERIFY( s.size() == 1 );
62   VERIFY( ret.first->getType() == 'a' );
63 
64   coord1[0] = 3.0;
65   ret = s.emplace('a', coord1);
66   VERIFY( !ret.second );
67   VERIFY( s.size() == 1 );
68   VERIFY( ret.first->getType() == 'a' );
69   VERIFY( ret.first->getCoords()[0] == 0.0 );
70 
71   auto it = s.emplace_hint(s.begin(), 'b', coord1);
72   VERIFY( it != s.end() );
73   VERIFY( it->getType() == 'b' );
74   VERIFY( it->getCoords()[0] == 3.0 );
75 
76   double *px = &coord1[0];
77   ret = s.emplace('c', std::move(coord1));
78   VERIFY( ret.second );
79   VERIFY( ret.first->getType() == 'c' );
80   VERIFY( &(ret.first->getCoords()[0]) == px );
81 }
82 
main()83 int main()
84 {
85   test01();
86   return 0;
87 }
88