array: New function reverse_array().
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 26 Feb 2019 04:28:47 +0000 (20:28 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 26 Feb 2019 04:28:47 +0000 (20:28 -0800)
An upcoming commit will introduce the first user.

src/libpspp/array.c
src/libpspp/array.h

index f37270c81da9796320de8507cb097a80ceca6869..9995cf3b628f6de2840d798a383887f28985f6da 100644 (file)
@@ -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;
+    }
+}
index fbebd56835f0ba59bf4afc522e17f7240e7e1c50..5205be935bf2fa05ac08f5b262e07ae4a8749572 100644 (file)
@@ -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 */