APIS Based on ordered data, such as binary search and binary search for a set in a specific range

Source: Internet
Author: User

Typedef int (* comparefunc) (void * pdata, void * pvalue );

 

/**
* In an ordered container, the binary method is used to find the insert position of an element.
* @ Param1 pparray: pointer array start address pointer
* @ Param2 count, number of elements contained in the pointer Array
* @ Param3 comparefunc, comparison function between elements
* @ Param4 pvalue, which is used to find the index value pointer of the underlying element
*/
// Int binaryfind (void ** pparray, int count, comparefunc, unsigned int unvalue)
Int binaryfind (void ** pparray, int count, comparefunc, void * pvalue)
{
Assert (null! = Pparray );
Assert (count> = 0 );
Assert (null! = Pvalue );

Int ibegin = 0;
Int iend = count-1;
Int imiddle = 0;

While (ibegin <= iend)
{
Imiddle = (unsigned) (ibegin + iend)> 1;

If (* comparefunc) (pparray [imiddle], pvalue)> 0)
{
Iend = imiddle-1;
}
Else if (* comparefunc) (pparray [imiddle], pvalue) <0)
{
Ibegin = imiddle + 1;
}
Else
{
Return imiddle;
}
}

Return-1;
}

/**
* In an ordered container, the binary method is used to find the insert position of an element.
* @ Param1 pparray: pointer array start address pointer
* @ Param2 capacity, pointer array capacity
* @ Param3 count, number of elements contained in the pointer Array
* @ Param4 comparefunc, comparison function between elements
* @ Param5 unvalue: Search for the index value of the underlying element
*/
// Int binaryfindwheretoinsert (void ** pparray, int capacity, int count, comparefunc, unsigned int unvalue)
Int binaryfindwheretoinsert (void ** pparray, int capacity, int count, comparefunc, void * pvalue)
{
Assert (null! = Pparray );
Assert (count> = 0 );
Assert (null! = Pvalue );

// First determine whether the container is full
If (count> = capacity)
{
Return-1;
}

// Determine whether the container is empty
If (0 = count)
{
Return 0;
}

Int ibegin = 0;
Int iend = count-1;
Int imiddle = 0;

// Determine whether the value is smaller than the array Header
If (* comparefunc) (pparray [ibegin], pvalue)> 0)
{
Return 0;
}
// Determine whether the value is greater than the end of the array
Else if (* comparefunc) (pparray [iend], pvalue) <0)
{
Return count;
}
// Determine whether it is equal to the array Header
Else if (* comparefunc) (pparray [ibegin], pvalue) = 0)
{
Return-1;
}
// Determine whether it is equal to the end of the array
Else if (* comparefunc) (pparray [iend], pvalue) = 0)
{
Return-1;
}
// The element is in the middle of the array, and the insert position is searched using the binary method.
Else
{
While (ibegin + 1! = Iend)
{
Imiddle = (unsigned) (ibegin + iend)> 1;

If (* comparefunc) (pparray [imiddle], pvalue)> 0)
{
Iend = imiddle;
}
Else if (* comparefunc) (pparray [imiddle], pvalue) <0)
{
Ibegin = imiddle;
}
Else
{
Return-1;
}
}

Return iend;
}

}

/**
* When a value is inserted into an ordered array, the position of the function must be determined by the binaryfindwheretoinsert function. The position of the function has been processed in various cases. Insert only
* The pointer array is used here.
* @ Param1 pparray: pointer array start address pointer
* @ Param2 capacity, pointer array capacity
* @ Param3 count, number of elements contained in the pointer Array
* @ Param4 position, comparison function between elements
* @ Param5 pvalue, the inserted element
*/
Void insertsortarray (void ** pparray, int capacity, int * pcount, int position, void * pvalue)
{
Assert (null! = Pparray );
Assert (null! = Pcount );
Assert (* pcount <capacity );
Assert (position <capacity );
Assert (null! = Pvalue );
Assert (* pcount> = 0 );
Assert (position> = 0 );
Assert (position <= * pcount );

// Move memory
Memmove (pparray + Position + 1, pparray + position, 4 * (* pcount-position ));
Pparray [position] = pvalue;
(* Pcount) ++;
}

/**
* The position of this function must be obtained by the binaryfind function. It has handled various situations and is only deleted here.
* The pointer array is used here.
* @ Param1 pparray: pointer array start address pointer
* @ Param2 capacity, pointer array capacity
* @ Param3 pcount, number of elements contained in the pointer array pointer
* @ Param4 position: Location of the element to be deleted
*/
Void deletesortarray (void ** pparray, int capacity, int * pcount, int position)
{
Assert (null! = Pparray );
Assert (null! = Pcount );
Assert (* pcount <capacity );
Assert (position <capacity );
Assert (capacity> 0 );
Assert (* pcount> = 0 );
Assert (position> = 0 );
Assert (position <= * pcount );

// Save the address to be deleted
Void * pdelete = pparray [position];

// Release the space corresponding to the delete address
Free (pdelete );

// Move memory
Memmove (pparray + position, pparray + Position + 1, 4 * (* pcount-position-1 ));

// Reduce the length of the array by one
* Pcount = * pcount-1;
}

Void destroyall (void ** pparray, int count)
{
Assert (null! = Pparray );

For (INT I = 0; I <count; ++ I)
{
Void * pdelete = pparray [I];

Free (pdelete );
}

Free (pparray );
}

/**
* In an ordered container, the binary method is used to locate the position of an element.
* @ Param1 pparray: pointer array start address pointer
* @ Param2 capacity, pointer array capacity
* @ Param3 count, number of elements contained in the pointer Array
* @ Param4 comparefunc, comparison function between elements
* @ Param5 pvalue, which is used to search for the index value of the underlying element
*/
Int binaryfindlowposition (void ** pparray, int capacity, int count, comparefunc, void * pvalue)
{
Assert (null! = Pparray );
Assert (count> = 0 );
Assert (null! = Pvalue );

// Determine whether the container is empty
If (0 = count)
{
Return-1;
}

Int ibegin = 0;
Int iend = count-1;
Int imiddle = 0;

// Determine whether the value is smaller than the array Header
If (* comparefunc) (pparray [ibegin], pvalue)> 0)
{
Return 0;
}
// Determine whether the value is greater than the end of the array
Else if (* comparefunc) (pparray [iend], pvalue) <0)
{
Return-1;
}
// Determine whether it is equal to the array Header
Else if (* comparefunc) (pparray [ibegin], pvalue) = 0)
{
Return 0;
}
// Determine whether it is equal to the end of the array
Else if (* comparefunc) (pparray [iend], pvalue) = 0)
{
Return count-1;
}
// The element is in the middle of the array, and the insert position is searched using the binary method.
Else
{
While (ibegin + 1! = Iend)
{
Imiddle = (unsigned) (ibegin + iend)> 1;

If (* comparefunc) (pparray [imiddle], pvalue)> 0)
{
Iend = imiddle;
}
Else if (* comparefunc) (pparray [imiddle], pvalue) <0)
{
Ibegin = imiddle;
}
Else
{
Return imiddle;
}
}

Return iend;
}
}

/**
* In an ordered container, the binary method is used to locate the position of an element, and the position of the element is inserted at a high position.
* @ Param1 pparray: pointer array start address pointer
* @ Param2 capacity, pointer array capacity
* @ Param3 count, number of elements contained in the pointer Array
* @ Param4 comparefunc, comparison function between elements
* @ Param5 pvalue, which is used to search for the index value of the underlying element
*/
Int binaryfindhighposition (void ** pparray, int capacity, int count, comparefunc, void * pvalue)
{
Assert (null! = Pparray );
Assert (count> = 0 );
Assert (null! = Pvalue );

// Determine whether the container is empty
If (0 = count)
{
Return-1;
}

Int ibegin = 0;
Int iend = count-1;
Int imiddle = 0;

// Determine whether the value is smaller than the array Header
If (* comparefunc) (pparray [ibegin], pvalue)> 0)
{
Return-1;
}
// Determine whether the value is greater than the end of the array
Else if (* comparefunc) (pparray [iend], pvalue) <0)
{
Return count-1;
}
// Determine whether it is equal to the array Header
Else if (* comparefunc) (pparray [ibegin], pvalue) = 0)
{
Return 0;
}
// Determine whether it is equal to the end of the array
Else if (* comparefunc) (pparray [iend], pvalue) = 0)
{
Return count-1;
}
// The element is in the middle of the array, and the insert position is searched using the binary method.
Else
{
While (ibegin + 1! = Iend)
{
Imiddle = (unsigned) (ibegin + iend)> 1;

If (* comparefunc) (pparray [imiddle], pvalue)> 0)
{
Iend = imiddle;
}
Else if (* comparefunc) (pparray [imiddle], pvalue) <0)
{
Ibegin = imiddle;
}
Else
{
Return imiddle;
}
}

Return iend-1;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.