Author | Jamozed <[email protected]> |
Date | 2021-09-11 01:55:47 |
Commit | 99a3dff6f85c984a3d2e7b5de49763dfa2d58396 |
Parent | cc99c0486e7f3ee9c3ca00c82be7ecd6d810d47b |
Add linked list utility
Diffstat
M | src/util/array.c | | | 2 | +- |
M | src/util/array.h | | | 2 | +- |
A | src/util/llist.c | | | 73 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/util/llist.h | | | 51 | +++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 126 insertions, 2 deletions
diff --git a/src/util/array.c b/src/util/array.c index 494acfd..b708190 100644 --- a/src/util/array.c +++ b/src/util/array.c @@ -36,7 +36,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include <stdlib.h> /* Initialise an array. */ -array array_init() { +array array_init(void) { return assert_calloc(1, sizeof (struct array_)); } diff --git a/src/util/array.h b/src/util/array.h index f8981c3..afce368 100644 --- a/src/util/array.h +++ b/src/util/array.h @@ -39,7 +39,7 @@ typedef struct array_ *array; struct array_ { ptr *data; UINT size; }; -extern array array_init(UINT size); +extern array array_init(void); extern void array_free(array a); extern void array_push(array a, ptr data); diff --git a/src/util/llist.c b/src/util/llist.c new file mode 100644 index 0000000..9b49735 --- /dev/null +++ b/src/util/llist.c @@ -0,0 +1,73 @@ +// util/llist.c +// Linked list utility source file for ESH +// Copyright (C) 2021, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "llist.h" +#include "util.h" + +#include <stdlib.h> + +/* Initialise a linked list node. */ +static lnode lnode_init(void) { + return assert_calloc(1, sizeof (struct lnode_)); +} + +/* Uninitialise a linked list node. */ +static void lnode_free(lnode node) { + if (node) { lnode_free(node->next); free(node); } +} + +/* Initialise a linked list. */ +llist llist_init() { + return assert_calloc(1, sizeof (struct llist_)); +} + +/* Uninitialise a linked list. */ +void llist_free(llist list) { + if (list) { lnode_free(list->head); free(list); } +} + +/* Push a pointer to a linked list. */ +void llist_push(llist list, ptr data) { + lnode node = lnode_init(); node->data = data; + node->next = list->head; list->size += 1; list->head = node; +} + +/* Pop a pointer from a linked list. */ +ptr llist_pop(llist list) { + lnode node = list->head; ptr data = node->data; + list->head = node->next; list->size -= 1; lnode_free(node); return data; +} + +/* Peek at a pointer on a linked list without popping. */ +ptr llist_peek(llist list) { + return list->head->data; +} diff --git a/src/util/llist.h b/src/util/llist.h new file mode 100644 index 0000000..7f5a9b4 --- /dev/null +++ b/src/util/llist.h @@ -0,0 +1,51 @@ +// util/llist.h +// Linked list utility header file for ESH +// Copyright (C) 2021, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#ifndef ESH_UTIL_LLIST_H_87ACJX8V +#define ESH_UTIL_LLIST_H_87ACJX8V + +#include "util.h" + +typedef struct lnode_ *lnode; +typedef struct llist_ *llist; + +struct lnode_ { lnode next; ptr data; }; +struct llist_ { lnode head; UINT size; }; + +extern llist llist_init(void); +extern void llist_free(llist list); + +extern void llist_push(llist list, ptr data); +extern ptr llist_pop(llist list); +extern ptr llist_peek(llist list); + +#endif // ESH_UTIL_LLIST_H_87ACJX8V