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 a pair of contiguous datasets
017 */
018public class ContiguousPairIterator extends BroadcastIterator {
019        private final int aMax; // maximum index in array
020        private final int aStep; // step over items
021        private final int bMax; // maximum index in array
022        private final int bStep;
023        private final int oStep;
024
025        public ContiguousPairIterator(Dataset a, Dataset b, Dataset o, boolean createIfNull) {
026                super(a, b, o);
027                aStep = a.getElementsPerItem();
028                aMax = a.getSize() * aStep;
029                bStep = b.getElementsPerItem();
030                bMax = b.getSize() * bStep;
031                if (outputA) {
032                        oStep = aStep;
033                } else if (outputB) {
034                        oStep = bStep;
035                } else if (o != null) {
036                        oStep = o.getElementsPerItem();
037                } else if (createIfNull) {
038                        oDataset = BroadcastUtils.createDataset(a, b, a.getShapeRef());
039                        oStep = oDataset.getElementsPerItem();
040                } else {
041                        oStep = 1;
042                }
043                maxShape = a.getShape();
044                reset();
045        }
046
047        @Override
048        public boolean hasNext() {
049                aIndex += aStep;
050                bIndex += bStep;
051
052                if (outputA) {
053                        oIndex = aIndex;
054                } else if (outputB) {
055                        oIndex = bIndex;
056                } else {
057                        oIndex += oStep;
058                }
059
060                if (aIndex >= aMax || bIndex >= bMax) {
061                        return false;
062                }
063                if (read) {
064                        if (asDouble) {
065                                aDouble = aDataset.getElementDoubleAbs(aIndex);
066                                bDouble = bDataset.getElementDoubleAbs(bIndex);
067                        } else {
068                                aLong = aDataset.getElementLongAbs(aIndex);
069                                bLong = bDataset.getElementLongAbs(bIndex);
070                        }
071                }
072                return true;
073        }
074
075        @Override
076        public int[] getPos() {
077                return null;
078        }
079
080        @Override
081        public void reset() {
082                aIndex = -aStep;
083                bIndex = -bStep;
084                oIndex = -oStep;
085                if (read) {
086                        storeCurrentValues();
087                }
088        }
089}