#ifndef SIGSUM_SORT_INTERNAL_H_INCLUDED
#define SIGSUM_SORT_INTERNAL_H_INCLUDED

#include <stdbool.h>

#include "crypto.h"

/* Returns true on success, false if there were duplicate hashes.
   Takes element size as argument, to be able to sort any struct
   starting with a sigsum_hash. */
bool
_sigsum_sort_by_hash (size_t n, size_t elem_size,
		      struct sigsum_hash *hashes);

/* Sorts a list defined by (SIZE, P). Last argument is the hash member
   of a list element, and it *must* be the first member (as backed up
   by _Static_assert). The size == 0 check is needed because the
   &(p)[0].member expression triggers an undefined behavior warning if
   p is NULL. */
#define SIGSUM_SORT_BY_HASH(size, p, member)	\
  ((void) sizeof(struct{\
      _Static_assert (offsetof (typeof ((p)[0]), member) == 0,		\
		      "hash member must be first");			\
    }),									\
    (((size) == 0) ? true						\
     : _sigsum_sort_by_hash((size),					\
			    sizeof ((p)[0]), &(p)[0].member)))

#endif /* SIGSUM_KEYS_INTERNAL_H_INCLUDED */
