mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-06 11:19:47 +00:00
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>
This commit is contained in:
parent
0b0a6b115d
commit
476c42acc5
20
src/vector.c
20
src/vector.c
@ -162,6 +162,26 @@ int git_vector_remove(git_vector *v, unsigned int idx)
|
|||||||
return GIT_SUCCESS;
|
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)
|
void git_vector_clear(git_vector *v)
|
||||||
{
|
{
|
||||||
assert(v);
|
assert(v);
|
||||||
|
@ -32,5 +32,5 @@ GIT_INLINE(void *) git_vector_get(git_vector *v, unsigned int position)
|
|||||||
|
|
||||||
int git_vector_insert(git_vector *v, void *element);
|
int git_vector_insert(git_vector *v, void *element);
|
||||||
int git_vector_remove(git_vector *v, unsigned int idx);
|
int git_vector_remove(git_vector *v, unsigned int idx);
|
||||||
|
void git_vector_uniq(git_vector *v);
|
||||||
#endif
|
#endif
|
||||||
|
@ -73,6 +73,28 @@ BEGIN_TEST(vector1, "don't read past array bounds on remove()")
|
|||||||
git_vector_free(&x);
|
git_vector_free(&x);
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
static int test_cmp(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
int n1 = *(int *)a;
|
||||||
|
int n2 = *(int *)b;
|
||||||
|
|
||||||
|
return n1 - n2;
|
||||||
|
}
|
||||||
|
|
||||||
|
BEGIN_TEST(vector2, "remove duplicates")
|
||||||
|
git_vector x;
|
||||||
|
must_pass(git_vector_init(&x, 5, test_cmp));
|
||||||
|
must_pass(git_vector_insert(&x, (void *) 0xdeadbeef));
|
||||||
|
must_pass(git_vector_insert(&x, (void *) 0xcafebabe));
|
||||||
|
must_pass(git_vector_insert(&x, (void *) 0xcafebabe));
|
||||||
|
must_pass(git_vector_insert(&x, (void *) 0xdeadbeef));
|
||||||
|
must_pass(git_vector_insert(&x, (void *) 0xcafebabe));
|
||||||
|
must_be_true(x.length == 5);
|
||||||
|
git_vector_uniq(&x);
|
||||||
|
must_be_true(x.length == 2);
|
||||||
|
git_vector_free(&x);
|
||||||
|
END_TEST
|
||||||
|
|
||||||
|
|
||||||
BEGIN_TEST(path0, "get the dirname of a path")
|
BEGIN_TEST(path0, "get the dirname of a path")
|
||||||
char dir[64], *dir2;
|
char dir[64], *dir2;
|
||||||
@ -480,6 +502,7 @@ BEGIN_SUITE(core)
|
|||||||
|
|
||||||
ADD_TEST(vector0);
|
ADD_TEST(vector0);
|
||||||
ADD_TEST(vector1);
|
ADD_TEST(vector1);
|
||||||
|
ADD_TEST(vector2);
|
||||||
|
|
||||||
ADD_TEST(path0);
|
ADD_TEST(path0);
|
||||||
ADD_TEST(path1);
|
ADD_TEST(path1);
|
||||||
|
Loading…
Reference in New Issue
Block a user