diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2016-09-23 12:42:33 +0200 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2016-10-06 11:04:55 +0200 |
commit | 0bd43371c27b5fee23768c1b369bf2c62601578f (patch) | |
tree | b9b04e70097439d19482f236018c35ea4e11dd05 | |
parent | 6708618c10f509ed1d22298628bf578a514d2624 (diff) | |
download | libgit2-0bd43371c27b5fee23768c1b369bf2c62601578f.tar.gz |
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.
-rw-r--r-- | src/pqueue.h | 1 | ||||
-rw-r--r-- | src/vector.c | 16 | ||||
-rw-r--r-- | src/vector.h | 5 | ||||
-rw-r--r-- | tests/core/vector.c | 29 |
4 files changed, 51 insertions, 0 deletions
diff --git a/src/pqueue.h b/src/pqueue.h index da7b74edf..76b14919e 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 db1dcf89a..baec8036f 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 96d149e16..cc4c314d5 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 c351655a7..336254cce 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)); +} |