mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 08:12:28 +00:00
parent
5e069a64d4
commit
d932948fb4
954 changed files with 1095 additions and 1342 deletions
31
libc/alg/reverse.internal.h
Normal file
31
libc/alg/reverse.internal.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_ALG_REVERSE_H_
|
||||
#define COSMOPOLITAN_LIBC_ALG_REVERSE_H_
|
||||
#include "libc/bits/xchg.internal.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
||||
/**
|
||||
* Reverses array.
|
||||
*
|
||||
* @param ARRAY is a typed array or a pointer to one
|
||||
* @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 /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_ALG_REVERSE_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue