a simple c queue
queue.h@0:8b473c4a0afc, 2015-02-14 (annotated)
- Committer:
- danilob
- Date:
- Sat Feb 14 23:25:22 2015 +0000
- Revision:
- 0:8b473c4a0afc
it has been improved
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
danilob | 0:8b473c4a0afc | 1 | /* |
danilob | 0:8b473c4a0afc | 2 | * queue.h |
danilob | 0:8b473c4a0afc | 3 | * |
danilob | 0:8b473c4a0afc | 4 | * Created on: Jan 15, 2015 |
danilob | 0:8b473c4a0afc | 5 | * Author: danilob |
danilob | 0:8b473c4a0afc | 6 | */ |
danilob | 0:8b473c4a0afc | 7 | |
danilob | 0:8b473c4a0afc | 8 | #ifndef QUEUE_H_ |
danilob | 0:8b473c4a0afc | 9 | #define QUEUE_H_ |
danilob | 0:8b473c4a0afc | 10 | |
danilob | 0:8b473c4a0afc | 11 | #include <stdint.h> |
danilob | 0:8b473c4a0afc | 12 | #include <stdbool.h> |
danilob | 0:8b473c4a0afc | 13 | |
danilob | 0:8b473c4a0afc | 14 | typedef uint32_t queue_index_t; |
danilob | 0:8b473c4a0afc | 15 | typedef double queue_data_t; |
danilob | 0:8b473c4a0afc | 16 | typedef uint32_t queue_size_t; |
danilob | 0:8b473c4a0afc | 17 | |
danilob | 0:8b473c4a0afc | 18 | typedef struct { |
danilob | 0:8b473c4a0afc | 19 | queue_index_t indexIn; |
danilob | 0:8b473c4a0afc | 20 | queue_index_t indexOut; |
danilob | 0:8b473c4a0afc | 21 | queue_size_t size; |
danilob | 0:8b473c4a0afc | 22 | queue_size_t elementCount; |
danilob | 0:8b473c4a0afc | 23 | queue_data_t * data; |
danilob | 0:8b473c4a0afc | 24 | } queue_t; |
danilob | 0:8b473c4a0afc | 25 | |
danilob | 0:8b473c4a0afc | 26 | // Allocates the memory to you queue (the data* pointer) and initializes all parts of the data structure. |
danilob | 0:8b473c4a0afc | 27 | void queue_init(queue_t* q, queue_size_t size); |
danilob | 0:8b473c4a0afc | 28 | |
danilob | 0:8b473c4a0afc | 29 | // Returns the size of the queue.. |
danilob | 0:8b473c4a0afc | 30 | queue_size_t queue_size(queue_t* q); |
danilob | 0:8b473c4a0afc | 31 | |
danilob | 0:8b473c4a0afc | 32 | // Returns true if the queue is full. |
danilob | 0:8b473c4a0afc | 33 | bool queueFull(queue_t* q); |
danilob | 0:8b473c4a0afc | 34 | |
danilob | 0:8b473c4a0afc | 35 | // Returns true if the queue is empty. |
danilob | 0:8b473c4a0afc | 36 | bool queue_empty(queue_t* q); |
danilob | 0:8b473c4a0afc | 37 | |
danilob | 0:8b473c4a0afc | 38 | // Pushes a new element into the queue. Reports an error if the queue is full. |
danilob | 0:8b473c4a0afc | 39 | void queue_push(queue_t* q, queue_data_t value); |
danilob | 0:8b473c4a0afc | 40 | |
danilob | 0:8b473c4a0afc | 41 | // Removes the oldest element in the queue. |
danilob | 0:8b473c4a0afc | 42 | queue_data_t queue_pop(queue_t* q); |
danilob | 0:8b473c4a0afc | 43 | |
danilob | 0:8b473c4a0afc | 44 | // Pushes a new element into the queue, making room by removing the oldest element. |
danilob | 0:8b473c4a0afc | 45 | void queue_overwritePush(queue_t* q, queue_data_t value); |
danilob | 0:8b473c4a0afc | 46 | |
danilob | 0:8b473c4a0afc | 47 | // Provides random-access read capability to the queue. |
danilob | 0:8b473c4a0afc | 48 | // Low-valued indexes access older queue elements while higher-value indexes access newer elements |
danilob | 0:8b473c4a0afc | 49 | // (according to the order that they were added). |
danilob | 0:8b473c4a0afc | 50 | queue_data_t queue_readElementAt(queue_t* q, queue_index_t index); |
danilob | 0:8b473c4a0afc | 51 | |
danilob | 0:8b473c4a0afc | 52 | // Returns a count of the elements currently contained in the queue. |
danilob | 0:8b473c4a0afc | 53 | queue_size_t queue_elementCount(queue_t* q); |
danilob | 0:8b473c4a0afc | 54 | |
danilob | 0:8b473c4a0afc | 55 | // Frees the storage that you malloc'd before. |
danilob | 0:8b473c4a0afc | 56 | void gueue_garbageCollect(queue_t* q); |
danilob | 0:8b473c4a0afc | 57 | |
danilob | 0:8b473c4a0afc | 58 | // Prints the current contents of the queue. Handy for debugging. |
danilob | 0:8b473c4a0afc | 59 | void queue_print(queue_t* q); |
danilob | 0:8b473c4a0afc | 60 | |
danilob | 0:8b473c4a0afc | 61 | // Performs a comprehensive test of all queue functions. |
danilob | 0:8b473c4a0afc | 62 | int queue_runTest(); |
danilob | 0:8b473c4a0afc | 63 | |
danilob | 0:8b473c4a0afc | 64 | |
danilob | 0:8b473c4a0afc | 65 | #endif /* QUEUE_H_ */ |