BLI_array: add BLI_array_deduplicate_ordered utility & tests

This commit is contained in:
2021-07-09 13:33:36 +10:00
parent ab70133db0
commit 7592a5097c
3 changed files with 83 additions and 0 deletions

View File

@@ -120,6 +120,35 @@ void _bli_array_permute(void *arr,
}
}
/**
* In-place array de-duplication of an ordered array.
*
* \return The new length of the array.
*
* Access via #BLI_array_deduplicate_ordered
*/
unsigned int _bli_array_deduplicate_ordered(void *arr, unsigned int arr_len, size_t arr_stride)
{
if (UNLIKELY(arr_len <= 1)) {
return arr_len;
}
const unsigned int arr_stride_uint = (unsigned int)arr_stride;
uint j = 0;
for (uint i = 0; i < arr_len; i++) {
if ((i == j) || (memcmp(POINTER_OFFSET(arr, arr_stride_uint * i),
POINTER_OFFSET(arr, arr_stride_uint * j),
arr_stride) == 0)) {
continue;
}
j += 1;
memcpy(POINTER_OFFSET(arr, arr_stride_uint * j),
POINTER_OFFSET(arr, arr_stride_uint * i),
arr_stride);
}
return j + 1;
}
/**
* Find the first index of an item in an array.
*