From a8a79712ef2af310bac56bb807c2bdb77c910b6b Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Wed, 5 Sep 2018 10:29:24 -0700 Subject: [PATCH] Binary sort Signed-off-by: James Ketrenos --- frontend/src/ketr-photos/ketr-photos.html | 28 +++++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/frontend/src/ketr-photos/ketr-photos.html b/frontend/src/ketr-photos/ketr-photos.html index 199d6b5..847eab2 100755 --- a/frontend/src/ketr-photos/ketr-photos.html +++ b/frontend/src/ketr-photos/ketr-photos.html @@ -341,14 +341,12 @@ return; } - var index, length = this.thumbnails.length - 1; - -This needs to become a binary sort between min/max set by either visibleThumbs, or the full -array. 'index' adjustments then need to be done based on these values instead of on the full -array or the search can end up bouncing over the 'visible' items + var index, start, stop, length = this.thumbnails.length - 1; + start = 0; + stop = this.thumbnails.length - 1; if (this.visibleThumbs.length) { - index = this.visibleThumbs[this.visibleThumbs.length >> 1]; + index = this.visibleThumbs[stop >> 1]; } else { index = length >> 1; } @@ -357,10 +355,20 @@ array or the search can end up bouncing over the 'visible' items while (pos != 0 && last != index) { last = index; /* safety escape in case the DOM changed and nothing matches */ - if (pos == +1) { /* Checked item was too low on the page, so look closer to start */ - index = index >> 1; - } else { /* Checked item was too high on the page, so look farther */ - index += (length - index) >> 1; + if (pos == +1) { /* Checked item was too far down page, so look closer to start */ + stop = index - 1; + if (stop < 0) { + console.log("No items in viewport -- all are below"); + return; + } + index = start + ((stop - start) >> 1); + } else { /* Checked item was too high up on the page, so look farther */ + start = index + 1; + if (start == length) { + console.log("No items in viewport -- all are above"); + return; + } + index += (stop - start) >> 1; } pos = this.checkPosition(index);