/[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 5 by dpavlin, Tue Sep 7 09:16:24 2004 UTC revision 29 by dpavlin, Fri Sep 24 15:44:27 2004 UTC
# Line 1  Line 1 
1  /*  /*
2     Fast filtering function using pre-sorted list elements     Fast filtering function using pre-sorted list elements
3     Dobrica Pavlinusic, dpavlin@rot13.org 2004-09-06     Dobrica Pavlinusic, dpavlin@rot13.org 2004-09-06
4       Matko Andjelinic, matko.andjelinic@gmail.com 2004-09-09 (contributed OO implementation)
5  */  */
6    
 var debug = 0;  
7    
8  function bfilter_init() {  function BFilter(arr) {
9          show_status();          this.id_cache = Array();
10  }          // total number of hits
11            this.hits = 0;
12    
13            this.results_html = '';
14    
15            // this function is called for each result
16            this.result = function (arr) {
17                    this.results_html += '<li><a href="'+arr[1]+'">'+
18                            (this.hits % 2 == 0 ? '<span style="background: #e0e0e0;">' : '') +
19                            arr[0] +
20                            (this.hits % 2 == 0 ? '</span>' : '') +
21                            '</a></li>';
22                    return true;
23            }
24    
25            // this function is called when updating innerHTML with results
26            this.display = function () {
27                    if (this.results_html) {
28                            return '<ul>'+this.results_html+'</ul>';
29                    } else {
30                            return null;
31                    }
32            }
33    
34    
35            if (! arr) {
36                    this.debug("ERROR: can't search empty array");
37                    return;
38            }
39    
40            this.arr = arr;
41            this.debug("index has "+this.arr.length+" parts");
42    
43            if (! arr.min_len) {
44                    this.results("ERROR: index structure problem");
45                    return;
46            } else {
47                    this.min_len = arr.min_len;
48            }
49    
50            this.debug("index part has "+this.min_len+" parts");
51    
52            this.show_status();
53    
54  var id_cache = Array();  }
55    
56  function element_id(id) {  BFilter.prototype.element_id = function (id,no_alert) {
57          if (id_cache[id]) {          if (this.id_cache[id]) {
58                  return id_cache[id];                  return this.id_cache[id];
59          } else {          } else {
60                  var el = document.getElementById(id);                  var el = document.getElementById(id);
61                  if (el) {                  if (el) {
62                          id_cache[id] = el;                          this.id_cache[id] = el;
63                          return el;                          return el;
64                  } else {                  } else {
65                          alert("can't find element id: "+id);                          if (! no_alert) {
66                                    alert("can't find element id: "+id);
67                            } else {
68                                    // don't look it up again
69                                    this.id_cache[id] = null;
70                            }
71                  }                  }
72          }          }
73            return null;
74  }  }
75    
 // total number of hits  
 var hits = 0;  
76    
77  function show_status(status) {  BFilter.prototype.show_status = function (status) {
78          var html;          var html;
79          if (hits > 0) {          if (this.hits > 0) {
80                  html = "shown "+hits+" entries";                  html = "shown "+this.hits+" entries";
81          } else {          } else {
82                  html = "no results";                  html = "no results";
83          }          }
84          if (! status) {          if (! status) {
85                  html = "Enter "+min_len+" letter"+(min_len == 1 ? '' : 's')+" to filter entries";                  html = "Enter "+this.min_len+" letter"+(this.min_len == 1 ? '' : 's')+" to filter entries";
86                  status = "";                  status = "";
87          }          }
88    
89          var el = element_id("status");          var el = this.element_id("status");
90          el.innerHTML = html+status+"\n";          el.innerHTML = html+status+"\n";
91  }  }
92    
93  var wait = 0;  BFilter.prototype.results = function (html,clean) {
   
 function results(html,clean) {  
94    
95          if (! html) { html = ""; }          if (! html) { html = ""; }
96    
97          // results_div.style.cursor = 'wait'; // 'auto'          // results_div.style.cursor = 'wait'; // 'auto'
98          var results_div = element_id("results");          var results_div = this.element_id("results");
99          if (clean) {          if (clean) {
100                  results_div.innerHTML = html+"\n";                  results_div.innerHTML = html;
101                    this.results_html = '';
102          } else {          } else {
103                  results_div.innerHTML += html+"\n";                  html = this.display();
104                    if (html) results_div.innerHTML += html;
105          }          }
106  }  }
107    
108    
109    BFilter.prototype.debug = function (html) {
110    
111            //return;
112    
113            // check if debugging is on
114            if (! html) { return 1; }
115    
116            var debug_div = this.element_id("debug",1);
117            if (! debug_div) { return null; }
118    
119            debug_div.innerHTML += html + "<br>\n";
120    
121            return null;
122    }
123    
124    
125  // modified binary search to find first element with substring  // modified binary search to find first element with substring
126  function binarySearch(arr, find) {  BFilter.prototype.binarySearch = function (arr, find) {
127          if (!arr ||          if (!arr || typeof (find) == "undefined" || !arr.length) {
                 typeof (arr) != "object" ||  
                 typeof (find) == "undefined" || !arr.length) {  
128                  return null;                  return null;
129          }          }
130          var low = 0;          var low = 0;
# Line 74  function binarySearch(arr, find) { Line 135  function binarySearch(arr, find) {
135                  var mid = (low + high) / 2;                  var mid = (low + high) / 2;
136                  var aTry = (mid < 1) ? 0 : parseInt(mid);                  var aTry = (mid < 1) ? 0 : parseInt(mid);
137                    
138                  var curr = arr[aTry].substr(0,find.length).toLowerCase();                  var curr = arr[aTry][0].substr(0,find.length).toLowerCase();
139                  if (debug) { results("low="+low+" high="+high+" lastTry="+lastTry+" "+aTry+": "+curr+"<br>"); }                  this.debug("low="+low+" high="+high+" lastTry="+lastTry+" "+aTry+": "+curr);
140                  if (curr < find) {                  if (curr < find) {
141                          low = aTry + 1;                          low = aTry + 1;
142                          continue;                          continue;
# Line 91  function binarySearch(arr, find) { Line 152  function binarySearch(arr, find) {
152                  }                  }
153                  return aTry;                  return aTry;
154          }          }
155          if (debug) { results("lastTry="+lastTry+"<br>"); }          this.debug("lastTry="+lastTry);
156    
157          if (typeof (lastTry) != "undefined") {          if (typeof (lastTry) != "undefined") {
158                  return lastTry;                  return lastTry;
# Line 100  function binarySearch(arr, find) { Line 161  function binarySearch(arr, find) {
161          }          }
162  }  }
163    
164  function bfilter(document, id, find, arr) {  BFilter.prototype.filter = function (document, find) {
165    
166          results('',1);          this.results('',1);
167          hits = 0;          this.hits = 0;
168    
169          if (find.length < min_len) {          if (find.length < this.min_len) {
170                  show_status();                  this.show_status();
171                  return;                  return;
172          }          }
173    
174          if (debug) { results("filter: '"+find+"'<br>"); }          this.debug("filter: '"+find+"'");
175    
176          var find_lc = find.toLowerCase();          var find_lc = find.toLowerCase();
177    
178          var part = find_lc.substr(0,min_len);          var part = find_lc.substr(0,this.min_len);
179    
180          // no part found          // no part found
181          if (! arr[part]) {          if (! this.arr[part]) {
182                  show_status(" for <em>"+find+"</em><br>");                  this.show_status(" for <em>"+find+"</em><br>");
183                    this.debug("no part "+part);
184                  return;                  return;
185          }          }
186    
187          // start anim icon          // start anim icon
188          //results("<img  src=\"pie.gif\">&nbsp;Please wait, filtering...\n",1);          //results("<img  src=\"pie.gif\">&nbsp;Please wait, filtering...\n",1);
189    
190            var i;
191    
192          // full part? (optimization)          // full part? (optimization)
193          if (find.length == min_len) {          if (find.length == this.min_len) {
194                  var html = '';                  for (i = 0; i < this.arr[part].length; i++) {
195                  for (var i = 0; i < arr[part].length; i++) {                          this.result(this.arr[part][i]);
196                          html += "<li>";                          this.hits++;
                         if (debug) { $html += i+": "; }  
                         html += arr[part][i]+"</li>\n";  
                         hits++;  
197                  }                  }
198                  results(html);                  this.results();
199          } else {          } else {
200    
201                  var from = binarySearch(arr[part], find_lc);                  var from = this.binarySearch(this.arr[part], find_lc);
202    
203                  if (from != null) {                  if (from != null) {
204                                    
205                          if (debug) { results("loop "+from+" ... "+arr[part].length)+"<br>\n"; }                          this.debug("loop "+from+" ... "+this.arr[part].length);
   
                         var html = '';  
206    
207                          for(var i = from ; i < arr[part].length ; i++) {                          for(i = from ; i < this.arr[part].length ; i++) {
208                                  if (arr[part][i].substring(0,find.length).toLowerCase() != find_lc) {                                  if (this.arr[part][i][0].substring(0,find.length).toLowerCase() != find_lc) {
209                                            this.debug("loop exit at "+i);
210                                          break;                                          break;
211                                  }                                  }
212                                  if (debug) { html += i+": "; }                                  this.result(this.arr[part][i]);
213                                  html += arr[part][i]+"<br>\n";                                  this.hits++;
                                 hits++;  
214                          }                          }
215    
216                          results(html);                          this.results();
                 } else {  
                         // clean results list  
 //                      results("",1);  
217                  }                  }
218    
219          }          }
220    
221          show_status(" for <em>"+find+"</em>");          this.show_status(" for <em>"+find+"</em>");
222    
223  }  }
224    

Legend:
Removed from v.5  
changed lines
  Added in v.29

  ViewVC Help
Powered by ViewVC 1.1.26