1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <unordered_set>
11 
12 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
13 //           class Alloc = allocator<Value>>
14 // class unordered_set
15 
16 // size_type erase(const key_type& k);
17 
18 #include <unordered_set>
19 #include <string>
20 #include <cassert>
21 
22 #include "min_allocator.h"
23 
24 #if __cplusplus >= 201103L
25 template <typename Unordered>
only_deletions(const Unordered & whole,const Unordered & part)26 bool only_deletions ( const Unordered &whole, const Unordered &part ) {
27     typename Unordered::const_iterator w = whole.begin();
28     typename Unordered::const_iterator p = part.begin();
29 
30     while ( w != whole.end () && p != part.end()) {
31         if ( *w == *p )
32             p++;
33         w++;
34         }
35 
36     return p == part.end();
37 }
38 #endif
39 
main()40 int main()
41 {
42     {
43         typedef std::unordered_set<int> C;
44         typedef int P;
45         P a[] =
46         {
47             P(1),
48             P(2),
49             P(3),
50             P(4),
51             P(1),
52             P(2)
53         };
54         C c(a, a + sizeof(a)/sizeof(a[0]));
55         assert(c.erase(5) == 0);
56         assert(c.size() == 4);
57         assert(c.count(1) == 1);
58         assert(c.count(2) == 1);
59         assert(c.count(3) == 1);
60         assert(c.count(4) == 1);
61 
62         assert(c.erase(2) == 1);
63         assert(c.size() == 3);
64         assert(c.count(1) == 1);
65         assert(c.count(3) == 1);
66         assert(c.count(4) == 1);
67 
68         assert(c.erase(2) == 0);
69         assert(c.size() == 3);
70         assert(c.count(1) == 1);
71         assert(c.count(3) == 1);
72         assert(c.count(4) == 1);
73 
74         assert(c.erase(4) == 1);
75         assert(c.size() == 2);
76         assert(c.count(1) == 1);
77         assert(c.count(3) == 1);
78 
79         assert(c.erase(4) == 0);
80         assert(c.size() == 2);
81         assert(c.count(1) == 1);
82         assert(c.count(3) == 1);
83 
84         assert(c.erase(1) == 1);
85         assert(c.size() == 1);
86         assert(c.count(3) == 1);
87 
88         assert(c.erase(1) == 0);
89         assert(c.size() == 1);
90         assert(c.count(3) == 1);
91 
92         assert(c.erase(3) == 1);
93         assert(c.size() == 0);
94 
95         assert(c.erase(3) == 0);
96         assert(c.size() == 0);
97     }
98 #if __cplusplus >= 201103L
99     {
100         typedef std::unordered_set<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;
101         typedef int P;
102         P a[] =
103         {
104             P(1),
105             P(2),
106             P(3),
107             P(4),
108             P(1),
109             P(2)
110         };
111         C c(a, a + sizeof(a)/sizeof(a[0]));
112         assert(c.erase(5) == 0);
113         assert(c.size() == 4);
114         assert(c.count(1) == 1);
115         assert(c.count(2) == 1);
116         assert(c.count(3) == 1);
117         assert(c.count(4) == 1);
118 
119         assert(c.erase(2) == 1);
120         assert(c.size() == 3);
121         assert(c.count(1) == 1);
122         assert(c.count(3) == 1);
123         assert(c.count(4) == 1);
124 
125         assert(c.erase(2) == 0);
126         assert(c.size() == 3);
127         assert(c.count(1) == 1);
128         assert(c.count(3) == 1);
129         assert(c.count(4) == 1);
130 
131         assert(c.erase(4) == 1);
132         assert(c.size() == 2);
133         assert(c.count(1) == 1);
134         assert(c.count(3) == 1);
135 
136         assert(c.erase(4) == 0);
137         assert(c.size() == 2);
138         assert(c.count(1) == 1);
139         assert(c.count(3) == 1);
140 
141         assert(c.erase(1) == 1);
142         assert(c.size() == 1);
143         assert(c.count(3) == 1);
144 
145         assert(c.erase(1) == 0);
146         assert(c.size() == 1);
147         assert(c.count(3) == 1);
148 
149         assert(c.erase(3) == 1);
150         assert(c.size() == 0);
151 
152         assert(c.erase(3) == 0);
153         assert(c.size() == 0);
154     }
155     {
156     typedef std::unordered_set<int> C;
157     C m, m2;
158     for ( int i = 0; i < 10; ++i ) {
159         m.insert(i);
160         m2.insert(i);
161         }
162 
163     C::iterator i = m2.begin();
164     int ctr = 0;
165     while (i != m2.end()) {
166         if (ctr++ % 2 == 0)
167             m2.erase(i++);
168         else
169             ++i;
170         }
171 
172     assert (only_deletions (m, m2));
173     }
174 #endif
175 }
176