void addRaw(double x, int w, List<Double> history) { if (centroidCount == 0) { Page page = new Page(pageSize, recordAllData); page.add(x, w, history); totalWeight += w; centroidCount++; data.add(page); } else { for (int i = 1; i < data.size(); i++) { if (data.get(i).centroids[0] > x) { Page newPage = data.get(i - 1).add(x, w, history); totalWeight += w; centroidCount++; if (newPage != null) { data.add(i, newPage); } return; } } Page newPage = data.get(data.size() - 1).add(x, w, history); totalWeight += w; centroidCount++; if (newPage != null) { data.add(data.size(), newPage); } } }
public Page add(double x, int w, List<Double> history) { for (int i = 0; i < active; i++) { if (centroids[i] >= x) { // insert at i if (active >= pageSize) { // split page Page newPage = split(); if (i < pageSize / 2) { addAt(i, x, w, history); } else { newPage.addAt(i - pageSize / 2, x, w, history); } return newPage; } else { addAt(i, x, w, history); return null; } } } // insert at end if (active >= pageSize) { // split page Page newPage = split(); newPage.addAt(newPage.active, x, w, history); return newPage; } else { addAt(active, x, w, history); return null; } }
private Page split() { assert active == pageSize; final int half = pageSize / 2; Page newPage = new Page(pageSize, recordAllData); System.arraycopy(centroids, half, newPage.centroids, 0, pageSize - half); System.arraycopy(counts, half, newPage.counts, 0, pageSize - half); if (history != null) { newPage.history = new ArrayList<List<Double>>(); newPage.history.addAll(history.subList(half, pageSize)); List<List<Double>> tmp = new ArrayList<List<Double>>(); tmp.addAll(history.subList(0, half)); history = tmp; } active = half; newPage.active = pageSize - half; newPage.totalCount = totalCount; totalCount = 0; for (int i = 0; i < half; i++) { totalCount += counts[i]; newPage.totalCount -= counts[i]; } return newPage; }
private void delete(Index index) { // don't want to delete empty pages here because other indexes would be screwed up. // this should almost never happen anyway since deletes only cause small ordering // changes totalWeight -= count(index); centroidCount--; data.get(index.page).delete(index.subPage); }