summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2020-10-01 14:28:26 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2020-10-01 14:28:26 (GMT)
commit7ab9bfa2000ccf02449b12af4d3d8d383db9cab3 (patch) (side-by-side diff)
tree4cb6006a60881bb4fa8a6c6a317e330e989bd3f6
parentb9f3a3d90f09668bb6c131f94bcd4c91e83a44b1 (diff)
downloadmailutils-7ab9bfa2000ccf02449b12af4d3d8d383db9cab3.tar.gz
mailutils-7ab9bfa2000ccf02449b12af4d3d8d383db9cab3.tar.bz2
Rewrite amd_msg_bsearch without recursion
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libmailutils/base/amd.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/libmailutils/base/amd.c b/libmailutils/base/amd.c
index 8a7ee9b..17ef4e4 100644
--- a/libmailutils/base/amd.c
+++ b/libmailutils/base/amd.c
@@ -254,18 +254,22 @@ amd_msg_bsearch (struct _amd_data *amd, mu_off_t first, mu_off_t last,
mu_off_t mid;
int rc;
- if (last < first)
- return 1;
-
- mid = (first + last) / 2;
- rc = amd->msg_cmp (amd->msg_array[mid], msg);
- if (rc > 0)
- return amd_msg_bsearch (amd, first, mid-1, msg, pret);
- *pret = mid;
- if (rc < 0)
- return amd_msg_bsearch (amd, mid+1, last, msg, pret);
- /* else */
- return 0;
+ while (first <= last)
+ {
+ mid = (first + last) / 2;
+ rc = amd->msg_cmp (amd->msg_array[mid], msg);
+ if (rc > 0)
+ last = mid - 1;
+ else
+ {
+ *pret = mid;
+ if (rc < 0)
+ first = mid + 1;
+ else
+ return 0;
+ }
+ }
+ return 1;
}
/* Search for message MSG in the message array of AMD.

Return to:

Send suggestions and report system problems to the System administrator.