diff options
author | Kirill A. Shutemov <kirill@shutemov.name> | 2011-07-02 00:41:49 +0300 |
---|---|---|
committer | Kirill A. Shutemov <kirill@shutemov.name> | 2011-07-05 17:52:39 +0300 |
commit | 476c42acc554e7b3f79c945c2a461d4e25dde41c (patch) | |
tree | 82808af7f61a45323ae8d71ed2504216c04e8a6b /src/vector.c | |
parent | 0b0a6b115d617fe79a0be0ddecd89738aefa574c (diff) | |
download | libgit2-476c42acc554e7b3f79c945c2a461d4e25dde41c.tar.gz |
vector: implement git_vector_uniq()
The routine remove duplictes from the vector. Only the last added element
of elements with equal keys remains in the vector.
Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
Diffstat (limited to 'src/vector.c')
-rw-r--r-- | src/vector.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/vector.c b/src/vector.c index 8d09350fc..2fc051f0c 100644 --- a/src/vector.c +++ b/src/vector.c @@ -162,6 +162,26 @@ int git_vector_remove(git_vector *v, unsigned int idx) return GIT_SUCCESS; } +void git_vector_uniq(git_vector *v) +{ + git_vector_cmp cmp; + unsigned int i, j; + + if (v->length <= 1) + return; + + git_vector_sort(v); + cmp = v->_cmp ? v->_cmp : strict_comparison; + + for (i = 0, j = 1 ; j < v->length; ++j) + if (!cmp(v->contents + i, v->contents + j)) + v->contents[i] = v->contents[j]; + else + v->contents[++i] = v->contents[j]; + + v->length -= j - i - 1; +} + void git_vector_clear(git_vector *v) { assert(v); |