1 /*
2 	This file is part of solidity.
3 
4 	solidity is free software: you can redistribute it and/or modify
5 	it under the terms of the GNU General Public License as published by
6 	the Free Software Foundation, either version 3 of the License, or
7 	(at your option) any later version.
8 
9 	solidity is distributed in the hope that it will be useful,
10 	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 	GNU General Public License for more details.
13 
14 	You should have received a copy of the GNU General Public License
15 	along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 // SPDX-License-Identifier: GPL-3.0
18 
19 #include <map>
20 
21 // Contains polyfills of STL functions and algorithms that will become available in C++20.
22 namespace solidity::cxx20
23 {
24 
25 // Taken from https://en.cppreference.com/w/cpp/container/map/erase_if.
26 template<class Key, class T, class Compare, class Alloc, class Pred>
erase_if(std::map<Key,T,Compare,Alloc> & _c,Pred _pred)27 typename std::map<Key, T, Compare, Alloc>::size_type erase_if(std::map<Key,T,Compare,Alloc>& _c, Pred _pred)
28 {
29 	auto old_size = _c.size();
30 	for (auto i = _c.begin(), last = _c.end(); i != last;)
31 		if (_pred(*i))
32 			i = _c.erase(i);
33 		else
34 			++i;
35 	return old_size - _c.size();
36 }
37 
38 // Taken from https://en.cppreference.com/w/cpp/container/unordered_map/erase_if.
39 template<class Key, class T, class Hash, class KeyEqual, class Alloc, class Pred>
40 typename std::unordered_map<Key, T, Hash, KeyEqual, Alloc>::size_type
erase_if(std::unordered_map<Key,T,Hash,KeyEqual,Alloc> & _c,Pred _pred)41 erase_if(std::unordered_map<Key, T, Hash, KeyEqual, Alloc>& _c, Pred _pred)
42 {
43 	auto old_size = _c.size();
44 	for (auto i = _c.begin(), last = _c.end(); i != last;)
45 		if (_pred(*i))
46 			i = _c.erase(i);
47 		else
48 			++i;
49 	return old_size - _c.size();
50 }
51 
52 // Taken from https://en.cppreference.com/w/cpp/container/vector/erase2
53 template<class T, class Alloc, class Pred>
54 constexpr typename std::vector<T, Alloc>::size_type
erase_if(std::vector<T,Alloc> & c,Pred pred)55 erase_if(std::vector<T, Alloc>& c, Pred pred)
56 {
57 	auto it = std::remove_if(c.begin(), c.end(), pred);
58 	auto r = std::distance(it, c.end());
59 	c.erase(it, c.end());
60 	return static_cast<typename std::vector<T, Alloc>::size_type>(r);
61 }
62 
63 }
64