修改开火延迟一直为0的错误
This commit is contained in:
@@ -42,21 +42,28 @@ private:
|
||||
type data[length];
|
||||
int head;
|
||||
int tail;
|
||||
int count;
|
||||
public:
|
||||
RoundQueue<type, length>() : head(0), tail(0) {};
|
||||
RoundQueue<type, length>() : head(0), tail(0), count(0) {};
|
||||
|
||||
constexpr int size() const {
|
||||
constexpr int capacity() const {
|
||||
return length;
|
||||
};
|
||||
|
||||
int size() const {
|
||||
return count;
|
||||
};
|
||||
|
||||
bool empty() const {
|
||||
return head == tail;
|
||||
return count == 0;
|
||||
};
|
||||
|
||||
void push(const type &obj) {
|
||||
data[head] = obj;
|
||||
head = (head + 1) % length;
|
||||
if (head == tail) {
|
||||
if (count < length) {
|
||||
count++;
|
||||
} else {
|
||||
tail = (tail + 1) % length;
|
||||
}
|
||||
};
|
||||
@@ -65,11 +72,15 @@ public:
|
||||
if (empty()) return false;
|
||||
obj = data[tail];
|
||||
tail = (tail + 1) % length;
|
||||
count--;
|
||||
return true;
|
||||
};
|
||||
|
||||
type &operator[](int idx) {
|
||||
while (tail + idx < 0) idx += length;
|
||||
if (count == 0) return data[0];
|
||||
if (idx < 0) idx += count;
|
||||
if (idx < 0) idx = 0;
|
||||
if (idx >= count) idx = count - 1;
|
||||
return data[(tail + idx) % length];
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user