| 1 |
/* slist.h |
| 2 |
* |
| 3 |
* Copyright 2026 Francois Techene |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
* a copy of this software and associated documentation files (the |
| 7 |
* "Software"), to deal in the Software without restriction, including |
| 8 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
* permit persons to whom the Software is furnished to do so, subject to |
| 11 |
* the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY |
| 20 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
* |
| 24 |
* Except as contained in this notice, the name(s) of the above copyright |
| 25 |
* holders shall not be used in advertising or otherwise to promote the sale, |
| 26 |
* use or other dealings in this Software without prior written |
| 27 |
* authorization. |
| 28 |
*/ |
| 29 |
|
| 30 |
#ifndef __SLIST_H__ |
| 31 |
#define __SLIST_H__ |
| 32 |
|
| 33 |
typedef struct _SListItem { |
| 34 |
|
| 35 |
void* data; |
| 36 |
struct _SListItem* next; |
| 37 |
|
| 38 |
} SListItem; |
| 39 |
|
| 40 |
typedef struct _SList { |
| 41 |
|
| 42 |
void* (*first)(struct _SList* self); |
| 43 |
void* (*last)(struct _SList* self); |
| 44 |
void* (*next)(struct _SList* self, SListItem* item); |
| 45 |
|
| 46 |
SListItem* head; |
| 47 |
int count; |
| 48 |
|
| 49 |
} SList; |
| 50 |
|
| 51 |
// SListItem //////////////// |
| 52 |
// |
| 53 |
SListItem* NewSListItem(); |
| 54 |
void DeleteSListItem(SListItem* item); |
| 55 |
|
| 56 |
// SList //////////////////// |
| 57 |
// |
| 58 |
SList* NewSList(); |
| 59 |
void DeleteSList(SList* self); |
| 60 |
|
| 61 |
void* SListFirst(SList* self); |
| 62 |
void* SListLast(SList* self); |
| 63 |
void* SListNext(SList* self, SListItem* item); |
| 64 |
|
| 65 |
void SListAppend(SList* self, void* data); |
| 66 |
void SListInsertAt(SList* self, void* data, int index); |
| 67 |
void SListInsertAfter(SList* self, void* data, void* value); |
| 68 |
|
| 69 |
void SListRemoveAt(SList* self, int index); |
| 70 |
void SListRemoveLast(SList* self); |
| 71 |
void SListRemoveValue(SList* self, void* data); |
| 72 |
void SListEmpty(SList* self); |
| 73 |
|
| 74 |
|
| 75 |
#define SL_FOREACH(item, list) \ |
| 76 |
for ((item) = (list)->first(list); \ |
| 77 |
(item) != (list)->last(list); \ |
| 78 |
(item) = (list)->next(list, item)) |
| 79 |
|
| 80 |
#endif |