Mercurial > dovecot > original-hg > dovecot-1.2
changeset 6127:32e229d89038 HEAD
Added BINARY_NUMBER_SEARCH() macro.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 20 Jul 2007 17:21:41 +0300 |
parents | 0d3583b02a32 |
children | 6d2bee707053 |
files | src/lib/bsearch-insert-pos.h |
diffstat | 1 files changed, 19 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/bsearch-insert-pos.h Fri Jul 20 17:01:39 2007 +0300 +++ b/src/lib/bsearch-insert-pos.h Fri Jul 20 17:21:41 2007 +0300 @@ -1,6 +1,25 @@ #ifndef __BSEARCH_INSERT_POS #define __BSEARCH_INSERT_POS +/* Binary search template */ +#define BINARY_NUMBER_SEARCH(data, count, value, idx_r) \ + unsigned int idx, left_idx, right_idx; \ + \ + idx = 0; left_idx = 0; right_idx = (count); \ + while (left_idx < right_idx) { \ + idx = (left_idx + right_idx) / 2; \ + \ + if ((data)[idx] < (value)) \ + left_idx = idx+1; \ + else if ((data)[idx] > (value)) \ + right_idx = idx; \ + else { \ + *(idx_r) = idx; \ + return TRUE; \ + } \ + } \ + return FALSE + /* If key is found, returns TRUE and sets idx_r to the position where the key was found. If key isn't found, returns FALSE and sets idx_r to the position where the key should be inserted. */