libBLOC
Barry List peeking functions 5613995 (2 years, 10 months ago)
diff --git a/include/BLOC/list.h b/include/BLOC/list.h
index 964362b..6dc7721 100644
--- a/include/BLOC/list.h
+++ b/include/BLOC/list.h
@@ -27,6 +27,8 @@ void list_add(List *list, void *obj);
void list_remove(List *list, void *obj);
void *list_pop_head(List *list);
void *list_pop_tail(List *list);
+void *list_peek_head(List *list);
+void *list_peek_tail(List *list);
unsigned int list_count(List *list);
#endif
diff --git a/list/list_peek.c b/list/list_peek.c
new file mode 100644
index 0000000..ebb77cb
--- /dev/null
+++ b/list/list_peek.c
@@ -0,0 +1,49 @@
+/*
+ * 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 <BLOC/object.h>
+#include <BLOC/list.h>
+#include "../assert.h"
+#include "list.h"
+
+/* Peek at the first object in a list */
+void *
+list_peek_head(List *list)
+{
+ ASSERT(list);
+ void *obj = NULL;
+ obj_lock(list);
+ if (list->header.start)
+ obj = obj_get(list->header.start->obj);
+ obj_unlock(list);
+ return obj;
+}
+
+/* Peek at the last object in a list */
+void *
+list_peek_tail(List *list)
+{
+ ASSERT(list);
+ void *obj = NULL;
+ obj_lock(list);
+ if (list->header.end)
+ obj = obj_get(list->header.end->obj);
+ obj_unlock(list);
+ return obj;
+}