
package com.netflix.hollow.tools.diff.exact;

import com.netflix.hollow.core.util.IntList;
import com.netflix.hollow.core.util.LongList;

/**
 * Not intended for external consumption.
 */
public class CombinedMatchPairResultsIterator {

    private final LongList[] shardedResults;

    private int currentShardList;
    private int currentShardListPosition;

    private int currentFromOrdinal;
    private final IntList list;

    public CombinedMatchPairResultsIterator(LongList[] shardedResults) {
        this.shardedResults = shardedResults;
        this.list = new IntList();
    }

    public boolean next() {
        list.clear();
        
        while(currentShardList < shardedResults.length) {
            if(currentShardListPosition < shardedResults[currentShardList].size()) {
                currentFromOrdinal = (int)(shardedResults[currentShardList].get(currentShardListPosition) >> 32);
                while(currentShardListPosition < shardedResults[currentShardList].size()
                        && (int)(shardedResults[currentShardList].get(currentShardListPosition) >> 32) == currentFromOrdinal) {
                    int toOrdinal = (int)shardedResults[currentShardList].get(currentShardListPosition);
                    list.add(toOrdinal);
                    currentShardListPosition++;
                }
                return true;
            }
            
            currentShardListPosition = 0;
            currentShardList++;
        }
        
        return false;
     }

    public int fromOrdinal() {
        return currentFromOrdinal;
    }

    public IntList toOrdinals() {
        return list;
    }

}
