Order-preserving selection from an index table is trivial in serial code, but in multi-threading is less straightforward, in particular if one wants to retain efficiency (the whole point of multi-threading) by avoiding linked lists. Consider the serial code
template<typename T>
std::vector<T> select_in_order(
std::vector<std::size_t> const&keys, // permutation of 0 ... key.size()-1
std::vector<T> const&data) // anything copyable
{ // select data[keys[i]] allowing keys.size() >= data.size()
std::vector<T> result;
for(auto key:keys)
if(key<data.size())
result.push_back(data[key]);
return result;
}
How can I do this multi-threaded (say using TBB or even OpenMP), in particular if data.size() < key.size()
?