/** * Given a table, generate the Next K items in Sorted Order starting from a specified * rowSnapShot (topRow), together with counts. * @param data The input table on which we want to compute the NextK list. * @return A NextKList. */ @Override public NextKList create(ITable data) { IndexComparator comp = this.recordOrder.getComparator(data); IntTreeTopK topK = new IntTreeTopK(this.maxSize, comp); IRowIterator rowIt = data.getRowIterator(); int i = rowIt.getNextRow(); int position = 0; VirtualRowSnapshot vw = new VirtualRowSnapshot(data); while (i >= 0) { vw.setRow(i); if ((this.topRow == null) || (this.topRow.compareTo(vw, this.recordOrder) <= 0)) topK.push(i); else position++; i = rowIt.getNextRow(); } Int2IntSortedMap topKList = topK.getTopK(); IRowOrder rowOrder = new ArrayRowOrder(topKList.keySet()); SmallTable topKRows = data.compress(this.recordOrder.toSubSchema(), rowOrder); List<Integer> count = new ArrayList<Integer>(topKList.size()); count.addAll(topKList.values()); return new NextKList(topKRows, count, position, data.getNumOfRows()); }
public Int2IntSortedMap getTopK() { final Int2IntSortedMap finalMap = new Int2IntRBTreeMap(this.greater); this.data.forEach((k, v) -> finalMap.put(k.intValue(), v.get())); return finalMap; }