tor  0.4.0.1-alpha
order.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 #ifndef TOR_ORDER_H
7 #define TOR_ORDER_H
8 
15 #include "lib/cc/compat_compiler.h"
16 #include "lib/cc/torint.h"
17 
18 /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
19  * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
20  * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
21  * the median. As a side effect, the elements of <b>array</b> are sorted. */
22 int find_nth_int(int *array, int n_elements, int nth);
23 time_t find_nth_time(time_t *array, int n_elements, int nth);
24 double find_nth_double(double *array, int n_elements, int nth);
25 int32_t find_nth_int32(int32_t *array, int n_elements, int nth);
26 uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
27 long find_nth_long(long *array, int n_elements, int nth);
28 static inline int
29 median_int(int *array, int n_elements)
30 {
31  return find_nth_int(array, n_elements, (n_elements-1)/2);
32 }
33 static inline time_t
34 median_time(time_t *array, int n_elements)
35 {
36  return find_nth_time(array, n_elements, (n_elements-1)/2);
37 }
38 static inline double
39 median_double(double *array, int n_elements)
40 {
41  return find_nth_double(array, n_elements, (n_elements-1)/2);
42 }
43 static inline uint32_t
44 median_uint32(uint32_t *array, int n_elements)
45 {
46  return find_nth_uint32(array, n_elements, (n_elements-1)/2);
47 }
48 static inline int32_t
49 median_int32(int32_t *array, int n_elements)
50 {
51  return find_nth_int32(array, n_elements, (n_elements-1)/2);
52 }
53 
54 static inline uint32_t
55 third_quartile_uint32(uint32_t *array, int n_elements)
56 {
57  return find_nth_uint32(array, n_elements, (n_elements*3)/4);
58 }
59 
60 #endif /* !defined(TOR_CONTAINER_H) */
Integer definitions used throughout Tor.
Utility macros to handle different features and behavior in different compilers.