package jfoley.vocabpress.mem;

import ciir.jfoley.chai.collections.util.MapFns;
import jfoley.vocabpress.scoring.PositionsPosting;
import jfoley.vocabpress.scoring.blockiter.BlockPostingsIterator;
import jfoley.vocabpress.scoring.blockiter.ListBlockPostingsIterator;
import jfoley.vocabpress.scoring.impl.SimplePositionsPosting;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jfoley.
 */
public class MemoryPositionsIndex {
	public Map<Integer, List<PositionsPosting>> positions;
	public Map<Integer, String> corpus;

	public InternSpace<Integer, String> terms;
	public InternSpace<Integer, String> docNames;

	public int nextDocumentId = 0;

	public void addDocument(String documentName, List<String> termVector) {
		int documentId = nextDocumentId++;
		docNames.put(documentId, documentName);

		Map<Integer, List<Integer>> postings = new HashMap<>();
		for (int pos = 0; pos < termVector.size(); pos++) {
			String k = termVector.get(pos);
			int idk = terms.getId(k);
			MapFns.extendListInMap(postings, idk, pos);
		}

		for (Map.Entry<Integer, List<Integer>> kv : postings.entrySet()) {
			SimplePositionsPosting posting = new SimplePositionsPosting(nextDocumentId, kv.getValue());
			MapFns.extendListInMap(positions, kv.getKey(), posting);
		}
	}

	public BlockPostingsIterator<PositionsPosting> getPositions(String term) {
		Integer termId = terms.getId(term);
		if(termId == null) {
			return BlockPostingsIterator.EMPTY;
		}
		return new ListBlockPostingsIterator<PositionsPosting>(positions.get(termId));
	}
}

@@ -53,31 +53,111 @@
 hai.
-collections.util.MapFns
+Checked;%0Aimport ciir.jfoley.chai.collections.util.MapFns;%0Aimport jfoley.vocabpress.scoring.CountPosting
 ;%0Aim
@@ -1393,32 +1393,42 @@
 ostingsIterator%3C
+? extends 
 PositionsPosting
@@ -1528,16 +1528,29 @@
 %09return 
+Checked.cast(
 BlockPos
@@ -1568,16 +1568,17 @@
 or.EMPTY
+)
 ;%0A%09%09%7D%0A%09%09
@@ -1614,25 +1614,27 @@
 terator%3C
-P
+%3E(p
 ositions
 Posting%3E
@@ -1629,38 +1629,132 @@
 ions
-Posting%3E(positions.get
+.get(termId));%0A%09%7D%0A%0A%09public BlockPostingsIterator%3C? extends CountPosting%3E getCounts(String term) %7B%0A%09%09return getPositions
 (term
-Id)
 );%0A%09

Add getCounts, fix warning