From 442f640c0470152122facbb5045548ce9cddcd60 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 25 Feb 2019 20:28:47 -0800 Subject: [PATCH] array: New function reverse_array(). An upcoming commit will introduce the first user. --- src/libpspp/array.c | 15 +++++++++++++++ src/libpspp/array.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/src/libpspp/array.c b/src/libpspp/array.c index f37270c81d..9995cf3b62 100644 --- a/src/libpspp/array.c +++ b/src/libpspp/array.c @@ -1027,3 +1027,18 @@ is_heap (const void *array, size_t count, size_t size, return true; } +/* Reverses the order of ARRAY, which contains COUNT elements of SIZE bytes + each. */ +void +reverse_array (void *array_, size_t count, size_t size) +{ + uint8_t *array = array_; + uint8_t *first = array; + uint8_t *last = array + (count - 1) * size; + for (size_t i = 0; i < count / 2; i++) + { + swap (first, last, size); + first += size; + last -= size; + } +} diff --git a/src/libpspp/array.h b/src/libpspp/array.h index fbebd56835..5205be935b 100644 --- a/src/libpspp/array.h +++ b/src/libpspp/array.h @@ -247,5 +247,8 @@ void sort_heap (void *array, size_t count, size_t size, bool is_heap (const void *array, size_t count, size_t size, algo_compare_func *compare, const void *aux); +/* Reverses the order of ARRAY, which contains COUNT elements of SIZE bytes + each. */ +void reverse_array (void *array, size_t count, size_t size); #endif /* algorithm.h */ -- 2.30.2