-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autocomplete.java
52 lines (41 loc) · 1.32 KB
/
Autocomplete.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Arrays;
/**
* Autocomplete.
*/
public class Autocomplete {
private Term[] terms;
/**
* Initializes a data structure from the given array of terms.
* This method throws a NullPointerException if terms is null.
*/
public Autocomplete(Term[] terms) {
if (terms == null) {
throw new NullPointerException();
}
this.terms = new Term[terms.length];
for (int i = 0; i < terms.length; i++) {
this.terms[i] = terms[i];
}
Arrays.sort(this.terms);
}
/**
* Returns all terms that start with the given prefix, in descending order of weight.
* This method throws a NullPointerException if prefix is null.
*/
public Term[] allMatches(String prefix) {
if (prefix == null) {
throw new NullPointerException();
}
int first = BinarySearch.firstIndexOf(terms, new Term(prefix, 0), Term.byPrefixOrder(prefix.length()));
if (first == -1) {
return new Term[0];
}
int last = BinarySearch.lastIndexOf (terms, new Term(prefix, 0), Term.byPrefixOrder(prefix.length()));
Term[] matched = new Term[1 + last - first];
for (int i = 0; i < matched.length; i++) {
matched[i] = terms[first++];
}
Arrays.sort(matched, Term.byDescendingWeightOrder());
return matched;
}
}