#ifndef COSMOPOLITAN_LIBC_ALG_ALG_H_
#define COSMOPOLITAN_LIBC_ALG_ALG_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
│ cosmopolitan § algorithms                                                ─╬─│┼
╚────────────────────────────────────────────────────────────────────────────│*/

void *bsearch(const void *, const void *, size_t, size_t,
              int cmp(const void *, const void *))
    paramsnonnull() nothrow nosideeffect;
void *bsearch_r(const void *, const void *, size_t, size_t,
                int cmp(const void *, const void *, void *), void *)
    paramsnonnull((1, 2, 5)) nothrow nosideeffect;
void djbsort(size_t n, int32_t[n]);
void qsort(void *, size_t, size_t, int (*)(const void *, const void *))
    paramsnonnull();
void qsort_r(void *, size_t, size_t,
             int cmp(const void *, const void *, void *), void *arg)
    paramsnonnull((1, 4));
int tarjan(int, const int (*)[2], int, int[], int[], int *)
    paramsnonnull((2, 4)) nocallback nothrow;

void *memmem(const void *, size_t, const void *, size_t)
    paramsnonnull() nothrow nocallback nosideeffect;
void *memmem16(const void *, size_t, const void *, size_t)
    paramsnonnull() nothrow nocallback nosideeffect;
void *wmemmem(const void *, size_t, const void *, size_t)
    paramsnonnull() nothrow nocallback nosideeffect;

#define __algalloc returnspointerwithnoaliases nothrow nocallback nodiscard

char *replacestr(const char *, const char *, const char *)
    paramsnonnull() __algalloc;
char16_t *replacestr16(const char16_t *, const char16_t *, const char16_t *)
    paramsnonnull() __algalloc;
wchar_t *replacewcs(const wchar_t *, const wchar_t *, const wchar_t *)
    paramsnonnull() __algalloc;

char *concatstr(const char *, ...) nullterminated() paramsnonnull() __algalloc;
char16_t *concatstr16(const char16_t *, ...) nullterminated()
    paramsnonnull() __algalloc;
wchar_t *concatwcs(const wchar_t *, ...) nullterminated()
    paramsnonnull() __algalloc;

/*───────────────────────────────────────────────────────────────────────────│─╗
│ cosmopolitan § algorithms » containers                                   ─╬─│┼
╚────────────────────────────────────────────────────────────────────────────│*/

struct critbit0 {
  void *root;
  size_t count;
};

bool critbit0_contains(struct critbit0 *, const char *) nothrow nosideeffect
    paramsnonnull();
bool critbit0_insert(struct critbit0 *, const char *) paramsnonnull();
bool critbit0_delete(struct critbit0 *, const char *) nothrow paramsnonnull();
void critbit0_clear(struct critbit0 *) nothrow paramsnonnull();
char *critbit0_get(struct critbit0 *, const char *);
intptr_t critbit0_allprefixed(struct critbit0 *, const char *,
                              intptr_t (*)(const char *, void *), void *)
    paramsnonnull((1, 2, 3)) nothrow;
bool critbit0_emplace(struct critbit0 *, char *, size_t) paramsnonnull();

/*───────────────────────────────────────────────────────────────────────────│─╗
│ cosmopolitan § algorithms » comparators                                  ─╬─│┼
╚────────────────────────────────────────────────────────────────────────────│*/

int cmpsb(/*const signed char[1]*/ const void *, const void *)
    paramsnonnull() nothrow nocallback nosideeffect;
int cmpub(/*const unsigned char[1]*/ const void *, const void *)
    paramsnonnull() nothrow nocallback nosideeffect;
int cmpsw(/*const signed short[1]*/ const void *, const void *)
    paramsnonnull() nothrow nocallback nosideeffect;
int cmpuw(/*const unsigned short[1]*/ const void *, const void *)
    paramsnonnull() nothrow nocallback nosideeffect;
int cmpsl(/*const signed int[1]*/ const void *, const void *)
    paramsnonnull() nothrow nocallback nosideeffect;
int cmpul(/*const unsigned int[1]*/ const void *, const void *)
    paramsnonnull() nothrow nocallback nosideeffect;
int cmpsq(/*const signed long[1]*/ const void *, const void *)
    paramsnonnull() nothrow nocallback nosideeffect;
int cmpuq(/*const unsigned long[1]*/ const void *, const void *)
    paramsnonnull() nothrow nocallback nosideeffect;

/*───────────────────────────────────────────────────────────────────────────│─╗
│ cosmopolitan § algorithms » generics                                     ─╬─│┼
╚────────────────────────────────────────────────────────────────────────────│*/

#if __STDC_VERSION__ + 0 >= 201112

#define memmem(haystack, haystacklen, needle, needlelen) \
  _Generic(*(haystack), wchar_t                          \
           : wmemmem, char16_t                           \
           : memmem16, default                           \
           : memmem)(haystack, haystacklen, needle, needlelen)

#define replacestr(s, needle, replacement) \
  _Generic(*(s), wchar_t                   \
           : replacewcs, char16_t          \
           : replacestr16, default         \
           : replacestr)(s, needle, replacement)

#define concatstr(s, ...)         \
  _Generic(*(s), wchar_t          \
           : concatwcs, char16_t  \
           : concatstr16, default \
           : concatstr)(s, __VA_ARGS__)

#endif /* C11 */

COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_ALG_H_ */
