From 28ba35171bca7d911bcd74724bf4dfdca46b4590 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Fri, 7 Nov 2025 23:28:54 -0600 Subject: initial commit --- pipeline.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 pipeline.c (limited to 'pipeline.c') diff --git a/pipeline.c b/pipeline.c new file mode 100644 index 0000000..477421f --- /dev/null +++ b/pipeline.c @@ -0,0 +1,59 @@ +#include "pipeline.h" +#include + +int pipeline_add_stage_after(struct pipeline_t *pl, struct pipeline_stage_t *stage, const char *name) +{ + if (!name) { + /* name == NULL: insert at end of pipeline */ + stage->next = NULL; + +#if 0 + if (pl->first) { + struct pipeline_stage_t *tail = pl->first; + for (; tail->next; tail = tail->next); + tail->next = stage; + } else { /* corner case: pipeline is empty */ + pl->first = stage; + } +#else + /* i like this implementation because it's pretty */ + struct pipeline_stage_t **ptail = &pl->first; + for (; *ptail; ptail = &(*ptail)->next); + *ptail = stage; +#endif + + return 1; + } + + for (struct pipeline_stage_t *cur = pl->first; cur; cur = cur->next) { + if (name == cur->name || !strcmp(name, cur->name)) { + stage->next = cur->next; + cur->next = stage; + return 1; + } + } + + return 0; +} + +int pipeline_add_stage_before(struct pipeline_t *pl, struct pipeline_stage_t *stage, const char *name) +{ + if (!name || !pl->first || name == pl->first->name || !strcmp(name, pl->first->name)) { + stage->next = pl->first; + pl->first = stage; + return 1; + } + + /* note that it's okay we don't properly check if prev->first->name equals name here, since that was done above. */ + for (struct pipeline_stage_t *cur = pl->first->next, *prev = pl->first; cur; prev = cur, cur = cur->next) { + if (name == cur->name || !strcmp(name, cur->name)) { + prev->next = stage; + stage->next = cur; + return 1; + } + } + + return 0; +} + + -- cgit v1.2.3-70-g09d2