public Iterator<Index> allAfter(double x) { if (data.size() == 0) { return iterator(0, 0); } else { for (int i = 1; i < data.size(); i++) { if (data.get(i).centroids[0] >= x) { Page previous = data.get(i - 1); for (int j = 0; j < previous.active; j++) { if (previous.centroids[j] > x) { return iterator(i - 1, j); } } return iterator(i, 0); } } Page last = data.get(data.size() - 1); for (int j = 0; j < last.active; j++) { if (last.centroids[j] > x) { return iterator(data.size() - 1, j); } } return iterator(data.size(), 0); } }
@Override public Iterable<? extends Centroid> centroids() { List<Centroid> r = new ArrayList<Centroid>(); Iterator<Index> ix = iterator(0, 0); while (ix.hasNext()) { Index index = ix.next(); Page current = data.get(index.page); Centroid centroid = new Centroid(current.centroids[index.subPage], current.counts[index.subPage]); if (current.history != null) { for (double x : current.history.get(index.subPage)) { centroid.insertData(x); } } r.add(centroid); } return r; }
@Override public void compress() { ArrayDigest reduced = new ArrayDigest(pageSize, compression); if (recordAllData) { reduced.recordAllData(); } List<Index> tmp = new ArrayList<Index>(); Iterator<Index> ix = this.iterator(0, 0); while (ix.hasNext()) { tmp.add(ix.next()); } Collections.shuffle(tmp, gen); for (Index index : tmp) { reduced.add(mean(index), count(index)); } data = reduced.data; centroidCount = reduced.centroidCount; }
/** * Returns an iterator which will give each element <= to x in non-increasing order. * * @param x The upper bound of all returned elements * @return An iterator that returns elements in non-increasing order. */ public Iterator<Index> allBefore(double x) { if (data.size() == 0) { return iterator(0, 0); } else { for (int i = 1; i < data.size(); i++) { if (data.get(i).centroids[0] > x) { Page previous = data.get(i - 1); for (int j = 0; j < previous.active; j++) { if (previous.centroids[j] > x) { return reverse(i - 1, j - 1); } } return reverse(i, -1); } } Page last = data.get(data.size() - 1); for (int j = 0; j < last.active; j++) { if (last.centroids[j] > x) { return reverse(data.size() - 1, j - 1); } } return reverse(data.size(), -1); } }
Iterator<Index> it = iterator(0, 0); Index a = it.next();