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

Contents of /trunk/bfilter.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations)
Tue Sep 7 09:29:36 2004 UTC (19 years, 7 months ago) by dpavlin
File MIME type: application/javascript
File size: 3128 byte(s)
cleanup

1 /*
2 Fast filtering function using pre-sorted list elements
3 Dobrica Pavlinusic, dpavlin@rot13.org 2004-09-06
4 */
5
6 var debug = 0;
7
8 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 // 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 if (typeof (lastTry) != "undefined") {
95 return lastTry;
96 } else {
97 return null;
98 }
99 }
100
101 function bfilter(document, id, find, arr) {
102
103 results('',1);
104 hits = 0;
105
106 if (find.length < min_len) {
107 show_status();
108 return;
109 }
110
111 if (debug) { results("filter: '"+find+"'<br>"); }
112
113 var find_lc = find.toLowerCase();
114
115 var part = find_lc.substr(0,min_len);
116
117 // no part found
118 if (! arr[part]) {
119 show_status(" for <em>"+find+"</em><br>");
120 return;
121 }
122
123 // start anim icon
124 //results("<img src=\"pie.gif\">&nbsp;Please wait, filtering...\n",1);
125
126 // full part? (optimization)
127 if (find.length == min_len) {
128 var html = '';
129 for (var i = 0; i < arr[part].length; i++) {
130 html += "<li>";
131 if (debug) { $html += i+": "; }
132 html += arr[part][i]+"</li>\n";
133 hits++;
134 }
135 results(html);
136 } else {
137
138 var from = binarySearch(arr[part], find_lc);
139
140 if (from != null) {
141
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++;
153 }
154
155 results(html);
156 }
157
158 }
159
160 show_status(" for <em>"+find+"</em>");
161
162 }
163

  ViewVC Help
Powered by ViewVC 1.1.26