001/*-
002 * Copyright 2016 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
012import java.util.Arrays;
013
014/**
015 * Base class for broadcast iterators where the second dataset could be broadcast to the first
016 * and it is also read into either bLong or bDouble fields.<p>
017 * For speed, there are public members. Note, index is not updated
018 */
019public abstract class BroadcastSelfIterator extends BroadcastIteratorBase {
020
021        /**
022         * @param a dataset to iterate over
023         * @param b dataset to iterate over (will broadcast to first)
024         * @return broadcast iterator
025         */
026        public static BroadcastSelfIterator createIterator(Dataset a, Dataset b) {
027                if (Arrays.equals(a.getShapeRef(), b.getShapeRef()) && a.getStrides() == null && b.getStrides() == null) {
028                        return a.getElementsPerItem() == 1 && b.getElementsPerItem() == 1 ? new ContiguousSingleIteratorElemental(a, b) : new ContiguousSingleIterator(a, b);
029                }
030                return new BroadcastSingleIterator(a, b);
031        }
032
033        /**
034         * @param a dataset to iterate over
035         * @param b dataset to iterate over (will broadcast to first)
036         */
037        protected BroadcastSelfIterator(Dataset a, Dataset b) {
038                super(a, b);
039                read = InterfaceUtils.isNumerical(b.getClass());
040                asDouble = aDataset.hasFloatingPointElements();
041                BroadcastUtils.checkItemSize(a, b, null);
042        }
043
044        @Override
045        protected void storeCurrentValues() {
046                if (bIndex >= 0) {
047                        if (asDouble) {
048                                bDouble = bDataset.getElementDoubleAbs(bIndex);
049                        } else {
050                                bLong = bDataset.getElementLongAbs(bIndex);
051                        }
052                }
053        }
054}