createheapoperationdata.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <random>
  4. #include <set>
  5. #include <string>
  6. #include "json.hpp"
  7. typedef unsigned int Key;
  8. Key createKey(std::mt19937_64& rng, std::set<Key>& insertedKeys);
  9. Key findKey(std::mt19937_64& rng, std::set<Key>& insertedKeys);
  10. int main(int argc, char** argv) {
  11. if (argc != 3) {
  12. printf("Usage: %s numOperations maxHeapSize\n", argv[0]);
  13. return EXIT_FAILURE;
  14. }
  15. unsigned int numOperations = 0, maxHeapSize = 0;
  16. if (sscanf(argv[1], "%u", &numOperations) != 1 || sscanf(argv[2], "%u", &maxHeapSize) != 1) {
  17. printf("numOperations and maxHeapSize must be positive integers\n");
  18. return EXIT_FAILURE;
  19. }
  20. nlohmann::json samples;
  21. std::set<Key> insertedKeys;
  22. size_t initialInserts = std::min(maxHeapSize, numOperations) / 2;
  23. size_t minHeapSize = 1 + maxHeapSize / 4;
  24. samples["metadata"]["numOperations"] = numOperations;
  25. samples["metadata"]["maxHeapSize"] = maxHeapSize;
  26. // C++11 random number tutorial: https://gist.github.com/PhDP/5289449
  27. // Seed random number generator
  28. std::random_device rd;
  29. std::mt19937_64 rng(rd());
  30. // Create distributions
  31. enum class HeapOperation { insert, removeMax, removeKey, change };
  32. std::uniform_int_distribution<int> opDist(0, 3);
  33. unsigned int totalZeros = (int) floor(log10((double) numOperations)) + 1;
  34. for (unsigned int op = 1; op <= numOperations; op++) {
  35. int opZeros = (int) floor(log10((double) op)) + 1;
  36. std::string opNum = std::string(totalZeros - opZeros, '0').append(std::to_string(op));
  37. nlohmann::json newOp;
  38. // Add random integer to array
  39. HeapOperation operation =
  40. ((op < initialInserts) ? HeapOperation::insert : (HeapOperation) opDist(rng));
  41. switch (operation) {
  42. case HeapOperation::insert:
  43. if (insertedKeys.size() < maxHeapSize) {
  44. newOp["operation"] = "insert";
  45. Key key = createKey(rng, insertedKeys);
  46. newOp["key"] = key;
  47. insertedKeys.insert(key);
  48. break;
  49. }
  50. case HeapOperation::removeMax:
  51. if (insertedKeys.size() > minHeapSize) {
  52. newOp["operation"] = "removeMax";
  53. insertedKeys.erase(--insertedKeys.end());
  54. break;
  55. }
  56. case HeapOperation::removeKey:
  57. if (insertedKeys.size() > minHeapSize) {
  58. newOp["operation"] = "removeKey";
  59. Key key = findKey(rng, insertedKeys);
  60. newOp["key"] = key;
  61. insertedKeys.erase(insertedKeys.find(key));
  62. break;
  63. }
  64. case HeapOperation::change:
  65. if (insertedKeys.size() > 1) {
  66. newOp["operation"] = "change";
  67. Key key = findKey(rng, insertedKeys);
  68. newOp["key"] = key;
  69. insertedKeys.erase(insertedKeys.find(key));
  70. Key newKey = createKey(rng, insertedKeys);
  71. newOp["newKey"] = newKey;
  72. insertedKeys.insert(newKey);
  73. break;
  74. }
  75. default:
  76. op--;
  77. continue;
  78. }
  79. samples["Op" + opNum] = newOp;
  80. }
  81. std::cout << samples.dump(2) << std::endl;
  82. return EXIT_SUCCESS;
  83. }
  84. Key createKey(std::mt19937_64& rng, std::set<Key>& insertedKeys) {
  85. std::uniform_int_distribution<Key> keyDist(0, 5000);
  86. while (true) {
  87. Key key = keyDist(rng);
  88. if (insertedKeys.count(key) == 0) {
  89. return key;
  90. }
  91. }
  92. }
  93. Key findKey(std::mt19937_64& rng, std::set<Key>& insertedKeys) {
  94. std::uniform_int_distribution<int> unifDist(0, 100);
  95. if (insertedKeys.empty()) {
  96. std::cerr << "findKey: called on empty set" << std::endl;
  97. exit(EXIT_FAILURE);
  98. }
  99. auto itr = insertedKeys.begin();
  100. while (true) {
  101. if (++itr == insertedKeys.end()) {
  102. itr = insertedKeys.begin();
  103. }
  104. if (unifDist(rng) < 500) {
  105. return *itr;
  106. }
  107. }
  108. }