2025-06-21 20:12:15 +03:00
|
|
|
=encoding UTF-8
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
|
|
BH_Algo - General algorithms
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head1 SYNTAX
|
|
|
|
|
|
|
|
|
|
#include <BH/Algo.h>
|
|
|
|
|
|
|
|
|
|
int data[4] = {5, 2, 3, 1};
|
|
|
|
|
int value, i;
|
|
|
|
|
|
|
|
|
|
value = 4;
|
|
|
|
|
BH_Partition(&value, data, 4, sizeof(int), intEqual);
|
|
|
|
|
BH_Sort(data, 4, sizeof(int), intEqual);
|
|
|
|
|
BH_HeapMake(data, 4, sizeof(int), intEqual);
|
|
|
|
|
|
|
|
|
|
cc prog.c -o prog -lbh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
|
|
The BH_Algo library provides a set of algorithms for working with data:
|
|
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
=item *
|
2025-06-21 20:12:15 +03:00
|
|
|
|
|
|
|
|
Value swapping (L</BH_Swap>)
|
|
|
|
|
|
|
|
|
|
=item *
|
|
|
|
|
|
|
|
|
|
Array partitioning (L</BH_Partition>)
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
=item *
|
2025-06-21 20:12:15 +03:00
|
|
|
|
|
|
|
|
Sorting (L</BH_Sort>)
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
=item *
|
2025-06-21 20:12:15 +03:00
|
|
|
|
|
|
|
|
Heap operations (L</BH_HeapMake>, L</BH_HeapRemove>, L</BH_HeapInsert>,
|
|
|
|
|
L</BH_HeapReplace>)
|
|
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
These algorithms allow you to efficiently perform various operations on arrays
|
2025-06-21 20:12:15 +03:00
|
|
|
and other data structures.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head1 API CALLS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 BH_Swap
|
|
|
|
|
|
|
|
|
|
void BH_Swap(void *dest,
|
|
|
|
|
void *src,
|
|
|
|
|
size_t size);
|
|
|
|
|
|
|
|
|
|
Swaps the values between the variables I<dest> and I<src> of the specified size
|
|
|
|
|
I<size>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 BH_Partition
|
|
|
|
|
|
|
|
|
|
void *BH_Partition(void *pivot,
|
|
|
|
|
void *array,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t element,
|
|
|
|
|
BH_EqualCallback equal);
|
|
|
|
|
|
|
|
|
|
Partitions the array of elements I<array> (with the number of elements I<size>
|
2025-06-22 18:48:26 +03:00
|
|
|
and the size of the element I<element>) into two groups relative to the pivot
|
2025-06-21 20:12:15 +03:00
|
|
|
element I<pivot>.
|
|
|
|
|
|
|
|
|
|
The I<equal> parameter takes a pointer to a function that compares two elements.
|
|
|
|
|
|
|
|
|
|
The I<pivot> parameter can refer to an element of the array being partitioned.
|
|
|
|
|
|
|
|
|
|
The function returns a pointer to the first element of the array that belongs to
|
|
|
|
|
the second group.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 BH_Sort
|
|
|
|
|
|
|
|
|
|
void BH_Sort(void *array,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t element,
|
|
|
|
|
BH_EqualCallback equal);
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
Sorts the array of elements I<array> (with the number of elements I<size> and
|
2025-06-21 20:12:15 +03:00
|
|
|
the size of the element I<element>).
|
|
|
|
|
|
|
|
|
|
The I<equal> parameter takes a pointer to a function that compares two elements.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 BH_HeapMake
|
|
|
|
|
|
|
|
|
|
void BH_HeapMake(void *array,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t element,
|
|
|
|
|
BH_EqualCallback equal);
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
Creates a heap in the array I<array> (with the number of elements I<size> and
|
2025-06-21 20:12:15 +03:00
|
|
|
the size of the element I<element>).
|
|
|
|
|
|
|
|
|
|
The I<equal> parameter takes a pointer to a function that compares two elements.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 BH_HeapRemove
|
|
|
|
|
|
|
|
|
|
void BH_HeapRemove(void *array,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t element,
|
|
|
|
|
BH_EqualCallback equal);
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
Extracts the top element of the heap in the array I<array> (with the number of
|
2025-06-21 20:12:15 +03:00
|
|
|
elements I<size> and the size of the element I<element>).
|
|
|
|
|
|
|
|
|
|
The I<equal> parameter takes a pointer to a function that compares two elements.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 BH_HeapInsert
|
|
|
|
|
|
|
|
|
|
void BH_HeapInsert(void *value,
|
|
|
|
|
void *array,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t element,
|
|
|
|
|
BH_EqualCallback equal);
|
|
|
|
|
|
|
|
|
|
Adds the element I<value> to the heap in the array I<array> (with the number of
|
|
|
|
|
elements I<size> and the size of the element I<element>).
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
If I<value> is NULL, it is assumed that the new value is at the end of the
|
2025-06-21 20:12:15 +03:00
|
|
|
array.
|
|
|
|
|
|
|
|
|
|
The I<equal> parameter takes a pointer to a function that compares two elements.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 BH_HeapReplace
|
|
|
|
|
|
|
|
|
|
void BH_HeapReplace(void *value,
|
|
|
|
|
void *array,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t element,
|
|
|
|
|
BH_EqualCallback equal);
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
Extracts the top element of the heap in the array I<array> (with the number of
|
|
|
|
|
elements I<size> and the size of the element I<element>) and adds the element
|
2025-06-21 20:12:15 +03:00
|
|
|
I<value> to it.
|
|
|
|
|
|
2025-06-22 18:48:26 +03:00
|
|
|
If I<value> is NULL, it is assumed that the new value is at the end of the
|
2025-06-21 20:12:15 +03:00
|
|
|
array.
|
|
|
|
|
|
|
|
|
|
The I<equal> parameter takes a pointer to a function that compares two elements.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
|
|
L<BH>
|