91 lines
1.4 KiB
C++
91 lines
1.4 KiB
C++
#ifndef ALSK_ALSK_CONTEXT_CONTEXT_H
|
|
#define ALSK_ALSK_CONTEXT_CONTEXT_H
|
|
|
|
#include <functional>
|
|
#include <tuple>
|
|
#include <vector>
|
|
|
|
#include <random>
|
|
|
|
#include "../skeleton/link/args/placeholders.h"
|
|
|
|
namespace alsk {
|
|
|
|
template<typename RNG>
|
|
class Context {
|
|
public:
|
|
using Id = std::size_t;
|
|
|
|
using Args = std::tuple<Id, RNG&>;
|
|
|
|
using Seed = decltype(+RNG::default_seed);
|
|
|
|
private:
|
|
Id _idCount;
|
|
|
|
std::vector<RNG> _rng;
|
|
std::vector<Args> _args;
|
|
|
|
public:
|
|
Seed seed;
|
|
|
|
public:
|
|
constexpr Context(): _idCount{0}, seed{RNG::default_seed} {}
|
|
|
|
void setup(Id idCount = 30) {
|
|
if(idCount <= _idCount) return;
|
|
Id baseId = _idCount;
|
|
_idCount = idCount;
|
|
|
|
RNG seeder{seed};
|
|
|
|
_rng.reserve(idCount);
|
|
for(Id id = baseId; id < idCount; ++id)
|
|
_rng.emplace_back(seeder());
|
|
|
|
_args.reserve(idCount);
|
|
for(Id id = baseId; id < idCount; ++id)
|
|
_args.emplace_back(id, std::ref(_rng[id]));
|
|
}
|
|
|
|
void reset() {
|
|
RNG seeder{seed};
|
|
|
|
_rng.clear();
|
|
for(Id id = 0; id < _idCount; ++id)
|
|
_rng.emplace_back(seeder());
|
|
|
|
_args.clear();
|
|
for(Id id = 0; id < _idCount; ++id)
|
|
_args.emplace_back(id, std::ref(_rng[id]));
|
|
}
|
|
|
|
constexpr Id maxId() const noexcept { return _idCount; }
|
|
|
|
Args& args(Id const& id) { return _args[id]; }
|
|
|
|
~Context() noexcept {}
|
|
};
|
|
|
|
using DefaultContext = Context<std::mt19937>;
|
|
|
|
namespace arg {
|
|
|
|
/**
|
|
* CtxId
|
|
* to get own's context identifier
|
|
*/
|
|
using CtxId = C<0>;
|
|
|
|
/**
|
|
* RNG
|
|
* for random number generator
|
|
*/
|
|
using RNG = C<1>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|