From ee2e73cd7b5a1de68c8316e916c4ef3a88302bed Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sun, 4 Aug 2013 18:53:43 +0200 Subject: piano: Generic linked lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduces generic linked list structure and functions (like append, delete, …). Removes a lot of copy&pasted code and improves code readability/reusability. Heads up: This change breaks libpiano’s ABI. --- src/libpiano/list.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/libpiano/list.c (limited to 'src/libpiano/list.c') diff --git a/src/libpiano/list.c b/src/libpiano/list.c new file mode 100644 index 0000000..2e83ab0 --- /dev/null +++ b/src/libpiano/list.c @@ -0,0 +1,112 @@ +/* +Copyright (c) 2013 + Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in 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: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +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 +AUTHORS OR COPYRIGHT HOLDERS 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 IN +THE SOFTWARE. +*/ + +#include + +#include "piano.h" + +#define PianoListForeach(l) for (; (l) != NULL; (l) = (void *) (l)->next) + +/* append element e to list l, return new list head + */ +void *PianoListAppend (PianoListHead_t * const l, PianoListHead_t * const e) { + assert (e != NULL); + assert (e->next == NULL); + + if (l == NULL) { + return e; + } else { + PianoListHead_t *curr = l; + while (curr->next != NULL) { + curr = curr->next; + } + curr->next = e; + return l; + } +} + +/* prepend element e to list l, returning new list head + */ +void *PianoListPrepend (PianoListHead_t * const l, PianoListHead_t * const e) { + assert (e != NULL); + assert (e->next == NULL); + + e->next = l; + return e; +} + +/* delete element e from list l, return new list head + */ +void *PianoListDelete (PianoListHead_t * const l, PianoListHead_t * const e) { + assert (l != NULL); + assert (e != NULL); + + PianoListHead_t *first = l, *curr = l, *prev = NULL; + PianoListForeach (curr) { + if (curr == e) { + /* found it! */ + if (prev != NULL) { + prev->next = curr->next; + } else { + /* no successor */ + first = curr->next; + } + break; + } + prev = curr; + } + + return first; +} + +/* get nth element of list + */ +void *PianoListGet (PianoListHead_t * const l, const size_t n) { + PianoListHead_t *curr = l; + size_t i = n; + + PianoListForeach (curr) { + if (i == 0) { + return curr; + } + --i; + } + + return NULL; +} + +/* count elements in list l + */ +size_t PianoListCount (const PianoListHead_t * const l) { + assert (l != NULL); + + size_t count = 0; + const PianoListHead_t *curr = l; + + PianoListForeach (curr) { + ++count; + } + + return count; +} + -- cgit v1.2.3