| 1 | /** |
| 2 | * @file Queue.h |
| 3 | */ |
| 4 | |
| 5 | #ifndef CGDS_QUEUE_H |
| 6 | #define CGDS_QUEUE_H |
| 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include "cgds/types.h" |
| 11 | #include "cgds/safe_alloc.h" |
| 12 | #include "cgds/List.h" |
| 13 | |
| 14 | /** |
| 15 | * @brief Queue containing generic data. |
| 16 | * @param dataSize Size in bytes of a queue element. |
| 17 | */ |
| 18 | typedef struct Queue { |
| 19 | size_t dataSize; ///< Size in bytes of a queue element. |
| 20 | List* list; ///< Internal list representation |
| 21 | } Queue; |
| 22 | |
| 23 | /** |
| 24 | * @brief Initialize an empty queue. |
| 25 | * @param queue "this" pointer. |
| 26 | * @param dataSize Size in bytes of a queue element. |
| 27 | */ |
| 28 | void _queue_init( |
| 29 | Queue* queue, ///< "this" pointer. |
| 30 | size_t dataSize ///< Size in bytes of a queue element. |
| 31 | ); |
| 32 | |
| 33 | /** |
| 34 | * @brief Return an allocated and initialized queue. |
| 35 | */ |
| 36 | Queue* _queue_new( |
| 37 | size_t dataSize ///< Size in bytes of a queue element. |
| 38 | ); |
| 39 | |
| 40 | /** |
| 41 | * @brief Return an allocated and initialized queue. |
| 42 | * @param type Type of a queue element (int, char*, ...). |
| 43 | * |
| 44 | * Usage: Queue* queue_new(<Type> type) |
| 45 | */ |
| 46 | #define queue_new(type) \ |
| 47 | { \ |
| 48 | _queue_new(sizeof(type)); \ |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @brief Copy constructor (shallow copy, ok for basic types). |
| 53 | */ |
| 54 | Queue* queue_copy( |
| 55 | Queue* queue ///< "this" pointer. |
| 56 | ); |
| 57 | |
| 58 | /** |
| 59 | * @brief Check if the queue is empty. |
| 60 | */ |
| 61 | bool queue_empty( |
| 62 | Queue* queue ///< "this" pointer. |
| 63 | ); |
| 64 | |
| 65 | /** |
| 66 | * @brief Return size of the current queue. |
| 67 | */ |
| 68 | UInt queue_size( |
| 69 | Queue* queue ///< "this" pointer. |
| 70 | ); |
| 71 | |
| 72 | /** |
| 73 | * @brief Add something at the end of the queue. |
| 74 | */ |
| 75 | void _queue_push( |
| 76 | Queue* queue, ///< "this" pointer. |
| 77 | void* data ///< Data to be pushed. |
| 78 | ); |
| 79 | |
| 80 | /** |
| 81 | * @brief Add something at the end of the queue. |
| 82 | * @param queue "this" pointer |
| 83 | * @param data Data to be pushed. |
| 84 | * |
| 85 | * Usage: void queue_push(Queue* queue, void data) |
| 86 | */ |
| 87 | #define queue_push(queue, data) \ |
| 88 | { \ |
| 89 | typeof((data)) tmp = data; \ |
| 90 | _queue_push(queue, &tmp); \ |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @brief Return what is at the beginning of the queue. |
| 95 | */ |
| 96 | void* _queue_peek( |
| 97 | Queue* queue ///< "this" pointer. |
| 98 | ); |
| 99 | |
| 100 | /** |
| 101 | * @brief Return what is at the beginning of the queue. |
| 102 | * @param queue "this" pointer. |
| 103 | * @param data Data to be assigned. |
| 104 | * |
| 105 | * Usage: void queue_peek(Queue* queue, void data) |
| 106 | */ |
| 107 | #define queue_peek(queue, data) \ |
| 108 | { \ |
| 109 | void* pData = _queue_peek(queue); \ |
| 110 | data = *((typeof(&data))pData); \ |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @brief Remove the beginning of the queue. |
| 115 | */ |
| 116 | void queue_pop( |
| 117 | Queue* queue ///< "this" pointer. |
| 118 | ); |
| 119 | |
| 120 | /** |
| 121 | * @brief Clear the entire queue. |
| 122 | */ |
| 123 | void queue_clear( |
| 124 | Queue* queue ///< "this" pointer. |
| 125 | ); |
| 126 | |
| 127 | /** |
| 128 | * @brief Destroy the queue: clear it, and free 'queue' pointer. |
| 129 | */ |
| 130 | void queue_destroy( |
| 131 | Queue* queue ///< "this" pointer. |
| 132 | ); |
| 133 | |
| 134 | #endif |