/[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 6 by dpavlin, Tue Sep 7 09:29:36 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 = 0;
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 || typeof (find) == "undefined" || !arr.length) {
65                    return null;
66            }
67            var low = 0;
68            var high = arr.length - 1;
69            var middlearr = parseInt(arr.length / 2);
70            var lastTry;
71            while (low <= high) {
72                    var mid = (low + high) / 2;
73                    var aTry = (mid < 1) ? 0 : parseInt(mid);
74            
75                    var curr = arr[aTry].substr(0,find.length).toLowerCase();
76                    if (debug) { results("low="+low+" high="+high+" lastTry="+lastTry+" "+aTry+": "+curr+"<br>"); }
77                    if (curr < find) {
78                            low = aTry + 1;
79                            continue;
80                    }
81                    if (curr > find) {
82                            high = aTry - 1;
83                            continue;
84                    }
85                    if (curr == find) {
86                            high = aTry - 1;
87                            lastTry = aTry;
88                            continue;
89                    }
90                    return aTry;
91            }
92            if (debug) { results("lastTry="+lastTry+"<br>"); }
93    
94  function bfilter(document, id, find, arr) {          if (typeof (lastTry) != "undefined") {
95                    return lastTry;
96            } else {
97                    return null;
98            }
99    }
100    
101          var debug = 0;  function bfilter(document, id, find, arr) {
102    
103          results('',1);          results('',1);
104          hits = 0;          hits = 0;
# Line 89  function bfilter(document, id, find, arr Line 127  function bfilter(document, id, find, arr
127          if (find.length == min_len) {          if (find.length == min_len) {
128                  var html = '';                  var html = '';
129                  for (var i = 0; i < arr[part].length; i++) {                  for (var i = 0; i < arr[part].length; i++) {
130                          html += "<li>"+arr[part][i]+"</li>\n";                          html += "<li>";
131                            if (debug) { $html += i+": "; }
132                            html += 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); }  
   
                         // get li element by ID  
   
                         if (debug) { results("cmp: "+text.substring(0,find.length)+" "+find+" ["+text+"]<br>\n"); }  
139    
140                          if (text.substring(0,find.length) == find_lc) {                  if (from != null) {
141                                  results("<li>"+text+"</li>\n");                  
142                            if (debug) { results("loop "+from+" ... "+arr[part].length)+"<br>\n"; }
143    
144                            var html = '';
145    
146                            for(var i = from ; i < arr[part].length ; i++) {
147                                    if (arr[part][i].substring(0,find.length).toLowerCase() != find_lc) {
148                                            break;
149                                    }
150                                    if (debug) { html += i+": "; }
151                                    html += arr[part][i]+"<br>\n";
152                                  hits++;                                  hits++;
153                          }                          }
154    
155                            results(html);
156                  }                  }
157    
158          }          }
159    
         // clean clock if no results  
         if (hits == 0) { results("",1); }  
160          show_status(" for <em>"+find+"</em>");          show_status(" for <em>"+find+"</em>");
161    
162  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26