Implement cxx20 polyfill and replace InvertibleMap entirely.

This commit is contained in:
Daniel Kirchner
2021-01-12 21:40:40 +01:00
parent 625d402dbb
commit 7fe03cbab0
7 changed files with 99 additions and 131 deletions
+1 -1
View File
@@ -8,6 +8,7 @@ set(sources
CommonData.h
CommonIO.cpp
CommonIO.h
cxx20.h
Exceptions.cpp
Exceptions.h
ErrorCodes.h
@@ -15,7 +16,6 @@ set(sources
FunctionSelector.h
IndentedWriter.cpp
IndentedWriter.h
InvertibleMap.h
IpfsHash.cpp
IpfsHash.h
JSON.cpp
-66
View File
@@ -1,66 +0,0 @@
/*
This file is part of solidity.
solidity 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 of the License, or
(at your option) any later version.
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <unordered_map>
/**
* Data structure that keeps track of values and keys of a mapping.
*/
template <class K, class V>
struct InvertibleMap
{
std::unordered_map<K, V> values;
void set(K _key, V _value)
{
values[_key] = _value;
}
std::optional<V> fetch(K _key)
{
auto it = values.find(_key);
if (it == values.end())
return std::nullopt;
else
return it->second;
}
void eraseKey(K _key)
{
values.erase(_key);
}
void eraseValue(V _value)
{
auto it = values.begin();
while (it != values.end())
{
if (it->second == _value)
it = values.erase(it);
else
++it;
}
}
void clear()
{
values.clear();
}
};
+52
View File
@@ -0,0 +1,52 @@
/*
This file is part of solidity.
solidity 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 of the License, or
(at your option) any later version.
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <map>
// Contains polyfills of STL functions and algorithms that will become available in C++20.
namespace solidity::cxx20
{
// Taken from https://en.cppreference.com/w/cpp/container/map/erase_if.
template< class Key, class T, class Compare, class Alloc, class Pred >
typename std::map<Key,T,Compare,Alloc>::size_type erase_if(std::map<Key,T,Compare,Alloc>& c, Pred pred)
{
auto old_size = c.size();
for (auto i = c.begin(), last = c.end(); i != last;)
if (pred(*i))
i = c.erase(i);
else
++i;
return old_size - c.size();
}
// Taken from https://en.cppreference.com/w/cpp/container/unordered_map/erase_if.
template<class Key, class T, class Hash, class KeyEqual, class Alloc, class Pred>
typename std::unordered_map<Key,T,Hash,KeyEqual,Alloc>::size_type
erase_if(std::unordered_map<Key,T,Hash,KeyEqual,Alloc>& c, Pred pred)
{
auto old_size = c.size();
for (auto i = c.begin(), last = c.end(); i != last;)
if (pred(*i))
i = c.erase(i);
else
++i;
return old_size - c.size();
}
}