about summary refs log tree commit diff
path: root/src/libnm-glib-aux/nm-prioq.h
blob: 1c16452baa4d6f0ae0d3bda16bcb92a7ecd91966 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#ifndef __NM_PRIOQ_H__
#define __NM_PRIOQ_H__

#define NM_PRIOQ_IDX_NULL G_MAXUINT

struct _NMPrioqItem;

typedef struct {
    struct {
        union {
            GCompareDataFunc compare_data_func;
            GCompareFunc     compare_func;
        };

        gpointer compare_data;

        struct _NMPrioqItem *items;

        unsigned n_items;
        unsigned n_allocated;

        bool compare_with_data;
    } _priv;
} NMPrioq;

#define NM_PRIOQ_ZERO                 \
    {                                 \
        ._priv =                      \
            {                         \
                .compare_func = NULL, \
            },                        \
    }

void nm_prioq_init(NMPrioq *q, GCompareFunc compare_func);
void nm_prioq_init_with_data(NMPrioq *q, GCompareDataFunc compare_func, gpointer compare_data);

void nm_prioq_destroy(NMPrioq *q);

#define nm_auto_prioq nm_auto(nm_prioq_destroy)

void     nm_prioq_put(NMPrioq *q, void *data, unsigned *idx);
gboolean nm_prioq_remove(NMPrioq *q, void *data, unsigned *idx);
gboolean nm_prioq_reshuffle(NMPrioq *q, void *data, unsigned *idx);

void nm_prioq_update(NMPrioq *q, void *data, unsigned *idx, bool queued /* or else remove */);

void *nm_prioq_peek_by_index(NMPrioq *q, unsigned idx) _nm_pure;

static inline void *
nm_prioq_peek(NMPrioq *q)
{
    return nm_prioq_peek_by_index(q, 0);
}

void *nm_prioq_pop(NMPrioq *q);

#define nm_prioq_for_each(q, p) for (unsigned _i = 0; (p = nm_prioq_peek_by_index((q), _i)); _i++)

_nm_pure static inline unsigned
nm_prioq_size(NMPrioq *q)
{
    nm_assert(q);
    return q->_priv.n_items;
}

_nm_pure static inline gboolean
nm_prioq_isempty(NMPrioq *q)
{
    return nm_prioq_size(q) <= 0;
}

#endif /* __NM_PRIOQ_H__ */