aboutsummaryrefslogtreecommitdiff
path: root/include/BH/Algo.h
blob: 2e5320e5f9672182993c403d71decce7ba882b63 (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
#ifndef BH_ALGO_H
#define BH_ALGO_H


#include "Common.h"


/**
 * Exchanges the values of two elements.
 *
 * \param dest	Pointer to the element
 * \param src	Pointer to the element
 * \param size	Element size in bytes
 */
void BH_Swap(void *dest,
             void *src,
             size_t size);


/**
 * Partitions the array relative to the specified pivot element.
 *
 * The pivot element can be part of the partitioned array.
 *
 * \param pivot     Pointer to the pivot element
 * \param array     Pointer to the array
 * \param size      Array size
 * \param element   Element size in bytes
 * \param equal     Comparision function
 *
 * \return Pointer to the first element of the second partition.
 */
void *BH_Partition(void *pivot,
                   void *array,
                   size_t size,
                   size_t element,
                   BH_EqualCallback equal);


/**
 * Sorts the array.
 *
 * \param array     Pointer to the array
 * \param size      Array size
 * \param element   Element size in bytes
 * \param equal     Comparision function
 */
void BH_Sort(void *array,
             size_t size,
             size_t element,
             BH_EqualCallback equal);


/**
 * Makes heap in an array from the array.
 *
 * \param array     Pointer to the array
 * \param size      Array size
 * \param element   Element size in bytes
 * \param equal     Comparision function
 */
void BH_HeapMake(void *array,
                 size_t size,
                 size_t element,
                 BH_EqualCallback equal);


/**
 * Removes top/first element from a heap in an array.
 *
 * \param array     Pointer to the array
 * \param size      Array size
 * \param element   Element size in bytes
 * \param equal     Comparasion function
 */
void BH_HeapRemove(void *array,
                   size_t size,
                   size_t element,
                   BH_EqualCallback equal);


/**
 * Inserts new element into heap in an array.
 *
 * If the pointer to value is NULL, this function assumes that the new value
 * is at the end of the array.
 *
 * \param value     Pointer to the value
 * \param array     Pointer to the array
 * \param size      Array size
 * \param element   Element size in bytes
 * \param equal     Comparasion function
 */
void BH_HeapInsert(void *value,
                   void *array,
                   size_t size,
                   size_t element,
                   BH_EqualCallback equal);


/**
 * Removes top/first element and inserts new element into heap in an array.
 *
 * If the pointer to value is NULL, this function assumes that the new value
 * is at the end of the array.
 *
 * This function is roughly equivalent to the following code:
 * \code
 * BH_HeapRemove(array, size, element, equal);
 * BH_HeapInsert(value, array, size - 1, element, equal);
 * \endcode
 *
 * \param value     Pointer to the value
 * \param array     Pointer to the array
 * \param size      Array size
 * \param element   Element size in bytes
 * \param equal     Comparasion function
 */
void BH_HeapReplace(void *value,
                    void *array,
                    size_t size,
                    size_t element,
                    BH_EqualCallback equal);


#endif /* BH_ALGO_H */