Wed, 08 Feb 2023 20:26:09 +0100
implement swap function for list elements - fixes #218
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 "common.h"
41 #include "allocator.h"
42 #include "iterator.h"
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
48 /**
49 * A comparator function comparing two list elements.
50 */
51 typedef int(*CxListComparator)(
52 void const *left,
53 void const *right
54 );
56 /**
57 * List class type.
58 */
59 typedef struct cx_list_class_s cx_list_class;
61 /**
62 * Structure for holding the base data of a list.
63 */
64 struct cx_list_s {
65 /**
66 * The list class definition.
67 */
68 cx_list_class const *cl;
69 /**
70 * The actual implementation in case the list class is delegating.
71 */
72 cx_list_class const *climpl;
73 /**
74 * The allocator to use.
75 */
76 CxAllocator const *allocator;
77 /**
78 * The comparator function for the elements.
79 */
80 CxListComparator cmpfunc;
81 /**
82 * The size of each element (payload only).
83 */
84 size_t itemsize;
85 /**
86 * The size of the list (number of currently stored elements).
87 */
88 size_t size;
89 /**
90 * The capacity of the list (maximum number of elements).
91 */
92 size_t capacity;
93 union {
94 /**
95 * An optional simple destructor for the list contents that admits the free() interface.
96 *
97 * @remark Set content_destructor_type to #CX_DESTRUCTOR_SIMPLE.
98 *
99 * @attention Read the documentation of the particular list implementation
100 * whether this destructor shall only destroy the contents or also free the memory.
101 */
102 cx_destructor_func simple_destructor;
103 /**
104 * An optional advanced destructor for the list contents providing additional data.
105 *
106 * @remark Set content_destructor_type to #CX_DESTRUCTOR_ADVANCED.
107 *
108 * @attention Read the documentation of the particular list implementation
109 * whether this destructor shall only destroy the contents or also free the memory.
110 */
111 cx_advanced_destructor advanced_destructor;
112 };
113 /**
114 * The type of destructor to use.
115 */
116 enum cx_destructor_type content_destructor_type;
117 };
119 /**
120 * The class definition for arbitrary lists.
121 */
122 struct cx_list_class_s {
123 /**
124 * Destructor function.
125 */
126 void (*destructor)(struct cx_list_s *list);
128 /**
129 * Member function for inserting a single elements.
130 * Implementors SHOULD see to performant implementations for corner cases.
131 */
132 int (*insert_element)(
133 struct cx_list_s *list,
134 size_t index,
135 void const *data
136 );
138 /**
139 * Member function for inserting multiple elements.
140 * Implementors SHOULD see to performant implementations for corner cases.
141 */
142 size_t (*insert_array)(
143 struct cx_list_s *list,
144 size_t index,
145 void const *data,
146 size_t n
147 );
149 /**
150 * Member function for inserting an element relative to an iterator position.
151 */
152 int (*insert_iter)(
153 struct cx_mut_iterator_s *iter,
154 void const *elem,
155 int prepend
156 );
158 /**
159 * Member function for removing an element.
160 */
161 int (*remove)(
162 struct cx_list_s *list,
163 size_t index
164 );
166 /**
167 * Member function for swapping two elements.
168 */
169 int (*swap)(
170 struct cx_list_s *list,
171 size_t i,
172 size_t j
173 );
175 /**
176 * Member function for element lookup.
177 */
178 void *(*at)(
179 struct cx_list_s const *list,
180 size_t index
181 );
183 /**
184 * Member function for finding an element.
185 */
186 size_t (*find)(
187 struct cx_list_s const *list,
188 void const *elem
189 );
191 /**
192 * Member function for sorting the list in place.
193 */
194 void (*sort)(struct cx_list_s *list);
196 /**
197 * Member function for comparing this list to another list of the same type.
198 */
199 int (*compare)(
200 struct cx_list_s const *list,
201 struct cx_list_s const *other
202 );
204 /**
205 * Member function for reversing the order of the items.
206 */
207 void (*reverse)(struct cx_list_s *list);
209 /**
210 * Member function for returning an iterator pointing to the specified index.
211 */
212 struct cx_iterator_s (*iterator)(
213 struct cx_list_s const *list,
214 size_t index
215 );
216 };
218 /**
219 * Common type for all list implementations.
220 */
221 typedef struct cx_list_s CxList;
223 /**
224 * Advises the list to store copies of the objects (default mode of operation).
225 *
226 * Retrieving objects from this list will yield pointers to the copies stored
227 * within this list.
228 *
229 * @param list the list
230 * @see cxListStorePointers()
231 */
232 __attribute__((__nonnull__))
233 void cxListStoreObjects(CxList *list);
235 /**
236 * Advises the list to only store pointers to the objects.
237 *
238 * Retrieving objects from this list will yield the original pointers stored.
239 *
240 * @note This function forcibly sets the element size to the size of a pointer.
241 * Invoking this function on a non-empty list that already stores copies of
242 * objects is undefined.
243 *
244 * @param list the list
245 * @see cxListStoreObjects()
246 */
247 __attribute__((__nonnull__))
248 void cxListStorePointers(CxList *list);
250 /**
251 * Returns true, if this list is storing pointers instead of the actual data.
252 *
253 * @param list
254 * @return
255 * @see cxListStorePointers()
256 */
257 __attribute__((__nonnull__))
258 bool cxListIsStoringPointers(CxList *list);
260 /**
261 * Adds an item to the end of the list.
262 *
263 * @param list the list
264 * @param elem a pointer to the element to add
265 * @return zero on success, non-zero on memory allocation failure
266 * @see cxListAddArray()
267 */
268 __attribute__((__nonnull__))
269 static inline int cxListAdd(
270 CxList *list,
271 void const *elem
272 ) {
273 return list->cl->insert_element(list, list->size, elem);
274 }
276 /**
277 * Adds multiple items to the end of the list.
278 *
279 * This method is more efficient than invoking cxListAdd() multiple times.
280 *
281 * If there is not enough memory to add all elements, the returned value is
282 * less than \p n.
283 *
284 * If this list is storing pointers instead of objects \p array is expected to
285 * be an array of pointers.
286 *
287 * @param list the list
288 * @param array a pointer to the elements to add
289 * @param n the number of elements to add
290 * @return the number of added elements
291 */
292 __attribute__((__nonnull__))
293 static inline size_t cxListAddArray(
294 CxList *list,
295 void const *array,
296 size_t n
297 ) {
298 return list->cl->insert_array(list, list->size, array, n);
299 }
301 /**
302 * Inserts an item at the specified index.
303 *
304 * If \p index equals the list \c size, this is effectively cxListAdd().
305 *
306 * @param list the list
307 * @param index the index the element shall have
308 * @param elem a pointer to the element to add
309 * @return zero on success, non-zero on memory allocation failure
310 * or when the index is out of bounds
311 * @see cxListInsertAfter()
312 * @see cxListInsertBefore()
313 */
314 __attribute__((__nonnull__))
315 static inline int cxListInsert(
316 CxList *list,
317 size_t index,
318 void const *elem
319 ) {
320 return list->cl->insert_element(list, index, elem);
321 }
323 /**
324 * Inserts multiple items to the list at the specified index.
325 * If \p index equals the list size, this is effectively cxListAddArray().
326 *
327 * This method is usually more efficient than invoking cxListInsert()
328 * multiple times.
329 *
330 * If there is not enough memory to add all elements, the returned value is
331 * less than \p n.
332 *
333 * If this list is storing pointers instead of objects \p array is expected to
334 * be an array of pointers.
335 *
336 * @param list the list
337 * @param index the index where to add the elements
338 * @param array a pointer to the elements to add
339 * @param n the number of elements to add
340 * @return the number of added elements
341 */
342 __attribute__((__nonnull__))
343 static inline size_t cxListInsertArray(
344 CxList *list,
345 size_t index,
346 void const *array,
347 size_t n
348 ) {
349 return list->cl->insert_array(list, index, array, n);
350 }
352 /**
353 * Inserts an element after the current location of the specified iterator.
354 *
355 * The used iterator remains operational, but all other active iterators should
356 * be considered invalidated.
357 *
358 * If \p iter is not a list iterator, the behavior is undefined.
359 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
360 *
361 * @param iter an iterator
362 * @param elem the element to insert
363 * @return zero on success, non-zero on memory allocation failure
364 * @see cxListInsert()
365 * @see cxListInsertBefore()
366 */
367 __attribute__((__nonnull__))
368 static inline int cxListInsertAfter(
369 CxMutIterator *iter,
370 void const *elem
371 ) {
372 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
373 }
375 /**
376 * Inserts an element before the current location of the specified iterator.
377 *
378 * The used iterator remains operational, but all other active iterators should
379 * be considered invalidated.
380 *
381 * If \p iter is not a list iterator, the behavior is undefined.
382 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
383 *
384 * @param iter an iterator
385 * @param elem the element to insert
386 * @return zero on success, non-zero on memory allocation failure
387 * @see cxListInsert()
388 * @see cxListInsertAfter()
389 */
390 __attribute__((__nonnull__))
391 static inline int cxListInsertBefore(
392 CxMutIterator *iter,
393 void const *elem
394 ) {
395 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
396 }
398 /**
399 * Removes the element at the specified index.
400 * @param list the list
401 * @param index the index of the element
402 * @return zero on success, non-zero if the index is out of bounds
403 */
404 __attribute__((__nonnull__))
405 static inline int cxListRemove(
406 CxList *list,
407 size_t index
408 ) {
409 return list->cl->remove(list, index);
410 }
412 /**
413 * Swaps two items in the list.
414 *
415 * Implementations should only allocate temporary memory for the swap, if
416 * it is necessary.
417 *
418 * @param list the list
419 * @param i the index of the first element
420 * @param j the index of the second element
421 * @return zero on success, non-zero if one of the indices is out of bounds
422 */
423 __attribute__((__nonnull__))
424 static inline int cxListSwap(
425 CxList *list,
426 size_t i,
427 size_t j
428 ) {
429 return list->cl->swap(list, i, j);
430 }
432 /**
433 * Returns a pointer to the element at the specified index.
434 *
435 * @param list the list
436 * @param index the index of the element
437 * @return a pointer to the element or \c NULL if the index is out of bounds
438 */
439 __attribute__((__nonnull__))
440 static inline void *cxListAt(
441 CxList *list,
442 size_t index
443 ) {
444 return list->cl->at(list, index);
445 }
447 /**
448 * Returns an iterator pointing to the item at the specified index.
449 *
450 * The returned iterator is position-aware.
451 *
452 * If the index is out of range, a past-the-end iterator will be returned.
453 *
454 * @param list the list
455 * @param index the index where the iterator shall point at
456 * @return a new iterator
457 */
458 __attribute__((__nonnull__, __warn_unused_result__))
459 static inline CxIterator cxListIterator(
460 CxList const *list,
461 size_t index
462 ) {
463 return list->cl->iterator(list, index);
464 }
466 /**
467 * Returns a mutating iterator pointing to the item at the specified index.
468 *
469 * The returned iterator is position-aware.
470 *
471 * If the index is out of range, a past-the-end iterator will be returned.
472 *
473 * @param list the list
474 * @param index the index where the iterator shall point at
475 * @return a new iterator
476 */
477 __attribute__((__nonnull__, __warn_unused_result__))
478 CxMutIterator cxListMutIterator(
479 CxList *list,
480 size_t index
481 );
483 /**
484 * Returns an iterator pointing to the first item of the list.
485 *
486 * The returned iterator is position-aware.
487 *
488 * If the list is empty, a past-the-end iterator will be returned.
489 *
490 * @param list the list
491 * @return a new iterator
492 */
493 __attribute__((__nonnull__, __warn_unused_result__))
494 static inline CxIterator cxListBegin(CxList const *list) {
495 return list->cl->iterator(list, 0);
496 }
498 /**
499 * Returns a mutating iterator pointing to the first item of the list.
500 *
501 * The returned iterator is position-aware.
502 *
503 * If the list is empty, a past-the-end iterator will be returned.
504 *
505 * @param list the list
506 * @return a new iterator
507 */
508 __attribute__((__nonnull__, __warn_unused_result__))
509 static inline CxMutIterator cxListBeginMut(CxList *list) {
510 return cxListMutIterator(list, 0);
511 }
513 /**
514 * Returns the index of the first element that equals \p elem.
515 *
516 * Determining equality is performed by the list's comparator function.
517 *
518 * @param list the list
519 * @param elem the element to find
520 * @return the index of the element or \c (size+1) if the element is not found
521 */
522 __attribute__((__nonnull__))
523 static inline size_t cxListFind(
524 CxList const *list,
525 void const *elem
526 ) {
527 return list->cl->find(list, elem);
528 }
530 /**
531 * Sorts the list in place.
532 *
533 * \remark The underlying sort algorithm is implementation defined.
534 *
535 * @param list the list
536 */
537 __attribute__((__nonnull__))
538 static inline void cxListSort(CxList *list) {
539 list->cl->sort(list);
540 }
542 /**
543 * Reverses the order of the items.
544 *
545 * @param list the list
546 */
547 __attribute__((__nonnull__))
548 static inline void cxListReverse(CxList *list) {
549 list->cl->reverse(list);
550 }
552 /**
553 * Compares a list to another list of the same type.
554 *
555 * First, the list sizes are compared.
556 * If they match, the lists are compared element-wise.
557 *
558 * @param list the list
559 * @param other the list to compare to
560 * @return zero, if both lists are equal element wise,
561 * negative if the first list is smaller, positive if the first list is larger
562 */
563 __attribute__((__nonnull__))
564 int cxListCompare(
565 CxList const *list,
566 CxList const *other
567 );
569 /**
570 * Deallocates the memory of the specified list structure.
571 *
572 * Also calls content a destructor function, depending on the configuration
573 * in CxList.content_destructor_type.
574 *
575 * This function itself is a destructor function for the CxList.
576 *
577 * @param list the list which shall be destroyed
578 */
579 __attribute__((__nonnull__))
580 void cxListDestroy(CxList *list);
582 #ifdef __cplusplus
583 } // extern "C"
584 #endif
586 #endif // UCX_LIST_H