1 #ifndef BENCHMARK_GENERATE_INPUT_HPP
2 #define BENCHMARK_GENERATE_INPUT_HPP
3
4 #include <algorithm>
5 #include <random>
6 #include <vector>
7 #include <string>
8 #include <climits>
9 #include <cstddef>
10
11 static const char Letters[] = {
12 '0','1','2','3','4',
13 '5','6','7','8','9',
14 'A','B','C','D','E','F',
15 'G','H','I','J','K',
16 'L','M','N','O','P',
17 'Q','R','S','T','U',
18 'V','W','X','Y','Z',
19 'a','b','c','d','e','f',
20 'g','h','i','j','k',
21 'l','m','n','o','p',
22 'q','r','s','t','u',
23 'v','w','x','y','z'
24 };
25 static const std::size_t LettersSize = sizeof(Letters);
26
getRandomEngine()27 inline std::default_random_engine& getRandomEngine() {
28 static std::default_random_engine RandEngine(std::random_device{}());
29 return RandEngine;
30 }
31
getRandomChar()32 inline char getRandomChar() {
33 std::uniform_int_distribution<> LettersDist(0, LettersSize-1);
34 return Letters[LettersDist(getRandomEngine())];
35 }
36
37 template <class IntT>
getRandomInteger()38 inline IntT getRandomInteger() {
39 std::uniform_int_distribution<IntT> dist;
40 return dist(getRandomEngine());
41 }
42
getRandomString(std::size_t Len)43 inline std::string getRandomString(std::size_t Len) {
44 std::string str(Len, 0);
45 std::generate_n(str.begin(), Len, &getRandomChar);
46 return str;
47 }
48
49 template <class IntT>
getDuplicateIntegerInputs(size_t N)50 inline std::vector<IntT> getDuplicateIntegerInputs(size_t N) {
51 std::vector<IntT> inputs(N, static_cast<IntT>(-1));
52 return inputs;
53 }
54
55 template <class IntT>
getSortedIntegerInputs(size_t N)56 inline std::vector<IntT> getSortedIntegerInputs(size_t N) {
57 std::vector<IntT> inputs;
58 for (size_t i=0; i < N; i += 1)
59 inputs.push_back(i);
60 return inputs;
61 }
62
63 template <class IntT>
getSortedLargeIntegerInputs(size_t N)64 std::vector<IntT> getSortedLargeIntegerInputs(size_t N) {
65 std::vector<IntT> inputs;
66 for (size_t i=0; i < N; ++i) {
67 inputs.push_back(i + N);
68 }
69 return inputs;
70 }
71
72 template <class IntT>
getSortedTopBitsIntegerInputs(size_t N)73 std::vector<IntT> getSortedTopBitsIntegerInputs(size_t N) {
74 std::vector<IntT> inputs = getSortedIntegerInputs<IntT>(N);
75 for (auto& E : inputs) E <<= ((sizeof(IntT) / 2) * CHAR_BIT);
76 return inputs;
77 }
78
79 template <class IntT>
getReverseSortedIntegerInputs(size_t N)80 inline std::vector<IntT> getReverseSortedIntegerInputs(size_t N) {
81 std::vector<IntT> inputs;
82 std::size_t i = N;
83 while (i > 0) {
84 --i;
85 inputs.push_back(i);
86 }
87 return inputs;
88 }
89
90 template <class IntT>
getPipeOrganIntegerInputs(size_t N)91 std::vector<IntT> getPipeOrganIntegerInputs(size_t N) {
92 std::vector<IntT> v; v.reserve(N);
93 for (size_t i = 0; i < N/2; ++i) v.push_back(i);
94 for (size_t i = N/2; i < N; ++i) v.push_back(N - i);
95 return v;
96 }
97
98
99 template <class IntT>
getRandomIntegerInputs(size_t N)100 std::vector<IntT> getRandomIntegerInputs(size_t N) {
101 std::vector<IntT> inputs;
102 for (size_t i=0; i < N; ++i) {
103 inputs.push_back(getRandomInteger<IntT>());
104 }
105 return inputs;
106 }
107
getDuplicateStringInputs(size_t N)108 inline std::vector<std::string> getDuplicateStringInputs(size_t N) {
109 std::vector<std::string> inputs(N, getRandomString(1024));
110 return inputs;
111 }
112
getRandomStringInputs(size_t N)113 inline std::vector<std::string> getRandomStringInputs(size_t N) {
114 std::vector<std::string> inputs;
115 for (size_t i=0; i < N; ++i) {
116 inputs.push_back(getRandomString(1024));
117 }
118 return inputs;
119 }
120
getSortedStringInputs(size_t N)121 inline std::vector<std::string> getSortedStringInputs(size_t N) {
122 std::vector<std::string> inputs = getRandomStringInputs(N);
123 std::sort(inputs.begin(), inputs.end());
124 return inputs;
125 }
126
getReverseSortedStringInputs(size_t N)127 inline std::vector<std::string> getReverseSortedStringInputs(size_t N) {
128 std::vector<std::string> inputs = getSortedStringInputs(N);
129 std::reverse(inputs.begin(), inputs.end());
130 return inputs;
131 }
132
getRandomCStringInputs(size_t N)133 inline std::vector<const char*> getRandomCStringInputs(size_t N) {
134 static std::vector<std::string> inputs = getRandomStringInputs(N);
135 std::vector<const char*> cinputs;
136 for (auto const& str : inputs)
137 cinputs.push_back(str.c_str());
138 return cinputs;
139 }
140
141
142 #endif // BENCHMARK_GENERATE_INPUT_HPP
143