Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Add utilities/parallel_markers.hpp
Browse files Browse the repository at this point in the history
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! ;)
  • Loading branch information
nathanielhourt committed Aug 9, 2017
1 parent ae4e4c7 commit 1f4753d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
15 changes: 3 additions & 12 deletions libraries/chain/message_handling_contexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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()};
}

Expand Down
39 changes: 39 additions & 0 deletions libraries/utilities/include/eos/utilities/parallel_markers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#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

0 comments on commit 1f4753d

Please sign in to comment.