barray: Work with more compare functions

Currently, this code requires a compare functions to return either -1, 0 or 1.
Values outside of this range will result in endless loops. Fix this by using if
instead of switch() for the result of the compare function.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2010-09-01 15:37:21 +02:00
parent e1b15b2bb8
commit 0783ee8ffb
1 changed files with 5 additions and 9 deletions

View File

@ -117,17 +117,13 @@
while(l < r) \ while(l < r) \
{ \ { \
int i = (r + l) / 2; \ int i = (r + l) / 2; \
switch(cmp(&e, &arr->tab[i])) \ int res = cmp(&e, &arr->tab[i]); \
{ \ if(res == 0) \
case -1: \ return; /* Already added, ignore */ \
if(res < 0) \
r = i; \ r = i; \
break; \ else \
case 0: \
return; \
case 1: \
l = i + 1; \ l = i + 1; \
break; \
} \
} \ } \
pfx##_array_splice(arr, r, 0, &e, 1); \ pfx##_array_splice(arr, r, 0, &e, 1); \
} \ } \