001/*-
002 * Copyright 2017 Diamond Light Source Ltd.
003 *
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
010package org.eclipse.january.dataset;
011
012/**
013 * Class to run over a pair of contiguous datasets
014 * @since 2.1
015 */
016public class BooleanContiguousIterator extends BooleanIterator {
017        private final int cMax; // maximum index in array
018        private final int cStep;
019
020        /**
021         * Construct a boolean iterator that stops at the position in choice dataset where its value matches
022         * the given boolean
023         * @param v boolean value
024         * @param a primary dataset
025         * @param c choice dataset
026         * @param o output dataset, can be null
027         * @param createIfNull if true create the output dataset if that is null
028         */
029        public BooleanContiguousIterator(boolean v, Dataset a, Dataset c, Dataset o, boolean createIfNull) {
030                super(v, a, c, o);
031                aMax = a.getSize() * aStep;
032                cStep = c.getElementsPerItem();
033                cMax = c.getSize() * cStep;
034                if (outputA) {
035                        oStep = aStep;
036                } else if (o != null) {
037                        oStep = o.getElementsPerItem();
038                } else if (createIfNull) {
039                        oDataset = BroadcastUtils.createDataset(a, c, a.getShapeRef());
040                        oStep = oDataset.getElementsPerItem();
041                } else {
042                        oStep = 1;
043                }
044                maxShape = a.getShape();
045                reset();
046        }
047
048        @Override
049        public boolean hasNext() {
050                do {
051                        index += aStep;
052                        cIndex += cStep;
053
054                        if (outputA) {
055                                oIndex = index;
056                        } else {
057                                oIndex += oStep;
058                        }
059
060                        if (index >= aMax || cIndex >= cMax) {
061                                return false;
062                        }
063                } while (cDataset.getElementBooleanAbs(cIndex) != value);
064
065                return true;
066        }
067
068        @Override
069        public int[] getPos() {
070                return null;
071        }
072
073        @Override
074        public void reset() {
075                index = -aStep;
076                cIndex = -cStep;
077                oIndex = -oStep;
078        }
079}