|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <vector> |
| 4 | +#include <array> |
| 5 | +#include <string> |
| 6 | +#include <random> |
| 7 | +#include <algorithm> |
| 8 | + |
| 9 | +template<typename RandomEngine = std::default_random_engine> |
| 10 | +class RandomNameGenerator{ |
| 11 | + struct DataCorrector{ |
| 12 | + void operator()(RandomNameGenerator *rng){ |
| 13 | + std::for_each( |
| 14 | + std::begin(rng->_containers), |
| 15 | + std::end(rng->_containers), |
| 16 | + [](ContainerType *container){ |
| 17 | + if(container->empty()) |
| 18 | + container->push_back(""); |
| 19 | + } |
| 20 | + ); |
| 21 | + } |
| 22 | + }; |
| 23 | +public: |
| 24 | + typedef std::vector<std::string> ContainerType; |
| 25 | + typedef ContainerType Prefixes; |
| 26 | + typedef ContainerType Cores; |
| 27 | + typedef ContainerType Sufixes; |
| 28 | + typedef std::string Result; |
| 29 | +public: |
| 30 | + RandomNameGenerator( |
| 31 | + const Prefixes &prefixes, |
| 32 | + const Cores &cores, |
| 33 | + const Sufixes &sufixes, |
| 34 | + RandomEngine &re |
| 35 | + ): _prefixes(prefixes), _cores(cores), _sufixes(sufixes), _re(re){ |
| 36 | + constructContainersArray(); |
| 37 | + DataCorrector()(this); |
| 38 | + constructDistros(); |
| 39 | + } |
| 40 | + |
| 41 | + Result operator()( |
| 42 | + size_t minCoreRepeats, |
| 43 | + size_t maxCoreRepeats |
| 44 | + ){ |
| 45 | + Result result; |
| 46 | + Distro coreRepeatsDistribution(minCoreRepeats, maxCoreRepeats); |
| 47 | + result += _prefixes[_distros[PrefixID](_re)]; |
| 48 | + |
| 49 | + size_t coreRepeats = coreRepeatsDistribution(_re); |
| 50 | + while(coreRepeats--) |
| 51 | + result += _cores[_distros[CoreID](_re)]; |
| 52 | + |
| 53 | + return result += _sufixes[_distros[SufixID](_re)]; |
| 54 | + } |
| 55 | +private: |
| 56 | + void constructDistros(){ |
| 57 | + _distros = std::array<Distro, Count>{ |
| 58 | + Distro(0, _prefixes.size()-1), |
| 59 | + Distro(0, _cores.size()-1), |
| 60 | + Distro(0, _sufixes.size()-1) |
| 61 | + }; |
| 62 | + } |
| 63 | + void constructContainersArray(){ |
| 64 | + _containers = std::array<ContainerType *, Count>{ |
| 65 | + &_prefixes, &_cores, &_sufixes |
| 66 | + }; |
| 67 | + } |
| 68 | +private: |
| 69 | + typedef std::uniform_int_distribution<int> Distro; |
| 70 | + enum{ PrefixID, CoreID, SufixID, Count /* <-- Have to be the last!*/}; |
| 71 | + std::array<Distro, Count> _distros; |
| 72 | + std::array<ContainerType *, Count> _containers; |
| 73 | +private: |
| 74 | + Prefixes _prefixes; |
| 75 | + Cores _cores; |
| 76 | + Sufixes _sufixes; |
| 77 | + RandomEngine &_re; |
| 78 | +}; |
0 commit comments