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 27 inline std::default_random_engine& getRandomEngine() { 28 static std::default_random_engine RandEngine(std::random_device{}()); 29 return RandEngine; 30 } 31 32 33 inline char getRandomChar() { 34 std::uniform_int_distribution<> LettersDist(0, LettersSize-1); 35 return Letters[LettersDist(getRandomEngine())]; 36 } 37 38 template <class IntT> 39 inline IntT getRandomInteger(IntT Min = 0, 40 IntT Max = std::numeric_limits<IntT>::max()) { 41 std::uniform_int_distribution<IntT> dist(Min, Max); 42 return dist(getRandomEngine()); 43 } 44 45 inline std::string getRandomString(std::size_t Len) { 46 std::string str(Len, 0); 47 std::generate_n(str.begin(), Len, &getRandomChar); 48 return str; 49 } 50 51 template <class IntT> 52 inline std::vector<IntT> getDuplicateIntegerInputs(size_t N) { 53 std::vector<IntT> inputs(N, static_cast<IntT>(-1)); 54 return inputs; 55 } 56 57 template <class IntT> 58 inline std::vector<IntT> getSortedIntegerInputs(size_t N) { 59 std::vector<IntT> inputs; 60 for (size_t i=0; i < N; i += 1) 61 inputs.push_back(i); 62 return inputs; 63 } 64 65 template <class IntT> 66 std::vector<IntT> getSortedLargeIntegerInputs(size_t N) { 67 std::vector<IntT> inputs; 68 for (size_t i=0; i < N; ++i) { 69 inputs.push_back(i + N); 70 } 71 return inputs; 72 } 73 74 template <class IntT> 75 std::vector<IntT> getSortedTopBitsIntegerInputs(size_t N) { 76 std::vector<IntT> inputs = getSortedIntegerInputs<IntT>(N); 77 for (auto& E : inputs) E <<= ((sizeof(IntT) / 2) * CHAR_BIT); 78 return inputs; 79 } 80 81 template <class IntT> 82 inline std::vector<IntT> getReverseSortedIntegerInputs(size_t N) { 83 std::vector<IntT> inputs; 84 std::size_t i = N; 85 while (i > 0) { 86 --i; 87 inputs.push_back(i); 88 } 89 return inputs; 90 } 91 92 template <class IntT> 93 std::vector<IntT> getPipeOrganIntegerInputs(size_t N) { 94 std::vector<IntT> v; v.reserve(N); 95 for (size_t i = 0; i < N/2; ++i) v.push_back(i); 96 for (size_t i = N/2; i < N; ++i) v.push_back(N - i); 97 return v; 98 } 99 100 101 template <class IntT> 102 std::vector<IntT> getRandomIntegerInputs(size_t N) { 103 std::vector<IntT> inputs; 104 for (size_t i=0; i < N; ++i) { 105 inputs.push_back(getRandomInteger<IntT>()); 106 } 107 return inputs; 108 } 109 110 inline std::vector<std::string> getDuplicateStringInputs(size_t N) { 111 std::vector<std::string> inputs(N, getRandomString(1024)); 112 return inputs; 113 } 114 115 inline std::vector<std::string> getRandomStringInputs(size_t N) { 116 std::vector<std::string> inputs; 117 for (size_t i=0; i < N; ++i) { 118 inputs.push_back(getRandomString(1024)); 119 } 120 return inputs; 121 } 122 123 inline std::vector<std::string> getSortedStringInputs(size_t N) { 124 std::vector<std::string> inputs = getRandomStringInputs(N); 125 std::sort(inputs.begin(), inputs.end()); 126 return inputs; 127 } 128 129 inline std::vector<std::string> getReverseSortedStringInputs(size_t N) { 130 std::vector<std::string> inputs = getSortedStringInputs(N); 131 std::reverse(inputs.begin(), inputs.end()); 132 return inputs; 133 } 134 135 inline std::vector<const char*> getRandomCStringInputs(size_t N) { 136 static std::vector<std::string> inputs = getRandomStringInputs(N); 137 std::vector<const char*> cinputs; 138 for (auto const& str : inputs) 139 cinputs.push_back(str.c_str()); 140 return cinputs; 141 } 142 143 144 #endif // BENCHMARK_GENERATE_INPUT_HPP 145