dovecot: Added BINARY_NUMBER_SEARCH() macro.

dovecot at dovecot.org dovecot at dovecot.org
Fri Jul 20 17:39:15 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/32e229d89038
changeset: 6127:32e229d89038
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Jul 20 17:21:41 2007 +0300
description:
Added BINARY_NUMBER_SEARCH() macro.

diffstat:

1 file changed, 19 insertions(+)
src/lib/bsearch-insert-pos.h |   19 +++++++++++++++++++

diffs (28 lines):

diff -r 0d3583b02a32 -r 32e229d89038 src/lib/bsearch-insert-pos.h
--- 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,5 +1,24 @@
 #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


More information about the dovecot-cvs mailing list