main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <iostream>
  2. #include <list>
  3. #include <memory>
  4. #include <ctime>
  5. #include <random>
  6. #include <cassert>
  7. #include "StackAllocator.h"
  8. #include "XorList.h"
  9. const int NUM_CONTENT = 20;
  10. int random_nums[NUM_CONTENT * 2];
  11. //
  12. //class A{
  13. // A() {
  14. // int f = 5, g = 0;
  15. // f / g
  16. // }
  17. // int a[1000000];
  18. //};
  19. #include <list>
  20. int main() {
  21. std::random_device rd;
  22. std::mt19937 mt(rd());
  23. std::uniform_int_distribution<int> random_gen(-128256512, 128256512);
  24. for (int i = 0; i < NUM_CONTENT * 2; i++) {
  25. random_nums[i] = random_gen(mt);
  26. }
  27. for (const auto &i : random_nums)
  28. std::cout << i << " ";
  29. std::cout << "\n\n";
  30. {
  31. std::list<int> b;
  32. std::clock_t start_std_allocator = std::clock();
  33. for (int i = 0; i < NUM_CONTENT * 2; i += 2) {
  34. b.push_front(random_nums[i]);
  35. if (b.front() != random_nums[i]) {
  36. std::cout << "AAAAAAAAAAAAAAAA: front is " << b.front() << ", expected: " << random_nums[i]
  37. << std::endl;
  38. }
  39. b.push_back(random_nums[i + 1]);
  40. if (b.back() != random_nums[i + 1]) {
  41. std::cout << "BBBBBBBBBBBBBBBB: back is " << b.back() << ", expected: " << random_nums[i + 1]
  42. << std::endl;
  43. }
  44. }
  45. std::clock_t end_std_allocator = std::clock();
  46. std::cout << "Time for std::list with std::allocator is "
  47. << (end_std_allocator - start_std_allocator) / (double) (CLOCKS_PER_SEC / 1000) << "ms" << std::endl;
  48. std::cout << "=============DATA BEGIN===========\n";
  49. for (auto k = b.begin(); k != b.end(); ++k)
  50. std::cout << *k << " ";
  51. std::cout << "\n==============DATA END============\n\n";
  52. }
  53. {
  54. XorList<int, StackAllocator<int>> a;
  55. std::clock_t start_stack_allocator = std::clock();
  56. for (int i = 0; i < NUM_CONTENT * 2; i += 2) {
  57. const int val = random_nums[i];
  58. a.push_front(val);
  59. if (a.front() != random_nums[i]) {
  60. std::cout << "AAAAAAAAAAAAAAAA: front is " << a.front() << ", expected: " << random_nums[i]
  61. << std::endl;
  62. }
  63. a.push_back(random_nums[i + 1]);
  64. if (a.back() != random_nums[i + 1]) {
  65. std::cout << "BBBBBBBBBBBBBBBB: back is " << a.back() << ", expected: " << random_nums[i + 1]
  66. << std::endl;
  67. }
  68. }
  69. std::clock_t end_stack_allocator = std::clock();
  70. std::cout << "Time for XorList with StackAllocator is "
  71. << (end_stack_allocator - start_stack_allocator) / (double) (CLOCKS_PER_SEC / 1000) << "ms"
  72. << std::endl;
  73. std::cout << "final size = " << a.size() << "\n";
  74. int size = a.size();
  75. for (int i = 0; i < size / 2; i++) {
  76. std::cout << "back = " << a.back() << std::endl;
  77. a.pop_back();
  78. }
  79. std::cout << "final size = " << a.size() << "\n";
  80. XorList<int, StackAllocator<int>> c;
  81. c = XorList<int, StackAllocator<int>>();
  82. c = a;
  83. std::cout << "=============DATA BEGIN===========\n";
  84. for (auto k = c.begin(); k != c.end(); ++k)
  85. std::cout << *k << " ";
  86. std::cout << "\n==============DATA END============\n\n";
  87. }
  88. //
  89. // XorList<int, StackAllocator<int>> a;
  90. // for (int i = 0; i < 50; i++) {
  91. // a.push_back(rand());
  92. // }
  93. // std::cout << "=============DATA BEGIN===========\n";
  94. // for (auto k = a.begin(); k != a.end(); ++k)
  95. // std::cout << *k << " ";
  96. // std::cout << "\n==============DATA END============\n\n";
  97. return 0;
  98. }