/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2012-2015 Emmanuel Keller / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.result.collector.collapsing;

import org.apache.commons.lang3.ArrayUtils;

import com.jaeksoft.searchlib.result.collector.AbstractBaseCollector;
import com.jaeksoft.searchlib.result.collector.CollapseDocInterface;
import com.jaeksoft.searchlib.result.collector.DocIdInterface;
import com.jaeksoft.searchlib.util.bitset.BitSetFactory;
import com.jaeksoft.searchlib.util.bitset.BitSetInterface;

public class CollapseBaseCollector extends
		AbstractBaseCollector<CollapseCollectorInterface> implements
		CollapseDocInterface, CollapseCollectorInterface, DocIdInterface {

	final int[] sourceIds;
	protected int totalCollapseCount = 0;
	protected final int[] ids;
	protected final int[][] collapseDocsArray;
	protected final int[] collapseCounts;
	protected int currentPos;
	protected int maxDoc;
	protected BitSetInterface bitSet;

	public CollapseBaseCollector(DocIdInterface sourceCollector, int size,
			boolean collectDocArray) {
		this.sourceIds = sourceCollector.getIds();
		this.totalCollapseCount = 0;
		this.collapseDocsArray = collectDocArray ? new int[size][] : null;
		this.collapseCounts = new int[size];
		this.ids = new int[size];
		this.currentPos = 0;
		this.maxDoc = sourceCollector.getMaxDoc();
		this.bitSet = null;
	}

	private CollapseBaseCollector(CollapseBaseCollector src) {
		this.sourceIds = src.sourceIds;
		this.totalCollapseCount = src.totalCollapseCount;
		this.collapseDocsArray = new int[src.collapseDocsArray.length][];
		int i = 0;
		for (int[] collDocArray : src.collapseDocsArray)
			this.collapseDocsArray[i++] = ArrayUtils.clone(collDocArray);
		this.collapseCounts = ArrayUtils.clone(src.collapseCounts);
		this.ids = ArrayUtils.clone(src.ids);
		this.currentPos = src.currentPos;
		this.maxDoc = src.maxDoc;
		this.bitSet = null;
	}

	@Override
	public CollapseBaseCollector duplicate(AbstractBaseCollector<?> base) {
		return new CollapseBaseCollector((CollapseBaseCollector) base);
	}

	@Override
	final public int collectDoc(final int sourcePos) {
		collapseCounts[currentPos] = 0;
		ids[currentPos] = sourceIds[sourcePos];
		int pos = currentPos;
		currentPos++;
		return pos;
	}

	@Override
	final public void endCollection() {
	}

	@Override
	final public void collectCollapsedDoc(final int sourcePos,
			final int collapsePos) {
		totalCollapseCount++;
		collapseCounts[collapsePos]++;
		if (collapseDocsArray == null)
			return;
		if (collapseDocsArray[collapsePos] == null)
			collapseDocsArray[collapsePos] = new int[] { sourceIds[sourcePos] };
		else
			collapseDocsArray[collapsePos] = ArrayUtils.add(
					collapseDocsArray[collapsePos], sourceIds[sourcePos]);
	}

	@Override
	public int getCollapsedCount() {
		return totalCollapseCount;
	}

	@Override
	final public void doSwap(final int pos1, final int pos2) {
		int id = ids[pos1];
		ids[pos1] = ids[pos2];
		ids[pos2] = id;

		int colCount = collapseCounts[pos1];
		collapseCounts[pos1] = collapseCounts[pos2];
		collapseCounts[pos2] = colCount;

		if (collapseDocsArray != null) {
			int[] colArray = collapseDocsArray[pos1];
			collapseDocsArray[pos1] = collapseDocsArray[pos2];
			collapseDocsArray[pos2] = colArray;
		}
	}

	@Override
	public int[] getIds() {
		return ids;
	}

	@Override
	public int getSize() {
		return currentPos;
	}

	@Override
	final public BitSetInterface getBitSet() {
		if (bitSet != null)
			return bitSet;
		bitSet = BitSetFactory.INSTANCE.newInstance(maxDoc);
		for (int id : ids)
			bitSet.set(id);
		return bitSet;
	}

	@Override
	public int[] getCollapseCounts() {
		return collapseCounts;
	}

	@Override
	public int[] getCollapsedDocs(long pos) {
		if (collapseDocsArray == null)
			return null;
		return collapseDocsArray[(int) pos];
	}

	@Override
	public int getMaxDoc() {
		return maxDoc;
	}

}

@@ -2275,16 +2275,56 @@
 eCount;%0A
+%09%09if (src.collapseDocsArray != null) %7B%0A%09
 %09%09this.c
@@ -2383,16 +2383,17 @@
 gth%5D%5B%5D;%0A
+%09
 %09%09int i 
@@ -2395,24 +2395,25 @@
 nt i = 0;%0A%09%09
+%09
 for (int%5B%5D c
@@ -2449,16 +2449,17 @@
 sArray)%0A
+%09
 %09%09%09this.
@@ -2515,16 +2515,65 @@
 Array);%0A
+%09%09%7D else %7B%0A%09%09%09this.collapseDocsArray = null;%0A%09%09%7D%0A
 %09%09this.c

Fix #1473