clean-code/VectorFiller/atlas/collection/impl/AutoThreadedVectorFactory.h

34 lines
1 KiB
C++

#pragma once
#include "../../generator/generator.h"
#include "AbstractVectorFactory.h"
#include <algorithm>
#include <execution>
namespace atlas::collection {
template <typename T>
class AutoThreadedVectorFactory : public AbstractVectorFactory<T> {
public:
AutoThreadedVectorFactory(std::function<std::unique_ptr<generator::Generator<T>>()> generator_factory)
: m_gen { std::move(generator_factory) }
{
}
AutoThreadedVectorFactory(AutoThreadedVectorFactory const&) = delete;
AutoThreadedVectorFactory(AutoThreadedVectorFactory&&) = delete;
AutoThreadedVectorFactory& operator=(AutoThreadedVectorFactory const&) = delete;
AutoThreadedVectorFactory& operator=(AutoThreadedVectorFactory&&) = delete;
auto fill(std::vector<T>& v) -> void override final
{
std::for_each(std::execution::par_unseq, v.begin(), v.end(), [this](auto& v) {
static thread_local auto gen { m_gen() };
v = gen->next();
});
}
private:
std::function<std::unique_ptr<generator::Generator<T>>()> m_gen;
};
}