Debian Codegraph @

android/app/jni/src/fixedQueue.h

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef _FIXED_QUEUE_H
21 #define _FIXED_QUEUE_H
22
23 #include <stdint.h>
24 #include "ta-log.h"
25
26 template<typename T, size_t items> struct FixedQueue {
27 size_t readPos, writePos;
28 T data[items];
29
30 T& operator[](size_t pos);
31 T& front();
32 T& back();
33 bool pop();
34 bool push(const T& item);
35
36 bool erase(size_t pos);
37
38 bool pop_front();
39 bool pop_back();
40 bool push_front(const T& item);
41 bool push_back(const T& item);
42 void clear();
43 bool empty();
44 size_t size();
45 size_t capacity();
46 FixedQueue():
47 readPos(0),
48 writePos(0) {}
49 };
50
51 template <typename T, size_t items> T& FixedQueue<T,items>::operator[](size_t pos) {
52 if (pos>=size()) {
53 logW("accessing invalid position. bug!");
54 }
55 return data[(readPos+pos)%items];
56 }
57
58 template <typename T, size_t items> T& FixedQueue<T,items>::front() {
59 return data[readPos];
60 }
61
62 template <typename T, size_t items> T& FixedQueue<T,items>::back() {
63 if (writePos==0) return data[items-1];
64 return data[writePos-1];
65 }
66
67 template <typename T, size_t items> bool FixedQueue<T,items>::erase(size_t pos) {
68 size_t curSize=size();
69 if (pos>=curSize) {
70 logW("accessing invalid position. bug!");
71 return false;
72 }
73 if (pos==0) {
74 return pop_front();
75 }
76 if (pos==curSize-1) {
77 return pop_back();
78 }
79
80 for (size_t i=pos+1, p=(readPos+pos)%items, p1=(readPos+pos+1)%items; i<curSize; i++) {
81 if (p>=items) p-=items;
82 if (p1>=items) p1-=items;
83 data[p]=data[p1];
84 p++;
85 p1++;
86 }
87
88 if (writePos>0) {
89 writePos--;
90 } else {
91 writePos=items-1;
92 }
93
94 return true;
95 }
96
97 template <typename T, size_t items> bool FixedQueue<T,items>::pop() {
98 if (readPos==writePos) return false;
99 if (++readPos>=items) readPos=0;
100 return true;
101 }
102
103 template <typename T, size_t items> bool FixedQueue<T,items>::push(const T& item) {
104 if (writePos==(readPos-1)) {
105 //logW("queue overflow!");
106 return false;
107 }
108 if (writePos==items-1 && readPos==0) {
109 //logW("queue overflow!");
110 return false;
111 }
112 data[writePos]=item;
113 if (++writePos>=items) writePos=0;
114 return true;
115 }
116
117 template <typename T, size_t items> bool FixedQueue<T,items>::pop_front() {
118 if (readPos==writePos) return false;
119 if (++readPos>=items) readPos=0;
120 return true;
121 }
122
123 template <typename T, size_t items> bool FixedQueue<T,items>::push_back(const T& item) {
124 if (writePos==(readPos-1)) {
125 //logW("queue overflow!");
126 return false;
127 }
128 if (writePos==items-1 && readPos==0) {
129 //logW("queue overflow!");
130 return false;
131 }
132 data[writePos]=item;
133 if (++writePos>=items) writePos=0;
134 return true;
135 }
136
137 template <typename T, size_t items> bool FixedQueue<T,items>::pop_back() {
138 if (readPos==writePos) return false;
139 if (writePos>0) {
140 writePos--;
141 } else {
142 writePos=items-1;
143 }
144 return true;
145 }
146
147 template <typename T, size_t items> bool FixedQueue<T,items>::push_front(const T& item) {
148 if (readPos==(writePos+1)) {
149 //logW("stack overflow!");
150 return false;
151 }
152 if (readPos==0 && writePos==items-1) {
153 //logW("stack overflow!");
154 return false;
155 }
156 if (readPos>0) {
157 readPos--;
158 } else {
159 readPos=items-1;
160 }
161 data[readPos]=item;
162 return true;
163 }
164
165 template <typename T, size_t items> void FixedQueue<T,items>::clear() {
166 readPos=0;
167 writePos=0;
168 }
169
170 template <typename T, size_t items> bool FixedQueue<T,items>::empty() {
171 return (readPos==writePos);
172 }
173
174 template <typename T, size_t items> size_t FixedQueue<T,items>::size() {
175 if (readPos>writePos) {
176 return items+writePos-readPos;
177 }
178 return writePos-readPos;
179 }
180
181 template <typename T, size_t items> size_t FixedQueue<T,items>::capacity() {
182 return items-1;
183 }
184
185 #endif