aboutsummaryrefslogtreecommitdiff
path: root/include/bh/algo.h
blob: b859b1fc7a2f25645babdd85bf84a00882b3e0b9 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef BHLIB_ALGO_H
#define BHLIB_ALGO_H

#include "bh.h"

/**
 * @brief   Swap two elements.
 *
 * @param   lhs     Pointer to the first element
 * @param   rhs     Pointer to the second element
 * @param   size    Element size
 */
void bh_swap(void *lhs,
             void *rhs,
             size_t size);

/**
 * @brief   Partition the array.
 *
 * @param   pivot   Pointer to the pivot element
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 *
 * @return  The return value is the pointer to the first element of the second 
 *          partition.
 *
 * @warning Pivot element can be a part of the partitioned array. 
 */
void *bh_partition(void *pivot,
                   void *array,
                   size_t size,
                   size_t element,
                   bh_equal_cb_t equal);

/**
 * @brief   Sort the array.
 *
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 */
void bh_sort(void *array,
             size_t size,
             size_t element,
             bh_equal_cb_t equal);

/**
 * @brief   Sort the array using insert sort.
 *
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 */
void bh_sort_insert(void *array,
                    size_t size,
                    size_t element,
                    bh_equal_cb_t equal);

/**
 * @brief   Sort the array using shell sort.
 *
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 */
void bh_sort_shell(void *array,
                   size_t size,
                   size_t element,
                   bh_equal_cb_t equal);

/**
 * @brief   Sort the array using intro sort.
 *
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 */
void bh_sort_intro(void *array,
                   size_t size,
                   size_t element,
                   bh_equal_cb_t equal);

/**
 * @brief   Sort the array using heap sort.
 *
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 */
void bh_sort_heap(void *array,
                  size_t size,
                  size_t element,
                  bh_equal_cb_t equal);

/**
 * @brief   Make heap from the array.
 *
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 *
 * @sa      bh_heap_remove, bh_heap_insert
 */
void bh_heap_make(void *array,
                  size_t size,
                  size_t element,
                  bh_equal_cb_t equal);

/**
 * @brief   Remove element from the heap.
 *
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 *
 * @warning Removed element is placed at the end of the array
 *
 * @sa      bh_heap_make, bh_heap_remove
 */
void bh_heap_remove(void *array,
                    size_t size,
                    size_t element,
                    bh_equal_cb_t equal);

/**
 * @brief   Insert element to the heap.
 *
 * @param   value   Pointer to the inserted value (optional)
 * @param   array   Pointer to the array
 * @param   size    Array size
 * @param   element Element size
 * @param   equal   Equal/compare function
 *
 * @warning If value is not passed, function assumes inserted element
 *          is already placed at the end of the array.
 *
 * @sa      bh_heap_make, bh_heap_remove
 */
void bh_heap_insert(void *value,
                    void *array,
                    size_t size,
                    size_t element,
                    bh_equal_cb_t equal);

#endif /* BHLIB_ALGO_H */