/[bfilter]/trunk/bfilter.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bfilter.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by dpavlin, Tue Sep 7 08:33:53 2004 UTC revision 2 by dpavlin, Tue Sep 7 08:37:14 2004 UTC
# Line 3  Line 3 
3     Dobrica Pavlinusic, dpavlin@rot13.org 2004-09-06     Dobrica Pavlinusic, dpavlin@rot13.org 2004-09-06
4  */  */
5    
6    var debug = 1;
7    
8  function bfilter_init() {  function bfilter_init() {
9          show_status();          show_status();
10  }  }
# Line 57  function results(html,clean) { Line 59  function results(html,clean) {
59          }          }
60  }  }
61    
62    // modified binary search to find first element with substring
63    function binarySearch(arr, find) {
64            if (!arr ||
65                    typeof (arr) != "object" ||
66                    typeof (find) == "undefined" || !arr.length) {
67                    return null;
68            }
69            var low = 0;
70            var high = arr.length - 1;
71            var middlearr = parseInt(arr.length / 2);
72            var lastTry;
73            while (low <= high) {
74                    var mid = (low + high) / 2;
75                    var aTry = (mid < 1) ? 0 : parseInt(mid);
76            
77                    var curr = arr[aTry].substr(0,find.length).toLowerCase();
78                    if (debug) { results("low="+low+" high="+high+" lastTry="+lastTry+" "+aTry+": "+curr+"<br>"); }
79                    if (curr < find) {
80                            low = aTry + 1;
81                            continue;
82                    }
83                    if (curr > find) {
84                            high = aTry - 1;
85                            continue;
86                    }
87                    if (curr == find) {
88                            high = aTry - 1;
89                            lastTry = aTry;
90                            continue;
91                    }
92                    return aTry;
93            }
94            if (debug) { results("lastTry="+lastTry+"<br>"); }
95    
96  function bfilter(document, id, find, arr) {          if (typeof (lastTry) != "undefined") {
97                    return lastTry;
98            } else {
99                    return null;
100            }
101    }
102    
103          var debug = 0;  function bfilter(document, id, find, arr) {
104    
105          results('',1);          results('',1);
106          hits = 0;          hits = 0;
# Line 89  function bfilter(document, id, find, arr Line 129  function bfilter(document, id, find, arr
129          if (find.length == min_len) {          if (find.length == min_len) {
130                  var html = '';                  var html = '';
131                  for (var i = 0; i < arr[part].length; i++) {                  for (var i = 0; i < arr[part].length; i++) {
132                          html += "<li>"+arr[part][i]+"</li>\n";                          html += "<li>"+i+": "+arr[part][i]+"</li>\n";
133                          hits++;                          hits++;
134                  }                  }
135                  results(html);                  results(html);
136          } else {          } else {
137    
138                  for (var i = 0; i < arr[part].length; i++) {                  var from = binarySearch(arr[part], find_lc);
   
                         var text = arr[part][i].toLowerCase();  
   
                         if (debug) { results(part+" "+i+": "+text); }  
139    
140                          // get li element by ID                  if (from != null) {
141                    
142                          if (debug) { results("cmp: "+text.substring(0,find.length)+" "+find+" ["+text+"]<br>\n"); }                          if (debug) { results("loop "+from+" ... "+arr[part].length)+"<br>\n"; }
143                            for(var i = from ; i < arr[part].length ; i++) {
144                          if (text.substring(0,find.length) == find_lc) {                                  if (arr[part][i].substring(0,find.length).toLowerCase() != find_lc) {
145                                  results("<li>"+text+"</li>\n");                                          break;
146                                    }
147                                    results(i+": "+arr[part][i]+"<br>\n");
148                                  hits++;                                  hits++;
149                          }                          }
150                    } else {
151                            // clean results list
152    //                      results("",1);
153                  }                  }
154    
155          }          }
156    
         // clean clock if no results  
         if (hits == 0) { results("",1); }  
157          show_status(" for <em>"+find+"</em>");          show_status(" for <em>"+find+"</em>");
158    
159  }  }

Legend:
Removed from v.1  
changed lines
  Added in v.2

  ViewVC Help
Powered by ViewVC 1.1.26