src/cx/list.h

changeset 890
54565fd74e74
parent 882
f8ca6e6c0d48
equal deleted inserted replaced
889:f549fd9fbd8f 890:54565fd74e74
54 struct cx_list_s { 54 struct cx_list_s {
55 CX_COLLECTION_BASE; 55 CX_COLLECTION_BASE;
56 /** 56 /**
57 * The list class definition. 57 * The list class definition.
58 */ 58 */
59 cx_list_class const *cl; 59 const cx_list_class *cl;
60 /** 60 /**
61 * The actual implementation in case the list class is delegating. 61 * The actual implementation in case the list class is delegating.
62 */ 62 */
63 cx_list_class const *climpl; 63 const cx_list_class *climpl;
64 }; 64 };
65 65
66 /** 66 /**
67 * The class definition for arbitrary lists. 67 * The class definition for arbitrary lists.
68 */ 68 */
80 * Implementors SHOULD see to performant implementations for corner cases. 80 * Implementors SHOULD see to performant implementations for corner cases.
81 */ 81 */
82 int (*insert_element)( 82 int (*insert_element)(
83 struct cx_list_s *list, 83 struct cx_list_s *list,
84 size_t index, 84 size_t index,
85 void const *data 85 const void *data
86 ); 86 );
87 87
88 /** 88 /**
89 * Member function for inserting multiple elements. 89 * Member function for inserting multiple elements.
90 * Implementors SHOULD see to performant implementations for corner cases. 90 * Implementors SHOULD see to performant implementations for corner cases.
91 * @see cx_list_default_insert_array() 91 * @see cx_list_default_insert_array()
92 */ 92 */
93 size_t (*insert_array)( 93 size_t (*insert_array)(
94 struct cx_list_s *list, 94 struct cx_list_s *list,
95 size_t index, 95 size_t index,
96 void const *data, 96 const void *data,
97 size_t n 97 size_t n
98 ); 98 );
99 99
100 /** 100 /**
101 * Member function for inserting sorted elements into a sorted list. 101 * Member function for inserting sorted elements into a sorted list.
102 * 102 *
103 * @see cx_list_default_insert_sorted() 103 * @see cx_list_default_insert_sorted()
104 */ 104 */
105 size_t (*insert_sorted)( 105 size_t (*insert_sorted)(
106 struct cx_list_s *list, 106 struct cx_list_s *list,
107 void const *sorted_data, 107 const void *sorted_data,
108 size_t n 108 size_t n
109 ); 109 );
110 110
111 /** 111 /**
112 * Member function for inserting an element relative to an iterator position. 112 * Member function for inserting an element relative to an iterator position.
113 */ 113 */
114 int (*insert_iter)( 114 int (*insert_iter)(
115 struct cx_iterator_s *iter, 115 struct cx_iterator_s *iter,
116 void const *elem, 116 const void *elem,
117 int prepend 117 int prepend
118 ); 118 );
119 119
120 /** 120 /**
121 * Member function for removing an element. 121 * Member function for removing an element.
142 142
143 /** 143 /**
144 * Member function for element lookup. 144 * Member function for element lookup.
145 */ 145 */
146 void *(*at)( 146 void *(*at)(
147 struct cx_list_s const *list, 147 const struct cx_list_s *list,
148 size_t index 148 size_t index
149 ); 149 );
150 150
151 /** 151 /**
152 * Member function for finding and optionally removing an element. 152 * Member function for finding and optionally removing an element.
153 */ 153 */
154 ssize_t (*find_remove)( 154 ssize_t (*find_remove)(
155 struct cx_list_s *list, 155 struct cx_list_s *list,
156 void const *elem, 156 const void *elem,
157 bool remove 157 bool remove
158 ); 158 );
159 159
160 /** 160 /**
161 * Member function for sorting the list in-place. 161 * Member function for sorting the list in-place.
167 * Optional member function for comparing this list 167 * Optional member function for comparing this list
168 * to another list of the same type. 168 * to another list of the same type.
169 * If set to \c NULL, comparison won't be optimized. 169 * If set to \c NULL, comparison won't be optimized.
170 */ 170 */
171 int (*compare)( 171 int (*compare)(
172 struct cx_list_s const *list, 172 const struct cx_list_s *list,
173 struct cx_list_s const *other 173 const struct cx_list_s *other
174 ); 174 );
175 175
176 /** 176 /**
177 * Member function for reversing the order of the items. 177 * Member function for reversing the order of the items.
178 */ 178 */
180 180
181 /** 181 /**
182 * Member function for returning an iterator pointing to the specified index. 182 * Member function for returning an iterator pointing to the specified index.
183 */ 183 */
184 struct cx_iterator_s (*iterator)( 184 struct cx_iterator_s (*iterator)(
185 struct cx_list_s const *list, 185 const struct cx_list_s *list,
186 size_t index, 186 size_t index,
187 bool backward 187 bool backward
188 ); 188 );
189 }; 189 };
190 190
204 */ 204 */
205 __attribute__((__nonnull__)) 205 __attribute__((__nonnull__))
206 size_t cx_list_default_insert_array( 206 size_t cx_list_default_insert_array(
207 struct cx_list_s *list, 207 struct cx_list_s *list,
208 size_t index, 208 size_t index,
209 void const *data, 209 const void *data,
210 size_t n 210 size_t n
211 ); 211 );
212 212
213 /** 213 /**
214 * Default implementation of a sorted insert. 214 * Default implementation of a sorted insert.
227 * @return the number of elements actually inserted 227 * @return the number of elements actually inserted
228 */ 228 */
229 __attribute__((__nonnull__)) 229 __attribute__((__nonnull__))
230 size_t cx_list_default_insert_sorted( 230 size_t cx_list_default_insert_sorted(
231 struct cx_list_s *list, 231 struct cx_list_s *list,
232 void const *sorted_data, 232 const void *sorted_data,
233 size_t n 233 size_t n
234 ); 234 );
235 235
236 /** 236 /**
237 * Default unoptimized sort implementation. 237 * Default unoptimized sort implementation.
300 * @param list 300 * @param list
301 * @return true, if this list is storing pointers 301 * @return true, if this list is storing pointers
302 * @see cxListStorePointers() 302 * @see cxListStorePointers()
303 */ 303 */
304 __attribute__((__nonnull__)) 304 __attribute__((__nonnull__))
305 static inline bool cxListIsStoringPointers(CxList const *list) { 305 static inline bool cxListIsStoringPointers(const CxList *list) {
306 return list->collection.store_pointer; 306 return list->collection.store_pointer;
307 } 307 }
308 308
309 /** 309 /**
310 * Returns the number of elements currently stored in the list. 310 * Returns the number of elements currently stored in the list.
311 * 311 *
312 * @param list the list 312 * @param list the list
313 * @return the number of currently stored elements 313 * @return the number of currently stored elements
314 */ 314 */
315 __attribute__((__nonnull__)) 315 __attribute__((__nonnull__))
316 static inline size_t cxListSize(CxList const *list) { 316 static inline size_t cxListSize(const CxList *list) {
317 return list->collection.size; 317 return list->collection.size;
318 } 318 }
319 319
320 /** 320 /**
321 * Adds an item to the end of the list. 321 * Adds an item to the end of the list.
326 * @see cxListAddArray() 326 * @see cxListAddArray()
327 */ 327 */
328 __attribute__((__nonnull__)) 328 __attribute__((__nonnull__))
329 static inline int cxListAdd( 329 static inline int cxListAdd(
330 CxList *list, 330 CxList *list,
331 void const *elem 331 const void *elem
332 ) { 332 ) {
333 return list->cl->insert_element(list, list->collection.size, elem); 333 return list->cl->insert_element(list, list->collection.size, elem);
334 } 334 }
335 335
336 /** 336 /**
350 * @return the number of added elements 350 * @return the number of added elements
351 */ 351 */
352 __attribute__((__nonnull__)) 352 __attribute__((__nonnull__))
353 static inline size_t cxListAddArray( 353 static inline size_t cxListAddArray(
354 CxList *list, 354 CxList *list,
355 void const *array, 355 const void *array,
356 size_t n 356 size_t n
357 ) { 357 ) {
358 return list->cl->insert_array(list, list->collection.size, array, n); 358 return list->cl->insert_array(list, list->collection.size, array, n);
359 } 359 }
360 360
373 */ 373 */
374 __attribute__((__nonnull__)) 374 __attribute__((__nonnull__))
375 static inline int cxListInsert( 375 static inline int cxListInsert(
376 CxList *list, 376 CxList *list,
377 size_t index, 377 size_t index,
378 void const *elem 378 const void *elem
379 ) { 379 ) {
380 return list->cl->insert_element(list, index, elem); 380 return list->cl->insert_element(list, index, elem);
381 } 381 }
382 382
383 /** 383 /**
388 * @return zero on success, non-zero on memory allocation failure 388 * @return zero on success, non-zero on memory allocation failure
389 */ 389 */
390 __attribute__((__nonnull__)) 390 __attribute__((__nonnull__))
391 static inline int cxListInsertSorted( 391 static inline int cxListInsertSorted(
392 CxList *list, 392 CxList *list,
393 void const *elem 393 const void *elem
394 ) { 394 ) {
395 void const *data = list->collection.store_pointer ? &elem : elem; 395 const void *data = list->collection.store_pointer ? &elem : elem;
396 return list->cl->insert_sorted(list, data, 1) == 0; 396 return list->cl->insert_sorted(list, data, 1) == 0;
397 } 397 }
398 398
399 /** 399 /**
400 * Inserts multiple items to the list at the specified index. 400 * Inserts multiple items to the list at the specified index.
417 */ 417 */
418 __attribute__((__nonnull__)) 418 __attribute__((__nonnull__))
419 static inline size_t cxListInsertArray( 419 static inline size_t cxListInsertArray(
420 CxList *list, 420 CxList *list,
421 size_t index, 421 size_t index,
422 void const *array, 422 const void *array,
423 size_t n 423 size_t n
424 ) { 424 ) {
425 return list->cl->insert_array(list, index, array, n); 425 return list->cl->insert_array(list, index, array, n);
426 } 426 }
427 427
443 * @return the number of added elements 443 * @return the number of added elements
444 */ 444 */
445 __attribute__((__nonnull__)) 445 __attribute__((__nonnull__))
446 static inline size_t cxListInsertSortedArray( 446 static inline size_t cxListInsertSortedArray(
447 CxList *list, 447 CxList *list,
448 void const *array, 448 const void *array,
449 size_t n 449 size_t n
450 ) { 450 ) {
451 return list->cl->insert_sorted(list, array, n); 451 return list->cl->insert_sorted(list, array, n);
452 } 452 }
453 453
467 * @see cxListInsertBefore() 467 * @see cxListInsertBefore()
468 */ 468 */
469 __attribute__((__nonnull__)) 469 __attribute__((__nonnull__))
470 static inline int cxListInsertAfter( 470 static inline int cxListInsertAfter(
471 CxIterator *iter, 471 CxIterator *iter,
472 void const *elem 472 const void *elem
473 ) { 473 ) {
474 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 0); 474 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 0);
475 } 475 }
476 476
477 /** 477 /**
490 * @see cxListInsertAfter() 490 * @see cxListInsertAfter()
491 */ 491 */
492 __attribute__((__nonnull__)) 492 __attribute__((__nonnull__))
493 static inline int cxListInsertBefore( 493 static inline int cxListInsertBefore(
494 CxIterator *iter, 494 CxIterator *iter,
495 void const *elem 495 const void *elem
496 ) { 496 ) {
497 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 1); 497 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 1);
498 } 498 }
499 499
500 /** 500 /**
574 * @param index the index where the iterator shall point at 574 * @param index the index where the iterator shall point at
575 * @return a new iterator 575 * @return a new iterator
576 */ 576 */
577 __attribute__((__nonnull__, __warn_unused_result__)) 577 __attribute__((__nonnull__, __warn_unused_result__))
578 static inline CxIterator cxListIteratorAt( 578 static inline CxIterator cxListIteratorAt(
579 CxList const *list, 579 const CxList *list,
580 size_t index 580 size_t index
581 ) { 581 ) {
582 return list->cl->iterator(list, index, false); 582 return list->cl->iterator(list, index, false);
583 } 583 }
584 584
593 * @param index the index where the iterator shall point at 593 * @param index the index where the iterator shall point at
594 * @return a new iterator 594 * @return a new iterator
595 */ 595 */
596 __attribute__((__nonnull__, __warn_unused_result__)) 596 __attribute__((__nonnull__, __warn_unused_result__))
597 static inline CxIterator cxListBackwardsIteratorAt( 597 static inline CxIterator cxListBackwardsIteratorAt(
598 CxList const *list, 598 const CxList *list,
599 size_t index 599 size_t index
600 ) { 600 ) {
601 return list->cl->iterator(list, index, true); 601 return list->cl->iterator(list, index, true);
602 } 602 }
603 603
645 * 645 *
646 * @param list the list 646 * @param list the list
647 * @return a new iterator 647 * @return a new iterator
648 */ 648 */
649 __attribute__((__nonnull__, __warn_unused_result__)) 649 __attribute__((__nonnull__, __warn_unused_result__))
650 static inline CxIterator cxListIterator(CxList const *list) { 650 static inline CxIterator cxListIterator(const CxList *list) {
651 return list->cl->iterator(list, 0, false); 651 return list->cl->iterator(list, 0, false);
652 } 652 }
653 653
654 /** 654 /**
655 * Returns a mutating iterator pointing to the first item of the list. 655 * Returns a mutating iterator pointing to the first item of the list.
676 * 676 *
677 * @param list the list 677 * @param list the list
678 * @return a new iterator 678 * @return a new iterator
679 */ 679 */
680 __attribute__((__nonnull__, __warn_unused_result__)) 680 __attribute__((__nonnull__, __warn_unused_result__))
681 static inline CxIterator cxListBackwardsIterator(CxList const *list) { 681 static inline CxIterator cxListBackwardsIterator(const CxList *list) {
682 return list->cl->iterator(list, list->collection.size - 1, true); 682 return list->cl->iterator(list, list->collection.size - 1, true);
683 } 683 }
684 684
685 /** 685 /**
686 * Returns a mutating backwards iterator pointing to the last item of the list. 686 * Returns a mutating backwards iterator pointing to the last item of the list.
707 * @return the index of the element or a negative 707 * @return the index of the element or a negative
708 * value when the element is not found 708 * value when the element is not found
709 */ 709 */
710 __attribute__((__nonnull__)) 710 __attribute__((__nonnull__))
711 static inline ssize_t cxListFind( 711 static inline ssize_t cxListFind(
712 CxList const *list, 712 const CxList *list,
713 void const *elem 713 const void *elem
714 ) { 714 ) {
715 return list->cl->find_remove((CxList*)list, elem, false); 715 return list->cl->find_remove((CxList*)list, elem, false);
716 } 716 }
717 717
718 /** 718 /**
726 * value when the element is not found or could not be removed 726 * value when the element is not found or could not be removed
727 */ 727 */
728 __attribute__((__nonnull__)) 728 __attribute__((__nonnull__))
729 static inline ssize_t cxListFindRemove( 729 static inline ssize_t cxListFindRemove(
730 CxList *list, 730 CxList *list,
731 void const *elem 731 const void *elem
732 ) { 732 ) {
733 return list->cl->find_remove(list, elem, true); 733 return list->cl->find_remove(list, elem, true);
734 } 734 }
735 735
736 /** 736 /**
766 * @return zero, if both lists are equal element wise, 766 * @return zero, if both lists are equal element wise,
767 * negative if the first list is smaller, positive if the first list is larger 767 * negative if the first list is smaller, positive if the first list is larger
768 */ 768 */
769 __attribute__((__nonnull__)) 769 __attribute__((__nonnull__))
770 int cxListCompare( 770 int cxListCompare(
771 CxList const *list, 771 const CxList *list,
772 CxList const *other 772 const CxList *other
773 ); 773 );
774 774
775 /** 775 /**
776 * Deallocates the memory of the specified list structure. 776 * Deallocates the memory of the specified list structure.
777 * 777 *

mercurial