SDDSlib
Loading...
Searching...
No Matches
binsert.c File Reference

Binary search and insertion functions. More...

#include "mdb.h"

Go to the source code of this file.

Functions

long binaryInsert (void **array, long members, void *newMember, int(*compare)(const void *c1, const void *c2), int32_t *duplicate)
 Inserts a new member into a sorted array using binary search.
 
long binaryIndexSearch (void **array, long members, void *key, int(*compare)(const void *c1, const void *c2), long bracket)
 Searches for a key in a sorted array of pointers using binary search.
 
long binaryArraySearch (void *array, size_t elemSize, long members, void *key, int(*compare)(const void *c1, const void *c2), long bracket)
 Searches for a key in a sorted array of data values using binary search.
 

Detailed Description

Binary search and insertion functions.

This file provides implementations for binary search and insertion operations on arrays of pointers or data values.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file binsert.c.

Function Documentation

◆ binaryArraySearch()

long binaryArraySearch ( void * array,
size_t elemSize,
long members,
void * key,
int(* compare )(const void *c1, const void *c2),
long bracket )

Searches for a key in a sorted array of data values using binary search.

This function performs a binary search on an array of data values (not pointers) to find the index of the key. If the key is not found and the bracket flag is set, it returns the index of the nearest element less than or equal to the key.

Parameters
arrayPointer to the array of data values.
elemSizeThe size of each element in the array.
membersThe number of members in the array.
keyPointer to the key to search for.
compareComparison function that defines the order of the elements.
bracketIf non-zero, returns the nearest index if the key is not found.
Returns
The index of the key if found, the nearest index if bracket is set, or -1 if the key is not found and bracket is not set.

Definition at line 156 of file binsert.c.

161{
162 long ih, il, im, comparison;
163
164 if (members == 0)
165 return -1;
166
167 il = 0;
168 ih = members - 1;
169 if ((comparison = (*compare)((void *)((char *)array + il * elemSize), key)) >= 0) {
170 if (comparison == 0)
171 return il;
172 im = 0;
173 } else if ((comparison = (*compare)((void *)((char *)array + ih * elemSize), key)) <= 0) {
174 if (comparison == 0)
175 return ih;
176 im = ih;
177 } else {
178 while ((ih - il) > 1) {
179 im = (il + ih) / 2;
180 if ((comparison = (*compare)((void *)((char *)array + im * elemSize), key)) == 0)
181 return im;
182 if (comparison > 0)
183 ih = im;
184 else
185 il = im;
186 }
187 im = ih;
188 }
189 if (!bracket)
190 return -1;
191
192 /* return index of nearest point less than or equal to the key */
193 comparison = (*compare)((void *)((char *)array + im * elemSize), key);
194 if (comparison <= 0)
195 return im;
196 comparison = (*compare)((void *)(((char *)array) + il * elemSize), key);
197 if (comparison <= 0)
198 return il;
199 return -1;
200}

◆ binaryIndexSearch()

long binaryIndexSearch ( void ** array,
long members,
void * key,
int(* compare )(const void *c1, const void *c2),
long bracket )

Searches for a key in a sorted array of pointers using binary search.

This function performs a binary search to find the index of the key in the array. If the key is not found and the bracket flag is set, it returns the index of the nearest element less than or equal to the key.

Parameters
arrayPointer to the array of pointers to members.
membersThe number of members in the array.
keyPointer to the key to search for.
compareComparison function that defines the order of the elements.
bracketIf non-zero, returns the nearest index if the key is not found.
Returns
The index of the key if found, the nearest index if bracket is set, or -1 if the key is not found and bracket is not set.

Definition at line 98 of file binsert.c.

99 {
100 long ih, il, im, comparison;
101
102 if (members == 0)
103 return -1;
104
105 il = 0;
106 ih = members - 1;
107 if ((comparison = (*compare)(array[il], key)) >= 0) {
108 if (comparison == 0)
109 return il;
110 im = 0;
111 } else if ((comparison = (*compare)(array[ih], key)) <= 0) {
112 if (comparison == 0)
113 return ih;
114 im = members;
115 } else {
116 while ((ih - il) > 1) {
117 im = (il + ih) / 2;
118 if ((comparison = (*compare)(array[im], key)) == 0)
119 return im;
120 if (comparison > 0)
121 ih = im;
122 else
123 il = im;
124 }
125 im = ih;
126 }
127 if (!bracket)
128 return -1;
129
130 /* return index of nearest point less than or equal to the key */
131 comparison = (*compare)(array[im], key);
132 if (comparison <= 0)
133 return im;
134 comparison = (*compare)(array[il], key);
135 if (comparison <= 0)
136 return il;
137 return -1;
138}

◆ binaryInsert()

long binaryInsert ( void ** array,
long members,
void * newMember,
int(* compare )(const void *c1, const void *c2),
int32_t * duplicate )

Inserts a new member into a sorted array using binary search.

This function performs a binary search to find the appropriate position for the new member in the sorted array. If a duplicate is found, it sets the duplicate flag and does not insert the new member.

Parameters
arrayPointer to the array of pointers to members.
membersThe number of members currently in the array.
newMemberPointer to the new member to be inserted.
compareComparison function that defines the order of the elements.
duplicatePointer to an integer that is set to 1 if a duplicate is found.
Returns
The index at which the new member was inserted, or the index of a duplicate.

Definition at line 39 of file binsert.c.

40 {
41 long ih, il, im, comparison;
42
43 *duplicate = 0;
44 if (members == 0) {
45 *array = newMember;
46 return 0;
47 }
48
49 il = 0;
50 ih = members - 1;
51 if ((comparison = (*compare)(array[il], newMember)) >= 0) {
52 if (comparison == 0) {
53 *duplicate = 1;
54 return 0;
55 }
56 im = 0;
57 } else if ((comparison = (*compare)(array[ih], newMember)) <= 0) {
58 if (comparison == 0) {
59 *duplicate = 1;
60 return ih;
61 }
62 im = members;
63 } else {
64 while ((ih - il) > 1) {
65 im = (il + ih) / 2;
66 if ((comparison = (*compare)(array[im], newMember)) == 0) {
67 *duplicate = 1;
68 return im;
69 }
70 if (comparison > 0)
71 ih = im;
72 else
73 il = im;
74 }
75 im = ih;
76 }
77 for (il = members; il > im; il--)
78 array[il] = array[il - 1];
79 array[im] = newMember;
80 return im;
81}