    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 integer dataset and return its items
    017 */
    018public class IntegerIterator extends IndexIterator {
    019        final private IntegerDataset indices;
    020        final private IndexIterator iter;
    021        final private int istep; // step over items
    022        final private int imax; // maximum index in array
    023
    024        /**
    025         * Constructor for an iterator over the items of an integer dataset
    026         *
    027         * @param index integer dataset
    028         * @param length of entire data array
    029         */
    030        public IntegerIterator(final Dataset index, final int length) {
    031                this(index, length, 1);
    032        }
    033
    034        /**
    035         * Constructor for an iterator over the items of an integer dataset
    036         *
    037         * @param index integer dataset
    038         * @param length of entire data array
    039         * @param isize number of elements in an item
    040         */
    041        public IntegerIterator(final Dataset index, final int length, final int isize) {
    042                indices = (IntegerDataset) (index instanceof IntegerDataset ? index : DatasetUtils.convertToDataset(index).cast(Dataset.INT32));
    043                iter = index.getIterator();
    044                istep = isize;
    045                imax = length*istep;
    046        }
    047
    048        @Override
    049        public boolean hasNext() {
    050                while (iter.hasNext()) {
    051                        index = istep*indices.getAbs(iter.index);
    052                        if (index < 0) {
    053                                index += imax;
    054                        }
    055                        return true;
    056                }
    057                return false;
    058        }
    059
    060        @Override
    061        public int[] getPos() {
    062                return null;
    063        }
    064
    065        @Override
    066        public void reset() {
    067                iter.reset();
    068        }
    069}