Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Sort available domain names into groups by common prefixes.

  • Loading branch information...
commit bfa74b7c20da3587510047193e9d0a7c2c26c8cb 1 parent c364b79
Johann C. Rocholl authored March 09, 2010

Showing 1 changed file with 54 additions and 15 deletions. Show diff stats Hide diff stats

  1. 69  media/search.js
69  media/search.js
@@ -158,19 +158,7 @@ function group_row(keys) {
158 158
 	return html;
159 159
 }
160 160
 
161  
-function p_html(start, end) {
162  
-	if ($.keys.length == 0) return '';
163  
-	var groups = {};
164  
-	if (end > $.keys.length) end = $.keys.length;
165  
-	for (var index = start; index < end; index++) {
166  
-		var key = $.keys[index];
167  
-		var prefix = key.substr(0, 3);
168  
-		if (prefix in groups) {
169  
-			groups[prefix].push(key);
170  
-		} else {
171  
-			groups[prefix] = [key];
172  
-		}
173  
-	}
  161
+function p_html(groups) {
174 162
 	var html = '';
175 163
 	for (var prefix in groups) {
176 164
 		html += group_row(groups[prefix]);
@@ -178,6 +166,55 @@ function p_html(start, end) {
178 166
 	return html;
179 167
 }
180 168
 
  169
+function split_group(groups, old_prefix) {
  170
+	// Count all prefixes (old_prefix + one more letter).
  171
+	var old_length = old_prefix.length;
  172
+	var new_length = old_length + 1;
  173
+	var prefixes = {};
  174
+	for (var index in groups[old_prefix]) {
  175
+		var key = groups[old_prefix][index];
  176
+		prefix = key.substr(0, new_length);
  177
+		if (prefix in prefixes) prefixes[prefix] += 1;
  178
+		else prefixes[prefix] = 1;
  179
+	}
  180
+	// Find the prefix with the highest count.
  181
+	var best_count = 0;
  182
+	var best_prefix = '';
  183
+	for (var prefix in prefixes) {
  184
+		if (prefixes[prefix] > best_count) {
  185
+			best_count = prefixes[prefix];
  186
+			best_prefix = prefix;
  187
+		}
  188
+	}
  189
+	// Move keys with the best prefix to a new group.
  190
+	groups[best_prefix] = [];
  191
+	for (var index in groups[old_prefix]) {
  192
+		var key = groups[old_prefix][index];
  193
+		if (key.substr(0, new_length) == best_prefix) {
  194
+			groups[old_prefix].splice(index, 1);
  195
+			groups[best_prefix].push(key);
  196
+		}
  197
+	}
  198
+}
  199
+
  200
+function make_groups(groups, max_count) {
  201
+	while (true) {
  202
+		// Find the largest group.
  203
+		var best_count = 0;
  204
+		var best_prefix = '';
  205
+		for (var prefix in groups) {
  206
+			if (groups[prefix].length > best_count) {
  207
+				best_count = groups[prefix].length;
  208
+				best_prefix = prefix;
  209
+			}
  210
+		}
  211
+		// If the largest group is small enough, we're done.
  212
+		if (best_count <= max_count) break;
  213
+		// Split the largest group into two groups.
  214
+		split_group(groups, best_prefix);
  215
+	}
  216
+}
  217
+
181 218
 function update_html() {
182 219
 	if ($.ajax_search.left + $.ajax_search.right == '') {
183 220
 		$("div#results_div").hide();
@@ -191,8 +228,10 @@ function update_html() {
191 228
 	for (var key in $.domains) $.keys.push(key);
192 229
 	$.keys.sort(function(a, b) {
193 230
 		return $.domains[b].score - $.domains[a].score });
194  
-	$.ajax_search.showing = DEFAULT_LIMIT;
195  
-	var html = p_html(0, DEFAULT_LIMIT);
  231
+	var groups = {};
  232
+	groups[$.ajax_search.left] = $.keys;
  233
+	make_groups(groups, 10);
  234
+	var html = p_html(groups);
196 235
 	$("div#welcome").hide();
197 236
 	if ($.keys.length == 0) {
198 237
 		$("div#results_div").hide();

0 notes on commit bfa74b7

Please sign in to comment.
Something went wrong with that request. Please try again.