@Test public void testStrings() { System.out.println("----- Strings -----"); String[] words = IndexNoun.words; LinearSearch<String> naive = new LinearSearch<>(words, new EditDistance(true)); long start = System.currentTimeMillis(); List<Neighbor<String, String>> neighbors = new ArrayList<>(); for (int i = 1000; i < 1100; i++) { naive.range(words[i], 1, neighbors); neighbors.clear(); } double time = (System.currentTimeMillis() - start) / 1000.0; System.out.format("String search: %.2fs%n", time); }
@Override public void range(T q, double radius, List<Neighbor<T, T>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } double[] dist = Arrays.stream(data).parallel().mapToDouble(x -> distance.d(q, x)).toArray(); for (int i = 0; i < data.length; i++) { if (dist[i] <= radius && q != data[i]) { neighbors.add(Neighbor.of(data[i], i, dist[i])); } } }
LinearSearch implements NearestNeighborSearch<T,T>, KNNSearch<T,T>, RNNSearch<T,T>, Serializable { @Override public void range(T q, double radius, List<Neighbor<T, T>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } double[] dist = Arrays.stream(data).parallel().mapToDouble(x -> distance.d(q, x)).toArray(); for (int i = 0; i < data.length; i++) { if (dist[i] <= radius && q != data[i]) { neighbors.add(Neighbor.of(data[i], i, dist[i])); } } } }
LinearSearch implements NearestNeighborSearch<T,T>, KNNSearch<T,T>, RNNSearch<T,T>, Serializable { @Override public void range(T q, double radius, List<Neighbor<T, T>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } double[] dist = Arrays.stream(data).parallel().mapToDouble(x -> distance.d(q, x)).toArray(); for (int i = 0; i < data.length; i++) { if (dist[i] <= radius && q != data[i]) { neighbors.add(Neighbor.of(data[i], i, dist[i])); } } } LinearSearch(T[] dataset, Distance<T> distance); }
LinearSearch implements NearestNeighborSearch<T,T>, KNNSearch<T,T>, RNNSearch<T,T>, Serializable { @Override public void range(T q, double radius, List<Neighbor<T, T>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } double[] dist = Arrays.stream(data).parallel().mapToDouble(x -> distance.d(q, x)).toArray(); for (int i = 0; i < data.length; i++) { if (dist[i] <= radius && q != data[i]) { neighbors.add(Neighbor.of(data[i], i, dist[i])); } } } LinearSearch(T[] dataset, Distance<T> distance); @Override String toString(); @Override Neighbor<T, T> nearest(T q); @Override @SuppressWarnings("unchecked") Neighbor<T, T>[] knn(T q, int k); @Override void range(T q, double radius, List<Neighbor<T, T>> neighbors); }
LinearSearch implements NearestNeighborSearch<T,T>, KNNSearch<T,T>, RNNSearch<T,T>, Serializable { @Override public void range(T q, double radius, List<Neighbor<T, T>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } double[] dist = Arrays.stream(data).parallel().mapToDouble(x -> distance.d(q, x)).toArray(); for (int i = 0; i < data.length; i++) { if (dist[i] <= radius && q != data[i]) { neighbors.add(Neighbor.of(data[i], i, dist[i])); } } } LinearSearch(T[] dataset, Distance<T> distance); @Override String toString(); @Override Neighbor<T, T> nearest(T q); @Override @SuppressWarnings("unchecked") Neighbor<T, T>[] knn(T q, int k); @Override void range(T q, double radius, List<Neighbor<T, T>> neighbors);  }