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) \ | |
eed1b5d2 | 47 | _queue_new(sizeof(type)) |
a7868768 BA |
48 | |
49 | /** | |
1ff641f9 | 50 | * @brief Copy constructor (shallow copy, ok for basic types). |
a7868768 BA |
51 | */ |
52 | Queue* queue_copy( | |
e45132ac | 53 | Queue* queue ///< "this" pointer. |
a7868768 BA |
54 | ); |
55 | ||
56 | /** | |
57 | * @brief Check if the queue is empty. | |
58 | */ | |
1ff641f9 | 59 | bool queue_empty( |
e45132ac | 60 | Queue* queue ///< "this" pointer. |
a7868768 BA |
61 | ); |
62 | ||
63 | /** | |
64 | * @brief Return size of the current queue. | |
65 | */ | |
66 | UInt queue_size( | |
e45132ac | 67 | Queue* queue ///< "this" pointer. |
a7868768 BA |
68 | ); |
69 | ||
70 | /** | |
71 | * @brief Add something at the end of the queue. | |
72 | */ | |
73 | void _queue_push( | |
e45132ac BA |
74 | Queue* queue, ///< "this" pointer. |
75 | void* data ///< Data to be pushed. | |
a7868768 BA |
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. | |
1ff641f9 | 82 | * |
a7868768 BA |
83 | * Usage: void queue_push(Queue* queue, void data) |
84 | */ | |
85 | #define queue_push(queue, data) \ | |
86 | { \ | |
eed1b5d2 | 87 | typeof(data) tmp = data; \ |
e45132ac | 88 | _queue_push(queue, &tmp); \ |
a7868768 BA |
89 | } |
90 | ||
91 | /** | |
92 | * @brief Return what is at the beginning of the queue. | |
93 | */ | |
94 | void* _queue_peek( | |
e45132ac | 95 | Queue* queue ///< "this" pointer. |
a7868768 BA |
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. | |
1ff641f9 | 102 | * |
a7868768 BA |
103 | * Usage: void queue_peek(Queue* queue, void data) |
104 | */ | |
105 | #define queue_peek(queue, data) \ | |
106 | { \ | |
e45132ac BA |
107 | void* pData = _queue_peek(queue); \ |
108 | data = *((typeof(&data))pData); \ | |
a7868768 BA |
109 | } |
110 | ||
111 | /** | |
112 | * @brief Remove the beginning of the queue. | |
113 | */ | |
114 | void queue_pop( | |
e45132ac | 115 | Queue* queue ///< "this" pointer. |
a7868768 BA |
116 | ); |
117 | ||
118 | /** | |
119 | * @brief Clear the entire queue. | |
120 | */ | |
121 | void queue_clear( | |
e45132ac | 122 | Queue* queue ///< "this" pointer. |
a7868768 BA |
123 | ); |
124 | ||
125 | /** | |
126 | * @brief Destroy the queue: clear it, and free 'queue' pointer. | |
127 | */ | |
128 | void queue_destroy( | |
e45132ac | 129 | Queue* queue ///< "this" pointer. |
a7868768 BA |
130 | ); |
131 | ||
132 | #endif |