aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/c-ares/src/lib/include/ares_llist.h
blob: 6aa0c78370401fddfbdade2a3ae3632ff23fefdd (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* MIT License
 *
 * Copyright (c) 2023 Brad House
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * SPDX-License-Identifier: MIT
 */
#ifndef __ARES__LLIST_H
#define __ARES__LLIST_H

/*! \addtogroup ares_llist LinkedList Data Structure
 *
 * This is a doubly-linked list data structure.
 *
 * Average time complexity:
 *  - Insert: O(1)   -- head or tail
 *  - Search: O(n)
 *  - Delete: O(1)   -- delete assumes you hold a node pointer
 *
 * @{
 */

struct ares_llist;

/*! Opaque data structure for linked list */
typedef struct ares_llist ares_llist_t;

struct ares_llist_node;

/*! Opaque data structure for a node in a linked list */
typedef struct ares_llist_node ares_llist_node_t;

/*! Callback to free user-defined node data
 *
 *  \param[in] data  user supplied data
 */
typedef void (*ares_llist_destructor_t)(void *data);

/*! Create a linked list object
 *
 *  \param[in] destruct  Optional. Destructor to call on all removed nodes
 *  \return linked list object or NULL on out of memory
 */
CARES_EXTERN ares_llist_t *ares_llist_create(ares_llist_destructor_t destruct);

/*! Replace destructor for linked list nodes.  Typically this is used
 *  when wanting to disable the destructor by using NULL.
 *
 *  \param[in] list      Initialized linked list object
 *  \param[in] destruct  replacement destructor, NULL is allowed
 */
CARES_EXTERN void
  ares_llist_replace_destructor(ares_llist_t           *list,
                                ares_llist_destructor_t destruct);

/*! Insert value as the first node in the linked list
 *
 *  \param[in] list   Initialized linked list object
 *  \param[in] val    user-supplied value.
 *  \return node object referencing place in list, or null if out of memory or
 *   misuse
 */
CARES_EXTERN ares_llist_node_t *ares_llist_insert_first(ares_llist_t *list,
                                                        void         *val);

/*! Insert value as the last node in the linked list
 *
 *  \param[in] list   Initialized linked list object
 *  \param[in] val    user-supplied value.
 *  \return node object referencing place in list, or null if out of memory or
 *   misuse
 */
CARES_EXTERN ares_llist_node_t *ares_llist_insert_last(ares_llist_t *list,
                                                       void         *val);

/*! Insert value before specified node in the linked list
 *
 *  \param[in] node  node referenced to insert before
 *  \param[in] val   user-supplied value.
 *  \return node object referencing place in list, or null if out of memory or
 *   misuse
 */
CARES_EXTERN ares_llist_node_t *
  ares_llist_insert_before(ares_llist_node_t *node, void *val);

/*! Insert value after specified node in the linked list
 *
 *  \param[in] node  node referenced to insert after
 *  \param[in] val   user-supplied value.
 *  \return node object referencing place in list, or null if out of memory or
 *   misuse
 */
CARES_EXTERN ares_llist_node_t *ares_llist_insert_after(ares_llist_node_t *node,
                                                        void              *val);

/*! Obtain first node in list
 *
 *  \param[in] list  Initialized list object
 *  \return first node in list or NULL if none
 */
CARES_EXTERN ares_llist_node_t *ares_llist_node_first(ares_llist_t *list);

/*! Obtain last node in list
 *
 *  \param[in] list  Initialized list object
 *  \return last node in list or NULL if none
 */
CARES_EXTERN ares_llist_node_t *ares_llist_node_last(ares_llist_t *list);

/*! Obtain a node based on its index.  This is an O(n) operation.
 *
 *  \param[in] list Initialized list object
 *  \param[in] idx  Index of node to retrieve
 *  \return node at index or NULL if invalid index
 */
CARES_EXTERN ares_llist_node_t *ares_llist_node_idx(ares_llist_t *list,
                                                    size_t        idx);

/*! Obtain next node in respect to specified node
 *
 *  \param[in] node  Node referenced
 *  \return node or NULL if none
 */
CARES_EXTERN ares_llist_node_t *ares_llist_node_next(ares_llist_node_t *node);

/*! Obtain previous node in respect to specified node
 *
 *  \param[in] node  Node referenced
 *  \return node or NULL if none
 */
CARES_EXTERN ares_llist_node_t *ares_llist_node_prev(ares_llist_node_t *node);


/*! Obtain value from node
 *
 *  \param[in] node  Node referenced
 *  \return user provided value from node
 */
CARES_EXTERN void              *ares_llist_node_val(ares_llist_node_t *node);

/*! Obtain the number of entries in the list
 *
 *  \param[in] list  Initialized list object
 *  \return count
 */
CARES_EXTERN size_t             ares_llist_len(const ares_llist_t *list);

/*! Clear all entries in the list, but don't destroy the list object.
 *
 *  \param[in] list  Initialized list object
 */
CARES_EXTERN void               ares_llist_clear(ares_llist_t *list);

/*! Obtain list object from referenced node
 *
 *  \param[in] node  Node referenced
 *  \return list object node belongs to
 */
CARES_EXTERN ares_llist_t      *ares_llist_node_parent(ares_llist_node_t *node);

/*! Obtain the first user-supplied value in the list
 *
 *  \param[in] list Initialized list object
 *  \return first user supplied value or NULL if none
 */
CARES_EXTERN void              *ares_llist_first_val(ares_llist_t *list);

/*! Obtain the last user-supplied value in the list
 *
 *  \param[in] list Initialized list object
 *  \return last user supplied value or NULL if none
 */
CARES_EXTERN void              *ares_llist_last_val(ares_llist_t *list);

/*! Take ownership of user-supplied value in list without calling destructor.
 *  Will unchain entry from list.
 *
 *  \param[in] node Node referenced
 *  \return user supplied value
 */
CARES_EXTERN void              *ares_llist_node_claim(ares_llist_node_t *node);

/*! Replace user-supplied value for node
 *
 *  \param[in] node Node referenced
 *  \param[in] val  new user-supplied value
 */
CARES_EXTERN void ares_llist_node_replace(ares_llist_node_t *node, void *val);

/*! Destroy the node, removing it from the list and calling destructor.
 *
 *  \param[in] node  Node referenced
 */
CARES_EXTERN void ares_llist_node_destroy(ares_llist_node_t *node);

/*! Destroy the list object and all nodes in the list.
 *
 *  \param[in] list Initialized list object
 */
CARES_EXTERN void ares_llist_destroy(ares_llist_t *list);

/*! Detach node from the current list and re-attach it to the new list as the
 *  last entry.
 *
 * \param[in] node       node to move
 * \param[in] new_parent new list
 */
CARES_EXTERN void ares_llist_node_mvparent_last(ares_llist_node_t *node,
                                                ares_llist_t      *new_parent);

/*! Detach node from the current list and re-attach it to the new list as the
 *  first entry.
 *
 * \param[in] node       node to move
 * \param[in] new_parent new list
 */
CARES_EXTERN void ares_llist_node_mvparent_first(ares_llist_node_t *node,
                                                 ares_llist_t      *new_parent);
/*! @} */

#endif /* __ARES__LLIST_H */