diff options
1 files changed, 151 insertions, 0 deletions
| -rw-r--r-- | Sem. 11/Examples/Bar/Queue/Queue.hpp | 151 |
diff --git a/Sem. 11/Examples/Bar/Queue/Queue.hpp b/Sem. 11/Examples/Bar/Queue/Queue.hpp new file mode 100644 index 0000000..5c9af06 --- /dev/null +++ b/Sem. 11/Examples/Bar/Queue/Queue.hpp @@ -0,0 +1,151 @@ +#pragma once +#include <iostream> +#include <exception> +template <typename T> +class MyQueue +{ + T* data; + size_t capacity; + size_t size; + + size_t get; + size_t put; + + void resize(); + + void copyFrom(const MyQueue<T>& other); + void free(); +public: + MyQueue(); + + MyQueue(const MyQueue<T>& other); + MyQueue<T>& operator=(const MyQueue<T>& other); + + void push(const T& obj); + void push(T&& obj); //позволява и местене + void pop(); + + const T& peek() const; + bool isEmpty() const; + + ~MyQueue(); + +}; + +template <typename T> +MyQueue<T>::MyQueue() +{ + capacity = 4; + data = new T[capacity]; + size = 0; + get = put = 0; +} + +template <typename T> +void MyQueue<T>::push(const T& obj) +{ + if (size == capacity) + resize(); + + data[put] = obj; //operator= + (++put) %= capacity; + size++; +} + +template <typename T> +void MyQueue<T>::push(T&& obj) +{ + if (size == capacity) + resize(); + + data[put] = std::move(obj); //move operator = + (++put) %= capacity; + size++; +} + +template <typename T> +bool MyQueue<T>::isEmpty() const +{ + return size == 0; +} + + +template <typename T> +const T& MyQueue<T>::peek() const +{ + if (isEmpty()) + throw std::logic_error("Empty queue!"); + + return data[get]; +} + +template <typename T> +void MyQueue<T>::pop() +{ + if (isEmpty()) + throw std::logic_error("Empty queue!"); + (++get) %= capacity; + size--; +} + +template <typename T> +void MyQueue<T>::resize() +{ + T* resizedData = new T[capacity * 2]; + for (size_t i = 0; i < size; i++) + { + resizedData[i] = data[get]; + (++get) %= capacity; + } + capacity *= 2; + delete[] data; + data = resizedData; + get = 0; + put = size; +} + + +template <typename T> +void MyQueue<T>::copyFrom(const MyQueue<T>& other) +{ + data = new T[other.capacity]; + for (int i = 0; i < other.capacity; i++) + data[i] = other.data[i]; + + get = other.get; + put = other.put; + + size = other.size; + capacity = other.capacity; + +} + +template <typename T> +void MyQueue<T>::free() +{ + delete[] data; +} + +template <typename T> +MyQueue<T>::MyQueue(const MyQueue<T>& other) +{ + copyFrom(other); + +} + +template <typename T> +MyQueue<T>& MyQueue<T>::operator=(const MyQueue<T>& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} + +template <typename T> +MyQueue<T>::~MyQueue() +{ + free(); +} |
