BarryServer : Git

All the code for all my projects
// BarryServer : Git / libBLOC / commit / b3106d3ca5d418eb70a698902e557178416aba18 / iterator

// Related

libBLOC

Barry Initial commit b3106d3 (2 years, 11 months ago)
diff --git a/iterator/iterator.c b/iterator/iterator.c
new file mode 100644
index 0000000..292b8d9
--- /dev/null
+++ b/iterator/iterator.c
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/*
+ * This file implements the Iterator object type.  Iterator objects are used to
+ * iterate an Iterable object.  Iterable objects are any objects that have
+ * an Iterable header.  Every item in this list is prefixed with an
+ * IterableEntry header.  They support bi-directional tracked movement, and
+ * automatically track position and handle relative movement, since the list
+ * links are not available from the objects themselves.
+ */
+
+#include <BLOC/object.h>
+#include <BLOC/iterator.h>
+#include "iterator.h"
+
+#define ITER_MAGIC 0x49544552 /* ITER */
+
+/* Iterator object type */
+struct ObjectType iteratorType = {
+	.name = "Iterator",
+	.size = sizeof(Iterator),
+};
+
+/* Create an Iterator object for an Iterable object */
+Iterator *
+iterator_create(void *iterable)
+{
+	Iterator *iter = NULL;
+	struct Iterable *i = iterable;
+	if (i->magic == ITER_MAGIC) {
+		iter = obj_new(&iteratorType);
+		iter->list = obj_get(iterable);
+	}
+	return iter;
+}
+
+/* Initialise an Iterable header */
+void
+iterable_init(struct Iterable *header)
+{
+	header->magic = ITER_MAGIC;
+	header->start = header->end = NULL;
+	header->entries = 0;
+}
diff --git a/iterator/iterator.h b/iterator/iterator.h
new file mode 100644
index 0000000..ee80025
--- /dev/null
+++ b/iterator/iterator.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef ITERATOR_H
+#define ITERATOR_H
+
+/* Structure for an Iterator object */
+struct Iterator {
+	struct Iterable *list;
+	struct IterableEntry *prev, *entry, *next;
+	unsigned int pos;
+};
+
+#endif
diff --git a/iterator/iterator_absolute.c b/iterator/iterator_absolute.c
new file mode 100644
index 0000000..94f97b6
--- /dev/null
+++ b/iterator/iterator_absolute.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stddef.h>
+#include <BLOC/iterator.h>
+#include "iterator.h"
+
+/* Get the first iteratable element */
+void *
+iterator_first(Iterator *iter)
+{
+	iter->entry = iter->list->start;
+	if (!iter->entry)
+		return NULL;
+	iter->prev = NULL;
+	iter->next = iter->entry->next;
+	iter->pos = 0;
+	return iter->entry->obj;
+}
+
+/* Get the last iteratable element */
+void *
+iterator_last(Iterator *iter)
+{
+	iter->entry = iter->list->end;
+	if (!iter->entry)
+		return NULL;
+	iter->prev = iter->entry->prev;
+	iter->next = NULL;
+	iter->pos = iter->list->entries - 1;
+	return iter->entry->obj;
+}
diff --git a/iterator/iterator_relative.c b/iterator/iterator_relative.c
new file mode 100644
index 0000000..d719718
--- /dev/null
+++ b/iterator/iterator_relative.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stddef.h>
+#include <BLOC/iterator.h>
+#include "iterator.h"
+
+/* Get the next iterable element */
+void *
+iterator_next(Iterator *iter)
+{
+	iter->entry = iter->next;
+	if (!iter->entry)
+		return NULL;
+	iter->prev = iter->entry->prev;
+	iter->next = iter->entry->next;
+	iter->pos++;
+	return iter->entry->obj;
+}
+
+/* Get the prev iterable element */
+void *
+iterator_prev(Iterator *iter)
+{
+	iter->entry = iter->prev;
+	if (!iter->entry)
+		return NULL;
+	iter->prev = iter->entry->prev;
+	iter->next = iter->entry->next;
+	iter->pos--;
+	return iter->entry->obj;
+}