|
| 1 | +/******************************************************************************* |
| 2 | +/* O P E N S O U R C E -- T S + + ** |
| 3 | +/******************************************************************************* |
| 4 | + * |
| 5 | + * @project TS++ |
| 6 | + * |
| 7 | + * @file QUEUE.H |
| 8 | + * |
| 9 | + * @authors CCHyper, ZivDero (adjustments for YR) |
| 10 | + * |
| 11 | + * @brief FIFO queue for events. |
| 12 | + * |
| 13 | + * @license TS++ is free software: you can redistribute it and/or |
| 14 | + * modify it under the terms of the GNU General Public License |
| 15 | + * as published by the Free Software Foundation, either version |
| 16 | + * 3 of the License, or (at your option) any later version. |
| 17 | + * |
| 18 | + * TS++ is distributed in the hope that it will be |
| 19 | + * useful, but WITHOUT ANY WARRANTY; without even the implied |
| 20 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 21 | + * PURPOSE. See the GNU General Public License for more details. |
| 22 | + * |
| 23 | + * You should have received a copy of the GNU General Public |
| 24 | + * License along with this program. |
| 25 | + * If not, see <http://www.gnu.org/licenses/>. |
| 26 | + * |
| 27 | + ******************************************************************************/ |
| 28 | +#pragma once |
| 29 | + |
| 30 | +#include <Unsorted.h> |
| 31 | + |
| 32 | + |
| 33 | +template<class T, int size> |
| 34 | +class QueueClass |
| 35 | +{ |
| 36 | +public: |
| 37 | + QueueClass(); |
| 38 | + ~QueueClass() { } |
| 39 | + |
| 40 | + T& operator[](int index); |
| 41 | + |
| 42 | + T& First(); |
| 43 | + void Init(); |
| 44 | + int Next(); |
| 45 | + bool Add(const T& q); |
| 46 | + |
| 47 | + int GetHead(); |
| 48 | + int GetTail(); |
| 49 | + T* GetArray(); |
| 50 | + |
| 51 | +public: |
| 52 | + int Count; |
| 53 | + |
| 54 | +private: |
| 55 | + int Head; |
| 56 | + int Tail; |
| 57 | + T Array[size]; |
| 58 | + int Timings[size]; |
| 59 | +}; |
| 60 | + |
| 61 | + |
| 62 | +template<class T, int size> |
| 63 | +inline QueueClass<T, size>::QueueClass() : |
| 64 | + Count(0) |
| 65 | +{ |
| 66 | + Init(); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +template<class T, int size> |
| 71 | +inline void QueueClass<T, size>::Init() |
| 72 | +{ |
| 73 | + Count = 0; |
| 74 | + Head = 0; |
| 75 | + Tail = 0; |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +template<class T, int size> |
| 80 | +inline bool QueueClass<T, size>::Add(const T& q) |
| 81 | +{ |
| 82 | + if (Count < size) |
| 83 | + { |
| 84 | + Array[Tail] = q; |
| 85 | + #pragma warning(suppress: 4996) |
| 86 | + Timings[Tail] = static_cast<int>(Imports::TimeGetTime()()); |
| 87 | + Tail = (Tail + 1) & (size - 1); |
| 88 | + Count = Count + 1; |
| 89 | + return true; |
| 90 | + } |
| 91 | + return false; |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +template<class T, int size> |
| 96 | +inline int QueueClass<T, size>::Next() |
| 97 | +{ |
| 98 | + if (Count) |
| 99 | + { |
| 100 | + Head = (Head + 1) & (size - 1); |
| 101 | + Count = Count - 1; |
| 102 | + } |
| 103 | + return Count; |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +template<class T, int size> |
| 108 | +inline T& QueueClass<T, size>::operator[](int index) |
| 109 | +{ |
| 110 | + return Array[(Head + index) & (size - 1)]; |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +template<class T, int size> |
| 115 | +inline T& QueueClass<T, size>::First() |
| 116 | +{ |
| 117 | + return Array[Head]; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +template<class T, int size> |
| 122 | +inline int QueueClass<T, size>::GetHead() |
| 123 | +{ |
| 124 | + return Head; |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +template<class T, int size> |
| 129 | +inline int QueueClass<T, size>::GetTail() |
| 130 | +{ |
| 131 | + return Tail; |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +template<class T, int size> |
| 136 | +inline T* QueueClass<T, size>::GetArray() |
| 137 | +{ |
| 138 | + return Array; |
| 139 | +} |
0 commit comments