summaryrefslogtreecommitdiffhomepage
path: root/docs/cqueue_api.md
blob: b399a14c0c20a78a041e812da02eda4a23069bd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# STC Container [cqueue](../stc/cqueue.h): Queue
![Queue](pics/queue.jpg)

This describes the API of the queue type **cqueue**.
See [std::queue](https://en.cppreference.com/w/cpp/container/queue) for a similar c++ class.

## Declaration

```c
#define using_cqueue(X, ctype)
```
The macro `using_cqueue()` must be instantiated in the global scope. **cqueue** uses normally
a **clist** type as underlying implementation, given as `ctype`. `X` is a type tag name and
will affect the names of all cqueue types and methods. E.g. declaring `using_cqueue(my, clist_my);`,
`X` should be replaced by `my` in all of the following documentation.

## Types

| Type name             | Type definition                        | Used to represent...     |
|:----------------------|:---------------------------------------|:-------------------------|
| `cqueue_X`            | Depends on underlying container type   | The cqueue type          |
| `cqueue_X_value_t`    |                   "                    | The cqueue element type  |
| `cqueue_X_rawvalue_t` |                   "                    | cqueue raw value type    |
| `cqueue_X_iter_t`     |                   "                    | cqueue iterator          |

## Header file

All cqueue definitions and prototypes may be included in your C source file by including a single header file.

```c
#include "stc/cqueue.h" /* includes default underlying implementation header cdeq.h */
```

## Methods

```c
cqueue_X                cqueue_X_init(void);
cqueue_X                cqueue_X_clone(cqueue_X q);
void                    cqueue_X_del(cqueue_X* self);

size_t                  cqueue_X_size(cqueue_X q);
bool                    cqueue_X_empty(cqueue_X q);
cqueue_X_value_t*       cqueue_X_front(cqueue_X* self);
cqueue_X_value_t*       cqueue_X_back(cqueue_X* self);

void                    cqueue_X_push_n(cqueue_X *self, const cqueue_X_rawvalue_t arr[], size_t size);
void                    cqueue_X_emplace(cqueue_X* self, cqueue_X_rawvalue_t raw);
void                    cqueue_X_push(cqueue_X* self, cqueue_X_value_t value);
void                    cqueue_X_pop(cqueue_X* self);

cqueue_X_iter_t         cqueue_X_begin(cqueue_X* self);
cqueue_X_iter_t         cqueue_X_end(cqueue_X* self);
void                    cqueue_X_next(cqueue_X_iter_t* it);
cqueue_X_value_t*       cqueue_X_itval(cqueue_X_iter_t it);

cqueue_X_value_t        cqueue_X_value_clone(cqueue_X_value_t val);
```

## Examples
```c
#include <stdio.h>
#include "stc/cqueue.h"

using_cdeq(i, int);
using_cqueue(i, cdeq_i);

int main() {
    cqueue_i queue = cqueue_i_init();

    // push() and pop() a few.
    c_forrange (i, 20)
        cqueue_i_push(&queue, i);

    c_forrange (5)
        cqueue_i_pop(&queue);

    c_foreach (i, cqueue_i, queue)
        printf(" %d", *i.ref);

    cqueue_i_del(&queue);
}
```
Output:
```
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
```
### Example 2
```c
#include <stdio.h>
#include "stc/cqueue.h"
#include "stc/clist.h"

using_cdeq(i, int);
using_cqueue(i, clist_i);

int main() {
    cqueue_i queue = cqueue_i_init();

    // push() and pop() a few.
    c_forrange (i, 20)
        cqueue_i_push(&queue, i);

    c_forrange (5)
        cqueue_i_pop(&queue);

    c_foreach (i, cqueue_i, queue)
        printf(" %d", *i.ref);

    cqueue_i_del(&queue);
}
```
Output:
```
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
```