    001/*-
    002 *******************************************************************************
    003 * Copyright (c) 2011, 2016 Diamond Light Source Ltd.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the Eclipse Public License v1.0
    006 * which accompanies this distribution, and is available at
    007 * http://www.eclipse.org/legal/epl-v10.html
    008 *
    009 * Contributors:
    010 *    Peter Chang - initial API and implementation and/or initial documentation
    011 *******************************************************************************/
    012
    013package org.eclipse.january.dataset;
    014
    015/**
    016 * Class to run over an iterator and visits positions where items in selection dataset are true
    017 */
    018public class BooleanIterator extends IndexIterator {
    019        final private BooleanDataset b;
    020        final private IndexIterator iterb;
    021        final private IndexIterator iterd;
    022        final private boolean v;
    023        final private int[] pos; // position in dataset
    024
    025        /**
    026         * Constructor for an iterator over the items of a boolean dataset that are
    027         * true
    028         *
    029         * @param iter dataset iterator
    030         * @param selection boolean dataset
    031         */
    032        public BooleanIterator(final IndexIterator iter, final Dataset selection) {
    033                this(iter, selection, true);
    034        }
    035
    036        /**
    037         * Constructor for an iterator over the items of a boolean dataset that match
    038         * given value
    039         *
    040         * @param iter dataset iterator
    041         * @param selection boolean dataset
    042         * @param value
    043         */
    044        public BooleanIterator(final IndexIterator iter, final Dataset selection, boolean value) {
    045                b = (BooleanDataset) DatasetUtils.cast(selection, Dataset.BOOL);
    046
    047                iterb = selection.getIterator();
    048                iterd = iter;
    049                pos = iterd.getPos();
    050                v = value;
    051        }
    052
    053        @Override
    054        public boolean hasNext() {
    055                while (iterb.hasNext() && iterd.hasNext()) {
    056                        if (b.getAbs(iterb.index) == v) {
    057                                index = iterd.index;
    058                                return true;
    059                        }
    060                }
    061                return false;
    062        }
    063
    064        @Override
    065        public int[] getPos() {
    066                return pos;
    067        }
    068
    069        @Override
    070        public void reset() {
    071                iterb.reset();
    072                iterd.reset();
    073        }
    074}