src/cx/list.h

changeset 500
eb9e7bd40a8e
parent 499
3dc9075df822
child 503
a89857072ace
equal deleted inserted replaced
499:3dc9075df822 500:eb9e7bd40a8e
52 void const *left, 52 void const *left,
53 void const *right 53 void const *right
54 ); 54 );
55 55
56 /** 56 /**
57 * Internal type for the list structure - use CxList instead. 57 * List class type.
58 */ 58 */
59 typedef struct cx_list_s cx_list_s; 59 typedef struct cx_list_class_s cx_list_class;
60
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 *cl;
69 /**
70 * The allocator to use.
71 */
72 CxAllocator *allocator;
73 /**
74 * The comparator function for the elements.
75 */
76 CxListComparator cmpfunc;
77 /**
78 * The size of each element (payload only).
79 */
80 size_t itemsize;
81 /**
82 * The size of the list (number of currently stored elements).
83 */
84 size_t size;
85 /**
86 * The capacity of the list (maximum number of elements).
87 */
88 size_t capacity;
89 };
60 90
61 /** 91 /**
62 * The class definition for arbitrary lists. 92 * The class definition for arbitrary lists.
63 */ 93 */
64 typedef struct { 94 struct cx_list_class_s {
65 /** 95 /**
66 * Member function for adding an element. 96 * Member function for adding an element.
67 */ 97 */
68 int (*add)( 98 int (*add)(
69 cx_list_s *list, 99 struct cx_list_s *list,
70 void const *elem 100 void const *elem
71 ); 101 );
72 102
73 /** 103 /**
74 * Member function for inserting an element. 104 * Member function for inserting an element.
75 */ 105 */
76 int (*insert)( 106 int (*insert)(
77 cx_list_s *list, 107 struct cx_list_s *list,
78 size_t index, 108 size_t index,
79 void const *elem 109 void const *elem
80 ); 110 );
81 111
82 /** 112 /**
83 * Member function for inserting an element relative to an iterator position. 113 * Member function for inserting an element relative to an iterator position.
84 */ 114 */
85 int (*insert_iter)( 115 int (*insert_iter)(
86 CxIterator *iter, 116 struct cx_iterator_s *iter,
87 void const *elem, 117 void const *elem,
88 int prepend 118 int prepend
89 ); 119 );
90 120
91 /** 121 /**
92 * Member function for removing an element. 122 * Member function for removing an element.
93 */ 123 */
94 int (*remove)( 124 int (*remove)(
95 cx_list_s *list, 125 struct cx_list_s *list,
96 size_t index 126 size_t index
97 ); 127 );
98 128
99 /** 129 /**
100 * Member function for element lookup. 130 * Member function for element lookup.
101 */ 131 */
102 void *(*at)( 132 void *(*at)(
103 cx_list_s const *list, 133 struct cx_list_s const *list,
104 size_t index 134 size_t index
105 ); 135 );
106 136
107 /** 137 /**
108 * Member function for finding an element. 138 * Member function for finding an element.
109 */ 139 */
110 size_t (*find)( 140 size_t (*find)(
111 cx_list_s const *list, 141 struct cx_list_s const *list,
112 void const *elem 142 void const *elem
113 ); 143 );
114 144
115 /** 145 /**
116 * Member function for sorting the list in place. 146 * Member function for sorting the list in place.
117 */ 147 */
118 void (*sort)(cx_list_s *list); 148 void (*sort)(struct cx_list_s *list);
119 149
120 /** 150 /**
121 * Member function for comparing this list to another list of the same type. 151 * Member function for comparing this list to another list of the same type.
122 */ 152 */
123 int (*compare)( 153 int (*compare)(
124 cx_list_s const *list, 154 struct cx_list_s const *list,
125 cx_list_s const *other 155 struct cx_list_s const *other
126 ); 156 );
127 157
128 /** 158 /**
129 * Member function for reversing the order of the items. 159 * Member function for reversing the order of the items.
130 */ 160 */
131 void (*reverse)(cx_list_s *list); 161 void (*reverse)(struct cx_list_s *list);
132 162
133 /** 163 /**
134 * Returns an iterator pointing to the specified index. 164 * Returns an iterator pointing to the specified index.
135 */ 165 */
136 CxIterator (*iterator)( 166 struct cx_iterator_s (*iterator)(
137 cx_list_s *list, 167 struct cx_list_s *list,
138 size_t index 168 size_t index
139 ); 169 );
140 } cx_list_class;
141
142 /**
143 * Structure for holding the base data of a list.
144 */
145 struct cx_list_s {
146 /**
147 * The list class definition.
148 */
149 cx_list_class *cl;
150 /**
151 * The allocator to use.
152 */
153 CxAllocator allocator;
154 /**
155 * The comparator function for the elements.
156 */
157 CxListComparator cmpfunc;
158 /**
159 * The size of each element (payload only).
160 */
161 size_t itemsize;
162 /**
163 * The size of the list (number of currently stored elements).
164 */
165 size_t size;
166 /**
167 * The capacity of the list (maximum number of elements).
168 */
169 size_t capacity;
170 }; 170 };
171 171
172 /** 172 /**
173 * Common type for all list implementations. 173 * Common type for all list implementations.
174 */ 174 */
175 typedef cx_list_s *CxList; 175 typedef struct cx_list_s CxList;
176 176
177 /** 177 /**
178 * Adds an item to the end of the list. 178 * Adds an item to the end of the list.
179 * 179 *
180 * @param list the list 180 * @param list the list
181 * @param elem a pointer to the element to add 181 * @param elem a pointer to the element to add
182 * @return zero on success, non-zero on memory allocation failure 182 * @return zero on success, non-zero on memory allocation failure
183 */ 183 */
184 static inline int cxListAdd( 184 static inline int cxListAdd(
185 CxList list, 185 CxList *list,
186 void const *elem 186 void const *elem
187 ) { 187 ) {
188 return list->cl->add(list, elem); 188 return list->cl->add(list, elem);
189 } 189 }
190 190
200 * or when the index is out of bounds 200 * or when the index is out of bounds
201 * @see cxListInsertAfter() 201 * @see cxListInsertAfter()
202 * @see cxListInsertBefore() 202 * @see cxListInsertBefore()
203 */ 203 */
204 static inline int cxListInsert( 204 static inline int cxListInsert(
205 CxList list, 205 CxList *list,
206 size_t index, 206 size_t index,
207 void const *elem 207 void const *elem
208 ) { 208 ) {
209 return list->cl->insert(list, index, elem); 209 return list->cl->insert(list, index, elem);
210 } 210 }
226 */ 226 */
227 static inline int cxListInsertAfter( 227 static inline int cxListInsertAfter(
228 CxIterator *iter, 228 CxIterator *iter,
229 void const *elem 229 void const *elem
230 ) { 230 ) {
231 return ((cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0); 231 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
232 } 232 }
233 233
234 /** 234 /**
235 * Inserts an element before the current location of the specified iterator. 235 * Inserts an element before the current location of the specified iterator.
236 * 236 *
248 */ 248 */
249 static inline int cxListInsertBefore( 249 static inline int cxListInsertBefore(
250 CxIterator *iter, 250 CxIterator *iter,
251 void const *elem 251 void const *elem
252 ) { 252 ) {
253 return ((cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); 253 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
254 } 254 }
255 255
256 /** 256 /**
257 * Removes the element at the specified index. 257 * Removes the element at the specified index.
258 * @param list the list 258 * @param list the list
259 * @param index the index of the element 259 * @param index the index of the element
260 * @return zero on success, non-zero if the index is out of bounds 260 * @return zero on success, non-zero if the index is out of bounds
261 */ 261 */
262 static inline int cxListRemove( 262 static inline int cxListRemove(
263 CxList list, 263 CxList *list,
264 size_t index 264 size_t index
265 ) { 265 ) {
266 return list->cl->remove(list, index); 266 return list->cl->remove(list, index);
267 } 267 }
268 268
272 * @param list the list 272 * @param list the list
273 * @param index the index of the element 273 * @param index the index of the element
274 * @return a pointer to the element or \c NULL if the index is out of bounds 274 * @return a pointer to the element or \c NULL if the index is out of bounds
275 */ 275 */
276 static inline void *cxListAt( 276 static inline void *cxListAt(
277 CxList list, 277 CxList *list,
278 size_t index 278 size_t index
279 ) { 279 ) {
280 return list->cl->at(list, index); 280 return list->cl->at(list, index);
281 } 281 }
282 282
290 * @param list the list 290 * @param list the list
291 * @param index the index where the iterator shall point at 291 * @param index the index where the iterator shall point at
292 * @return a new iterator 292 * @return a new iterator
293 */ 293 */
294 static inline CxIterator cxListIterator( 294 static inline CxIterator cxListIterator(
295 CxList list, 295 CxList *list,
296 size_t index 296 size_t index
297 ) { 297 ) {
298 return list->cl->iterator(list, index); 298 return list->cl->iterator(list, index);
299 } 299 }
300 300
306 * If the list is empty, a past-the-end iterator will be returned. 306 * If the list is empty, a past-the-end iterator will be returned.
307 * 307 *
308 * @param list the list 308 * @param list the list
309 * @return a new iterator 309 * @return a new iterator
310 */ 310 */
311 static inline CxIterator cxListBegin(CxList list) { 311 static inline CxIterator cxListBegin(CxList *list) {
312 return list->cl->iterator(list, 0); 312 return list->cl->iterator(list, 0);
313 } 313 }
314 314
315 /** 315 /**
316 * Returns the index of the first element that equals \p elem. 316 * Returns the index of the first element that equals \p elem.
320 * @param list the list 320 * @param list the list
321 * @param elem the element to find 321 * @param elem the element to find
322 * @return the index of the element or \c (size+1) if the element is not found 322 * @return the index of the element or \c (size+1) if the element is not found
323 */ 323 */
324 static inline size_t cxListFind( 324 static inline size_t cxListFind(
325 CxList list, 325 CxList *list,
326 void const *elem 326 void const *elem
327 ) { 327 ) {
328 return list->cl->find(list, elem); 328 return list->cl->find(list, elem);
329 } 329 }
330 330
333 * 333 *
334 * \remark The underlying sort algorithm is implementation defined. 334 * \remark The underlying sort algorithm is implementation defined.
335 * 335 *
336 * @param list the list 336 * @param list the list
337 */ 337 */
338 static inline void cxListSort(CxList list) { 338 static inline void cxListSort(CxList *list) {
339 list->cl->sort(list); 339 list->cl->sort(list);
340 } 340 }
341 341
342 /** 342 /**
343 * Reverses the order of the items. 343 * Reverses the order of the items.
344 * 344 *
345 * @param list the list 345 * @param list the list
346 */ 346 */
347 static inline void cxListReverse(CxList list) { 347 static inline void cxListReverse(CxList *list) {
348 list->cl->reverse(list); 348 list->cl->reverse(list);
349 } 349 }
350 350
351 /** 351 /**
352 * Compares a list to another list of the same type. 352 * Compares a list to another list of the same type.
356 * @param list the list 356 * @param list the list
357 * @param other the list to compare to 357 * @param other the list to compare to
358 * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger 358 * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
359 */ 359 */
360 static inline int cxListCompare( 360 static inline int cxListCompare(
361 CxList list, 361 CxList *list,
362 CxList other 362 CxList *other
363 ) { 363 ) {
364 return list->cl->compare(list, other); 364 return list->cl->compare(list, other);
365 } 365 }
366 366
367 #ifdef __cplusplus 367 #ifdef __cplusplus

mercurial