提交 1f4753d0 编写于 作者: N Nathan Hourt

Add utilities/parallel_markers.hpp

Move logic from apply_context::unused_authorizations out to a header so
other code can use it.

The logic in question takes an array of data, and a parallel array of
"markers" (booleans, in this instance), and returns a range containing
only the data elements whose corresponding marker matches a provided
value (i.e. the boolean was true).

Stay tuned to see how else I use this code! ;)
上级 ae4e4c7f
......@@ -4,11 +4,10 @@
#include <eos/chain/key_value_object.hpp>
#include <eos/chain/chain_controller.hpp>
#include <eos/utilities/parallel_markers.hpp>
#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <boost/range/combine.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
namespace eos { namespace chain {
......@@ -48,15 +47,7 @@ bool apply_context::all_authorizations_used() const {
}
vector<types::AccountPermission> apply_context::unused_authorizations() const {
auto RemoveUsed = boost::adaptors::filtered([](const auto& tuple) {
return !boost::get<0>(tuple);
});
auto ToPermission = boost::adaptors::transformed([](const auto& tuple) {
return boost::get<1>(tuple);
});
// zip the parallel arrays, filter out the used authorizations, and return just the permissions that are left
auto range = boost::combine(used_authorizations, msg.authorization) | RemoveUsed | ToPermission;
auto range = utilities::FilterDataByMarker(msg.authorization, used_authorizations, false);
return {range.begin(), range.end()};
}
......
#pragma once
#include <boost/range/combine.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
namespace eos { namespace utilities {
/**
* @brief Return values in DataRange corresponding to matching Markers
*
* Takes two parallel ranges, a Data range containing data values, and a Marker range containing markers on the
* corresponding data values. Returns a new Data range containing only the values corresponding to markers which match
* markerValue
*
* For example:
* @code{.cpp}
* vector<char> data = {'A', 'B', 'C'};
* vector<bool> markers = {true, false, true};
* auto markedData = FilterDataByMarker(data, markers, true);
* // markedData contains {'A', 'C'}
* @endcode
*/
template<typename DataRange, typename MarkerRange, typename Marker>
DataRange FilterDataByMarker(DataRange data, MarkerRange markers, const Marker& markerValue) {
auto RemoveMismatchedMarkers = boost::adaptors::filtered([&markerValue](const auto& tuple) {
return boost::get<0>(tuple) == markerValue;
});
auto ToData = boost::adaptors::transformed([](const auto& tuple) {
return boost::get<1>(tuple);
});
// Zip the ranges together, filter out data with markers that don't match, and return the data without the markers
auto range = boost::combine(markers, data) | RemoveMismatchedMarkers | ToData;
return {range.begin(), range.end()};
}
}} // namespace eos::utilities
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册