You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

67 lines
1.3 KiB

#pragma once
#include <stdio.h>
#include <string.h>
namespace iflytop {
using namespace std;
template <typename T>
class LoopQueue {
private:
T *arr;
int maxSize;
int rear;
int front;
public:
LoopQueue(/* args */) {
maxSize = 1;
rear = 0;
front = 0;
}
~LoopQueue() {}
void initialize(int size) {
arr = (T *)malloc(size * sizeof(T));
ZASSERT(arr != NULL);
maxSize = size;
}
bool push(T *data) {
if (isFull()) {
return false;
}
memcpy(&arr[rear], data, sizeof(T));
rear = (rear + 1) % maxSize;
return true;
}
bool pop(T *data) {
if (isEmpty()) {
return false;
}
memcpy(data, &arr[front], sizeof(T));
front = (front + 1) % maxSize;
return true;
}
bool popMuti(T *data, int numdata) {
if (numdata > numElements()) return false;
for (int i = 0; i < numdata; i++) {
pop(&data[i]);
}
return true;
}
bool getHeader(T *data) {
if (isEmpty()) {
return false;
}
memcpy(data, &arr[front], sizeof(T));
return true;
}
int size() { return maxSize; }
int numElements() { return (rear + maxSize - front) % maxSize; }
bool isEmpty() { return rear == front; }
bool isFull() { return (rear + 1) % maxSize == front; }
};
} // namespace iflytop