HW12/simple_circular_buffer.h
ubuntu201711081 7ffcbafaa2 add comment
2020-12-14 10:57:12 +00:00

36 lines
1012 B
C

#ifndef _SIMPLE_CIRCULAR_BUFFER_
#define _SIMPLE_CIRCULAR_BUFFER_
#include<stddef.h>
//queue type declare
#define queue_struct(queue_type,queue_size) struct{\
queue_type data [(queue_size)+1];\
size_t begin;\
size_t end;\
}
//queue's attribute
#define queue_size(queue) ((sizeof((queue)->data)/sizeof((queue)->data[0])) - 1)
#define queue_isempty(queue) ((queue)->begin == (queue)->end)
#define queue_isfull(queue) ((queue)->begin == (((queue)->end + 1) % queue_size(queue)))
//queue's methods
#define queue_init(queue) do{\
(queue)->begin = 0;\
(queue)->end = 0;\
}while(0)
//this do not check that queue is full.
#define enqueue(queue,element) do{ \
(queue)->data[(queue)->end] = (element);\
(queue)->end = ((queue)->end + 1) % (queue_size(queue) + 1);\
}while(0)
//this do not check that queue is empty.
#define dequeue(queue) \
(((queue)->begin = ((queue)->begin + 1) % (queue_size(queue) + 1)) ,\
(queue)->data[((queue)->begin + queue_size(queue)) % (queue_size(queue) + 1)])
#endif