diff mbox

[09/34] ptrlist: add backward iterator

Message ID 20170707134002.49500-10-luc.vanoostenryck@gmail.com (mailing list archive)
State Rejected, archived
Headers show

Commit Message

Luc Van Oostenryck July 7, 2017, 1:39 p.m. UTC
---
 ptrlist.c | 32 ++++++++++++++++++++++++++++++++
 ptrlist.h |  2 ++
 2 files changed, 34 insertions(+)
diff mbox

Patch

diff --git a/ptrlist.c b/ptrlist.c
index 5798aec43..031c53262 100644
--- a/ptrlist.c
+++ b/ptrlist.c
@@ -273,3 +273,35 @@  int ptr_cur_beg(struct ptr_cur *cur, struct ptr_list *head)
 	cur->n = -1;
 	return 1;
 }
+
+int ptr_cur_prev(struct ptr_cur *cur)
+{
+	do {
+		struct ptr_list *curl = cur->l;
+
+		if (--cur->n >= 0)
+			return 1;
+
+		if (curl == cur->h)
+			break;
+		curl = curl->prev;
+		cur->l = curl;
+		cur->n = curl->nr;
+	} while (1);
+
+	return 0;
+}
+
+int ptr_cur_end(struct ptr_cur *cur, struct ptr_list *head)
+{
+	struct ptr_list *prev;
+
+	if (!head)
+		return 0;
+
+	prev = head->prev;
+	cur->h = head;
+	cur->l = prev;
+	cur->n = prev->nr;
+	return 1;
+}
diff --git a/ptrlist.h b/ptrlist.h
index b46967c58..883cca952 100644
--- a/ptrlist.h
+++ b/ptrlist.h
@@ -53,6 +53,8 @@  extern int linearize_ptr_list(struct ptr_list *, void **, int);
 
 int ptr_cur_beg(struct ptr_cur *cur, struct ptr_list *head);
 int ptr_cur_next(struct ptr_cur *cur);
+int ptr_cur_end(struct ptr_cur *cur, struct ptr_list *head);
+int ptr_cur_prev(struct ptr_cur *cur);
 
 /*
  * Hey, who said that you can't do overloading in C?