2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
30 * \brief Interface for list implementations.
32 * \author Olaf Wintermann
34 * \copyright 2-Clause BSD License
41 #include "allocator.h"
48 #ifndef CX_STORE_POINTERS
50 * Special constant used for creating collections that are storing pointers.
52 #define CX_STORE_POINTERS 0
56 * A comparator function comparing two list elements.
58 typedef int(*CxListComparator)(
66 typedef struct cx_list_class_s cx_list_class;
69 * Structure for holding the base data of a list.
73 * The list class definition.
75 cx_list_class const *cl;
77 * The actual implementation in case the list class is delegating.
79 cx_list_class const *climpl;
81 * The allocator to use.
83 CxAllocator const *allocator;
85 * The comparator function for the elements.
87 CxListComparator cmpfunc;
89 * The size of each element (payload only).
93 * The size of the list (number of currently stored elements).
97 * The capacity of the list (maximum number of elements).
102 * An optional simple destructor for the list contents that admits the free() interface.
104 * @remark Set content_destructor_type to #CX_DESTRUCTOR_SIMPLE.
106 * @attention Read the documentation of the particular list implementation
107 * whether this destructor shall only destroy the contents or also free the memory.
109 cx_destructor_func simple_destructor;
111 * An optional advanced destructor for the list contents providing additional data.
113 * @remark Set content_destructor_type to #CX_DESTRUCTOR_ADVANCED.
115 * @attention Read the documentation of the particular list implementation
116 * whether this destructor shall only destroy the contents or also free the memory.
118 cx_advanced_destructor advanced_destructor;
121 * The type of destructor to use.
123 enum cx_destructor_type content_destructor_type;
127 * The class definition for arbitrary lists.
129 struct cx_list_class_s {
131 * Destructor function.
133 void (*destructor)(struct cx_list_s *list);
136 * Member function for inserting a single elements.
137 * Implementors SHOULD see to performant implementations for corner cases.
139 int (*insert_element)(
140 struct cx_list_s *list,
146 * Member function for inserting multiple elements.
147 * Implementors SHOULD see to performant implementations for corner cases.
149 size_t (*insert_array)(
150 struct cx_list_s *list,
157 * Member function for inserting an element relative to an iterator position.
160 struct cx_mut_iterator_s *iter,
166 * Member function for removing an element.
169 struct cx_list_s *list,
174 * Member function for removing all elements.
176 void (*clear)(struct cx_list_s *list);
179 * Member function for swapping two elements.
182 struct cx_list_s *list,
188 * Member function for element lookup.
191 struct cx_list_s const *list,
196 * Member function for finding an element.
199 struct cx_list_s const *list,
204 * Member function for sorting the list in place.
206 void (*sort)(struct cx_list_s *list);
209 * Member function for comparing this list to another list of the same type.
212 struct cx_list_s const *list,
213 struct cx_list_s const *other
217 * Member function for reversing the order of the items.
219 void (*reverse)(struct cx_list_s *list);
222 * Member function for returning an iterator pointing to the specified index.
224 struct cx_iterator_s (*iterator)(
225 struct cx_list_s const *list,
232 * Common type for all list implementations.
234 typedef struct cx_list_s CxList;
237 * Invokes the configured destructor function for a specific element.
239 * Usually only used by list implementations. There should be no need
240 * to invoke this function manually.
242 * @param list the list
243 * @param elem the element
245 __attribute__((__nonnull__))
246 void cx_list_invoke_destructor(
247 struct cx_list_s const *list,
252 * Invokes the simple destructor function for a specific element.
254 * Usually only used by list implementations. There should be no need
255 * to invoke this function manually.
257 * @param list the list
258 * @param elem the element
260 __attribute__((__nonnull__))
261 void cx_list_invoke_simple_destructor(
262 struct cx_list_s const *list,
267 * Invokes the advanced destructor function for a specific element.
269 * Usually only used by list implementations. There should be no need
270 * to invoke this function manually.
272 * @param list the list
273 * @param elem the element
275 __attribute__((__nonnull__))
276 void cx_list_invoke_advanced_destructor(
277 struct cx_list_s const *list,
282 * Advises the list to store copies of the objects (default mode of operation).
284 * Retrieving objects from this list will yield pointers to the copies stored
287 * @param list the list
288 * @see cxListStorePointers()
290 __attribute__((__nonnull__))
291 void cxListStoreObjects(CxList *list);
294 * Advises the list to only store pointers to the objects.
296 * Retrieving objects from this list will yield the original pointers stored.
298 * @note This function forcibly sets the element size to the size of a pointer.
299 * Invoking this function on a non-empty list that already stores copies of
300 * objects is undefined.
302 * @param list the list
303 * @see cxListStoreObjects()
305 __attribute__((__nonnull__))
306 void cxListStorePointers(CxList *list);
309 * Returns true, if this list is storing pointers instead of the actual data.
313 * @see cxListStorePointers()
315 __attribute__((__nonnull__))
316 bool cxListIsStoringPointers(CxList const *list);
319 * Adds an item to the end of the list.
321 * @param list the list
322 * @param elem a pointer to the element to add
323 * @return zero on success, non-zero on memory allocation failure
324 * @see cxListAddArray()
326 __attribute__((__nonnull__))
327 static inline int cxListAdd(
331 return list->cl->insert_element(list, list->size, elem);
335 * Adds multiple items to the end of the list.
337 * This method is more efficient than invoking cxListAdd() multiple times.
339 * If there is not enough memory to add all elements, the returned value is
342 * If this list is storing pointers instead of objects \p array is expected to
343 * be an array of pointers.
345 * @param list the list
346 * @param array a pointer to the elements to add
347 * @param n the number of elements to add
348 * @return the number of added elements
350 __attribute__((__nonnull__))
351 static inline size_t cxListAddArray(
356 return list->cl->insert_array(list, list->size, array, n);
360 * Inserts an item at the specified index.
362 * If \p index equals the list \c size, this is effectively cxListAdd().
364 * @param list the list
365 * @param index the index the element shall have
366 * @param elem a pointer to the element to add
367 * @return zero on success, non-zero on memory allocation failure
368 * or when the index is out of bounds
369 * @see cxListInsertAfter()
370 * @see cxListInsertBefore()
372 __attribute__((__nonnull__))
373 static inline int cxListInsert(
378 return list->cl->insert_element(list, index, elem);
382 * Inserts multiple items to the list at the specified index.
383 * If \p index equals the list size, this is effectively cxListAddArray().
385 * This method is usually more efficient than invoking cxListInsert()
388 * If there is not enough memory to add all elements, the returned value is
391 * If this list is storing pointers instead of objects \p array is expected to
392 * be an array of pointers.
394 * @param list the list
395 * @param index the index where to add the elements
396 * @param array a pointer to the elements to add
397 * @param n the number of elements to add
398 * @return the number of added elements
400 __attribute__((__nonnull__))
401 static inline size_t cxListInsertArray(
407 return list->cl->insert_array(list, index, array, n);
411 * Inserts an element after the current location of the specified iterator.
413 * The used iterator remains operational, but all other active iterators should
414 * be considered invalidated.
416 * If \p iter is not a list iterator, the behavior is undefined.
417 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
419 * @param iter an iterator
420 * @param elem the element to insert
421 * @return zero on success, non-zero on memory allocation failure
422 * @see cxListInsert()
423 * @see cxListInsertBefore()
425 __attribute__((__nonnull__))
426 static inline int cxListInsertAfter(
430 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
434 * Inserts an element before the current location of the specified iterator.
436 * The used iterator remains operational, but all other active iterators should
437 * be considered invalidated.
439 * If \p iter is not a list iterator, the behavior is undefined.
440 * If \p iter is a past-the-end iterator, the new element gets appended to the list.
442 * @param iter an iterator
443 * @param elem the element to insert
444 * @return zero on success, non-zero on memory allocation failure
445 * @see cxListInsert()
446 * @see cxListInsertAfter()
448 __attribute__((__nonnull__))
449 static inline int cxListInsertBefore(
453 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
457 * Removes the element at the specified index.
459 * If an element destructor function is specified, it is called before
460 * removing the element.
462 * @param list the list
463 * @param index the index of the element
464 * @return zero on success, non-zero if the index is out of bounds
466 __attribute__((__nonnull__))
467 static inline int cxListRemove(
471 return list->cl->remove(list, index);
475 * Removes all elements from this list.
477 * If an element destructor function is specified, it is called for each
478 * element before removing them.
480 * @param list the list
482 __attribute__((__nonnull__))
483 static inline void cxListClear(CxList *list) {
484 list->cl->clear(list);
488 * Swaps two items in the list.
490 * Implementations should only allocate temporary memory for the swap, if
493 * @param list the list
494 * @param i the index of the first element
495 * @param j the index of the second element
496 * @return zero on success, non-zero if one of the indices is out of bounds
498 __attribute__((__nonnull__))
499 static inline int cxListSwap(
504 return list->cl->swap(list, i, j);
508 * Returns a pointer to the element at the specified index.
510 * @param list the list
511 * @param index the index of the element
512 * @return a pointer to the element or \c NULL if the index is out of bounds
514 __attribute__((__nonnull__))
515 static inline void *cxListAt(
519 return list->cl->at(list, index);
523 * Returns an iterator pointing to the item at the specified index.
525 * The returned iterator is position-aware.
527 * If the index is out of range, a past-the-end iterator will be returned.
529 * @param list the list
530 * @param index the index where the iterator shall point at
531 * @return a new iterator
533 __attribute__((__nonnull__, __warn_unused_result__))
534 static inline CxIterator cxListIteratorAt(
538 return list->cl->iterator(list, index, false);
542 * Returns a backwards iterator pointing to the item at the specified index.
544 * The returned iterator is position-aware.
546 * If the index is out of range, a past-the-end iterator will be returned.
548 * @param list the list
549 * @param index the index where the iterator shall point at
550 * @return a new iterator
552 __attribute__((__nonnull__, __warn_unused_result__))
553 static inline CxIterator cxListBackwardsIteratorAt(
557 return list->cl->iterator(list, index, true);
561 * Returns a mutating iterator pointing to the item at the specified index.
563 * The returned iterator is position-aware.
565 * If the index is out of range, a past-the-end iterator will be returned.
567 * @param list the list
568 * @param index the index where the iterator shall point at
569 * @return a new iterator
571 __attribute__((__nonnull__, __warn_unused_result__))
572 CxMutIterator cxListMutIteratorAt(
578 * Returns a mutating backwards iterator pointing to the item at the
581 * The returned iterator is position-aware.
583 * If the index is out of range, a past-the-end iterator will be returned.
585 * @param list the list
586 * @param index the index where the iterator shall point at
587 * @return a new iterator
589 __attribute__((__nonnull__, __warn_unused_result__))
590 CxMutIterator cxListMutBackwardsIteratorAt(
596 * Returns an iterator pointing to the first item of the list.
598 * The returned iterator is position-aware.
600 * If the list is empty, a past-the-end iterator will be returned.
602 * @param list the list
603 * @return a new iterator
605 __attribute__((__nonnull__, __warn_unused_result__))
606 static inline CxIterator cxListIterator(CxList const *list) {
607 return list->cl->iterator(list, 0, false);
611 * Returns a mutating iterator pointing to the first item of the list.
613 * The returned iterator is position-aware.
615 * If the list is empty, a past-the-end iterator will be returned.
617 * @param list the list
618 * @return a new iterator
620 __attribute__((__nonnull__, __warn_unused_result__))
621 static inline CxMutIterator cxListMutIterator(CxList *list) {
622 return cxListMutIteratorAt(list, 0);
627 * Returns a backwards iterator pointing to the last item of the list.
629 * The returned iterator is position-aware.
631 * If the list is empty, a past-the-end iterator will be returned.
633 * @param list the list
634 * @return a new iterator
636 __attribute__((__nonnull__, __warn_unused_result__))
637 static inline CxIterator cxListBackwardsIterator(CxList const *list) {
638 return list->cl->iterator(list, list->size - 1, true);
642 * Returns a mutating backwards iterator pointing to the last item of the list.
644 * The returned iterator is position-aware.
646 * If the list is empty, a past-the-end iterator will be returned.
648 * @param list the list
649 * @return a new iterator
651 __attribute__((__nonnull__, __warn_unused_result__))
652 static inline CxMutIterator cxListMutBackwardsIterator(CxList *list) {
653 return cxListMutBackwardsIteratorAt(list, list->size - 1);
657 * Returns the index of the first element that equals \p elem.
659 * Determining equality is performed by the list's comparator function.
661 * @param list the list
662 * @param elem the element to find
663 * @return the index of the element or \c (size+1) if the element is not found
665 __attribute__((__nonnull__))
666 static inline size_t cxListFind(
670 return list->cl->find(list, elem);
674 * Sorts the list in place.
676 * \remark The underlying sort algorithm is implementation defined.
678 * @param list the list
680 __attribute__((__nonnull__))
681 static inline void cxListSort(CxList *list) {
682 list->cl->sort(list);
686 * Reverses the order of the items.
688 * @param list the list
690 __attribute__((__nonnull__))
691 static inline void cxListReverse(CxList *list) {
692 list->cl->reverse(list);
696 * Compares a list to another list of the same type.
698 * First, the list sizes are compared.
699 * If they match, the lists are compared element-wise.
701 * @param list the list
702 * @param other the list to compare to
703 * @return zero, if both lists are equal element wise,
704 * negative if the first list is smaller, positive if the first list is larger
706 __attribute__((__nonnull__))
713 * Deallocates the memory of the specified list structure.
715 * Also calls content a destructor function, depending on the configuration
716 * in CxList.content_destructor_type.
718 * This function itself is a destructor function for the CxList.
720 * @param list the list which shall be destroyed
722 __attribute__((__nonnull__))
723 void cxListDestroy(CxList *list);