Line data Source code
1 : /** 2 : * @file sclist.c 3 : * @brief Basic single chained generic list 4 : * 5 : * @author François Cerbelle (Fanfan), francois@cerbelle.net 6 : * 7 : * @internal 8 : * Created: 15/11/2024 9 : * Revision: none 10 : * Last modified: 2024-11-16 16:27 11 : * Compiler: gcc 12 : * Organization: Cerbelle.net 13 : * Copyright: Copyright (c) 2024, François Cerbelle 14 : * 15 : * This source code is released for free distribution under the terms of the 16 : * GNU General Public License as published by the Free Software Foundation. 17 : */ 18 : 19 : #include "sclist.h" 20 : 21 : #include <stdlib.h> 22 : #include <stdio.h> 23 : #include <assert.h> 24 : 25 : /** Private list record structure 26 : */ 27 : typedef struct sclistrecord_s { 28 : void* value; /**< Pointer to value */ 29 : struct sclistrecord_s* next; /**< Next record in the list */ 30 : } sclistrecord_t; 31 : 32 : /** Opaque sclist structure 33 : */ 34 : typedef struct sclist_s { 35 : sclistrecord_t* first; /**< Pointer to the first record */ 36 : sclistrecord_t* last; /**< Pointer to the last record */ 37 : } sclist_t; 38 : 39 129 : sclist_t* sclist_new() { 40 : sclist_t* sclist; 41 129 : if (NULL==(sclist=malloc(sizeof(struct sclist_s)))) { 42 0 : perror("sclist_new OOM"); 43 0 : abort(); 44 : } 45 129 : sclist->first=NULL; 46 129 : sclist->last=NULL; 47 : 48 129 : return sclist; 49 : } 50 : 51 6 : void sclist_del(sclist_t* sclist) { 52 6 : assert(sclist); 53 : 54 12 : while (sclist->first!=NULL) { 55 6 : sclistrecord_t* first = sclist->first; 56 6 : sclist->first = sclist->first->next; 57 6 : free(first); 58 : } 59 6 : free(sclist); 60 6 : } 61 : 62 189 : sclistrecord_t* sclist_addrecord(sclist_t* sclist, void* value) { 63 : sclistrecord_t* record; 64 : 65 189 : assert(sclist); 66 : 67 : /* Create the record record */ 68 189 : if (NULL==(record=malloc(sizeof(struct sclistrecord_s)))) { 69 0 : perror("sclist_addrecord OOM"); 70 0 : abort(); 71 : } 72 189 : record->value = value; 73 189 : record->next = NULL; 74 : 75 : /* Add to the list */ 76 189 : if (NULL==sclist->first) { 77 : /* First record to be added in an empty list */ 78 : /* A sentinel record would avoid this test and optimize performances 79 : * at the cost of sentinel size RAM consumption */ 80 84 : sclist->first = record; 81 84 : sclist->last = record; 82 : } else { 83 : /* Add to the end of a non-empty list */ 84 105 : sclist->last->next = record; 85 105 : sclist->last = record; 86 : } 87 189 : return record; 88 : } 89 : 90 65 : void sclist_remrecord(sclist_t* sclist, sclistrecord_t* record) { 91 : sclistrecord_t* cur; 92 : sclistrecord_t* prev; 93 : 94 65 : assert(sclist); 95 65 : assert(record); 96 : 97 78 : for (cur=sclist->first, prev=NULL; (cur)&&(cur!=record); prev=cur,cur=cur->next); 98 : 99 : /* If found */ 100 65 : if (NULL!=cur) { 101 : /* Remove from the chain */ 102 61 : if (sclist->first==cur) 103 53 : sclist->first = cur->next; 104 : else 105 8 : prev->next = cur->next; 106 : /* Update the last pointer if needed */ 107 61 : if (sclist->last==cur) 108 52 : sclist->last = prev; 109 : } else { 110 4 : fprintf(stderr,"sclist_remrecord record not found.\n"); 111 4 : abort(); 112 : } 113 61 : } 114 : 115 176 : sclistrecord_t* sclist_firstrecord(const sclist_t* sclist) { 116 176 : assert(sclist); 117 : 118 176 : return sclist->first; 119 : } 120 : 121 201 : sclistrecord_t* sclist_nextrecord(const sclistrecord_t* record) { 122 201 : assert(record); 123 : 124 201 : return record->next; 125 : } 126 : 127 233 : void* sclist_getvalue(sclistrecord_t* record) { 128 233 : assert(record); 129 : 130 233 : return record->value; 131 : } 132 : /* vim: set tw=80: */