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 { | |
e45132ac BA |
19 | size_t dataSize; ///< Size in bytes of a queue element. |
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( | |
e45132ac BA |
29 | Queue* queue, ///< "this" pointer. |
30 | size_t dataSize ///< Size in bytes of a queue element. | |
a7868768 BA |
31 | ); |
32 | ||
1ff641f9 | 33 | /** |
a7868768 BA |
34 | * @brief Return an allocated and initialized queue. |
35 | */ | |
36 | Queue* _queue_new( | |
e45132ac | 37 | size_t dataSize ///< Size in bytes of a queue element. |
a7868768 BA |
38 | ); |
39 | ||
1ff641f9 | 40 | /** |
a7868768 BA |
41 | * @brief Return an allocated and initialized queue. |
42 | * @param type Type of a queue element (int, char*, ...). | |
1ff641f9 | 43 | * |
a7868768 BA |
44 | * Usage: Queue* queue_new(<Type> type) |
45 | */ | |
46 | #define queue_new(type) \ | |
e45132ac BA |
47 | { \ |
48 | _queue_new(sizeof(type)); \ | |
49 | } | |
a7868768 BA |
50 | |
51 | /** | |
1ff641f9 | 52 | * @brief Copy constructor (shallow copy, ok for basic types). |
a7868768 BA |
53 | */ |
54 | Queue* queue_copy( | |
e45132ac | 55 | Queue* queue ///< "this" pointer. |
a7868768 BA |
56 | ); |
57 | ||
58 | /** | |
59 | * @brief Check if the queue is empty. | |
60 | */ | |
1ff641f9 | 61 | bool queue_empty( |
e45132ac | 62 | Queue* queue ///< "this" pointer. |
a7868768 BA |
63 | ); |
64 | ||
65 | /** | |
66 | * @brief Return size of the current queue. | |
67 | */ | |
68 | UInt queue_size( | |
e45132ac | 69 | Queue* queue ///< "this" pointer. |
a7868768 BA |
70 | ); |
71 | ||
72 | /** | |
73 | * @brief Add something at the end of the queue. | |
74 | */ | |
75 | void _queue_push( | |
e45132ac BA |
76 | Queue* queue, ///< "this" pointer. |
77 | void* data ///< Data to be pushed. | |
a7868768 BA |
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. | |
1ff641f9 | 84 | * |
a7868768 BA |
85 | * Usage: void queue_push(Queue* queue, void data) |
86 | */ | |
87 | #define queue_push(queue, data) \ | |
88 | { \ | |
e45132ac BA |
89 | typeof((data)) tmp = data; \ |
90 | _queue_push(queue, &tmp); \ | |
a7868768 BA |
91 | } |
92 | ||
93 | /** | |
94 | * @brief Return what is at the beginning of the queue. | |
95 | */ | |
96 | void* _queue_peek( | |
e45132ac | 97 | Queue* queue ///< "this" pointer. |
a7868768 BA |
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. | |
1ff641f9 | 104 | * |
a7868768 BA |
105 | * Usage: void queue_peek(Queue* queue, void data) |
106 | */ | |
107 | #define queue_peek(queue, data) \ | |
108 | { \ | |
e45132ac BA |
109 | void* pData = _queue_peek(queue); \ |
110 | data = *((typeof(&data))pData); \ | |
a7868768 BA |
111 | } |
112 | ||
113 | /** | |
114 | * @brief Remove the beginning of the queue. | |
115 | */ | |
116 | void queue_pop( | |
e45132ac | 117 | Queue* queue ///< "this" pointer. |
a7868768 BA |
118 | ); |
119 | ||
120 | /** | |
121 | * @brief Clear the entire queue. | |
122 | */ | |
123 | void queue_clear( | |
e45132ac | 124 | Queue* queue ///< "this" pointer. |
a7868768 BA |
125 | ); |
126 | ||
127 | /** | |
128 | * @brief Destroy the queue: clear it, and free 'queue' pointer. | |
129 | */ | |
130 | void queue_destroy( | |
e45132ac | 131 | Queue* queue ///< "this" pointer. |
a7868768 BA |
132 | ); |
133 | ||
134 | #endif |