Sat, 09 Oct 2021 11:12:48 +0200
remove cxListLast (can be realized via cxListAt and index=size-1)
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * \file list.h
30 * \brief Interface for list implementations.
31 * \author Mike Becker
32 * \author Olaf Wintermann
33 * \version 3.0
34 * \copyright 2-Clause BSD License
35 */
37 #ifndef UCX_LIST_H
38 #define UCX_LIST_H
40 #include <stdlib.h>
41 #include "allocator.h"
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
47 /**
48 * A comparator function comparing two list elements.
49 */
50 typedef int(*CxListComparator)(void const *left, void const *right);
52 /**
53 * Internal type for the list structure - use CxList instead.
54 */
55 typedef struct cx_list_s cx_list_s;
57 /**
58 * The class definition for arbitrary lists.
59 */
60 typedef struct {
61 /**
62 * Member function for adding an element.
63 */
64 int (*add)(cx_list_s *list, void *elem);
66 /**
67 * Member function for inserting an element.
68 */
69 int (*insert)(cx_list_s *list, size_t index, void *elem);
71 /**
72 * Member function for removing an element.
73 */
74 int (*remove)(cx_list_s *list, size_t index);
76 /**
77 * Member function for element lookup.
78 */
79 void *(*at)(cx_list_s *list, size_t index);
81 /**
82 * Member function for finding an element.
83 */
84 size_t (*find)(cx_list_s *list, void *elem);
86 /**
87 * Member function for sorting the list in place.
88 */
89 void (*sort)(cx_list_s *list);
90 } cx_list_class;
92 /**
93 * Structure for holding the base data of a list.
94 */
95 struct cx_list_s {
96 /**
97 * The list class definition.
98 */
99 cx_list_class *cl;
100 /**
101 * The allocator to use.
102 */
103 CxAllocator allocator;
104 /**
105 * The comparator function for the elements.
106 */
107 CxListComparator cmpfunc;
108 /**
109 * The size of each element (payload only).
110 */
111 size_t itemsize;
112 /**
113 * The size of the list (number of currently stored elements).
114 */
115 size_t size;
116 /**
117 * The capacity of the list (maximum number of elements).
118 */
119 size_t capacity;
120 };
122 /**
123 * Common type for all list implementations.
124 */
125 typedef cx_list_s *CxList;
127 /**
128 * Adds an item to the end of the list.
129 *
130 * \remark It is implementation defined whether
131 * the contents of the element are copied or the
132 * pointer is added to the list. In the latter case
133 * the \c itemsize of the list SHALL be \c sizeof(void*).
134 *
135 * @param list the list
136 * @param elem a pointer to the element to add
137 * @return zero on success, non-zero on memory allocation failure
138 */
139 static inline int cxListAdd(CxList list, void *elem) {
140 return list->cl->add(list, elem);
141 }
143 /**
144 * Inserts an item at the specified index.
145 *
146 * If \p index equals the list \c size, this is effectively cxListAdd().
147 *
148 * \remark It is implementation defined whether
149 * the contents of the element are copied or the
150 * pointer is added to the list. In the latter case
151 * the \c itemsize of the list SHALL be \c sizeof(void*).
152 *
153 * @param list the list
154 * @param index the index the element shall have
155 * @param elem a pointer to the element to add
156 * @return zero on success, non-zero on memory allocation failure
157 * or when the index is out of bounds
158 */
159 static inline int cxListInsert(CxList list, size_t index, void *elem) {
160 return list->cl->insert(list, index, elem);
161 }
163 /**
164 * Removes the element at the specified index.
165 * @param list the list
166 * @param index the index of the element
167 * @return zero on success, non-zero if the index is out of bounds
168 */
169 static inline int cxListRemove(CxList list, size_t index) {
170 return list->cl->remove(list, index);
171 }
173 /**
174 * Returns a pointer to the element at the specified index.
175 *
176 * @param list the list
177 * @param index the index of the element
178 * @return a pointer to the element or \c NULL if the index is out of bounds
179 */
180 static inline void *cxListAt(CxList list, size_t index) {
181 return list->cl->at(list, index);
182 }
184 /**
185 * Returns the index of the first element that equals \p elem.
186 *
187 * Determining equality is performed by the list's comparator function.
188 *
189 * @param list the list
190 * @param elem the element to find
191 * @return the index of the element or \c (size+1) if the element is not found
192 */
193 static inline size_t cxListFind(CxList list, void *elem) {
194 return list->cl->find(list, elem);
195 }
197 /**
198 * Sorts the list in place.
199 *
200 * \remark The underlying sort algorithm is implementation defined.
201 *
202 * @param list the list
203 */
204 static inline void cxListSort(CxList list) {
205 list->cl->sort(list);
206 }
208 #ifdef __cplusplus
209 } /* extern "C" */
210 #endif
212 #endif /* UCX_LIST_H */