Advertisement
KShah

Untitled

Apr 8th, 2022
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <size_t N>
  5. class StackStorage {
  6.  private:
  7.   std::vector<char*> memory_;
  8.  public:
  9.   StackStorage() = default;
  10.   ~StackStorage() = default;
  11.  
  12.   void* allocate() {
  13.     void* ptr = memory_.back();
  14.     memory_.pop_back();
  15.     return ptr;
  16.   }
  17.  
  18.   void deallocate(void* p, size_t n) {} // ignoring deallocate
  19.  
  20.   static StackStorage<N>& get_instance() {
  21.     static StackStorage<N> alloc;
  22.     return alloc;
  23.   }
  24. };
  25.  
  26. template <typename T>
  27. class StackAllocator {
  28.  public:
  29.   StackAllocator() = default;
  30.  
  31.   template<typename U>
  32.   StackAllocator(const StackAllocator<U>& other) {}
  33.  
  34.   ~StackAllocator() = default;
  35.  
  36.  
  37.   using value_type = T;
  38.   using pointer = T*;
  39.   using const_pointer = const T*;
  40.   using reference = T&;
  41.   using const_refernce = const T&;
  42.   using size_type = std::size_t;
  43.  
  44.   T* allocate(size_t n) {
  45.     const size_t alloc_size = n * sizeof(T);
  46.     if (alloc_size <= 4) {
  47.       return static_cast<T*>(StackStorage<4>::get_instance().allocate());
  48.     }
  49.     if (alloc_size <= 8) {
  50.       return static_cast<T*>(StackStorage<8>::get_instance().allocate());
  51.     }
  52.     if (alloc_size <= 16) {
  53.       return static_cast<T*>(StackStorage<16>::get_instance().allocate());
  54.     }
  55.     return static_cast<T*>(StackStorage<32>::get_instance().allocate());
  56.   }
  57.  
  58.   void deallocate(T* p, size_t n) {
  59.     const size_t alloc_size = n * sizeof(T);
  60.     if (alloc_size <= 4) {
  61.       StackStorage<4>::get_instance().deallocate(p, 4);
  62.       return;
  63.     }
  64.     if (alloc_size <= 8) {
  65.       StackStorage<8>::get_instance().deallocate(p, 8);
  66.       return;
  67.     }
  68.     if (alloc_size <= 16) {
  69.       StackStorage<16>::get_instance().deallocate(p, 16);
  70.       return;
  71.     }
  72.     StackStorage<32>::get_instance().deallocate(p, 32);
  73.   }
  74.  
  75.   template <typename U>
  76.   struct rebind {
  77.     typedef StackAllocator<U> other;
  78.   };
  79.  
  80.   template<typename U>
  81.   bool operator==(const StackAllocator<U>& other) const {
  82.     return true;
  83.   }
  84.  
  85.   template<typename U>
  86.   bool operator!=(const StackAllocator<U>& other) const {
  87.     return false;
  88.   }
  89. };
  90.  
  91. // List
  92. template<typename T, typename Allocator = std::allocator<T>>
  93. class List {
  94.  private:
  95.   struct Node {
  96.     T node_value_;
  97.     Node* next;
  98.     Node* prev;
  99.  
  100.     Node(): next(nullptr), prev(nullptr) {}
  101.     ~Node() = default;
  102.     Node(const T& value): node_value_(value) {}
  103.   };
  104.  
  105.   size_t size_;
  106.   Node* head_;
  107.  
  108.  
  109.   Allocator alloc;
  110.   typename Allocator::template rebind<Node>::other allocator;
  111.  
  112.   void put_after(Node* p, Node* value) const {
  113.     value->prev = p;
  114.     value->next = p->next;
  115.     p->next = value;
  116.     p->next->prev = value;
  117.   }
  118.  
  119.  public:
  120.   // not implamented
  121.   template<bool is_const>
  122.   class Iterator {
  123.    public:
  124.     std::conditional_t<is_const, const Node*, Node*> ptr_;
  125.  
  126.     Iterator(): ptr_(nullptr) {}
  127.     Iterator(std::conditional_t<is_const, const Node*, Node*> ptr): ptr_(ptr) {}
  128.     ~Iterator() = default;
  129.  
  130.     Iterator& operator++() {
  131.       ptr_ = ptr_->next;
  132.       return *this;
  133.     }
  134.  
  135.     Iterator operator++(int) {
  136.       Iterator copy = *this;
  137.       ++ptr_;
  138.       return copy;
  139.     }
  140.  
  141.     Iterator& operator--() {
  142.       ptr_ = ptr_->prev;
  143.       return *this;
  144.     }
  145.  
  146.     Iterator operator--(int) {
  147.       Iterator copy = *this;
  148.       --ptr_;
  149.       return copy;
  150.     }
  151.  
  152.     bool operator==(const Iterator& other) const {
  153.       return ptr_ == other.ptr_;
  154.     }
  155.  
  156.     bool operator!=(const Iterator& other) const {
  157.       return ptr_ != other.ptr_;
  158.     }
  159.  
  160.     std::conditional_t<is_const, const T*, T*> operator*() const {
  161.       return ptr_->val_;
  162.     }
  163.  
  164.     std::conditional_t<is_const, const T&, T&> operator->() const {
  165.       return &(ptr_->val_);
  166.     }
  167.   };
  168.  
  169.   using iterator = Iterator<false>;
  170.   using const_iterator = Iterator<true>;
  171.   using reverse_iterator = std::reverse_iterator<iterator>;
  172.   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  173.   using alloc_traits = std::allocator_traits<typename Allocator::template rebind<Node>::other>;
  174.  
  175.   // 1
  176.   List();
  177.   explicit List(size_t count);
  178.   explicit List(size_t count, const T& value);
  179.   explicit List(const Allocator& alloc = Allocator());
  180.   explicit List(int count, const Allocator& alloc = Allocator());
  181.   explicit List(size_t count, const T& value, const Allocator& alloc = Allocator());
  182.  
  183.   // 2
  184.   List(const List& other) {} // not
  185.  
  186.   List& operator=(const List& other) {} // not
  187.  
  188.   ~List() {} // not
  189.  
  190.   // 3
  191.   [[nodiscard]] size_t size() const {
  192.     return size_;
  193.   }
  194.  
  195.   Allocator get_allocator() const {
  196.     return alloc;
  197.   }
  198.  
  199.   // 4
  200.   void push_back(const T& value) {
  201.     insert(end(), value);
  202.   }
  203.   void push_front(const T& value) {
  204.     insert(begin(), value);
  205.   }
  206.   void pop_back() {
  207.     erase(--end());
  208.   }
  209.   void pop_front() {
  210.     erase(begin());
  211.   }
  212.  
  213.   // 5
  214.   iterator begin() {
  215.     return iterator(head_->next);
  216.   }
  217.   const_iterator begin() const {
  218.     return const_iterator(head_->next);
  219.   }
  220.   const_iterator cbegin() const {
  221.     return const_iterator(head_->next);
  222.   }
  223.  
  224.   iterator end() {
  225.     return iterator(head_);
  226.   }
  227.   const_iterator end() const {
  228.     return const_iterator(head_);
  229.   }
  230.   const_iterator cend() const {
  231.     return const_iterator(head_);
  232.   }
  233.  
  234.   reverse_iterator rbegin() {
  235.     return reverse_iterator(head_);
  236.   }
  237.  
  238.   const_reverse_iterator rbegin() const {
  239.     return const_reverse_iterator(head_);
  240.   }
  241.   const_reverse_iterator crbegin() const {
  242.     return const_reverse_iterator(head_);
  243.   }
  244.  
  245.   reverse_iterator rend() {
  246.     return reverse_iterator(head_->next);
  247.   }
  248.   const_reverse_iterator rend() const {
  249.     return const_reverse_iterator(head_->next);
  250.   }
  251.   const_reverse_iterator crend() const {
  252.     return const_reverse_iterator(head_->next);
  253.   }
  254.  
  255.   // 6
  256.   iterator insert(const_iterator pos, const T& value) {}
  257.   iterator erase(const_iterator pos) {}
  258. };
  259.  
  260. int main() {
  261.   return 0;
  262. }
  263.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement