CreateData.cxx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <random>
  4. #include <vector>
  5. #include "json.hpp"
  6. int main(int argc, char** argv) {
  7. if (argc != 2) {
  8. std::cerr << "Usage: " << argv[0] << "numOps" << std::endl;
  9. exit(EXIT_FAILURE);
  10. }
  11. unsigned int numOps = 0;
  12. if (sscanf(argv[1], "%d", &numOps) != 1 || numOps == 0) {
  13. std::cerr << "Usage: " << argv[0] << "numOps" << std::endl;
  14. std::cerr << "numOps must be a positive integer" << std::endl;
  15. exit(EXIT_FAILURE);
  16. }
  17. // C++11 random number tutorial: https://gist.github.com/PhDP/5289449
  18. // Seed random number generator
  19. std::mt19937_64 rng(time(0));
  20. // Create uniform distribution
  21. std::uniform_int_distribution<int> unif(
  22. std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
  23. std::uniform_int_distribution<int> opDist(0,10);
  24. nlohmann::json result;
  25. std::vector<int> keys;
  26. result["metadata"]["numOps"] = numOps;
  27. unsigned int totalZeros = (int) floor(log10((double) numOps)) + 1;
  28. for (size_t op = 1; op <= numOps; op++) {
  29. int operation = opDist(rng);
  30. int opDigits = (int) floor(log10((double) op)) + 1;
  31. std::string opKey = std::string(totalZeros - opDigits, '0')
  32. .append(std::to_string(op));
  33. if (operation == 0 && !keys.empty()) {
  34. result[opKey] = nlohmann::json();
  35. result[opKey]["operation"] = "Delete";
  36. result[opKey]["key"] = keys.back();
  37. keys.pop_back();
  38. } else if (operation == 1 && !keys.empty()) {
  39. result[opKey] = nlohmann::json();
  40. result[opKey]["operation"] = "DeleteMin";
  41. std::sort(keys.rbegin(), keys.rend());
  42. keys.pop_back();
  43. } else {
  44. int x = unif(rng);
  45. result[opKey] = nlohmann::json();
  46. result[opKey]["operation"] = "Insert";
  47. result[opKey]["key"] = x;
  48. keys.push_back(x);
  49. }
  50. }
  51. std::cout << result.dump(2) << std::endl;
  52. }