src/cx/list.h

Fri, 08 Oct 2021 19:47:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 08 Oct 2021 19:47:31 +0200
changeset 473
1bd4b8c28722
parent 469
0458bff0b1cd
child 474
9c1fccda16bc
permissions
-rw-r--r--

add cx_linked_list_{prev, remove, reverse}

changes assertions for some low level methods (loc_next is now always mandatory)

390
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
453
bb144d08cd44 add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents: 439
diff changeset
28 /**
bb144d08cd44 add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents: 439
diff changeset
29 * \file list.h
bb144d08cd44 add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents: 439
diff changeset
30 * \brief Interface for list implementations.
bb144d08cd44 add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents: 439
diff changeset
31 * \author Mike Becker
bb144d08cd44 add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents: 439
diff changeset
32 * \author Olaf Wintermann
bb144d08cd44 add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents: 439
diff changeset
33 * \version 3.0
bb144d08cd44 add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents: 439
diff changeset
34 * \copyright 2-Clause BSD License
bb144d08cd44 add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents: 439
diff changeset
35 */
390
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 #ifndef UCX_LIST_H
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 #define UCX_LIST_H
d345541018fa starts ucx 3.0 development
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
40 #include <stdlib.h>
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
41 #include "allocator.h"
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
42
415
067aa769796a fix missing extern "C"
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
43 #ifdef __cplusplus
067aa769796a fix missing extern "C"
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
44 extern "C" {
067aa769796a fix missing extern "C"
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
45 #endif
067aa769796a fix missing extern "C"
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
46
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
47 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
48 * A comparator function comparing two list elements.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
49 */
412
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 406
diff changeset
50 typedef int(*CxListComparator)(void const *left, void const *right);
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
51
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
52 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
53 * Internal type for the list structure - use CxList instead.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
54 */
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
55 typedef struct cx_list_s cx_list_s;
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
56
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
57 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
58 * The class definition for arbitrary lists.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
59 */
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
60 typedef struct {
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
61 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
62 * Member function for adding an element.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
63 */
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
64 int (*add)(cx_list_s *list, void *elem);
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
65
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
66 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
67 * Member function for inserting an element.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
68 */
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
69 int (*insert)(cx_list_s *list, size_t index, void *elem);
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
70
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
71 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
72 * Member function for removing an element.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
73 */
438
cd3069757010 add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents: 435
diff changeset
74 int (*remove)(cx_list_s *list, size_t index);
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
75
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
76 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
77 * Member function for element lookup.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
78 */
439
9a5adedd6de6 add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents: 438
diff changeset
79 void *(*at)(cx_list_s *list, size_t index);
9a5adedd6de6 add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents: 438
diff changeset
80
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
81 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
82 * Member function for finding an element.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
83 */
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
84 size_t (*find)(cx_list_s *list, void *elem);
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
85
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
86 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
87 * Member function for retrieving the last element.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
88 */
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
89 void *(*last)(cx_list_s *list);
469
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
90
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
91 /**
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
92 * Member function for sorting the list in place.
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
93 */
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
94 void (*sort)(cx_list_s *list);
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
95 } cx_list_class;
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
96
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
97 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
98 * Structure for holding the base data of a list.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
99 */
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
100 struct cx_list_s {
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
101 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
102 * The list class definition.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
103 */
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
104 cx_list_class *cl;
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
105 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
106 * The allocator to use.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
107 */
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
108 CxAllocator allocator;
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
109 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
110 * The comparator function for the elements.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
111 */
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
112 CxListComparator cmpfunc;
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
113 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
114 * The size of each element (payload only).
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
115 */
401
e6a8f7fb0c45 copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
116 size_t itemsize;
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
117 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
118 * The size of the list (number of currently stored elements).
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
119 */
401
e6a8f7fb0c45 copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
120 size_t size;
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
121 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
122 * The capacity of the list (maximum number of elements).
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
123 */
401
e6a8f7fb0c45 copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents: 398
diff changeset
124 size_t capacity;
435
0fe204d50f54 change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents: 415
diff changeset
125 };
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
126
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
127 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
128 * Common type for all list implementations.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
129 */
412
af766caea48d removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents: 406
diff changeset
130 typedef cx_list_s *CxList;
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
131
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
132 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
133 * Adds an item to the end of the list.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
134 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
135 * \remark It is implementation defined whether
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
136 * the contents of the element are copied or the
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
137 * pointer is added to the list. In the latter case
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
138 * the \c itemsize of the list SHALL be \c sizeof(void*).
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
139 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
140 * @param list the list
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
141 * @param elem a pointer to the element to add
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
142 * @return zero on success, non-zero on memory allocation failure
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
143 */
469
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
144 static inline int cxListAdd(CxList list, void *elem) {
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
145 return list->cl->add(list, elem);
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
146 }
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
147
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
148 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
149 * Inserts an item at the specified index.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
150 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
151 * If \p index equals the list \c size, this is effectively cxListAdd().
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
152 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
153 * \remark It is implementation defined whether
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
154 * the contents of the element are copied or the
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
155 * pointer is added to the list. In the latter case
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
156 * the \c itemsize of the list SHALL be \c sizeof(void*).
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
157 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
158 * @param list the list
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
159 * @param index the index the element shall have
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
160 * @param elem a pointer to the element to add
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
161 * @return zero on success, non-zero on memory allocation failure
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
162 * or when the index is out of bounds
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
163 */
469
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
164 static inline int cxListInsert(CxList list, size_t index, void *elem) {
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
165 return list->cl->insert(list, index, elem);
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
166 }
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
167
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
168 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
169 * Removes the element at the specified index.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
170 * @param list the list
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
171 * @param index the index of the element
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
172 * @return zero on success, non-zero if the index is out of bounds
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
173 */
469
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
174 static inline int cxListRemove(CxList list, size_t index) {
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
175 return list->cl->remove(list, index);
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
176 }
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
177
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
178 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
179 * Returns a pointer to the element at the specified index.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
180 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
181 * @param list the list
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
182 * @param index the index of the element
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
183 * @return a pointer to the element or \c NULL if the index is out of bounds
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
184 */
469
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
185 static inline void *cxListAt(CxList list, size_t index) {
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
186 return list->cl->at(list, index);
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
187 }
439
9a5adedd6de6 add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents: 438
diff changeset
188
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
189 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
190 * Returns the index of the first element that equals \p elem.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
191 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
192 * Determining equality is performed by the list's comparator function.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
193 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
194 * @param list the list
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
195 * @param elem the element to find
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
196 * @return the index of the element or \c (size+1) if the element is not found
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
197 */
469
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
198 static inline size_t cxListFind(CxList list, void *elem) {
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
199 return list->cl->find(list, elem);
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
200 }
398
8d506ed6c1c0 adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents: 393
diff changeset
201
464
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
202 /**
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
203 * Returns a pointer to the last element of the list.
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
204 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
205 * This is effectively the same as cxListAt() with \c index=size-1, but
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
206 * this implementation may be more efficient depending on the list structure
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
207 * and the conrecte implementation of cxListAt().
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
208 *
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
209 * @param list the list
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
210 * @return a pointer to the last element or \c NULL if the list is empty
7fafc95968fc add documentation for list.h
Mike Becker <universe@uap-core.de>
parents: 460
diff changeset
211 */
469
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
212 static inline void *cxListLast(CxList list) {
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
213 return list->cl->last(list);
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
214 }
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
215
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
216 /**
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
217 * Sorts the list in place.
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
218 *
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
219 * \remark The underlying sort algorithm is implementation defined.
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
220 *
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
221 * @param list the list
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
222 */
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
223 static inline void cxListSort(CxList list) {
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
224 list->cl->sort(list);
0458bff0b1cd add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents: 464
diff changeset
225 }
404
86ebc3745e62 adds cxListLast
Mike Becker <universe@uap-core.de>
parents: 401
diff changeset
226
415
067aa769796a fix missing extern "C"
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
227 #ifdef __cplusplus
067aa769796a fix missing extern "C"
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
228 } /* extern "C" */
067aa769796a fix missing extern "C"
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
229 #endif
067aa769796a fix missing extern "C"
Mike Becker <universe@uap-core.de>
parents: 412
diff changeset
230
393
8c0421ccbb58 no C99 comments
Mike Becker <universe@uap-core.de>
parents: 390
diff changeset
231 #endif /* UCX_LIST_H */

mercurial