| 1 |
/* slist.h |
| 2 |
* |
| 3 |
* Copyright 2025 Francois Techene |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifndef SLIST_H |
| 22 |
#define SLIST_H |
| 23 |
|
| 24 |
typedef struct _SListItem { |
| 25 |
|
| 26 |
void* data; |
| 27 |
struct _SListItem* next; |
| 28 |
|
| 29 |
} SListItem; |
| 30 |
|
| 31 |
typedef struct _SList { |
| 32 |
|
| 33 |
void* (*first)(struct _SList* self); |
| 34 |
void* (*last)(struct _SList* self); |
| 35 |
void* (*next)(struct _SList* self, SListItem* item); |
| 36 |
|
| 37 |
SListItem* head; |
| 38 |
int count; |
| 39 |
|
| 40 |
} SList; |
| 41 |
|
| 42 |
// SListItem //////////////// |
| 43 |
// |
| 44 |
SListItem* SListItem_new(); |
| 45 |
void SListItem_delete(SListItem** item); |
| 46 |
|
| 47 |
// SList //////////////////// |
| 48 |
// |
| 49 |
SList* SList_new(); |
| 50 |
void SList_delete(SList** self); |
| 51 |
|
| 52 |
void* slist_first(SList* self); |
| 53 |
void* slist_last(SList* self); |
| 54 |
void* slist_next(SList* self, SListItem* item); |
| 55 |
|
| 56 |
void slist_append(SList* self, void* data); |
| 57 |
void slist_insert_at(SList* self, void* data, int index); |
| 58 |
void slist_insert_after(SList* self, void* data, void* value); |
| 59 |
|
| 60 |
void slist_remove_at(SList* self, int index); |
| 61 |
void slist_remove_last(SList* self); |
| 62 |
void slist_remove_value(SList* self, void* data); |
| 63 |
void slist_empty(SList* self); |
| 64 |
|
| 65 |
|
| 66 |
#define SL_FOREACH(item, list) \ |
| 67 |
for ((item) = (list)->first(list); \ |
| 68 |
(item) != (list)->last(list); \ |
| 69 |
(item) = (list)->next(list, item)) |
| 70 |
|
| 71 |
#endif |