Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <cmath>
- #include <stdexcept>
- #include <iostream>
- #include <string>
- #include <cstddef>
- #include <vector>
- template<typename T>
- class Deque {
- private:
- std::vector<T> head, tail;
- public:
- //Deque(std::vector<T> h, std::vector<T> t) : head(h), tail(t) {}
- [[nodiscard]] bool Empty() const {
- return head.empty() && tail.empty();
- }
- [[nodiscard]] size_t Size() const {
- return head.size() + tail.size();
- }
- void Clear() {
- head.clear();
- tail.clear();
- }
- const T &operator[](size_t i) const {
- size_t index = 0;
- if (index == i) {
- if (!tail.empty()){
- return tail[tail.size() - 1];
- }else{
- return head[0];
- }
- }
- while ((index + 1) < tail.size()) {
- ++index;
- if (index == i) {
- return tail[tail.size() - 1 - i];
- }
- }
- ++index;
- if (index == i) {
- return head[0];
- }
- size_t head_i = 0;
- while (head_i != head.size()) {
- ++index;
- ++head_i;
- if (index == i) {
- break;
- }
- }
- return head[head_i];
- }
- T &operator[](size_t i) {
- size_t index = 0;
- if (index == i) {
- if (!tail.empty()){
- return tail[tail.size() - 1];
- }else{
- return head[0];
- }
- }
- while ((index + 1) < tail.size()) {
- ++index;
- if (index == i) {
- return tail[tail.size() - 1 - i];
- }
- }
- ++index;
- if (index == i) {
- return head[0];
- }
- size_t head_i = 0;
- while (head_i != head.size()) {
- ++index;
- ++head_i;
- if (index == i) {
- break;
- }
- }
- return head[head_i];
- }
- const T &At(size_t i) const {
- if (head.size() == 0 && tail.size() == 0) {
- throw std::out_of_range("incorrect index");
- }
- if (i >= head.size() + tail.size()) {
- throw std::out_of_range("incorrect index");
- } else {
- return (*this)[i];
- }
- } // throws std::out_of_range on incorrect index
- T &At(size_t i) {
- if (head.empty() && tail.empty()) {
- throw std::out_of_range("incorrect index");
- }
- if (i >= head.size() + tail.size()) {
- throw std::out_of_range("incorrect index");
- } else {
- return (*this)[i];
- }
- } // throws std::out_of_range on incorrect index
- [[nodiscard]] const T &Front() const {
- if (!tail.empty()) {
- return tail.back();
- } else {
- return head.front();
- }
- }
- T &Front() {
- if (!tail.empty()) {
- return tail.back();
- } else {
- return head.front();
- }
- }
- const T &Back() const {
- if (!head.empty()) {
- return head.back();
- } else {
- return tail.front();
- }
- }
- T &Back() {
- if (!head.empty()) {
- return head.back();
- } else {
- return tail.front();
- }
- }
- void PushFront(const T &elem) {
- tail.push_back(elem);
- }
- void PushBack(const T &elem) {
- head.push_back(elem);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement