ucx/cx/list.h

changeset 818
bc782cca0759
parent 816
839fefbdedc7
equal deleted inserted replaced
817:22257f6d06a3 818:bc782cca0759
28 /** 28 /**
29 * \file list.h 29 * \file list.h
30 * \brief Interface for list implementations. 30 * \brief Interface for list implementations.
31 * \author Mike Becker 31 * \author Mike Becker
32 * \author Olaf Wintermann 32 * \author Olaf Wintermann
33 * \version 3.0
34 * \copyright 2-Clause BSD License 33 * \copyright 2-Clause BSD License
35 */ 34 */
36 35
37 #ifndef UCX_LIST_H 36 #ifndef UCX_LIST_H
38 #define UCX_LIST_H 37 #define UCX_LIST_H
51 50
52 /** 51 /**
53 * Structure for holding the base data of a list. 52 * Structure for holding the base data of a list.
54 */ 53 */
55 struct cx_list_s { 54 struct cx_list_s {
56 CX_COLLECTION_MEMBERS 55 CX_COLLECTION_BASE;
57 /** 56 /**
58 * The list class definition. 57 * The list class definition.
59 */ 58 */
60 cx_list_class const *cl; 59 cx_list_class const *cl;
61 /** 60 /**
99 98
100 /** 99 /**
101 * Member function for inserting an element relative to an iterator position. 100 * Member function for inserting an element relative to an iterator position.
102 */ 101 */
103 int (*insert_iter)( 102 int (*insert_iter)(
104 struct cx_mut_iterator_s *iter, 103 struct cx_iterator_s *iter,
105 void const *elem, 104 void const *elem,
106 int prepend 105 int prepend
107 ); 106 );
108 107
109 /** 108 /**
135 struct cx_list_s const *list, 134 struct cx_list_s const *list,
136 size_t index 135 size_t index
137 ); 136 );
138 137
139 /** 138 /**
140 * Member function for finding an element. 139 * Member function for finding and optionally removing an element.
141 */ 140 */
142 ssize_t (*find)( 141 ssize_t (*find_remove)(
143 struct cx_list_s const *list, 142 struct cx_list_s *list,
144 void const *elem 143 void const *elem,
144 bool remove
145 ); 145 );
146 146
147 /** 147 /**
148 * Member function for sorting the list in-place. 148 * Member function for sorting the list in-place.
149 */ 149 */
211 * @return true, if this list is storing pointers 211 * @return true, if this list is storing pointers
212 * @see cxListStorePointers() 212 * @see cxListStorePointers()
213 */ 213 */
214 __attribute__((__nonnull__)) 214 __attribute__((__nonnull__))
215 static inline bool cxListIsStoringPointers(CxList const *list) { 215 static inline bool cxListIsStoringPointers(CxList const *list) {
216 return list->store_pointer; 216 return list->collection.store_pointer;
217 } 217 }
218 218
219 /** 219 /**
220 * Returns the number of elements currently stored in the list. 220 * Returns the number of elements currently stored in the list.
221 * 221 *
222 * @param list the list 222 * @param list the list
223 * @return the number of currently stored elements 223 * @return the number of currently stored elements
224 */ 224 */
225 __attribute__((__nonnull__)) 225 __attribute__((__nonnull__))
226 static inline size_t cxListSize(CxList const *list) { 226 static inline size_t cxListSize(CxList const *list) {
227 return list->size; 227 return list->collection.size;
228 } 228 }
229 229
230 /** 230 /**
231 * Adds an item to the end of the list. 231 * Adds an item to the end of the list.
232 * 232 *
238 __attribute__((__nonnull__)) 238 __attribute__((__nonnull__))
239 static inline int cxListAdd( 239 static inline int cxListAdd(
240 CxList *list, 240 CxList *list,
241 void const *elem 241 void const *elem
242 ) { 242 ) {
243 return list->cl->insert_element(list, list->size, elem); 243 return list->cl->insert_element(list, list->collection.size, elem);
244 } 244 }
245 245
246 /** 246 /**
247 * Adds multiple items to the end of the list. 247 * Adds multiple items to the end of the list.
248 * 248 *
263 static inline size_t cxListAddArray( 263 static inline size_t cxListAddArray(
264 CxList *list, 264 CxList *list,
265 void const *array, 265 void const *array,
266 size_t n 266 size_t n
267 ) { 267 ) {
268 return list->cl->insert_array(list, list->size, array, n); 268 return list->cl->insert_array(list, list->collection.size, array, n);
269 } 269 }
270 270
271 /** 271 /**
272 * Inserts an item at the specified index. 272 * Inserts an item at the specified index.
273 * 273 *
334 * @see cxListInsert() 334 * @see cxListInsert()
335 * @see cxListInsertBefore() 335 * @see cxListInsertBefore()
336 */ 336 */
337 __attribute__((__nonnull__)) 337 __attribute__((__nonnull__))
338 static inline int cxListInsertAfter( 338 static inline int cxListInsertAfter(
339 CxMutIterator *iter, 339 CxIterator *iter,
340 void const *elem 340 void const *elem
341 ) { 341 ) {
342 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0); 342 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 0);
343 } 343 }
344 344
345 /** 345 /**
346 * Inserts an element before the current location of the specified iterator. 346 * Inserts an element before the current location of the specified iterator.
347 * 347 *
357 * @see cxListInsert() 357 * @see cxListInsert()
358 * @see cxListInsertAfter() 358 * @see cxListInsertAfter()
359 */ 359 */
360 __attribute__((__nonnull__)) 360 __attribute__((__nonnull__))
361 static inline int cxListInsertBefore( 361 static inline int cxListInsertBefore(
362 CxMutIterator *iter, 362 CxIterator *iter,
363 void const *elem 363 void const *elem
364 ) { 364 ) {
365 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); 365 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 1);
366 } 366 }
367 367
368 /** 368 /**
369 * Removes the element at the specified index. 369 * Removes the element at the specified index.
370 * 370 *
479 * @param list the list 479 * @param list the list
480 * @param index the index where the iterator shall point at 480 * @param index the index where the iterator shall point at
481 * @return a new iterator 481 * @return a new iterator
482 */ 482 */
483 __attribute__((__nonnull__, __warn_unused_result__)) 483 __attribute__((__nonnull__, __warn_unused_result__))
484 CxMutIterator cxListMutIteratorAt( 484 CxIterator cxListMutIteratorAt(
485 CxList *list, 485 CxList *list,
486 size_t index 486 size_t index
487 ); 487 );
488 488
489 /** 489 /**
497 * @param list the list 497 * @param list the list
498 * @param index the index where the iterator shall point at 498 * @param index the index where the iterator shall point at
499 * @return a new iterator 499 * @return a new iterator
500 */ 500 */
501 __attribute__((__nonnull__, __warn_unused_result__)) 501 __attribute__((__nonnull__, __warn_unused_result__))
502 CxMutIterator cxListMutBackwardsIteratorAt( 502 CxIterator cxListMutBackwardsIteratorAt(
503 CxList *list, 503 CxList *list,
504 size_t index 504 size_t index
505 ); 505 );
506 506
507 /** 507 /**
528 * 528 *
529 * @param list the list 529 * @param list the list
530 * @return a new iterator 530 * @return a new iterator
531 */ 531 */
532 __attribute__((__nonnull__, __warn_unused_result__)) 532 __attribute__((__nonnull__, __warn_unused_result__))
533 static inline CxMutIterator cxListMutIterator(CxList *list) { 533 static inline CxIterator cxListMutIterator(CxList *list) {
534 return cxListMutIteratorAt(list, 0); 534 return cxListMutIteratorAt(list, 0);
535 } 535 }
536 536
537 537
538 /** 538 /**
545 * @param list the list 545 * @param list the list
546 * @return a new iterator 546 * @return a new iterator
547 */ 547 */
548 __attribute__((__nonnull__, __warn_unused_result__)) 548 __attribute__((__nonnull__, __warn_unused_result__))
549 static inline CxIterator cxListBackwardsIterator(CxList const *list) { 549 static inline CxIterator cxListBackwardsIterator(CxList const *list) {
550 return list->cl->iterator(list, list->size - 1, true); 550 return list->cl->iterator(list, list->collection.size - 1, true);
551 } 551 }
552 552
553 /** 553 /**
554 * Returns a mutating backwards iterator pointing to the last item of the list. 554 * Returns a mutating backwards iterator pointing to the last item of the list.
555 * 555 *
559 * 559 *
560 * @param list the list 560 * @param list the list
561 * @return a new iterator 561 * @return a new iterator
562 */ 562 */
563 __attribute__((__nonnull__, __warn_unused_result__)) 563 __attribute__((__nonnull__, __warn_unused_result__))
564 static inline CxMutIterator cxListMutBackwardsIterator(CxList *list) { 564 static inline CxIterator cxListMutBackwardsIterator(CxList *list) {
565 return cxListMutBackwardsIteratorAt(list, list->size - 1); 565 return cxListMutBackwardsIteratorAt(list, list->collection.size - 1);
566 } 566 }
567 567
568 /** 568 /**
569 * Returns the index of the first element that equals \p elem. 569 * Returns the index of the first element that equals \p elem.
570 * 570 *
578 __attribute__((__nonnull__)) 578 __attribute__((__nonnull__))
579 static inline ssize_t cxListFind( 579 static inline ssize_t cxListFind(
580 CxList const *list, 580 CxList const *list,
581 void const *elem 581 void const *elem
582 ) { 582 ) {
583 return list->cl->find(list, elem); 583 return list->cl->find_remove((CxList*)list, elem, false);
584 }
585
586 /**
587 * Removes and returns the index of the first element that equals \p elem.
588 *
589 * Determining equality is performed by the list's comparator function.
590 *
591 * @param list the list
592 * @param elem the element to find and remove
593 * @return the index of the now removed element or a negative
594 * value when the element is not found or could not be removed
595 */
596 __attribute__((__nonnull__))
597 static inline ssize_t cxListFindRemove(
598 CxList *list,
599 void const *elem
600 ) {
601 return list->cl->find_remove(list, elem, true);
584 } 602 }
585 603
586 /** 604 /**
587 * Sorts the list in-place. 605 * Sorts the list in-place.
588 * 606 *

mercurial