Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / benchmarks / GenerateInput.h
blobdb376614382bc494273df277f932bd6729ffba93
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef BENCHMARK_GENERATE_INPUT_H
10 #define BENCHMARK_GENERATE_INPUT_H
12 #include <algorithm>
13 #include <climits>
14 #include <cstddef>
15 #include <random>
16 #include <string>
17 #include <vector>
19 static const char Letters[] = {
20 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
21 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
22 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
23 static const std::size_t LettersSize = sizeof(Letters);
25 inline std::default_random_engine& getRandomEngine() {
26 static std::default_random_engine RandEngine(std::random_device{}());
27 return RandEngine;
30 inline char getRandomChar() {
31 std::uniform_int_distribution<> LettersDist(0, LettersSize - 1);
32 return Letters[LettersDist(getRandomEngine())];
35 template <class IntT>
36 inline IntT getRandomInteger(IntT Min, IntT Max) {
37 std::uniform_int_distribution<unsigned long long> dist(Min, Max);
38 return static_cast<IntT>(dist(getRandomEngine()));
41 inline std::string getRandomString(std::size_t Len) {
42 std::string str(Len, 0);
43 std::generate_n(str.begin(), Len, &getRandomChar);
44 return str;
47 template <class IntT>
48 inline std::vector<IntT> getDuplicateIntegerInputs(std::size_t N) {
49 std::vector<IntT> inputs(N, static_cast<IntT>(-1));
50 return inputs;
53 template <class IntT>
54 inline std::vector<IntT> getSortedIntegerInputs(std::size_t N) {
55 std::vector<IntT> inputs;
56 inputs.reserve(N);
57 for (std::size_t i = 0; i < N; i += 1)
58 inputs.push_back(i);
59 return inputs;
62 template <class IntT>
63 std::vector<IntT> getSortedLargeIntegerInputs(std::size_t N) {
64 std::vector<IntT> inputs;
65 inputs.reserve(N);
66 for (std::size_t i = 0; i < N; ++i)
67 inputs.push_back(i + N);
68 return inputs;
71 template <class IntT>
72 std::vector<IntT> getSortedTopBitsIntegerInputs(std::size_t N) {
73 std::vector<IntT> inputs = getSortedIntegerInputs<IntT>(N);
74 for (auto& E : inputs)
75 E <<= ((sizeof(IntT) / 2) * CHAR_BIT);
76 return inputs;
79 template <class IntT>
80 inline std::vector<IntT> getReverseSortedIntegerInputs(std::size_t N) {
81 std::vector<IntT> inputs;
82 inputs.reserve(N);
83 std::size_t i = N;
84 while (i > 0) {
85 --i;
86 inputs.push_back(i);
88 return inputs;
91 template <class IntT>
92 std::vector<IntT> getPipeOrganIntegerInputs(std::size_t N) {
93 std::vector<IntT> v;
94 v.reserve(N);
95 for (std::size_t i = 0; i < N / 2; ++i)
96 v.push_back(i);
97 for (std::size_t i = N / 2; i < N; ++i)
98 v.push_back(N - i);
99 return v;
102 template <class IntT>
103 std::vector<IntT> getRandomIntegerInputs(std::size_t N) {
104 std::vector<IntT> inputs;
105 inputs.reserve(N);
106 for (std::size_t i = 0; i < N; ++i)
107 inputs.push_back(getRandomInteger<IntT>(0, std::numeric_limits<IntT>::max()));
108 return inputs;
111 inline std::vector<std::string> getDuplicateStringInputs(std::size_t N) {
112 std::vector<std::string> inputs(N, getRandomString(1024));
113 return inputs;
116 inline std::vector<std::string> getRandomStringInputs(std::size_t N) {
117 std::vector<std::string> inputs;
118 inputs.reserve(N);
119 for (std::size_t i = 0; i < N; ++i)
120 inputs.push_back(getRandomString(1024));
121 return inputs;
124 inline std::vector<std::string> getPrefixedRandomStringInputs(std::size_t N) {
125 std::vector<std::string> inputs;
126 inputs.reserve(N);
127 constexpr int kSuffixLength = 32;
128 const std::string prefix = getRandomString(1024 - kSuffixLength);
129 for (std::size_t i = 0; i < N; ++i)
130 inputs.push_back(prefix + getRandomString(kSuffixLength));
131 return inputs;
134 inline std::vector<std::string> getSortedStringInputs(std::size_t N) {
135 std::vector<std::string> inputs = getRandomStringInputs(N);
136 std::sort(inputs.begin(), inputs.end());
137 return inputs;
140 inline std::vector<std::string> getReverseSortedStringInputs(std::size_t N) {
141 std::vector<std::string> inputs = getSortedStringInputs(N);
142 std::reverse(inputs.begin(), inputs.end());
143 return inputs;
146 inline std::vector<const char*> getRandomCStringInputs(std::size_t N) {
147 static std::vector<std::string> inputs = getRandomStringInputs(N);
148 std::vector<const char*> cinputs;
149 for (auto const& str : inputs)
150 cinputs.push_back(str.c_str());
151 return cinputs;
154 #endif // BENCHMARK_GENERATE_INPUT_H