File: GenerateInput.h

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (174 lines) | stat: -rw-r--r-- 5,363 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef BENCHMARK_GENERATE_INPUT_H
#define BENCHMARK_GENERATE_INPUT_H

#include <algorithm>
#include <climits>
#include <cstddef>
#include <random>
#include <string>
#include <vector>

static const char Letters[] = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
    'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
static const std::size_t LettersSize = sizeof(Letters);

inline std::default_random_engine& getRandomEngine() {
  static std::default_random_engine RandEngine(std::random_device{}());
  return RandEngine;
}

inline char getRandomChar() {
  std::uniform_int_distribution<> LettersDist(0, LettersSize - 1);
  return Letters[LettersDist(getRandomEngine())];
}

template <class IntT>
inline IntT getRandomInteger(IntT Min, IntT Max) {
  std::uniform_int_distribution<unsigned long long> dist(Min, Max);
  return static_cast<IntT>(dist(getRandomEngine()));
}

inline std::string getRandomString(std::size_t Len) {
  std::string str(Len, 0);
  std::generate_n(str.begin(), Len, &getRandomChar);
  return str;
}

template <class IntT>
inline std::vector<IntT> getDuplicateIntegerInputs(std::size_t N) {
  std::vector<IntT> inputs(N, static_cast<IntT>(-1));
  return inputs;
}

template <class IntT>
inline std::vector<IntT> getSortedIntegerInputs(std::size_t N) {
  std::vector<IntT> inputs;
  inputs.reserve(N);
  for (std::size_t i = 0; i < N; i += 1)
    inputs.push_back(i);
  return inputs;
}

template <class IntT>
std::vector<IntT> getSortedLargeIntegerInputs(std::size_t N) {
  std::vector<IntT> inputs;
  inputs.reserve(N);
  for (std::size_t i = 0; i < N; ++i)
    inputs.push_back(i + N);
  return inputs;
}

template <class IntT>
std::vector<IntT> getSortedTopBitsIntegerInputs(std::size_t N) {
  std::vector<IntT> inputs = getSortedIntegerInputs<IntT>(N);
  for (auto& E : inputs)
    E <<= ((sizeof(IntT) / 2) * CHAR_BIT);
  return inputs;
}

template <class IntT>
inline std::vector<IntT> getReverseSortedIntegerInputs(std::size_t N) {
  std::vector<IntT> inputs;
  inputs.reserve(N);
  std::size_t i = N;
  while (i > 0) {
    --i;
    inputs.push_back(i);
  }
  return inputs;
}

template <class IntT>
std::vector<IntT> getPipeOrganIntegerInputs(std::size_t N) {
  std::vector<IntT> v;
  v.reserve(N);
  for (std::size_t i = 0; i < N / 2; ++i)
    v.push_back(i);
  for (std::size_t i = N / 2; i < N; ++i)
    v.push_back(N - i);
  return v;
}

template <class IntT>
std::vector<IntT> getRandomIntegerInputs(std::size_t N) {
  std::vector<IntT> inputs;
  inputs.reserve(N);
  for (std::size_t i = 0; i < N; ++i)
    inputs.push_back(getRandomInteger<IntT>(0, std::numeric_limits<IntT>::max()));
  return inputs;
}

inline std::vector<std::string> getRandomStringInputsWithLength(std::size_t N, std::size_t len) { // N-by-len
  std::vector<std::string> inputs;
  inputs.reserve(N);
  for (std::size_t i = 0; i < N; ++i)
    inputs.push_back(getRandomString(len));
  return inputs;
}

inline std::vector<std::string> getDuplicateStringInputs(std::size_t N) {
  std::vector<std::string> inputs(N, getRandomString(1024));
  return inputs;
}

inline std::vector<std::string> getRandomStringInputs(std::size_t N) {
  return getRandomStringInputsWithLength(N, 1024);
}

template <class IntT>
std::vector<std::vector<IntT>> getRandomIntegerInputsWithLength(std::size_t N, std::size_t len) { // N-by-len
  std::vector<std::vector<IntT>> inputs;
  inputs.reserve(N);
  for (std::size_t i = 0; i < N; ++i)
    inputs.push_back(getRandomIntegerInputs<IntT>(len));
  return inputs;
}

inline std::vector<std::string> getSSORandomStringInputs(size_t N) {
  std::vector<std::string> inputs;
  for (size_t i = 0; i < N; ++i)
    inputs.push_back(getRandomString(10)); // SSO
  return inputs;
}

inline std::vector<std::string> getPrefixedRandomStringInputs(size_t N) {
  std::vector<std::string> inputs;
  inputs.reserve(N);
  constexpr int kSuffixLength = 32;
  const std::string prefix    = getRandomString(1024 - kSuffixLength);
  for (std::size_t i = 0; i < N; ++i)
    inputs.push_back(prefix + getRandomString(kSuffixLength));
  return inputs;
}

inline std::vector<std::string> getSortedStringInputs(std::size_t N) {
  std::vector<std::string> inputs = getRandomStringInputs(N);
  std::sort(inputs.begin(), inputs.end());
  return inputs;
}

inline std::vector<std::string> getReverseSortedStringInputs(std::size_t N) {
  std::vector<std::string> inputs = getSortedStringInputs(N);
  std::reverse(inputs.begin(), inputs.end());
  return inputs;
}

inline std::vector<const char*> getRandomCStringInputs(std::size_t N) {
  static std::vector<std::string> inputs = getRandomStringInputs(N);
  std::vector<const char*> cinputs;
  for (auto const& str : inputs)
    cinputs.push_back(str.c_str());
  return cinputs;
}

#endif // BENCHMARK_GENERATE_INPUT_H