123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include <iostream>
- #include <list>
- #include <memory>
- #include <ctime>
- #include <random>
- #include <cassert>
- #include "StackAllocator.h"
- #include "XorList.h"
- const int NUM_CONTENT = 20;
- int random_nums[NUM_CONTENT * 2];
- //
- //class A{
- // A() {
- // int f = 5, g = 0;
- // f / g
- // }
- // int a[1000000];
- //};
- #include <list>
- int main() {
- std::random_device rd;
- std::mt19937 mt(rd());
- std::uniform_int_distribution<int> random_gen(-128256512, 128256512);
- for (int i = 0; i < NUM_CONTENT * 2; i++) {
- random_nums[i] = random_gen(mt);
- }
- for (const auto &i : random_nums)
- std::cout << i << " ";
- std::cout << "\n\n";
- {
- std::list<int> b;
- std::clock_t start_std_allocator = std::clock();
- for (int i = 0; i < NUM_CONTENT * 2; i += 2) {
- b.push_front(random_nums[i]);
- if (b.front() != random_nums[i]) {
- std::cout << "AAAAAAAAAAAAAAAA: front is " << b.front() << ", expected: " << random_nums[i]
- << std::endl;
- }
- b.push_back(random_nums[i + 1]);
- if (b.back() != random_nums[i + 1]) {
- std::cout << "BBBBBBBBBBBBBBBB: back is " << b.back() << ", expected: " << random_nums[i + 1]
- << std::endl;
- }
- }
- std::clock_t end_std_allocator = std::clock();
- std::cout << "Time for std::list with std::allocator is "
- << (end_std_allocator - start_std_allocator) / (double) (CLOCKS_PER_SEC / 1000) << "ms" << std::endl;
- std::cout << "=============DATA BEGIN===========\n";
- for (auto k = b.begin(); k != b.end(); ++k)
- std::cout << *k << " ";
- std::cout << "\n==============DATA END============\n\n";
- }
- {
- XorList<int, StackAllocator<int>> a;
- std::clock_t start_stack_allocator = std::clock();
- for (int i = 0; i < NUM_CONTENT * 2; i += 2) {
- const int val = random_nums[i];
- a.push_front(val);
- if (a.front() != random_nums[i]) {
- std::cout << "AAAAAAAAAAAAAAAA: front is " << a.front() << ", expected: " << random_nums[i]
- << std::endl;
- }
- a.push_back(random_nums[i + 1]);
- if (a.back() != random_nums[i + 1]) {
- std::cout << "BBBBBBBBBBBBBBBB: back is " << a.back() << ", expected: " << random_nums[i + 1]
- << std::endl;
- }
- }
- std::clock_t end_stack_allocator = std::clock();
- std::cout << "Time for XorList with StackAllocator is "
- << (end_stack_allocator - start_stack_allocator) / (double) (CLOCKS_PER_SEC / 1000) << "ms"
- << std::endl;
- std::cout << "final size = " << a.size() << "\n";
- int size = a.size();
- for (int i = 0; i < size / 2; i++) {
- std::cout << "back = " << a.back() << std::endl;
- a.pop_back();
- }
- std::cout << "final size = " << a.size() << "\n";
- XorList<int, StackAllocator<int>> c;
- c = XorList<int, StackAllocator<int>>();
- c = a;
- std::cout << "=============DATA BEGIN===========\n";
- for (auto k = c.begin(); k != c.end(); ++k)
- std::cout << *k << " ";
- std::cout << "\n==============DATA END============\n\n";
- }
- //
- // XorList<int, StackAllocator<int>> a;
- // for (int i = 0; i < 50; i++) {
- // a.push_back(rand());
- // }
- // std::cout << "=============DATA BEGIN===========\n";
- // for (auto k = a.begin(); k != a.end(); ++k)
- // std::cout << *k << " ";
- // std::cout << "\n==============DATA END============\n\n";
- return 0;
- }
|