1 /**
2  * @file lv_utils.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include <stddef.h>
10 
11 #include "lv_utils.h"
12 
13 /*********************
14  *      DEFINES
15  *********************/
16 
17 /**********************
18  *      TYPEDEFS
19  **********************/
20 
21 /**********************
22  *  STATIC PROTOTYPES
23  **********************/
24 
25 /**********************
26  *  STATIC VARIABLES
27  **********************/
28 
29 /**********************
30  *      MACROS
31  **********************/
32 
33 /**********************
34  *   GLOBAL FUNCTIONS
35  **********************/
36 
37 /** Searches base[0] to base[n - 1] for an item that matches *key.
38  *
39  * @note The function cmp must return negative if its first
40  *  argument (the search key) is less than its second (a table entry),
41  *  zero if equal, and positive if greater.
42  *
43  *  @note Items in the array must be in ascending order.
44  *
45  * @param key    Pointer to item being searched for
46  * @param base   Pointer to first element to search
47  * @param n      Number of elements
48  * @param size   Size of each element
49  * @param cmp    Pointer to comparison function (see #unicode_list_compare as a comparison function
50  * example)
51  *
52  * @return a pointer to a matching item, or NULL if none exists.
53  */
_lv_utils_bsearch(const void * key,const void * base,uint32_t n,uint32_t size,int32_t (* cmp)(const void * pRef,const void * pElement))54 void * _lv_utils_bsearch(const void * key, const void * base, uint32_t n, uint32_t size,
55                          int32_t (*cmp)(const void * pRef, const void * pElement))
56 {
57     const char * middle;
58     int32_t c;
59 
60     for(middle = base; n != 0;) {
61         middle += (n / 2) * size;
62         if((c = (*cmp)(key, middle)) > 0) {
63             n    = (n / 2) - ((n & 1) == 0);
64             base = (middle += size);
65         }
66         else if(c < 0) {
67             n /= 2;
68             middle = base;
69         }
70         else {
71             return (char *)middle;
72         }
73     }
74     return NULL;
75 }
76 
77 /**********************
78  *   STATIC FUNCTIONS
79  **********************/
80