order.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_ORDER_H
  6. #define TOR_ORDER_H
  7. /**
  8. * \file order.h
  9. *
  10. * \brief Header for order.c.
  11. **/
  12. #include "lib/cc/compat_compiler.h"
  13. #include "lib/cc/torint.h"
  14. /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
  15. * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
  16. * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
  17. * the median. As a side effect, the elements of <b>array</b> are sorted. */
  18. int find_nth_int(int *array, int n_elements, int nth);
  19. time_t find_nth_time(time_t *array, int n_elements, int nth);
  20. double find_nth_double(double *array, int n_elements, int nth);
  21. int32_t find_nth_int32(int32_t *array, int n_elements, int nth);
  22. uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
  23. long find_nth_long(long *array, int n_elements, int nth);
  24. static inline int
  25. median_int(int *array, int n_elements)
  26. {
  27. return find_nth_int(array, n_elements, (n_elements-1)/2);
  28. }
  29. static inline time_t
  30. median_time(time_t *array, int n_elements)
  31. {
  32. return find_nth_time(array, n_elements, (n_elements-1)/2);
  33. }
  34. static inline double
  35. median_double(double *array, int n_elements)
  36. {
  37. return find_nth_double(array, n_elements, (n_elements-1)/2);
  38. }
  39. static inline uint32_t
  40. median_uint32(uint32_t *array, int n_elements)
  41. {
  42. return find_nth_uint32(array, n_elements, (n_elements-1)/2);
  43. }
  44. static inline int32_t
  45. median_int32(int32_t *array, int n_elements)
  46. {
  47. return find_nth_int32(array, n_elements, (n_elements-1)/2);
  48. }
  49. static inline uint32_t
  50. third_quartile_uint32(uint32_t *array, int n_elements)
  51. {
  52. return find_nth_uint32(array, n_elements, (n_elements*3)/4);
  53. }
  54. #endif /* !defined(TOR_CONTAINER_H) */