|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "arrayList.h" |
| 4 | +#include "myExceptions.h" |
| 5 | + |
| 6 | +template <typename T> |
| 7 | +class queue : private arrayList<T> { |
| 8 | + public: |
| 9 | + using typename arrayList<T>::size_t; |
| 10 | + queue(); |
| 11 | + queue(const queue& rhs); |
| 12 | + queue(queue&& rhs); |
| 13 | + queue& operator=(const queue& rhs); |
| 14 | + queue& operator=(queue&& rhs); |
| 15 | + ~queue() {} |
| 16 | + |
| 17 | + bool empty() const; |
| 18 | + void enqueue(const T& x); |
| 19 | + template <typename... Ts> |
| 20 | + void enqueue(Ts&&... params); |
| 21 | + T dequeue(); |
| 22 | + |
| 23 | + private: |
| 24 | + using arrayList<T>::length; |
| 25 | + using arrayList<T>::p; |
| 26 | + size_t head; |
| 27 | + size_t size; |
| 28 | + void enlarge(); |
| 29 | + size_t tail() const { return (head + size) % length; } |
| 30 | +}; |
| 31 | + |
| 32 | +template <typename T> |
| 33 | +void queue<T>::enqueue(const T& x) { |
| 34 | + enlarge(); |
| 35 | + p[tail()] = x; |
| 36 | + size++; |
| 37 | +} |
| 38 | + |
| 39 | +template <typename T> |
| 40 | +template <typename... Ts> |
| 41 | +void queue<T>::enqueue(Ts&&... params) { |
| 42 | + enlarge(); |
| 43 | + p[tail()] = T(std::forward<Ts>(params)...); |
| 44 | + // 对于 local objects,上一步中的拷贝赋值可能会优化为移动赋值。 |
| 45 | + size++; |
| 46 | +} |
| 47 | + |
| 48 | +template <typename T> |
| 49 | +T queue<T>::dequeue() { |
| 50 | + if (empty()) { |
| 51 | + throw illegalIndex("Underflow. queue is empty!"); |
| 52 | + } |
| 53 | + auto x = p[head++]; |
| 54 | + head %= length; |
| 55 | + size--; |
| 56 | + return x; |
| 57 | +} |
| 58 | + |
| 59 | +template <typename T> |
| 60 | +queue<T>::queue() : arrayList<T>(), head(0), size(0) {} |
| 61 | + |
| 62 | +template <typename T> |
| 63 | +queue<T>::queue(const queue& rhs) |
| 64 | + : arrayList<T>(rhs), head(rhs.head), size(rhs.size) {} |
| 65 | + |
| 66 | +template <typename T> |
| 67 | +queue<T>::queue(queue&& rhs) |
| 68 | + : arrayList<T>(std::move(rhs)), head(rhs.head), size(rhs.size) {} |
| 69 | + |
| 70 | +template <typename T> |
| 71 | +queue<T>& queue<T>::operator=(const queue& rhs) { |
| 72 | + arrayList<T>::operator=(rhs); |
| 73 | + head = rhs.head; |
| 74 | + size = rhs.size; |
| 75 | + return *this; |
| 76 | +} |
| 77 | + |
| 78 | +template <typename T> |
| 79 | +queue<T>& queue<T>::operator=(queue&& rhs) { |
| 80 | + arrayList<T>::operator=(std::move(rhs)); |
| 81 | + head = rhs.head; |
| 82 | + size = rhs.size; |
| 83 | + return *this; |
| 84 | +} |
| 85 | + |
| 86 | +template <typename T> |
| 87 | +bool queue<T>::empty() const { |
| 88 | + return size == 0; |
| 89 | +} |
| 90 | + |
| 91 | +template <typename T> |
| 92 | +void queue<T>::enlarge() { |
| 93 | + if (size == length) { |
| 94 | + T* tp = new T[length * 2]; |
| 95 | + for (size_t i = 0; i < size; i++) tp[i] = p[(head + i) % length]; |
| 96 | + delete[] p; |
| 97 | + p = tp; |
| 98 | + length *= 2; |
| 99 | + head = 0; |
| 100 | + } |
| 101 | +} |
0 commit comments