Advertisement
Art_Uspen

Untitled

Mar 9th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <cassert>
  2. #include <cmath>
  3. #include <stdexcept>
  4. #include <iostream>
  5. #include <string>
  6. #include <cstddef>
  7. #include <vector>
  8.  
  9. template<typename T>
  10. class Deque {
  11. private:
  12.     std::vector<T> head, tail;
  13.  
  14. public:
  15.     //Deque(std::vector<T> h, std::vector<T> t) : head(h), tail(t) {}
  16.  
  17.     [[nodiscard]] bool Empty() const {
  18.         return head.empty() && tail.empty();
  19.     }
  20.  
  21.     [[nodiscard]] size_t Size() const {
  22.         return head.size() + tail.size();
  23.     }
  24.  
  25.     void Clear() {
  26.         head.clear();
  27.         tail.clear();
  28.     }
  29.  
  30.     const T &operator[](size_t i) const {
  31.         size_t index = 0;
  32.         if (index == i) {
  33.             if (!tail.empty()){
  34.                 return tail[tail.size() - 1];
  35.             }else{
  36.                 return head[0];
  37.             }
  38.         }
  39.         while ((index + 1) < tail.size()) {
  40.             ++index;
  41.             if (index == i) {
  42.                 return tail[tail.size() - 1 - i];
  43.             }
  44.         }
  45.         ++index;
  46.         if (index == i) {
  47.             return head[0];
  48.         }
  49.         size_t head_i = 0;
  50.         while (head_i != head.size()) {
  51.             ++index;
  52.             ++head_i;
  53.             if (index == i) {
  54.                 break;
  55.             }
  56.         }
  57.         return head[head_i];
  58.     }
  59.  
  60.     T &operator[](size_t i) {
  61.         size_t index = 0;
  62.         if (index == i) {
  63.             if (!tail.empty()){
  64.                 return tail[tail.size() - 1];
  65.             }else{
  66.                 return head[0];
  67.             }
  68.         }
  69.         while ((index + 1) < tail.size()) {
  70.             ++index;
  71.             if (index == i) {
  72.                 return tail[tail.size() - 1 - i];
  73.             }
  74.         }
  75.         ++index;
  76.         if (index == i) {
  77.             return head[0];
  78.         }
  79.         size_t head_i = 0;
  80.         while (head_i != head.size()) {
  81.             ++index;
  82.             ++head_i;
  83.             if (index == i) {
  84.                 break;
  85.             }
  86.         }
  87.         return head[head_i];
  88.     }
  89.  
  90.     const T &At(size_t i) const {
  91.         if (head.size() == 0 && tail.size() == 0) {
  92.             throw std::out_of_range("incorrect index");
  93.         }
  94.         if (i >= head.size() + tail.size()) {
  95.             throw std::out_of_range("incorrect index");
  96.         } else {
  97.             return (*this)[i];
  98.         }
  99.     }  // throws std::out_of_range on incorrect index
  100.  
  101.     T &At(size_t i) {
  102.         if (head.empty() && tail.empty()) {
  103.             throw std::out_of_range("incorrect index");
  104.         }
  105.         if (i >= head.size() + tail.size()) {
  106.             throw std::out_of_range("incorrect index");
  107.         } else {
  108.             return (*this)[i];
  109.         }
  110.     }  // throws std::out_of_range on incorrect index
  111.  
  112.     [[nodiscard]] const T &Front() const {
  113.         if (!tail.empty()) {
  114.             return tail.back();
  115.         } else {
  116.             return head.front();
  117.         }
  118.     }
  119.  
  120.     T &Front() {
  121.         if (!tail.empty()) {
  122.             return tail.back();
  123.         } else {
  124.             return head.front();
  125.         }
  126.     }
  127.  
  128.     const T &Back() const {
  129.         if (!head.empty()) {
  130.             return head.back();
  131.         } else {
  132.             return tail.front();
  133.         }
  134.     }
  135.  
  136.     T &Back() {
  137.         if (!head.empty()) {
  138.             return head.back();
  139.         } else {
  140.             return tail.front();
  141.         }
  142.     }
  143.  
  144.     void PushFront(const T &elem) {
  145.         tail.push_back(elem);
  146.     }
  147.  
  148.     void PushBack(const T &elem) {
  149.         head.push_back(elem);
  150.     }
  151. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement