-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafesearch.h
More file actions
60 lines (50 loc) · 2.87 KB
/
safesearch.h
File metadata and controls
60 lines (50 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*************************************************************************************************\
* *
* F I L E N A M E : s a f e s e a r c h . h *
* *
* D E S C R I P T I O N : *
* Header definitions for the safety-critical binary search library. *
* Defines return status codes and result structures with strict alignment. *
* *
\*************************************************************************************************/
#ifndef SAFESEARCH_H
#define SAFESEARCH_H
#include <stddef.h>
#include <stdint.h>
/*-----------------------------------------------------------------------------------------------*/
/* T Y P E D E F I N I T I O N S */
/*-----------------------------------------------------------------------------------------------*/
typedef enum
{
OK_FOUND = 0, /* Key found at index */
OK_NOT_FOUND = 1, /* Key not found; index is insertion point */
ERR_NULL_PTR = 2, /* Input array pointer was NULL (and n > 0) */
ERR_BAD_LEN = 3 /* Invalid length provided (reserved) */
} status_t;
typedef struct
{
status_t status; /* Result status of the operation */
size_t index; /* Found index or insertion point */
} search_result_t;
/*-----------------------------------------------------------------------------------------------*/
/* F U N C T I O N P R O T O T Y P E S */
/*-----------------------------------------------------------------------------------------------*/
/*
* Finds the first position where element >= key.
*/
search_result_t lower_bound_i32 ( const int32_t *a,
size_t n,
int32_t key );
/*
* Finds the first position where element > key.
*/
search_result_t upper_bound_i32 ( const int32_t *a,
size_t n,
int32_t key );
/*
* Checks for existence of key. Returns index if found, else insertion point.
*/
search_result_t binary_search_i32 ( const int32_t *a,
size_t n,
int32_t key );
#endif /* SAFESEARCH_H */