mirror of
https://github.com/deltachat/deltachat-core.git
synced 2025-10-06 03:50:08 +02:00
add functions for comparing and duplicating arrays
This commit is contained in:
parent
e6f14ab8e0
commit
17b080fc11
2 changed files with 40 additions and 0 deletions
|
@ -390,6 +390,14 @@ void stress_functions(mrmailbox_t* mailbox)
|
||||||
mrarray_empty(arr);
|
mrarray_empty(arr);
|
||||||
assert( mrarray_get_cnt(arr) == 0 );
|
assert( mrarray_get_cnt(arr) == 0 );
|
||||||
|
|
||||||
|
mrarray_add_id(arr, 13);
|
||||||
|
mrarray_add_id(arr, 7);
|
||||||
|
mrarray_add_id(arr, 666);
|
||||||
|
mrarray_add_id(arr, 0);
|
||||||
|
mrarray_add_id(arr, 5000);
|
||||||
|
mrarray_sort(arr);
|
||||||
|
assert( mrarray_get_id(arr, 0)==0 && mrarray_get_id(arr, 1)==7 && mrarray_get_id(arr, 2)==13 && mrarray_get_id(arr, 3)==666 );
|
||||||
|
|
||||||
mrarray_unref(arr);
|
mrarray_unref(arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,38 @@ void mrarray_unref(mrarray_t* array)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
mrarray_t* mrarray_duplicate(const mrarray_t* array)
|
||||||
|
{
|
||||||
|
mrarray_t* ret = NULL;
|
||||||
|
|
||||||
|
if( array==NULL || array->m_magic != MR_ARRAY_MAGIC ) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = mrarray_new(array->m_mailbox, array->m_allocated);
|
||||||
|
ret->m_count = array->m_count;
|
||||||
|
memcpy(ret->m_array, array->m_array, array->m_count * sizeof(uintptr_t));
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int cmp_intptr_t(const void* p1, const void* p2)
|
||||||
|
{
|
||||||
|
uintptr_t v1 = *(uintptr_t*)p1, v2 = *(uintptr_t*)p2;
|
||||||
|
return (v1<v2)? -1 : ((v1>v2)? 1 : 0); /* CAVE: do not use v1-v2 as the uintptr_t may be 64bit and the return value may be 32bit only... */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void mrarray_sort(mrarray_t* array)
|
||||||
|
{
|
||||||
|
if( array == NULL || array->m_magic != MR_ARRAY_MAGIC || array->m_count <= 1 ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
qsort(array->m_array, array->m_count, sizeof(uintptr_t), cmp_intptr_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Empty an array object. Allocated data is not freed by this function, only the count is set to null.
|
* Empty an array object. Allocated data is not freed by this function, only the count is set to null.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue