Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <list>
  6. #pragma comment(linker, "/STACK:1488228666")
  7.  
  8. constexpr std::size_t size_of_block_allocation = (1 << 9);
  9.  
  10. class Block_Allocation
  11. {
  12. public:
  13.     char* begin_place;
  14.     std::size_t count_free_memory;
  15.     std::size_t count_used_memory;
  16.     using size_type = std::size_t;
  17.     using pointer = char*;
  18.     Block_Allocation* prev;
  19.  
  20.     Block_Allocation()
  21.     {
  22.         prev = nullptr;
  23.     }
  24.     Block_Allocation(const size_type count_memory, Block_Allocation* prev_)
  25.     {
  26.         begin_place = new char[count_memory];
  27.         count_free_memory = count_memory;
  28.         count_used_memory = 0;
  29.         prev = prev_;
  30.     }
  31.     ~Block_Allocation()
  32.     {
  33.         if (prev != nullptr)
  34.         {
  35.             delete prev;
  36.         }
  37.         delete[] begin_place;
  38.     }
  39.     pointer get_current_place()
  40.     {
  41.         return begin_place + count_used_memory;
  42.     }
  43.     pointer allocate(size_type cnt)
  44.     {
  45.         pointer ptr = get_current_place();
  46.         count_free_memory -= cnt;
  47.         count_used_memory += cnt;
  48.         return ptr;
  49.     }
  50.     void deallocate(size_type cnt)
  51.     {
  52.  
  53.     }
  54. };
  55.  
  56. class Universal_Allocator
  57. {
  58. public:
  59.     Block_Allocation* head;
  60.     //std::vector<Block_Allocation*> blocks;
  61. public:
  62.     typedef std::size_t size_type;
  63.     Universal_Allocator()
  64.     {
  65.         head = nullptr;
  66.     }
  67.     ~Universal_Allocator()
  68.     {
  69.         delete head;
  70.         //for (auto block : blocks)
  71.         //{
  72.         //  delete block;
  73.         //}
  74.     }
  75.     template < typename T >
  76.     T* allocate(const size_type count_object)
  77.     {
  78.         if (head != nullptr && (head->count_free_memory) >= count_object * sizeof(T))
  79.         {
  80.             return reinterpret_cast<T*>(head->allocate(count_object * sizeof(T)));
  81.         }
  82.         else
  83.         {
  84.             Block_Allocation* new_block = new Block_Allocation(std::max(count_object * sizeof(T), size_of_block_allocation), head);
  85.             head = new_block;
  86.             //blocks.push_back(new_block);
  87.             return reinterpret_cast<T*>(head->allocate(count_object * sizeof(T)));
  88.         }
  89.         /*pointer ptr = blocks.back()->get_current_place();
  90.         if (blocks.back()->count_free_memory < size_of(T))
  91.         {
  92.         Block_Allocation* block = new Block_Allocation;
  93.         //ptr = &block;
  94.         blocks.push_back(*block);
  95.         ptr = &blocks.back()->;
  96.         }
  97.         while (count_object > 0)
  98.         {
  99.         size_type can_write = std::min(count_object, (blocks.back()->count_free_memory) / size_of(T));
  100.         blocks.back()->allocate(can_write * size_of(T));
  101.         count_object -= can_write;
  102.         if (count_object != 0)
  103.         {
  104.         Block_Allocation* block = new Block_Allocation;
  105.         blocks.push_back(*block);
  106.         }
  107.         }
  108.         return ptr;
  109.         */
  110.     }
  111. };
  112.  
  113. template < typename T >
  114. class Stack_Allocator
  115. {
  116. public:
  117.     std::shared_ptr<Universal_Allocator> allocator;
  118. public:
  119.     using pointer = T*;
  120.     using size_type = std::size_t;
  121.     using value_type = T;
  122.     template <typename U>
  123.     struct rebind
  124.     {
  125.         typedef Stack_Allocator<U> other;
  126.     };
  127.  
  128.  
  129.     //template <typename U>
  130.     //using rebind = Stack_Allocator<U>;
  131.  
  132.     Stack_Allocator(): allocator(std::make_shared<Universal_Allocator>())
  133.     {
  134.     }
  135.     template < class U >
  136.     Stack_Allocator(const Stack_Allocator<U>& other) : allocator(other.allocator) {};
  137.     ~Stack_Allocator()
  138.     {
  139.  
  140.     }
  141.     pointer allocate(size_type count_object)
  142.     {
  143.         return allocator->allocate<T>(count_object);
  144.     }
  145.     void deallocate(pointer ptr, size_type object)
  146.     {
  147.  
  148.     }
  149. };
  150.  
  151. template <typename T>
  152. T* get_xor_address(T* first, T* second)
  153. {
  154.     return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(first) ^ reinterpret_cast<uintptr_t>(second));
  155. }
  156.  
  157.  
  158. template <typename T>
  159. class node
  160. {
  161. private:
  162.     node* xor_neighbours;
  163.     T val;
  164. public:
  165.     node(T value)
  166.     {
  167.         val = value;
  168.     }
  169.     node(T value, node* prev_value = nullptr, node* next_value = nullptr)
  170.     {
  171.         val = value;
  172.         xor_neighbours = get_xor_address(prev_value, next_value);
  173.     }
  174. };
  175.  
  176. template <typename T>
  177. class Xor_List_iterator : public std::iterator < std::bidirectional_iterator_tag, T>
  178. {
  179. private:
  180.     node* current_node;
  181.     node* prev_node;
  182. public:
  183.     Xor_List_iterator(const node* current_node_, const node* prev_node_) : current_node(current_node_), prev_node(prev_node_)
  184.     {
  185.  
  186.     }
  187.     Xor_List_iterator& operator=(const Xor_List_iterator& it)
  188.     {
  189.         current_node = it.current_node;
  190.         prev_node = it.prev_node;
  191.         return (*this);
  192.     }
  193.     bool operator==(const Xor_List_iterator& it) const
  194.     {
  195.         return (current_node == it.current_node && prev_node == it.prev_node);
  196.     }
  197.     bool operator!=(const Xor_List_iterator& it) const
  198.     {
  199.         return !(current_node == it.current_node && prev_node == it.prev_node);
  200.     }
  201.     node& operator*()
  202.     {
  203.         return (*current_node);
  204.     }
  205.     node* operator->()
  206.     {
  207.         return current_node;
  208.     }
  209. // a b c d e  
  210.     Xor_List_iterator& operator++()
  211.     {
  212.         node* new_current_code = get_xor_address<node>(current_node->xor_neighbours, prev_node);
  213.         //node* new_current_node = reinterpret_cast<node*>(reinterpret_cast<uintptr_t>(current_node->xor_neighbours)
  214.         //^ (reinterpret_cast<uintptr_t>(prev_node)));
  215.         prev_node = current_node;
  216.         current_node = new_current_node;
  217.         return (*this);
  218.     }
  219.  
  220.     Xor_List_iterator operator++(int)
  221.     {
  222.         Xor_List_iterator it = *this;
  223.         (*this).operator++();
  224.         return it;
  225.     }
  226.     Xor_List_iterator& operator--()
  227.     {
  228.         node* new_prev_node = get_xor_address<node>(current_node, prev_node->xor_neighbours);
  229.         current_node = prev_node;
  230.         prev_node = new_prev_node;
  231.         return (*this);
  232.     }
  233.     Xor_List_iterator operator--(int)
  234.     {
  235.         Xor_List_iterator it = *this;
  236.         (*this).operator--();
  237.         return it;
  238.     }
  239. };
  240.  
  241. //std::list<int, Stack_Allocator<int>>
  242.  
  243. //template <typename T>
  244. //T* get_xor_address(T* first, T* second)
  245. //{
  246. //  return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(first) ^ reinterpret_cast<uintptr_t>(second));
  247. //}
  248.  
  249. template < typename T, typename Allocator>
  250. class Xor_List
  251. {
  252. public:
  253.     using size_type = std::size_t;
  254.     //using size_type = std::size;
  255.     using iterator = Xor_List_iterator<T>;
  256.     using const_iterator = Xor_List_iterator<const T>;
  257.     using reverse_iterator = std::reverse_iterator<iterator>;
  258.     using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  259.  
  260.     typename Allocator::template rebind<node>::other allocator;
  261.     node* it_begin;
  262.     node* it_end;
  263.     size_type size_list;
  264.  
  265.     explicit Xor_List(const Allocator& alloc = Allocator()) : allocator(alloc), it_begin(nullptr), it_end(nullptr), size_list(0) {};
  266.     iterator begin()
  267.     {
  268.         return iterator(begin, nullptr);
  269.     }
  270.     iterator end()
  271.     {
  272.         return iterator(nullptr, end);
  273.     }
  274.     const_iterator begin() const
  275.     {
  276.         return const_iterator(begin, nullptr);
  277.     }
  278.     const_iterator cbegin() const
  279.     {
  280.         return const_iterator(begin, nullptr);
  281.     }
  282.     const_iterator end() const
  283.     {
  284.         return const_iterator(nullptr, end);
  285.     }
  286.     const_iterator cend() const
  287.     {
  288.         return const_iterator(nullptr, end);
  289.     }
  290.     reverse_iterator rbegin()
  291.     {
  292.         return reverse_iterator(end());
  293.     }
  294.     reverse_iterator rend()
  295.     {
  296.         return reverse_iterator(begin());
  297.     }
  298.     const const_reverse_iterator rbegin() const
  299.     {
  300.         return const_reverse_iterator(end());
  301.     }
  302.     const const_reverse_iterator crbegin() const
  303.     {
  304.         return const_reverse_iterator(end());
  305.     }
  306.     const const_reverse_iterator rend() const
  307.     {
  308.         return const_reverse_iterator(begin());
  309.     }
  310.     const const_reverse_iterator crend() const
  311.     {
  312.         return const_reverse_iterator(begin());
  313.     }
  314.     Xor_List(size_type count, const T& value = T(), const Allocator& alloc = Allocator()): allocator(alloc), size_list(count)
  315.     {
  316.         it_end = nullptr;
  317.         it_begin = nullptr;
  318.         for (size_type i = 0; i < count; i++)
  319.         {
  320.             push_back(value);
  321.             if (i == 0)
  322.             {
  323.                 it_begin = it_end;
  324.             }
  325.         }
  326.     }
  327.     Xor_List(const Xor_List& other)
  328.     {
  329.         allocator = other.allocator;
  330.         it_begin = other.it_begin;
  331.         it_end = other.it_end;
  332.         size = other.size;
  333.     }
  334.     Xor_List(const Xor_List&& other)
  335.     {
  336.         allocator = std::move(other.allocator);
  337.         it_begin = std::move(other.it_begin);
  338.         it_end = std::move(other.it_end);
  339.         size = std::move(other.size);
  340.     }
  341.     ~Xor_List()
  342.     {
  343.         for (iterator it = begin(); it != end(); it++)
  344.         {
  345.             delete (*it);
  346.         }
  347.     }
  348.     Xor_List& operator=(const Xor_List& other)
  349.     {
  350.         allocator = other.allocator;
  351.         it_begin = other.it_begin;
  352.         it_end = other.it_end;
  353.         size = other.size;
  354.         return (*this);
  355.     }
  356.     Xor_List&& operator=(const Xor_List&& other)
  357.     {
  358.         allocator = std::move(other.allocator);
  359.         it_begin = std::move(other.it_begin);
  360.         it_end = std::move(other.it_end);
  361.         size = std::move(other.size);
  362.         return (*this);
  363.     }
  364.     size_type size()
  365.     {
  366.         return size_list;
  367.     }
  368.     void push_back(const T& value)
  369.     {
  370.         node* next = new node(value, it_end, nullptr);
  371.         if (it_end != nullptr)
  372.         {
  373.             it_end->xor_neighbours = get_xor_address(it_end->xor_neighbours, next);
  374.         }
  375.         it_end = next;
  376.         size_list++;
  377.     }
  378.     void push_back(T&& value)
  379.     {
  380.         node* next = new node(std::move(value), it_end, nullptr);
  381.         if (it_end != nullptr)
  382.         {
  383.             it_end->xor_neighbours = get_xor_address(it_end->xor_neighbours, next);
  384.         }
  385.         it_end = next;
  386.         size_list++;
  387.     }
  388.     void push_front(const T& value)
  389.     {
  390.         node* prev = new node(value, nullptr, it_begin);
  391.         if (it_begin != nullptr)
  392.         {
  393.             it_begin->xor_neighbours = get_xor_address(it_begin->xor_neighbours, prev);
  394.         }
  395.         it_begin = prev;
  396.         size_list++;
  397.     }
  398.     void push_front(T&& value)
  399.     {
  400.         node* prev = new node(std::move(value), nullptr, it_begin);
  401.         if (it_begin != nullptr)
  402.         {
  403.             it_begin->xor_neighbours = get_xor_address(it_begin->xor_neighbours, prev);
  404.         }
  405.         it_begin = prev;
  406.         size_list++;
  407.     }
  408.     // a b c d e
  409.     node* get_next(node* pred)
  410.     {
  411.         return xor_address(xor_neighbours, pred);
  412.     }
  413.     node* get_prev(node* next)
  414.     {
  415.         return xor_address(xor_neighbours, next);
  416.     }
  417.     void update(node* other)
  418.     {
  419.         if (this != nullptr)
  420.         {
  421.             xor_neighbours ^= other;
  422.         }
  423.     }
  424.     void pop_back()
  425.     {
  426.         node* current = it_end;
  427.         node* prev = current->get_prev(nullptr);
  428.         prev.update(current);
  429.         size_list--;
  430.         delete current;
  431.     }
  432.     void pop_front()
  433.     {
  434.         node* current = it_begin;
  435.         node* next = current->get_next(nullptr);
  436.         next.update(current);
  437.         size_list--;
  438.         delete current;
  439.     }
  440.     void insert_before(iterator it, const T& value)
  441.     {
  442.         if (it == it_begin)
  443.         {
  444.             push_front(value);
  445.             return;
  446.         }
  447.         iterator it1 = --it;
  448.         it++;
  449.         node* pred = *it1;
  450.         node* next = *it;
  451.         node* current = new node(value, pred, next);
  452.         pred.update(current);
  453.         next.update(current);
  454.         size_list++;
  455.     }
  456.     void insert_before(iterator it, T&& value)
  457.     {
  458.         if (it == it_begin)
  459.         {
  460.             push_front(std::move(value));
  461.             return;
  462.         }
  463.         iterator it1 = --it;
  464.         it++;
  465.         node* pred = *it1;
  466.         node* next = *it;
  467.         node* current = new node(std::move(value), pred, next);
  468.         pred.update(current);
  469.         next.update(current);
  470.         size_list++;
  471.     }
  472.     void insert_after(iterator it, const T& value)
  473.     {
  474.         if (it == it_end)
  475.         {
  476.             push_back(value);
  477.             return;
  478.         }
  479.         iterator it1 = ++it;
  480.         it--;
  481.         node* next = *it1;
  482.         node* pred = *it;
  483.         node* current = new node(value, pred, next);
  484.         pred.update(current);
  485.         next.update(current);
  486.         size_list++;
  487.     }
  488.     void insert_after(iterator it, T&& value)
  489.     {
  490.         if (it == it_end)
  491.         {
  492.             push_back(std::move(value));
  493.             return;
  494.         }
  495.         iterator it1 = ++it;
  496.         it--;
  497.         node* next = *it1;
  498.         node* pred = *it;
  499.         node* current = new node(std::move(value), pred, next);
  500.         pred.update(current);
  501.         next.update(current);
  502.         size_list++;
  503.     }
  504.     void erase(iterator it)
  505.     {
  506.         node* pred = *(--it);
  507.         it++;
  508.         node* next = *(++it);
  509.         node* current = *(--it);
  510.         node* pred1 = pred;
  511.         pred.update(current);
  512.         pred.update(next);
  513.         next.update(current);
  514.         next.update(pred1);
  515.         size_list--;
  516.         delete current;
  517.     }
  518. };
  519.  
  520. int main()
  521. {
  522.     Stack_Allocator<int> allocator;
  523.     allocator.allocate(10000);
  524.     std::list<int, Stack_Allocator<int>> list1;
  525.     //for (int i = 1; i <= 600000; i++)
  526.     //{
  527.     //  list1.push_back(5);
  528.     //}
  529.     return 0;
  530. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement