Commit 0bd43371c27b5fee23768c1b369bf2c62601578f

Carlos Martín Nieto 2016-09-23T12:42:33

vector, pqueue: add git_vector_reverse and git_pqueue_reverse This is a convenience function to reverse the contents of a vector and a pqueue in-place. The pqueue function is useful in the case where we're treating it as a LIFO queue.

diff --git a/src/pqueue.h b/src/pqueue.h
index da7b74e..76b1491 100644
--- a/src/pqueue.h
+++ b/src/pqueue.h
@@ -35,6 +35,7 @@ extern int git_pqueue_init(
 #define git_pqueue_clear git_vector_clear
 #define git_pqueue_size  git_vector_length
 #define git_pqueue_get   git_vector_get
+#define git_pqueue_reverse git_vector_reverse
 
 /**
  * Insert a new item into the queue
diff --git a/src/vector.c b/src/vector.c
index db1dcf8..baec803 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -401,3 +401,19 @@ int git_vector_verify_sorted(const git_vector *v)
 
 	return 0;
 }
+
+void git_vector_reverse(git_vector *v)
+{
+	size_t a, b;
+
+	a = 0;
+	b = v->length - 1;
+
+	while (a < b) {
+		void *tmp = v->contents[a];
+		v->contents[a] = v->contents[b];
+		v->contents[b] = tmp;
+		a++;
+		b--;
+	}
+}
diff --git a/src/vector.h b/src/vector.h
index 96d149e..cc4c314 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -118,4 +118,9 @@ GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp)
 /* Just use this in tests, not for realz. returns -1 if not sorted */
 int git_vector_verify_sorted(const git_vector *v);
 
+/**
+ * Reverse the vector in-place.
+ */
+void git_vector_reverse(git_vector *v);
+
 #endif
diff --git a/tests/core/vector.c b/tests/core/vector.c
index c351655..336254c 100644
--- a/tests/core/vector.c
+++ b/tests/core/vector.c
@@ -376,3 +376,32 @@ void test_core_vector__grow_and_shrink(void)
 
 	git_vector_free(&x);
 }
+
+void test_core_vector__reverse(void)
+{
+	git_vector v = GIT_VECTOR_INIT;
+	size_t i;
+
+	void *in1[] = {(void *) 0x0, (void *) 0x1, (void *) 0x2, (void *) 0x3};
+	void *out1[] = {(void *) 0x3, (void *) 0x2, (void *) 0x1, (void *) 0x0};
+
+	void *in2[] = {(void *) 0x0, (void *) 0x1, (void *) 0x2, (void *) 0x3, (void *) 0x4};
+	void *out2[] = {(void *) 0x4, (void *) 0x3, (void *) 0x2, (void *) 0x1, (void *) 0x0};
+
+	for (i = 0; i < 4; i++)
+		cl_git_pass(git_vector_insert(&v, in1[i]));
+
+	git_vector_reverse(&v);
+
+	for (i = 0; i < 4; i++)
+		cl_assert_equal_p(out1[i], git_vector_get(&v, i));
+
+	git_vector_clear(&v);
+	for (i = 0; i < 5; i++)
+		cl_git_pass(git_vector_insert(&v, in2[i]));
+
+	git_vector_reverse(&v);
+
+	for (i = 0; i < 5; i++)
+		cl_assert_equal_p(out2[i], git_vector_get(&v, i));
+}