/[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

Annotation of /trunk/bfilter.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Tue Sep 7 08:37:14 2004 UTC (19 years, 7 months ago) by dpavlin
File MIME type: application/javascript
File size: 3110 byte(s)
when filtering use modified binary search to find first element

1 dpavlin 1 /*
2     Fast filtering function using pre-sorted list elements
3     Dobrica Pavlinusic, dpavlin@rot13.org 2004-09-06
4     */
5    
6 dpavlin 2 var debug = 1;
7    
8 dpavlin 1 function bfilter_init() {
9     show_status();
10     }
11    
12     var id_cache = Array();
13    
14     function element_id(id) {
15     if (id_cache[id]) {
16     return id_cache[id];
17     } else {
18     var el = document.getElementById(id);
19     if (el) {
20     id_cache[id] = el;
21     return el;
22     } else {
23     alert("can't find element id: "+id);
24     }
25     }
26     }
27    
28     // total number of hits
29     var hits = 0;
30    
31     function show_status(status) {
32     var html;
33     if (hits > 0) {
34     html = "shown "+hits+" entries";
35     } else {
36     html = "no results";
37     }
38     if (! status) {
39     html = "Enter "+min_len+" letter"+(min_len == 1 ? '' : 's')+" to filter entries";
40     status = "";
41     }
42    
43     var el = element_id("status");
44     el.innerHTML = html+status+"\n";
45     }
46    
47     var wait = 0;
48    
49     function results(html,clean) {
50    
51     if (! html) { html = ""; }
52    
53     // results_div.style.cursor = 'wait'; // 'auto'
54     var results_div = element_id("results");
55     if (clean) {
56     results_div.innerHTML = html+"\n";
57     } else {
58     results_div.innerHTML += html+"\n";
59     }
60     }
61    
62 dpavlin 2 // 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 dpavlin 1
96 dpavlin 2 if (typeof (lastTry) != "undefined") {
97     return lastTry;
98     } else {
99     return null;
100     }
101     }
102    
103 dpavlin 1 function bfilter(document, id, find, arr) {
104    
105     results('',1);
106     hits = 0;
107    
108     if (find.length < min_len) {
109     show_status();
110     return;
111     }
112    
113     if (debug) { results("filter: '"+find+"'<br>"); }
114    
115     var find_lc = find.toLowerCase();
116    
117     var part = find_lc.substr(0,min_len);
118    
119     // no part found
120     if (! arr[part]) {
121     show_status(" for <em>"+find+"</em><br>");
122     return;
123     }
124    
125     // start anim icon
126     //results("<img src=\"pie.gif\">&nbsp;Please wait, filtering...\n",1);
127    
128     // full part? (optimization)
129     if (find.length == min_len) {
130     var html = '';
131     for (var i = 0; i < arr[part].length; i++) {
132 dpavlin 2 html += "<li>"+i+": "+arr[part][i]+"</li>\n";
133 dpavlin 1 hits++;
134     }
135     results(html);
136     } else {
137    
138 dpavlin 2 var from = binarySearch(arr[part], find_lc);
139 dpavlin 1
140 dpavlin 2 if (from != null) {
141    
142     if (debug) { results("loop "+from+" ... "+arr[part].length)+"<br>\n"; }
143     for(var i = from ; i < arr[part].length ; i++) {
144     if (arr[part][i].substring(0,find.length).toLowerCase() != find_lc) {
145     break;
146     }
147     results(i+": "+arr[part][i]+"<br>\n");
148 dpavlin 1 hits++;
149     }
150 dpavlin 2 } else {
151     // clean results list
152     // results("",1);
153 dpavlin 1 }
154    
155     }
156    
157     show_status(" for <em>"+find+"</em>");
158    
159     }
160    

  ViewVC Help
Powered by ViewVC 1.1.26