https://github.com/costava/c-queue
Generic, type-safe queue using a circular buffer
https://github.com/costava/c-queue
c c99 queue
Last synced: 4 days ago
JSON representation
Generic, type-safe queue using a circular buffer
- Host: GitHub
- URL: https://github.com/costava/c-queue
- Owner: Costava
- License: bsd-2-clause
- Created: 2021-05-15T05:56:17.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-15T05:58:53.000Z (about 5 years ago)
- Last Synced: 2025-03-27T23:44:47.731Z (over 1 year ago)
- Topics: c, c99, queue
- Language: C
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# C Queue
- Generic, type-safe queue using a circular buffer
- Uses macros to generate a struct (where queue state is managed) and functions to act on that struct for any given type
- The internal buffer is automatically resized (2x) when necessary to fit additional items
Dependencies:
- C99 standard library
## Usage
```C
#include "q.h"
// Generate the code for the type `unsigned int`
// "uint" is suffixed to "q" in the function names
Q_GENERATE_FOR_TYPE(unsigned int, uint)
int main(void) {
quint nums;
unsigned int val;
// Initialize with capacity of 64
quint_init(&nums, 64);
quint_enqueue(&nums, 111u);
quint_enqueue(&nums, 222u);
quint_enqueue(&nums, 333u);
quint_try_dequeue(&nums, &val); // Returned true and val is 111u
quint_try_dequeue(&nums, &val); // Returned true and val is 222u
quint_try_dequeue(&nums, &val); // Returned true and val is 333u
quint_try_dequeue(&nums, &val); // Returned false and val remains 333u
quint_enqueue(&nums, 444u);
quint_clear(&nums);
quint_try_dequeue(&nums, &val); // Returned false and val remains 333u
quint_deinit(&nums);
return 0;
}
```
`Q_GENERATE_FOR_TYPE` is a convenience macro that calls both
`Q_GENERATE_HEADER_CODE` and `Q_GENERATE_IMPLEMENTATION_CODE`.
Alternatively, these two macros can be called separately
as seen in `test/car.h` and `test/car.c` respectively.
## License
BSD 2-Clause License. See file `LICENSE.txt`.