#include #include #include #include #include #include #include int main(int argc, char** argv) { // Defining values and two map vectors std::vector values{10,20,30,40,50,60}; std::vector first_map{3,5,1,3,4,2,2,4}; std::vector second_map{2,2,5,1,3,5,4,2}; for (auto iter = first_map.begin(); iter != first_map.end(); ++iter) { std::cout << *iter << "\t"; } std::cout << std::endl; for (auto iter = second_map.begin(); iter != second_map.end(); ++iter) { std::cout << *iter << "\t"; } std::cout << std::endl; // Gathering the values std::vector first_gathered_values(first_map.size()); std::vector second_gathered_values(second_map.size()); std::transform( std::execution::par, first_map.begin(), first_map.end(), // Input first_gathered_values.begin(), // Output [p_values = values.data()](int key) { return p_values[key]; } ); std::transform( std::execution::par, second_map.begin(), second_map.end(), // Input second_gathered_values.begin(), // Output [p_values = values.data()](int key) { return p_values[key]; } ); for (auto iter = first_gathered_values.begin(); iter != first_gathered_values.end(); ++iter) { std::cout << *iter << "\t"; } std::cout << std::endl; for (auto iter = second_gathered_values.begin(); iter != second_gathered_values.end(); ++iter) { std::cout << *iter << "\t"; } std::cout << std::endl; // Transform - If first gathered value is greater than second gathered value, return first_id std::vector outputs(first_map.size()); auto counting_view = std::ranges::views::iota(0, int{first_map.size()}); std::transform( std::execution::par, counting_view.begin(), counting_view.end(), // Input outputs.begin(), // Output [ p_first_map = first_map.data(), p_second_map = second_map.data(), p_first_gathered_values = first_gathered_values.data(), p_second_gathered_values = second_gathered_values.data()] (int key) { int first_id = p_first_map[key]; int second_id = p_second_map[key]; int first_value = p_first_gathered_values[key]; int second_value = p_second_gathered_values[key]; int selected_id = first_id; if (first_value < second_value) { selected_id = second_id; } return selected_id; } ); for (auto iter = outputs.begin(); iter != outputs.end(); ++iter) { std::cout << *iter << "\t"; } std::cout << std::endl; // Sort and remove duplicates std::sort(std::execution::par, outputs.begin(), outputs.end()); auto new_end = std::unique(std::execution::par, outputs.begin(), outputs.end()); int new_size = new_end - outputs.begin(); for (auto iter = outputs.begin(); iter != new_end; ++iter) { std::cout << *iter << "\t"; } std::cout << std::endl; // Scatter a new value std::for_each( std::execution::par, outputs.begin(), new_end, // Input [p_values = values.data(), new_value = 123](int key) { p_values[key] = new_value; } ); for (auto iter = values.begin(); iter != values.end(); ++iter) { std::cout << *iter << "\t"; } std::cout << std::endl; }