1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdio.h>
5 
6 #include "hashmap.h"
7 
8 typedef struct OrderedSet OrderedSet;
9 
_ordered_set_new(const struct hash_ops * ops HASHMAP_DEBUG_PARAMS)10 static inline OrderedSet* _ordered_set_new(const struct hash_ops *ops  HASHMAP_DEBUG_PARAMS) {
11         return (OrderedSet*) _ordered_hashmap_new(ops  HASHMAP_DEBUG_PASS_ARGS);
12 }
13 #define ordered_set_new(ops) _ordered_set_new(ops  HASHMAP_DEBUG_SRC_ARGS)
14 
15 int _ordered_set_ensure_allocated(OrderedSet **s, const struct hash_ops *ops  HASHMAP_DEBUG_PARAMS);
16 #define ordered_set_ensure_allocated(s, ops) _ordered_set_ensure_allocated(s, ops  HASHMAP_DEBUG_SRC_ARGS)
17 
18 int _ordered_set_ensure_put(OrderedSet **s, const struct hash_ops *ops, void *p  HASHMAP_DEBUG_PARAMS);
19 #define ordered_set_ensure_put(s, hash_ops, key) _ordered_set_ensure_put(s, hash_ops, key  HASHMAP_DEBUG_SRC_ARGS)
20 
ordered_set_clear(OrderedSet * s)21 static inline void ordered_set_clear(OrderedSet *s) {
22         return ordered_hashmap_clear((OrderedHashmap*) s);
23 }
24 
ordered_set_clear_free(OrderedSet * s)25 static inline void ordered_set_clear_free(OrderedSet *s) {
26         return ordered_hashmap_clear_free((OrderedHashmap*) s);
27 }
28 
ordered_set_free(OrderedSet * s)29 static inline OrderedSet* ordered_set_free(OrderedSet *s) {
30         return (OrderedSet*) ordered_hashmap_free((OrderedHashmap*) s);
31 }
32 
ordered_set_free_free(OrderedSet * s)33 static inline OrderedSet* ordered_set_free_free(OrderedSet *s) {
34         return (OrderedSet*) ordered_hashmap_free_free((OrderedHashmap*) s);
35 }
36 
ordered_set_contains(OrderedSet * s,const void * p)37 static inline int ordered_set_contains(OrderedSet *s, const void *p) {
38         return ordered_hashmap_contains((OrderedHashmap*) s, p);
39 }
40 
ordered_set_put(OrderedSet * s,void * p)41 static inline int ordered_set_put(OrderedSet *s, void *p) {
42         return ordered_hashmap_put((OrderedHashmap*) s, p, p);
43 }
44 
ordered_set_get(OrderedSet * s,const void * p)45 static inline void *ordered_set_get(OrderedSet *s, const void *p) {
46         return ordered_hashmap_get((OrderedHashmap*) s, p);
47 }
48 
ordered_set_size(OrderedSet * s)49 static inline unsigned ordered_set_size(OrderedSet *s) {
50         return ordered_hashmap_size((OrderedHashmap*) s);
51 }
52 
ordered_set_isempty(OrderedSet * s)53 static inline bool ordered_set_isempty(OrderedSet *s) {
54         return ordered_hashmap_isempty((OrderedHashmap*) s);
55 }
56 
ordered_set_iterate(OrderedSet * s,Iterator * i,void ** value)57 static inline bool ordered_set_iterate(OrderedSet *s, Iterator *i, void **value) {
58         return ordered_hashmap_iterate((OrderedHashmap*) s, i, value, NULL);
59 }
60 
ordered_set_remove(OrderedSet * s,void * p)61 static inline void* ordered_set_remove(OrderedSet *s, void *p) {
62         return ordered_hashmap_remove((OrderedHashmap*) s, p);
63 }
64 
ordered_set_first(OrderedSet * s)65 static inline void* ordered_set_first(OrderedSet *s) {
66         return ordered_hashmap_first((OrderedHashmap*) s);
67 }
68 
ordered_set_steal_first(OrderedSet * s)69 static inline void* ordered_set_steal_first(OrderedSet *s) {
70         return ordered_hashmap_steal_first((OrderedHashmap*) s);
71 }
72 
ordered_set_get_strv(OrderedSet * s)73 static inline char** ordered_set_get_strv(OrderedSet *s) {
74         return _hashmap_get_strv(HASHMAP_BASE((OrderedHashmap*) s));
75 }
76 
ordered_set_reserve(OrderedSet * s,unsigned entries_add)77 static inline int ordered_set_reserve(OrderedSet *s, unsigned entries_add) {
78         return ordered_hashmap_reserve((OrderedHashmap*) s, entries_add);
79 }
80 
81 int ordered_set_consume(OrderedSet *s, void *p);
82 int _ordered_set_put_strdup(OrderedSet **s, const char *p  HASHMAP_DEBUG_PARAMS);
83 #define ordered_set_put_strdup(s, p) _ordered_set_put_strdup(s, p  HASHMAP_DEBUG_SRC_ARGS)
84 int _ordered_set_put_strdupv(OrderedSet **s, char **l  HASHMAP_DEBUG_PARAMS);
85 #define ordered_set_put_strdupv(s, l) _ordered_set_put_strdupv(s, l  HASHMAP_DEBUG_SRC_ARGS)
86 int ordered_set_put_string_set(OrderedSet **s, OrderedSet *l);
87 void ordered_set_print(FILE *f, const char *field, OrderedSet *s);
88 
89 #define _ORDERED_SET_FOREACH(e, s, i) \
90         for (Iterator i = ITERATOR_FIRST; ordered_set_iterate((s), &i, (void**)&(e)); )
91 #define ORDERED_SET_FOREACH(e, s) \
92         _ORDERED_SET_FOREACH(e, s, UNIQ_T(i, UNIQ))
93 
94 #define ordered_set_clear_with_destructor(s, f)                 \
95         ({                                                      \
96                 OrderedSet *_s = (s);                           \
97                 void *_item;                                    \
98                 while ((_item = ordered_set_steal_first(_s)))   \
99                         f(_item);                               \
100                 _s;                                             \
101         })
102 #define ordered_set_free_with_destructor(s, f)                  \
103         ordered_set_free(ordered_set_clear_with_destructor(s, f))
104 
105 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedSet*, ordered_set_free);
106 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedSet*, ordered_set_free_free);
107 
108 #define _cleanup_ordered_set_free_ _cleanup_(ordered_set_freep)
109 #define _cleanup_ordered_set_free_free_ _cleanup_(ordered_set_free_freep)
110