Changeset 801b7ac for view.c


Ignore:
Timestamp:
Feb 6, 2007, 6:05:13 PM (17 years ago)
Author:
Alejandro R. Sedeño <asedeno@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
7a20e4c
Parents:
2566560
Message:
functions.c: tweak owl_function_calculate_topmsg_normal to not suck as
much. This resolves the delay in jumping from the first message to the
last message.

fmtext.c: get rid of a debug message and an unused variable.

view.c: Convert another linear search to binary search.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • view.c

    rf1e629d r801b7ac  
    9797int owl_view_get_nearest_to_msgid(owl_view *v, int targetid)
    9898{
    99   int i, bestdist=-1, bestpos=0, curid, curdist;
     99  int first, last, mid = 0, max, bestdist, curid = 0;
    100100
    101   for (i=0; i<owl_view_get_size(v); i++) {
    102     curid = owl_message_get_id(owl_view_get_element(v, i));
    103     curdist = abs(targetid-curid);
    104     if (bestdist<0 || curdist<bestdist) {
    105       bestdist = curdist;
    106       bestpos = i;
     101  first = 0;
     102  last = max = owl_view_get_size(v) - 1;
     103  while (first <= last) {
     104    mid = (first + last) / 2;
     105    curid = owl_message_get_id(owl_view_get_element(v, mid));
     106    if (curid == targetid) {
     107      return(mid);
     108    } else if (curid < targetid) {
     109      first = mid + 1;
     110    } else {
     111      last = mid - 1;
    107112    }
    108113  }
    109   return (bestpos);
     114  bestdist = abs(targetid-curid);
     115  if (curid < targetid && mid+1 < max) {
     116    curid = owl_message_get_id(owl_view_get_element(v, mid+1));
     117    mid = (bestdist < abs(targetid-curid)) ? mid : mid+1;
     118  }
     119  else if (curid > targetid && mid-1 >= 0) {
     120    curid = owl_message_get_id(owl_view_get_element(v, mid-1));
     121    mid = (bestdist < abs(targetid-curid)) ? mid : mid-1;
     122  }
     123  return mid;
    110124}
    111125
Note: See TracChangeset for help on using the changeset viewer.