Commit | Line | Data |
---|---|---|
a7868768 BA |
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" | |
195fd722 | 12 | #include "cgds/List.h" |
a7868768 BA |
13 | |
14 | /** | |
15 | * @brief Queue containing generic data. | |
16 | * @param dataSize Size in bytes of a queue element. | |
17 | */ | |
18 | typedef struct Queue { | |
a7868768 | 19 | size_t dataSize; ///< Size in bytes of a queue element. |
195fd722 | 20 | List* list; ///< Internal list representation |
a7868768 BA |
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 | _queue_new(sizeof(type)) | |
48 | ||
49 | /** | |
50 | * @brief Copy constructor (works well if items do not have allocated sub-pointers). | |
51 | */ | |
52 | Queue* queue_copy( | |
53 | Queue* queue ///< "this" pointer. | |
54 | ); | |
55 | ||
56 | /** | |
57 | * @brief Check if the queue is empty. | |
58 | */ | |
59 | Bool queue_empty( | |
60 | Queue* queue ///< "this" pointer. | |
61 | ); | |
62 | ||
63 | /** | |
64 | * @brief Return size of the current queue. | |
65 | */ | |
66 | UInt queue_size( | |
67 | Queue* queue ///< "this" pointer. | |
68 | ); | |
69 | ||
70 | /** | |
71 | * @brief Add something at the end of the queue. | |
72 | */ | |
73 | void _queue_push( | |
74 | Queue* queue, ///< "this" pointer. | |
75 | void* data ///< Data to be pushed. | |
76 | ); | |
77 | ||
78 | /** | |
79 | * @brief Add something at the end of the queue. | |
80 | * @param queue "this" pointer | |
81 | * @param data Data to be pushed. | |
82 | * | |
83 | * Usage: void queue_push(Queue* queue, void data) | |
84 | */ | |
85 | #define queue_push(queue, data) \ | |
86 | { \ | |
87 | typeof((data)) tmp = data; \ | |
88 | _queue_push(queue, &tmp); \ | |
89 | } | |
90 | ||
91 | /** | |
92 | * @brief Return what is at the beginning of the queue. | |
93 | */ | |
94 | void* _queue_peek( | |
95 | Queue* queue ///< "this" pointer. | |
96 | ); | |
97 | ||
98 | /** | |
99 | * @brief Return what is at the beginning of the queue. | |
100 | * @param queue "this" pointer. | |
101 | * @param data Data to be assigned. | |
102 | * | |
103 | * Usage: void queue_peek(Queue* queue, void data) | |
104 | */ | |
105 | #define queue_peek(queue, data) \ | |
106 | { \ | |
107 | void* pData = _queue_peek(queue); \ | |
108 | data = *((typeof(&data))pData); \ | |
109 | } | |
110 | ||
111 | /** | |
112 | * @brief Remove the beginning of the queue. | |
113 | */ | |
114 | void queue_pop( | |
115 | Queue* queue ///< "this" pointer. | |
116 | ); | |
117 | ||
118 | /** | |
119 | * @brief Clear the entire queue. | |
120 | */ | |
121 | void queue_clear( | |
122 | Queue* queue ///< "this" pointer. | |
123 | ); | |
124 | ||
125 | /** | |
126 | * @brief Destroy the queue: clear it, and free 'queue' pointer. | |
127 | */ | |
128 | void queue_destroy( | |
129 | Queue* queue ///< "this" pointer. | |
130 | ); | |
131 | ||
132 | #endif |