cosmopolitan/libc/mem/reverse.internal.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

30 lines
982 B
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
#ifndef COSMOPOLITAN_LIBC_ALG_REVERSE_H_
#define COSMOPOLITAN_LIBC_ALG_REVERSE_H_
2022-08-11 19:13:18 +00:00
#include "libc/intrin/xchg.internal.h"
2020-06-15 14:18:57 +00:00
/**
* Reverses array.
*
* @param ARRAY is a typed array or a pointer to one
2020-06-15 14:18:57 +00:00
* @param COUNT is the number of items
* @return pointer to start of array
* @see ARRAYLEN()
*/
#define reverse(ARRAY, COUNT) \
({ \
autotype(&(ARRAY)[0]) Array = (ARRAY); \
size_t Count = (COUNT); \
if (Count) { \
size_t Start = 0; \
size_t End = Count - 1; \
while (Start < End) { \
xchg(&Array[Start], &Array[End]); \
++Start; \
--End; \
} \
} \
Array; \
})
#endif /* COSMOPOLITAN_LIBC_ALG_REVERSE_H_ */