Commit 0bd43371 by Carlos Martín Nieto

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.
parent 6708618c
......@@ -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
......
......@@ -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--;
}
}
......@@ -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
......@@ -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));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment