From: Ben Pfaff Date: Tue, 26 Feb 2019 04:28:47 +0000 (-0800) Subject: array: New function reverse_array(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=442f640c0470152122facbb5045548ce9cddcd60;p=pspp array: New function reverse_array(). An upcoming commit will introduce the first user. --- 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 */