Skip to content

Instantly share code, notes, and snippets.

@sjolsen
Created January 10, 2014 21:39
Show Gist options
  • Save sjolsen/8363193 to your computer and use it in GitHub Desktop.
Save sjolsen/8363193 to your computer and use it in GitHub Desktop.
Program to generate input for xor_key.cc
#include <random>
using integer = int;
using distribution = std::uniform_int_distribution <integer>;
std::mt19937 engine {};
#include <iostream>
void make_key (integer N, std::ostream& output)
{
static distribution x_dist {0, (1 << 15) - 1}; // [0, 2^15)
for (integer i = 1; i <= N; ++i)
{
output << x_dist (engine);
if (i < N)
output << ' ';
else
output << '\n';
}
}
void make_query (integer N, std::ostream& output)
{
static distribution a_dist {0, (1 << 15) - 1}; // [0, 2^15)
distribution qs_dist {1, N};
const integer query_size = qs_dist (engine);
distribution qoff_dist {0, N - query_size};
const integer query_offset = qoff_dist (engine);
const integer a = a_dist (engine);
const integer p = query_offset + 1; // p and q are 1-based indices
const integer q = p + query_size - 1; // Query range is inclusive of q
output << a << ' ' << p << ' ' << q << '\n';
}
void make_test_case (std::ostream& output)
{
static distribution N_dist {1, 100000};
static distribution Q_dist {1, 50000};
const integer N = N_dist (engine);
const integer Q = Q_dist (engine);
output << N << ' ' << Q << '\n';
make_key (N, output);
for (integer query = 1; query <= Q; ++query)
make_query (N, output);
}
void make_tests (integer T, std::ostream& output)
{
output << T << '\n';
for (integer test_case = 1; test_case <= T; ++test_case)
{
make_test_case (output);
output.flush ();
}
}
#include <fstream>
#include <cstring>
void xor_testcase (int argc, const char* const* argv)
{
std::ofstream output_file;
std::ostream* output;
integer T = 1;
if (argc >= 2)
T = std::stoi (argv [1]);
if (argc < 3 ||
std::strcmp (argv [2], "-") == 0)
{
output = &std::cout;
}
else
{
output_file.open (argv [2]);
output = &output_file;
}
make_tests (T, *output);
}
int main (int argc, const char* const* argv)
{
try
{
xor_testcase (argc, argv);
}
catch (const std::exception& e)
{
std::cerr << e.what () << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment