src/cx/list.h

changeset 489
af6be1e123aa
parent 488
9138acaa494b
child 490
e66551b47466
equal deleted inserted replaced
488:9138acaa494b 489:af6be1e123aa
45 #endif 45 #endif
46 46
47 /** 47 /**
48 * A comparator function comparing two list elements. 48 * A comparator function comparing two list elements.
49 */ 49 */
50 typedef int(*CxListComparator)(void const *left, void const *right); 50 typedef int(*CxListComparator)(
51 void const *left,
52 void const *right
53 );
51 54
52 /** 55 /**
53 * Internal type for the list structure - use CxList instead. 56 * Internal type for the list structure - use CxList instead.
54 */ 57 */
55 typedef struct cx_list_s cx_list_s; 58 typedef struct cx_list_s cx_list_s;
59 */ 62 */
60 typedef struct { 63 typedef struct {
61 /** 64 /**
62 * Member function for adding an element. 65 * Member function for adding an element.
63 */ 66 */
64 int (*add)(cx_list_s *list, void *elem); 67 int (*add)(
68 cx_list_s *list,
69 void const *elem
70 );
65 71
66 /** 72 /**
67 * Member function for inserting an element. 73 * Member function for inserting an element.
68 */ 74 */
69 int (*insert)(cx_list_s *list, size_t index, void *elem); 75 int (*insert)(
76 cx_list_s *list,
77 size_t index,
78 void const *elem
79 );
70 80
71 /** 81 /**
72 * Member function for removing an element. 82 * Member function for removing an element.
73 */ 83 */
74 int (*remove)(cx_list_s *list, size_t index); 84 int (*remove)(
85 cx_list_s *list,
86 size_t index
87 );
75 88
76 /** 89 /**
77 * Member function for element lookup. 90 * Member function for element lookup.
78 */ 91 */
79 void *(*at)(cx_list_s *list, size_t index); 92 void *(*at)(
93 cx_list_s const *list,
94 size_t index
95 );
80 96
81 /** 97 /**
82 * Member function for finding an element. 98 * Member function for finding an element.
83 */ 99 */
84 size_t (*find)( 100 size_t (*find)(
85 cx_list_s *list, 101 cx_list_s const *list,
86 void *elem 102 void const *elem
87 ); 103 );
88 104
89 /** 105 /**
90 * Member function for sorting the list in place. 106 * Member function for sorting the list in place.
91 */ 107 */
93 109
94 /** 110 /**
95 * Member function for comparing this list to another list of the same type. 111 * Member function for comparing this list to another list of the same type.
96 */ 112 */
97 int (*compare)( 113 int (*compare)(
98 cx_list_s *list, 114 cx_list_s const *list,
99 cx_list_s *other 115 cx_list_s const *other
100 ); 116 );
101 } cx_list_class; 117 } cx_list_class;
102 118
103 /** 119 /**
104 * Structure for holding the base data of a list. 120 * Structure for holding the base data of a list.
145 * 161 *
146 * @param list the list 162 * @param list the list
147 * @param elem a pointer to the element to add 163 * @param elem a pointer to the element to add
148 * @return zero on success, non-zero on memory allocation failure 164 * @return zero on success, non-zero on memory allocation failure
149 */ 165 */
150 static inline int cxListAdd(CxList list, void *elem) { 166 static inline int cxListAdd(
167 CxList list,
168 void const *elem
169 ) {
151 return list->cl->add(list, elem); 170 return list->cl->add(list, elem);
152 } 171 }
153 172
154 /** 173 /**
155 * Inserts an item at the specified index. 174 * Inserts an item at the specified index.
165 * @param index the index the element shall have 184 * @param index the index the element shall have
166 * @param elem a pointer to the element to add 185 * @param elem a pointer to the element to add
167 * @return zero on success, non-zero on memory allocation failure 186 * @return zero on success, non-zero on memory allocation failure
168 * or when the index is out of bounds 187 * or when the index is out of bounds
169 */ 188 */
170 static inline int cxListInsert(CxList list, size_t index, void *elem) { 189 static inline int cxListInsert(
190 CxList list,
191 size_t index,
192 void const *elem
193 ) {
171 return list->cl->insert(list, index, elem); 194 return list->cl->insert(list, index, elem);
172 } 195 }
173 196
174 /** 197 /**
175 * Removes the element at the specified index. 198 * Removes the element at the specified index.
176 * @param list the list 199 * @param list the list
177 * @param index the index of the element 200 * @param index the index of the element
178 * @return zero on success, non-zero if the index is out of bounds 201 * @return zero on success, non-zero if the index is out of bounds
179 */ 202 */
180 static inline int cxListRemove(CxList list, size_t index) { 203 static inline int cxListRemove(
204 CxList list,
205 size_t index
206 ) {
181 return list->cl->remove(list, index); 207 return list->cl->remove(list, index);
182 } 208 }
183 209
184 /** 210 /**
185 * Returns a pointer to the element at the specified index. 211 * Returns a pointer to the element at the specified index.
186 * 212 *
187 * @param list the list 213 * @param list the list
188 * @param index the index of the element 214 * @param index the index of the element
189 * @return a pointer to the element or \c NULL if the index is out of bounds 215 * @return a pointer to the element or \c NULL if the index is out of bounds
190 */ 216 */
191 static inline void *cxListAt(CxList list, size_t index) { 217 static inline void *cxListAt(
218 CxList list,
219 size_t index
220 ) {
192 return list->cl->at(list, index); 221 return list->cl->at(list, index);
193 } 222 }
194 223
195 /** 224 /**
196 * Returns the index of the first element that equals \p elem. 225 * Returns the index of the first element that equals \p elem.
199 * 228 *
200 * @param list the list 229 * @param list the list
201 * @param elem the element to find 230 * @param elem the element to find
202 * @return the index of the element or \c (size+1) if the element is not found 231 * @return the index of the element or \c (size+1) if the element is not found
203 */ 232 */
204 static inline size_t cxListFind(CxList list, void *elem) { 233 static inline size_t cxListFind(
234 CxList list,
235 void const *elem
236 ) {
205 return list->cl->find(list, elem); 237 return list->cl->find(list, elem);
206 } 238 }
207 239
208 /** 240 /**
209 * Sorts the list in place. 241 * Sorts the list in place.

mercurial