    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
    013// This is generated from CompoundDoubleDataset.java by fromcpddouble.py
    014
    015package org.eclipse.january.dataset;
    016
    017import java.util.Arrays;
    018
    019import org.apache.commons.math3.complex.Complex;
    020
    021/**
    022 * Extend compound dataset for int values // PRIM_TYPE
    023 */
    024public class CompoundIntegerDataset extends AbstractCompoundDataset {
    025        // pin UID to base class
    026        private static final long serialVersionUID = Dataset.serialVersionUID;
    027
    028        protected int[] data; // subclass alias // PRIM_TYPE
    029
    030        @Override
    031        protected void setData() {
    032                data = (int[]) odata; // PRIM_TYPE
    033        }
    034
    035        protected int[] createArray(final int size) { // PRIM_TYPE
    036                int[] array = null; // PRIM_TYPE
    037
    038                try {
    039                        array = new int[isize * size]; // PRIM_TYPE
    040                } catch (OutOfMemoryError e) {
    041                        logger.error("The size of the dataset ({}) that is being created is too large "
    042                                        + "and there is not enough memory to hold it.", size);
    043                        throw new OutOfMemoryError("The dimensions given are too large, and there is "
    044                                        + "not enough memory available in the Java Virtual Machine");
    045                }
    046                return array;
    047        }
    048
    049        @Override
    050        public int getDType() {
    051                return Dataset.ARRAYINT32; // DATA_TYPE
    052        }
    053
    054        /**
    055         * Create a null dataset
    056         */
    057        CompoundIntegerDataset() {
    058        }
    059
    060        /**
    061         * Create a null dataset
    062         * @param itemSize
    063         */
    064        CompoundIntegerDataset(final int itemSize) {
    065                isize = itemSize;
    066        }
    067
    068        /**
    069         * Create a zero-filled dataset of given item size and shape
    070         * @param itemSize
    071         * @param shape
    072         */
    073        CompoundIntegerDataset(final int itemSize, final int[] shape) {
    074                isize = itemSize;
    075                if (shape != null) {
    076                        size = ShapeUtils.calcSize(shape);
    077                        this.shape = shape.clone();
    078
    079                        try {
    080                                odata = data = createArray(size);
    081                        } catch (Throwable t) {
    082                                logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    083                                throw new IllegalArgumentException(t);
    084                        }
    085                }
    086        }
    087
    088        /**
    089         * Copy a dataset
    090         * @param dataset
    091         */
    092        CompoundIntegerDataset(final CompoundIntegerDataset dataset) {
    093                isize = dataset.isize;
    094
    095                copyToView(dataset, this, true, true);
    096                try {
    097                        if (dataset.stride == null) {
    098                                if (dataset.data != null) {
    099                                        odata = data = dataset.data.clone();
    100                                }
    101                        } else {
    102                                offset = 0;
    103                                stride = null;
    104                                base = null;
    105                                odata = data = createArray(size);
    106                                IndexIterator iter = dataset.getIterator();
    107                                for (int j = 0; iter.hasNext();) {
    108                                        for (int i = 0; i < isize; i++) {
    109                                                data[j++] = dataset.data[iter.index + i];
    110                                        }
    111                                }
    112                        }
    113                } catch (Throwable t) {
    114                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    115                        throw new IllegalArgumentException(t);
    116                }
    117        }
    118
    119        /**
    120         * Create a dataset using given dataset
    121         * @param dataset
    122         */
    123        CompoundIntegerDataset(final CompoundDataset dataset) {
    124                copyToView(dataset, this, true, false);
    125                offset = 0;
    126                stride = null;
    127                base = null;
    128                isize = dataset.getElementsPerItem();
    129                try {
    130                        odata = data = createArray(size);
    131                } catch (Throwable t) {
    132                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    133                        throw new IllegalArgumentException(t);
    134                }
    135
    136                IndexIterator iter = dataset.getIterator();
    137                for (int j = 0; iter.hasNext();) {
    138                        for (int i = 0; i < isize; i++) {
    139                                data[j++] = (int) dataset.getElementLongAbs(iter.index + i); // GET_ELEMENT_WITH_CAST
    140                        }
    141                }
    142        }
    143
    144        /**
    145         * Create a dataset using given data (elements are grouped together)
    146         * @param itemSize
    147         * @param data
    148         * @param shape
    149         *            (can be null to create 1D dataset)
    150         */
    151        CompoundIntegerDataset(final int itemSize, final int[] data, int... shape) { // PRIM_TYPE
    152                isize = itemSize;
    153                if (data != null) {
    154                        if (shape == null || (shape.length == 0 && data.length > isize)) {
    155                                shape = new int[] { data.length / isize };
    156                        }
    157                        size = ShapeUtils.calcSize(shape);
    158                        if (size * isize != data.length) {
    159                                throw new IllegalArgumentException(String.format("Shape %s is not compatible with size of data array, %d",
    160                                                Arrays.toString(shape), data.length / isize));
    161                        }
    162                        this.shape = size == 0 ? null : shape.clone();
    163
    164                        odata = this.data = data;
    165                }
    166        }
    167
    168        /**
    169         * Create a dataset using given datasets
    170         * @param datasets
    171         */
    172        CompoundIntegerDataset(final Dataset... datasets) {
    173                if (datasets.length < 1) {
    174                        throw new IllegalArgumentException("Array of datasets must have length greater than zero");
    175                }
    176
    177                for (int i = 1; i < datasets.length; i++) {
    178                        datasets[0].checkCompatibility(datasets[i]);
    179                }
    180
    181                isize = datasets.length;
    182                size = ShapeUtils.calcSize(datasets[0].getShapeRef());
    183                shape = datasets[0].getShape();
    184
    185                try {
    186                        odata = data = createArray(size);
    187                } catch (Throwable t) {
    188                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    189                        throw new IllegalArgumentException(t);
    190                }
    191
    192                IndexIterator[] iters = new IndexIterator[isize];
    193                for (int i = 0; i < datasets.length; i++) {
    194                        iters[i] = datasets[i].getIterator();
    195                }
    196
    197                for (int j = 0; iters[0].hasNext();) {
    198                        data[j++] = (int) datasets[0].getElementLongAbs(iters[0].index); // GET_ELEMENT_WITH_CAST
    199                        for (int i = 1; i < datasets.length; i++) {
    200                                iters[i].hasNext();
    201                                data[j++] = (int) datasets[i].getElementLongAbs(iters[i].index); // GET_ELEMENT_WITH_CAST
    202                        }
    203                }
    204        }
    205
    206        /**
    207         * Cast a dataset to this compound type. If repeat is set, the first element of each item in the given dataset is
    208         * repeated across all elements of an item. Otherwise, each item comprises a truncated or zero-padded copy of
    209         * elements from the given dataset.
    210         * @param itemSize
    211         * @param repeat
    212         *            repeat first element
    213         * @param dataset
    214         */
    215        CompoundIntegerDataset(final int itemSize, final boolean repeat, final Dataset dataset) {
    216                isize = itemSize;
    217                size = dataset.getSize();
    218                shape = dataset.getShape();
    219                name = new String(dataset.getName());
    220
    221                try {
    222                        odata = data = createArray(size);
    223                } catch (Throwable t) {
    224                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    225                        throw new IllegalArgumentException(t);
    226                }
    227                final int os = dataset.getElementsPerItem();
    228
    229                IndexIterator iter = dataset.getIterator();
    230                if (repeat) {
    231                        int i = 0;
    232                        while (iter.hasNext()) {
    233                                final int v = (int) dataset.getElementLongAbs(iter.index); // PRIM_TYPE // GET_ELEMENT_WITH_CAST
    234                                for (int k = 0; k < isize; k++) {
    235                                        data[i++] = v;
    236                                }
    237                        }
    238                } else {
    239                        final int kmax = Math.min(isize, os);
    240                        int i = 0;
    241                        while (iter.hasNext()) {
    242                                for (int k = 0; k < kmax; k++) {
    243                                        data[i + k] = (int) dataset.getElementLongAbs(iter.index + k); // GET_ELEMENT_WITH_CAST
    244                                }
    245                                i += isize;
    246                        }
    247                }
    248        }
    249
    250        @Override
    251        public boolean equals(Object obj) {
    252                if (this == obj) {
    253                        return true;
    254                }
    255
    256                if (obj == null) {
    257                        return false;
    258                }
    259
    260                if (!getClass().equals(obj.getClass())) {
    261                        if (getRank() == 0) { // for zero-rank datasets
    262                                return obj.equals(getObjectAbs(offset));
    263                        }
    264                        return false;
    265                }
    266
    267                CompoundIntegerDataset other = (CompoundIntegerDataset) obj;
    268                if (isize != other.isize) {
    269                        return false;
    270                }
    271                if (size != other.size) {
    272                        return false;
    273                }
    274                if (!Arrays.equals(shape, other.shape)) {
    275                        return false;
    276                }
    277                if (data == other.data && stride == null && other.stride == null) {
    278                        return true;
    279                }
    280
    281                IndexIterator iter = getIterator();
    282                IndexIterator oiter = other.getIterator();
    283                while (iter.hasNext() && oiter.hasNext()) {
    284                        for (int j = 0; j < isize; j++) {
    285                                if (data[iter.index+j] != other.data[oiter.index+j]) {
    286                                        return false;
    287                                }
    288                        }
    289                }
    290                return true;
    291        }
    292
    293        @Override
    294        public int hashCode() {
    295                return super.hashCode();
    296        }
    297
    298        @Override
    299        public CompoundIntegerDataset clone() {
    300                return new CompoundIntegerDataset(this);
    301        }
    302
    303        /**
    304         * Create a dataset from an object which could be a Java list, array (of arrays...) or Number. Ragged
    305         * sequences or arrays are padded with zeros. The item size is the last dimension of the corresponding
    306         * elemental dataset
    307         *
    308         * @param obj
    309         * @return dataset with contents given by input
    310         */
    311        static CompoundIntegerDataset createFromObject(final Object obj) {
    312                IntegerDataset result = IntegerDataset.createFromObject(obj); // CLASS_TYPE
    313                return createCompoundDatasetWithLastDimension(result, true);
    314        }
    315
    316        /**
    317         * Create a 1D dataset from an object which could be a Java list, array (of arrays...) or Number. Ragged
    318         * sequences or arrays are padded with zeros.
    319         *
    320         * @param itemSize
    321         * @param obj
    322         * @return dataset with contents given by input
    323         */
    324        public static CompoundIntegerDataset createFromObject(final int itemSize, final Object obj) {
    325                IntegerDataset result = IntegerDataset.createFromObject(obj); // CLASS_TYPE
    326                boolean zeroRank = result.shape == null ? false : result.shape.length == 0;
    327                if (zeroRank) {
    328                        result.resize(itemSize); // special case of single item
    329                        result.fill(obj);
    330                }
    331                CompoundIntegerDataset ds = new CompoundIntegerDataset(itemSize, result.getData(), null);
    332                if (zeroRank) {
    333                        ds.setShape(new int[0]);
    334                }
    335                return ds;
    336        }
    337
    338        /**
    339         * @param stop
    340         * @return a new 1D dataset, filled with values determined by parameters
    341         */
    342        static CompoundIntegerDataset createRange(final int itemSize, final double stop) {
    343                return createRange(itemSize, 0., stop, 1.);
    344        }
    345
    346        /**
    347         * @param start
    348         * @param stop
    349         * @param step
    350         * @return a new 1D dataset, filled with values determined by parameters
    351         */
    352        static CompoundIntegerDataset createRange(final int itemSize, final double start, final double stop,
    353                        final double step) {
    354                int size = calcSteps(start, stop, step);
    355                CompoundIntegerDataset result = new CompoundIntegerDataset(itemSize, new int[] { size });
    356                for (int i = 0; i < size; i++) {
    357                        result.data[i * result.isize] = (int) (start + i * step); // PRIM_TYPE // ADD_CAST
    358                }
    359                return result;
    360        }
    361
    362        /**
    363         * @param shape
    364         * @return a dataset filled with ones
    365         */
    366        static CompoundIntegerDataset ones(final int itemSize, final int... shape) {
    367                return new CompoundIntegerDataset(itemSize, shape).fill(1);
    368        }
    369
    370        /**
    371         * Create a compound dataset using last dimension of given dataset
    372         * @param a
    373         * @param shareData if true, then share data when possible otherwise copy it
    374         * @return compound dataset
    375         */
    376        public static CompoundIntegerDataset createCompoundDatasetWithLastDimension(final Dataset a, final boolean shareData) {
    377                if (a.getElementsPerItem() != 1) {
    378                        logger.error("Need a single-element dataset");
    379                        throw new IllegalArgumentException("Need a single-element dataset");
    380                }
    381                if (a.getDType() != Dataset.INT32) { // DATA_TYPE
    382                        logger.error("Dataset type must be int"); // PRIM_TYPE
    383                        throw new IllegalArgumentException("Dataset type must be int"); // PRIM_TYPE
    384                }
    385
    386                final int[] shape = a.getShapeRef();
    387                if (shape == null) {
    388                        return new CompoundIntegerDataset(0);
    389                }
    390
    391                final int rank = shape.length - 1;
    392                final int is = rank < 0 ? 1 : shape[rank];
    393
    394                CompoundIntegerDataset result = new CompoundIntegerDataset(is);
    395
    396                result.shape = rank > 0 ? Arrays.copyOf(shape, rank) : (rank < 0 ? new int[0] : new int[] {1});
    397                result.size = ShapeUtils.calcSize(result.shape);
    398                result.odata = shareData ? a.flatten().getBuffer() : a.clone().getBuffer();
    399                result.setName(a.getName());
    400                result.setData();
    401                return result;
    402        }
    403
    404        @Override
    405        public IntegerDataset asNonCompoundDataset(final boolean shareData) { // CLASS_TYPE
    406                IntegerDataset result = new IntegerDataset(); // CLASS_TYPE
    407                final int is = getElementsPerItem();
    408                final int rank = is == 1 ? shape.length : shape.length + 1;
    409                final int[] nshape = Arrays.copyOf(shape, rank);
    410                if (is != 1)
    411                        nshape[rank-1] = is;
    412
    413                result.shape = nshape;
    414                result.size = ShapeUtils.calcSize(nshape);
    415                result.odata = shareData && isContiguous() ? data : clone().getBuffer();
    416                result.setName(name);
    417                result.setData();
    418                return result;
    419        }
    420
    421        @Override
    422        public CompoundIntegerDataset fill(Object obj) {
    423                setDirty();
    424                if (obj instanceof Complex) {
    425                        obj = new Complex(((Complex) obj).getReal(), 0);
    426                }
    427                int[] vr = DTypeUtils.toIntegerArray(obj, isize); // PRIM_TYPE // CLASS_TYPE
    428                IndexIterator iter = getIterator();
    429
    430                while (iter.hasNext()) {
    431                        for (int i = 0; i < isize; i++) {
    432                                data[iter.index + i] = vr[i]; // PRIM_TYPE
    433                        }
    434                }
    435
    436                return this;
    437        }
    438
    439        /**
    440         * This is a typed version of {@link #getBuffer()}
    441         * @return data buffer as linear array
    442         */
    443        public int[] getData() { // PRIM_TYPE
    444                return data;
    445        }
    446
    447        @Override
    448        protected int getBufferLength() {
    449                if (data == null)
    450                        return 0;
    451                return data.length;
    452        }
    453
    454        @Override
    455        public CompoundIntegerDataset getView(boolean deepCopyMetadata) {
    456                CompoundIntegerDataset view = new CompoundIntegerDataset(isize);
    457                copyToView(this, view, true, deepCopyMetadata);
    458                view.setData();
    459                return view;
    460        }
    461
    462        /**
    463         * Get values at absolute index in the internal array. This is an internal method with no checks so can be
    464         * dangerous. Use with care or ideally with an iterator.
    465         *
    466         * @param index
    467         *            absolute index
    468         * @return values
    469         */
    470        public int[] getAbs(final int index) { // PRIM_TYPE
    471                int[] result = new int[isize]; // PRIM_TYPE
    472                for (int i = 0; i < isize; i++) {
    473                        result[i] = data[index + i];
    474                }
    475                return result;
    476        }
    477
    478        /**
    479         * Get values at absolute index in the internal array. This is an internal method with no checks so can be
    480         * dangerous. Use with care or ideally with an iterator.
    481         *
    482         * @param index
    483         *            absolute index
    484         * @param values
    485         */
    486        public void getAbs(final int index, final int[] values) { // PRIM_TYPE
    487                for (int i = 0; i < isize; i++) {
    488                        values[i] = data[index + i];
    489                }
    490        }
    491
    492        @Override
    493        public boolean getElementBooleanAbs(final int index) {
    494                return data[index] != 0;
    495        }
    496
    497        @Override
    498        public double getElementDoubleAbs(final int index) {
    499                return data[index];
    500        }
    501
    502        @Override
    503        public long getElementLongAbs(final int index) {
    504                return data[index]; // OMIT_TOLONG_INT
    505        }
    506
    507        @Override
    508        protected void setItemDirect(final int dindex, final int sindex, final Object src) {
    509                setDirty();
    510                int[] dsrc = (int[]) src; // PRIM_TYPE
    511                for (int i = 0; i < isize; i++) {
    512                        data[dindex + i] = dsrc[sindex + i];
    513                }
    514        }
    515
    516        /**
    517         * Set values at absolute index in the internal array. This is an internal method with no checks so can be
    518         * dangerous. Use with care or ideally with an iterator.
    519         *
    520         * @param index
    521         *            absolute index
    522         * @param val
    523         *            new values
    524         */
    525        public void setAbs(final int index, final int[] val) { // PRIM_TYPE
    526                setDirty();
    527                for (int i = 0; i < isize; i++) {
    528                        data[index + i] = val[i];
    529                }
    530        }
    531
    532        /**
    533         * Set element value at absolute index in the internal array. This is an internal method with no checks so can be
    534         * dangerous. Use with care or ideally with an iterator.
    535         *
    536         * @param index
    537         *            absolute index
    538         * @param val
    539         *            new value
    540         */
    541        public void setAbs(final int index, final int val) { // PRIM_TYPE
    542                setDirty();
    543                data[index] = val;
    544        }
    545
    546        @Override
    547        public Object getObject() {
    548                return getIntArray(); // PRIM_TYPE
    549        }
    550
    551        @Override
    552        public Object getObject(final int i) {
    553                return getIntArray(i); // PRIM_TYPE
    554        }
    555
    556        @Override
    557        public Object getObject(final int i, final int j) {
    558                return getIntArray(i, j); // PRIM_TYPE
    559        }
    560
    561        @Override
    562        public Object getObject(final int... pos) {
    563                return getIntArray(pos); // PRIM_TYPE
    564        }
    565
    566        @Override
    567        public byte[] getByteArray() {
    568                byte[] result = new byte[isize];
    569                int index = getFirst1DIndex();
    570                for (int k = 0; k < isize; k++) {
    571                        result[k] = (byte) data[index + k]; // OMIT_UPCAST
    572                }
    573                return result;
    574        }
    575
    576        @Override
    577        public byte[] getByteArray(final int i) {
    578                byte[] result = new byte[isize];
    579                int index = get1DIndex(i);
    580                for (int k = 0; k < isize; k++) {
    581                        result[k] = (byte) data[index + k]; // OMIT_UPCAST
    582                }
    583                return result;
    584        }
    585
    586        @Override
    587        public byte[] getByteArray(final int i, final int j) {
    588                byte[] result = new byte[isize];
    589                int index = get1DIndex(i, j);
    590                for (int k = 0; k < isize; k++) {
    591                        result[k] = (byte) data[index + k]; // OMIT_UPCAST
    592                }
    593                return result;
    594        }
    595
    596        @Override
    597        public byte[] getByteArray(final int... pos) {
    598                byte[] result = new byte[isize];
    599                int index = get1DIndex(pos);
    600                for (int k = 0; k < isize; k++) {
    601                        result[k] = (byte) data[index + k]; // OMIT_UPCAST
    602                }
    603                return result;
    604        }
    605
    606        @Override
    607        public short[] getShortArray() {
    608                short[] result = new short[isize];
    609                int index = getFirst1DIndex();
    610                for (int k = 0; k < isize; k++) {
    611                        result[k] = (short) data[index + k]; // OMIT_UPCAST
    612                }
    613                return result;
    614        }
    615
    616        @Override
    617        public short[] getShortArray(final int i) {
    618                short[] result = new short[isize];
    619                int index = get1DIndex(i);
    620                for (int k = 0; k < isize; k++) {
    621                        result[k] = (short) data[index + k]; // OMIT_UPCAST
    622                }
    623                return result;
    624        }
    625
    626        @Override
    627        public short[] getShortArray(final int i, final int j) {
    628                short[] result = new short[isize];
    629                int index = get1DIndex(i, j);
    630                for (int k = 0; k < isize; k++) {
    631                        result[k] = (short) data[index + k]; // OMIT_UPCAST
    632                }
    633                return result;
    634        }
    635
    636        @Override
    637        public short[] getShortArray(final int... pos) {
    638                short[] result = new short[isize];
    639                int index = get1DIndex(pos);
    640                for (int k = 0; k < isize; k++) {
    641                        result[k] = (short) data[index + k]; // OMIT_UPCAST
    642                }
    643                return result;
    644        }
    645
    646        @Override
    647        public int[] getIntArray() {
    648                int[] result = new int[isize];
    649                int index = getFirst1DIndex();
    650                for (int k = 0; k < isize; k++) {
    651                        result[k] = data[index + k]; // OMIT_UPCAST
    652                }
    653                return result;
    654        }
    655
    656        @Override
    657        public int[] getIntArray(final int i) {
    658                int[] result = new int[isize];
    659                int index = get1DIndex(i);
    660                for (int k = 0; k < isize; k++) {
    661                        result[k] = data[index + k]; // OMIT_UPCAST
    662                }
    663                return result;
    664        }
    665
    666        @Override
    667        public int[] getIntArray(final int i, final int j) {
    668                int[] result = new int[isize];
    669                int index = get1DIndex(i, j);
    670                for (int k = 0; k < isize; k++) {
    671                        result[k] = data[index + k]; // OMIT_UPCAST
    672                }
    673                return result;
    674        }
    675
    676        @Override
    677        public int[] getIntArray(final int... pos) {
    678                int[] result = new int[isize];
    679                int index = get1DIndex(pos);
    680                for (int k = 0; k < isize; k++) {
    681                        result[k] = data[index + k]; // OMIT_UPCAST
    682                }
    683                return result;
    684        }
    685
    686        @Override
    687        public long[] getLongArray() {
    688                long[] result = new long[isize];
    689                int index = getFirst1DIndex();
    690                for (int k = 0; k < isize; k++) {
    691                        result[k] = data[index + k]; // OMIT_UPCAST
    692                }
    693                return result;
    694        }
    695
    696        @Override
    697        public long[] getLongArray(final int i) {
    698                long[] result = new long[isize];
    699                int index = get1DIndex(i);
    700                for (int k = 0; k < isize; k++) {
    701                        result[k] = data[index + k]; // OMIT_UPCAST
    702                }
    703                return result;
    704        }
    705
    706        @Override
    707        public long[] getLongArray(final int i, final int j) {
    708                long[] result = new long[isize];
    709                int index = get1DIndex(i, j);
    710                for (int k = 0; k < isize; k++) {
    711                        result[k] = data[index + k]; // OMIT_UPCAST
    712                }
    713                return result;
    714        }
    715
    716        @Override
    717        public long[] getLongArray(final int... pos) {
    718                long[] result = new long[isize];
    719                int index = get1DIndex(pos);
    720                for (int k = 0; k < isize; k++) {
    721                        result[k] = data[index + k]; // OMIT_UPCAST
    722                }
    723                return result;
    724        }
    725
    726        @Override
    727        public float[] getFloatArray() {
    728                float[] result = new float[isize];
    729                int index = getFirst1DIndex();
    730                for (int k = 0; k < isize; k++) {
    731                        result[k] = data[index + k]; // OMIT_REAL_CAST
    732                }
    733                return result;
    734        }
    735
    736        @Override
    737        public float[] getFloatArray(final int i) {
    738                float[] result = new float[isize];
    739                int index = get1DIndex(i);
    740                for (int k = 0; k < isize; k++) {
    741                        result[k] = data[index + k]; // OMIT_REAL_CAST
    742                }
    743                return result;
    744        }
    745
    746        @Override
    747        public float[] getFloatArray(final int i, final int j) {
    748                float[] result = new float[isize];
    749                int index = get1DIndex(i, j);
    750                for (int k = 0; k < isize; k++) {
    751                        result[k] = data[index + k]; // OMIT_REAL_CAST
    752                }
    753                return result;
    754        }
    755
    756        @Override
    757        public float[] getFloatArray(final int... pos) {
    758                float[] result = new float[isize];
    759                int index = get1DIndex(pos);
    760                for (int k = 0; k < isize; k++) {
    761                        result[k] = data[index + k]; // OMIT_REAL_CAST
    762                }
    763                return result;
    764        }
    765
    766        @Override
    767        public double[] getDoubleArray() {
    768                double[] result = new double[isize];
    769                int index = getFirst1DIndex();
    770                for (int k = 0; k < isize; k++) {
    771                        result[k] = data[index + k]; // OMIT_REAL_CAST
    772                }
    773                return result;
    774        }
    775
    776        @Override
    777        public double[] getDoubleArray(final int i) {
    778                double[] result = new double[isize];
    779                int index = get1DIndex(i);
    780                for (int k = 0; k < isize; k++) {
    781                        result[k] = data[index + k]; // OMIT_REAL_CAST
    782                }
    783                return result;
    784        }
    785
    786        @Override
    787        public double[] getDoubleArray(final int i, final int j) {
    788                double[] result = new double[isize];
    789                int index = get1DIndex(i, j);
    790                for (int k = 0; k < isize; k++) {
    791                        result[k] = data[index + k]; // OMIT_REAL_CAST
    792                }
    793                return result;
    794        }
    795
    796        @Override
    797        public double[] getDoubleArray(final int... pos) {
    798                double[] result = new double[isize];
    799                int index = get1DIndex(pos);
    800                for (int k = 0; k < isize; k++) {
    801                        result[k] = data[index + k]; // OMIT_REAL_CAST
    802                }
    803                return result;
    804        }
    805
    806        @Override
    807        public void getDoubleArrayAbs(final int index, final double[] darray) {
    808                for (int i = 0; i < isize; i++) {
    809                        darray[i] = data[index + i];
    810                }
    811        }
    812
    813        @Override
    814        public String getString() {
    815                return getStringAbs(getFirst1DIndex());
    816        }
    817
    818        @Override
    819        public String getString(final int i) {
    820                return getStringAbs(get1DIndex(i));
    821        }
    822
    823        @Override
    824        public String getString(final int i, final int j) {
    825                return getStringAbs(get1DIndex(i, j));
    826        }
    827
    828        @Override
    829        public String getString(final int... pos) {
    830                return getStringAbs(get1DIndex(pos));
    831        }
    832
    833        @Override
    834        protected double getFirstValue() {
    835                return data[getFirst1DIndex()];
    836        }
    837
    838        @Override
    839        protected double getFirstValue(int i) {
    840                return data[get1DIndex(i)];
    841        }
    842
    843        @Override
    844        protected double getFirstValue(int i, int j) {
    845                return data[get1DIndex(i, j)];
    846        }
    847
    848        @Override
    849        protected double getFirstValue(final int... pos) {
    850                return data[get1DIndex(pos)];
    851        }
    852
    853        @Override
    854        public Object getObjectAbs(final int index) {
    855                int[] result = new int[isize]; // PRIM_TYPE
    856                for (int i = 0; i < isize; i++) {
    857                        result[i] = data[index + i];
    858                }
    859                return result;
    860        }
    861
    862        @Override
    863        public String getStringAbs(final int index) {
    864                StringBuilder s = new StringBuilder();
    865                s.append('(');
    866                s.append(stringFormat == null ? String.format("%d", data[index]) : // FORMAT_STRING
    867                        stringFormat.format(data[index]));
    868                for (int i = 1; i < isize; i++) {
    869                        s.append(' ');
    870                        s.append(stringFormat == null ? String.format("%d", data[index + i]) : // FORMAT_STRING
    871                                stringFormat.format(data[index + i]));
    872                }
    873                s.append(')');
    874                return s.toString();
    875        }
    876
    877        @Override
    878        public void setObjectAbs(final int index, Object obj) {
    879                if (obj instanceof Complex) {
    880                        obj = new Complex(((Complex) obj).getReal(), 0);
    881                }
    882                int[] oa = DTypeUtils.toIntegerArray(obj, isize); // PRIM_TYPE // CLASS_TYPE
    883                setAbs(index, oa);
    884        }
    885
    886        @Override
    887        public void set(final Object obj) {
    888                setItem(DTypeUtils.toIntegerArray(obj, isize)); // CLASS_TYPE
    889        }
    890
    891        @Override
    892        public void set(final Object obj, final int i) {
    893                setItem(DTypeUtils.toIntegerArray(obj, isize), i); // CLASS_TYPE
    894        }
    895
    896        @Override
    897        public void set(final Object obj, final int i, final int j) {
    898                setItem(DTypeUtils.toIntegerArray(obj, isize), i, j); // CLASS_TYPE
    899        }
    900
    901        @Override
    902        public void set(final Object obj, int... pos) {
    903                if (pos == null || (pos.length == 0 && shape.length > 0)) {
    904                        pos = new int[shape.length];
    905                }
    906
    907                setItem(DTypeUtils.toIntegerArray(obj, isize), pos); // CLASS_TYPE
    908        }
    909
    910        /**
    911         * Set values at first position. The dataset must not be null
    912         *
    913         * @param d
    914         * @since 2.0
    915         */
    916        public void setItem(final int[] d) { // PRIM_TYPE
    917                if (d.length > isize) {
    918                        throw new IllegalArgumentException("Array is larger than number of elements in an item");
    919                }
    920                setAbs(getFirst1DIndex(), d);
    921        }
    922
    923        /**
    924         * Set values at given position. The dataset must be 1D
    925         *
    926         * @param d
    927         * @param i
    928         */
    929        public void setItem(final int[] d, final int i) { // PRIM_TYPE
    930                if (d.length > isize) {
    931                        throw new IllegalArgumentException("Array is larger than number of elements in an item");
    932                }
    933                setAbs(get1DIndex(i), d);
    934        }
    935
    936        /**
    937         * Set values at given position. The dataset must be 1D
    938         *
    939         * @param d
    940         * @param i
    941         * @param j
    942         */
    943        public void setItem(final int[] d, final int i, final int j) { // PRIM_TYPE
    944                if (d.length > isize) {
    945                        throw new IllegalArgumentException("Array is larger than number of elements in an item");
    946                }
    947                setAbs(get1DIndex(i, j), d);
    948        }
    949
    950        /**
    951         * Set values at given position
    952         *
    953         * @param d
    954         * @param pos
    955         */
    956        public void setItem(final int[] d, final int... pos) { // PRIM_TYPE
    957                if (d.length > isize) {
    958                        throw new IllegalArgumentException("Array is larger than number of elements in an item");
    959                }
    960                setAbs(get1DIndex(pos), d);
    961        }
    962
    963        private void setDoubleArrayAbs(final int index, final double[] d) {
    964                for (int i = 0; i < isize; i++)
    965                        data[index + i] = (int) d[i]; // ADD_CAST
    966        }
    967
    968        @Override
    969        public void resize(int... newShape) {
    970                setDirty();
    971                IndexIterator iter = getIterator();
    972                int nsize = ShapeUtils.calcSize(newShape);
    973                int[] ndata; // PRIM_TYPE
    974                try {
    975                        ndata = createArray(nsize);
    976                } catch (Throwable t) {
    977                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    978                        throw new IllegalArgumentException(t);
    979                }
    980
    981                int i = 0;
    982                while (iter.hasNext() && i < nsize) {
    983                        for (int j = 0; j < isize; j++) {
    984                                ndata[i++] = data[iter.index + j];
    985                        }
    986                }
    987
    988                odata = data = ndata;
    989                size = nsize;
    990                shape = newShape;
    991                stride = null;
    992                offset = 0;
    993                base = null;
    994        }
    995
    996        @Override
    997        public CompoundIntegerDataset getSlice(final SliceIterator siter) {
    998                CompoundIntegerDataset result = new CompoundIntegerDataset(isize, siter.getShape());
    999                int[] rdata = result.data; // PRIM_TYPE
    1000                IndexIterator riter = result.getIterator();
    1001
    1002                while (siter.hasNext() && riter.hasNext()) {
    1003                        for (int i = 0; i < isize; i++)
    1004                                rdata[riter.index + i] = data[siter.index + i];
    1005                }
    1006
    1007                result.setName(name + BLOCK_OPEN + Slice.createString(siter.shape, siter.start, siter.stop, siter.step) + BLOCK_CLOSE);
    1008                return result;
    1009        }
    1010
    1011        @Override
    1012        public IntegerDataset getElementsView(int element) { // CLASS_TYPE
    1013                if (element < 0)
    1014                        element += isize;
    1015                if (element < 0 || element > isize) {
    1016                        throw new IllegalArgumentException(String.format("Invalid choice of element: %d/%d", element, isize));
    1017                }
    1018
    1019                IntegerDataset view = new IntegerDataset(shape); // CLASS_TYPE
    1020
    1021                copyToView(this, view, true, true);
    1022                view.setData();
    1023                if (view.stride == null) {
    1024                        int[] offset = new int[1];
    1025                        view.stride = createStrides(this, offset);
    1026                        view.offset = offset[0] + element;
    1027                        view.base = base == null ? this : base;
    1028                } else {
    1029                        view.offset += element;
    1030                }
    1031
    1032                return view;
    1033        }
    1034
    1035        @Override
    1036        public IntegerDataset getElements(int element) { // CLASS_TYPE
    1037                final IntegerDataset elements = new IntegerDataset(shape); // CLASS_TYPE
    1038
    1039                copyElements(elements, element);
    1040                return elements;
    1041        }
    1042
    1043        @Override
    1044        public void copyElements(Dataset destination, int element) {
    1045                if (element < 0)
    1046                        element += isize;
    1047                if (element < 0 || element > isize) {
    1048                        throw new IllegalArgumentException(String.format("Invalid choice of element: %d/%d", element, isize));
    1049                }
    1050                if (getElementClass() != destination.getElementClass()) {
    1051                        throw new IllegalArgumentException("Element class of destination does not match this dataset");
    1052                }
    1053
    1054                final IndexIterator it = getIterator(element);
    1055                final int[] elements = ((IntegerDataset) destination).data; // CLASS_TYPE // PRIM_TYPE
    1056                destination.setDirty();
    1057
    1058                int n = 0;
    1059                while (it.hasNext()) {
    1060                        elements[n] = data[it.index];
    1061                        n++;
    1062                }
    1063        }
    1064
    1065        @Override
    1066        public void setElements(Dataset source, int element) {
    1067                setDirty();
    1068                if (element < 0)
    1069                        element += isize;
    1070                if (element < 0 || element > isize) {
    1071                        throw new IllegalArgumentException(String.format("Invalid choice of element: %d/%d", element, isize));
    1072                }
    1073                if (getElementClass() != source.getElementClass()) {
    1074                        throw new IllegalArgumentException("Element class of destination does not match this dataset");
    1075                }
    1076
    1077                final IndexIterator it = getIterator(element);
    1078                final int[] elements = ((IntegerDataset) source).data; // CLASS_TYPE // PRIM_TYPE
    1079
    1080                int n = 0;
    1081                while (it.hasNext()) {
    1082                        data[it.index] = elements[n];
    1083                        n++;
    1084                }
    1085        }
    1086
    1087        @Override
    1088        public void fillDataset(Dataset result, IndexIterator iter) {
    1089                IndexIterator riter = result.getIterator();
    1090                result.setDirty();
    1091
    1092                int[] rdata = ((CompoundIntegerDataset) result).data; // PRIM_TYPE
    1093
    1094                while (riter.hasNext() && iter.hasNext()) {
    1095                        for (int i = 0; i < isize; i++) {
    1096                                rdata[riter.index + i] = data[iter.index + i];
    1097                        }
    1098                }
    1099        }
    1100
    1101        @Override
    1102        public CompoundIntegerDataset setByBoolean(final Object o, Dataset selection) {
    1103                setDirty();
    1104                if (o instanceof Dataset) {
    1105                        Dataset ds = (Dataset) o;
    1106                        final int length = ((Number) selection.sum()).intValue();
    1107                        if (length != ds.getSize()) {
    1108                                throw new IllegalArgumentException(
    1109                                                "Number of true items in selection does not match number of items in dataset");
    1110                        }
    1111
    1112                        IndexIterator iter = ds.getIterator();
    1113                        BooleanIterator biter = getBooleanIterator(selection);
    1114
    1115                        if (ds instanceof AbstractCompoundDataset) {
    1116                                if (isize != ds.getElementsPerItem()) {
    1117                                        throw new IllegalArgumentException("Input dataset is not compatible with slice");
    1118                                }
    1119
    1120                                while (biter.hasNext() && iter.hasNext()) {
    1121                                        for (int i = 0; i < isize; i++) {
    1122                                                data[biter.index + i] = (int) ds.getElementLongAbs(iter.index + i); // GET_ELEMENT_WITH_CAST
    1123                                        }
    1124                                }
    1125                        } else {
    1126                                while (biter.hasNext() && iter.hasNext()) {
    1127                                        data[biter.index] = (int) ds.getElementLongAbs(iter.index); // GET_ELEMENT_WITH_CAST
    1128                                        for (int i = 1; i < isize; i++) {
    1129                                                data[biter.index + i] = 0;
    1130                                        }
    1131                                }
    1132                        }
    1133                } else {
    1134                        try {
    1135                                final int[] vr = DTypeUtils.toIntegerArray(o, isize); // PRIM_TYPE // CLASS_TYPE
    1136
    1137                                final BooleanIterator biter = getBooleanIterator(selection);
    1138
    1139                                while (biter.hasNext()) {
    1140                                        for (int i = 0; i < isize; i++) {
    1141                                                data[biter.index + i] = vr[i];
    1142                                        }
    1143                                }
    1144                        } catch (IllegalArgumentException e) {
    1145                                throw new IllegalArgumentException("Object for setting is not a dataset or number");
    1146                        }
    1147                }
    1148                return this;
    1149        }
    1150
    1151        @Override
    1152        public CompoundIntegerDataset setBy1DIndex(final Object o, Dataset index) {
    1153                setDirty();
    1154                if (o instanceof Dataset) {
    1155                        Dataset ds = (Dataset) o;
    1156                        if (index.getSize() != ds.getSize()) {
    1157                                throw new IllegalArgumentException(
    1158                                                "Number of items in selection does not match number of items in dataset");
    1159                        }
    1160
    1161                        IndexIterator oiter = ds.getIterator();
    1162                        final IntegerIterator iter = new IntegerIterator(index, size, isize);
    1163
    1164                        if (ds instanceof AbstractCompoundDataset) {
    1165                                if (isize != ds.getElementsPerItem()) {
    1166                                        throw new IllegalArgumentException("Input dataset is not compatible with slice");
    1167                                }
    1168
    1169                                double[] temp = new double[isize];
    1170                                while (iter.hasNext() && oiter.hasNext()) {
    1171                                        ((AbstractCompoundDataset) ds).getDoubleArrayAbs(oiter.index, temp);
    1172                                        setDoubleArrayAbs(iter.index, temp);
    1173                                }
    1174                                while (iter.hasNext() && oiter.hasNext()) {
    1175                                        for (int i = 0; i < isize; i++) {
    1176                                                data[iter.index + i] = (int) ds.getElementLongAbs(oiter.index + i); // GET_ELEMENT_WITH_CAST
    1177                                        }
    1178                                }
    1179                        } else {
    1180                                while (iter.hasNext() && oiter.hasNext()) {
    1181                                        data[iter.index] = (int) ds.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    1182                                        for (int i = 1; i < isize; i++) {
    1183                                                data[iter.index + i] = 0;
    1184                                        }
    1185                                }
    1186                        }
    1187                } else {
    1188                        try {
    1189                                final int[] vr = DTypeUtils.toIntegerArray(o, isize); // PRIM_TYPE // CLASS_TYPE
    1190
    1191                                final IntegerIterator iter = new IntegerIterator(index, size, isize);
    1192
    1193                                while (iter.hasNext()) {
    1194                                        setAbs(iter.index, vr);
    1195                                }
    1196                        } catch (IllegalArgumentException e) {
    1197                                throw new IllegalArgumentException("Object for setting is not a dataset or number");
    1198                        }
    1199                }
    1200                return this;
    1201        }
    1202
    1203        @Override
    1204        public CompoundIntegerDataset setByIndexes(final Object o, final Object... indexes) {
    1205                setDirty();
    1206                final IntegersIterator iter = new IntegersIterator(shape, indexes);
    1207                final int[] pos = iter.getPos();
    1208
    1209                if (o instanceof Dataset) {
    1210                        Dataset ds = (Dataset) o;
    1211                        if (ShapeUtils.calcSize(iter.getShape()) != ds.getSize()) {
    1212                                throw new IllegalArgumentException(
    1213                                                "Number of items in selection does not match number of items in dataset");
    1214                        }
    1215
    1216                        IndexIterator oiter = ds.getIterator();
    1217
    1218                        if (ds instanceof AbstractCompoundDataset) {
    1219                                if (isize != ds.getElementsPerItem()) {
    1220                                        throw new IllegalArgumentException("Input dataset is not compatible with slice");
    1221                                }
    1222
    1223                                double[] temp = new double[isize];
    1224                                while (iter.hasNext() && oiter.hasNext()) {
    1225                                        ((AbstractCompoundDataset) ds).getDoubleArray(temp, pos);
    1226                                        setDoubleArrayAbs(get1DIndex(pos), temp);
    1227                                }
    1228                        } else {
    1229                                while (iter.hasNext() && oiter.hasNext()) {
    1230                                        int n = get1DIndex(pos);
    1231                                        data[n] = (int) ds.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    1232                                        for (int i = 1; i < isize; i++) {
    1233                                                data[n + i] = 0;
    1234                                        }
    1235                                }
    1236                        }
    1237                } else {
    1238                        try {
    1239                                final int[] vr = DTypeUtils.toIntegerArray(o, isize); // PRIM_TYPE // CLASS_TYPE
    1240
    1241                                while (iter.hasNext()) {
    1242                                        setAbs(get1DIndex(pos), vr);
    1243                                }
    1244                        } catch (IllegalArgumentException e) {
    1245                                throw new IllegalArgumentException("Object for setting is not a dataset or number");
    1246                        }
    1247                }
    1248                return this;
    1249        }
    1250
    1251        @Override
    1252        CompoundIntegerDataset setSlicedView(Dataset view, Dataset d) {
    1253                setDirty();
    1254                final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(view, d);
    1255
    1256                final int is = view.getElementsPerItem();
    1257
    1258                if (is > 1) {
    1259                        if (d.getElementsPerItem() == 1) {
    1260                                while (it.hasNext()) {
    1261                                        final int bv = (int) it.bLong; // PRIM_TYPE // BCAST_WITH_CAST d.getElementLongAbs(it.bIndex);
    1262                                        data[it.aIndex] = bv;
    1263                                        for (int j = 1; j < is; j++) {
    1264                                                data[it.aIndex + j] = bv;
    1265                                        }
    1266                                }
    1267                        } else {
    1268                                while (it.hasNext()) {
    1269                                        data[it.aIndex] = (int) it.bLong; // BCAST_WITH_CAST d.getElementLongAbs(it.bIndex);
    1270                                        for (int j = 1; j < is; j++) {
    1271                                                data[it.aIndex + j] = (int) d.getElementLongAbs(it.bIndex + j); // GET_ELEMENT_WITH_CAST
    1272                                        }
    1273                                }
    1274                        }
    1275                } else {
    1276                        while (it.hasNext()) {
    1277                                data[it.aIndex] = (int) it.bLong; // BCAST_WITH_CAST d.getElementLongAbs(it.bIndex);
    1278                        }
    1279                }
    1280                return this;
    1281        }
    1282
    1283        @Override
    1284        public CompoundIntegerDataset setSlice(final Object o, final IndexIterator siter) {
    1285                setDirty();
    1286                if (o instanceof IDataset) {
    1287                        final IDataset ds = (IDataset) o;
    1288                        final int[] oshape = ds.getShape();
    1289
    1290                        if (!ShapeUtils.areShapesCompatible(siter.getShape(), oshape)) {
    1291                                throw new IllegalArgumentException(String.format(
    1292                                                "Input dataset is not compatible with slice: %s cf %s", Arrays.toString(oshape),
    1293                                                Arrays.toString(siter.getShape())));
    1294                        }
    1295
    1296                        if (ds instanceof Dataset) {
    1297                                final Dataset ads = (Dataset) ds;
    1298                                IndexIterator oiter = ads.getIterator();
    1299
    1300                                if (ds instanceof AbstractCompoundDataset) {
    1301                                        if (isize != ads.getElementsPerItem()) {
    1302                                                throw new IllegalArgumentException("Input dataset is not compatible with slice");
    1303                                        }
    1304
    1305                                        while (siter.hasNext() && oiter.hasNext()) {
    1306                                                for (int i = 0; i < isize; i++) {
    1307                                                        data[siter.index + i] = (int) ads.getElementLongAbs(oiter.index + i); // GET_ELEMENT_WITH_CAST
    1308                                                }
    1309                                        }
    1310                                } else {
    1311                                        while (siter.hasNext() && oiter.hasNext()) {
    1312                                                data[siter.index] = (int) ads.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    1313                                                for (int i = 1; i < isize; i++) {
    1314                                                        data[siter.index + i] = 0;
    1315                                                }
    1316                                        }
    1317                                }
    1318                        } else {
    1319                                final IndexIterator oiter = new PositionIterator(oshape);
    1320                                final int[] pos = oiter.getPos();
    1321
    1322                                if (ds.getElementsPerItem() == 1) {
    1323                                        while (siter.hasNext() && oiter.hasNext()) {
    1324                                                data[siter.index] = ds.getInt(pos); // PRIM_TYPE
    1325                                                for (int i = 1; i < isize; i++) {
    1326                                                        data[siter.index + i] = 0;
    1327                                                }
    1328                                        }
    1329                                } else {
    1330                                        while (siter.hasNext() && oiter.hasNext()) {
    1331                                                final int[] val = DTypeUtils.toIntegerArray(ds.getObject(pos), isize); // PRIM_TYPE // CLASS_TYPE
    1332                                                for (int i = 0; i < isize; i++) {
    1333                                                        data[siter.index + i] = val[i];
    1334                                                }
    1335                                        }
    1336                                }
    1337                        }
    1338                } else {
    1339                        try {
    1340                                final int[] vr = DTypeUtils.toIntegerArray(o, isize); // PRIM_TYPE // CLASS_TYPE
    1341
    1342                                while (siter.hasNext()) {
    1343                                        for (int i = 0; i < isize; i++) {
    1344                                                data[siter.index + i] = vr[i];
    1345                                        }
    1346                                }
    1347                        } catch (IllegalArgumentException e) {
    1348                                throw new IllegalArgumentException("Object for setting slice is not a dataset or number");
    1349                        }
    1350                }
    1351                return this;
    1352        }
    1353
    1354        @Override
    1355        public void copyItemsFromAxes(final int[] pos, final boolean[] axes, final Dataset dest) {
    1356                int[] ddata = (int[]) dest.getBuffer(); // PRIM_TYPE
    1357
    1358                if (dest.getElementsPerItem() != isize) {
    1359                        throw new IllegalArgumentException(String.format(
    1360                                        "Destination dataset is incompatible as it has %d elements per item not %d",
    1361                                        dest.getElementsPerItem(), isize));
    1362                }
    1363
    1364                SliceIterator siter = getSliceIteratorFromAxes(pos, axes);
    1365                int[] sshape = ShapeUtils.squeezeShape(siter.getShape(), false);
    1366
    1367                IndexIterator diter = dest.getSliceIterator(null, sshape, null);
    1368
    1369                if (ddata.length < ShapeUtils.calcSize(sshape)) {
    1370                        throw new IllegalArgumentException("destination array is not large enough");
    1371                }
    1372
    1373                dest.setDirty();
    1374                while (siter.hasNext() && diter.hasNext()) {
    1375                        for (int i = 0; i < isize; i++) {
    1376                                ddata[diter.index + i] = data[siter.index + i];
    1377                        }
    1378                }
    1379        }
    1380
    1381        @Override
    1382        public void setItemsOnAxes(final int[] pos, final boolean[] axes, final Object src) {
    1383                setDirty();
    1384                int[] sdata = (int[]) src; // PRIM_TYPE
    1385
    1386                SliceIterator siter = getSliceIteratorFromAxes(pos, axes);
    1387
    1388                if (sdata.length < ShapeUtils.calcSize(siter.getShape())) {
    1389                        throw new IllegalArgumentException("source array is not large enough");
    1390                }
    1391
    1392                for (int i = 0; siter.hasNext(); i++) {
    1393                        for (int j = 0; j < isize; j++) {
    1394                                data[siter.index + j] = sdata[isize * i + j];
    1395                        }
    1396                }
    1397        }
    1398
    1399        @Override
    1400        public boolean containsNans() {
    1401                return false;
    1402        }
    1403
    1404        @Override
    1405        public boolean containsInfs() {
    1406                return false;
    1407        }
    1408
    1409        @Override
    1410        public boolean containsInvalidNumbers() {
    1411                return false;
    1412        }
    1413
    1414        @Override
    1415        public CompoundIntegerDataset iadd(final Object b) {
    1416                setDirty();
    1417                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1418                boolean useLong = bds.getElementClass().equals(Long.class);
    1419                int is = bds.getElementsPerItem();
    1420                if (bds.getSize() == 1) {
    1421                        final IndexIterator it = getIterator();
    1422                        final int bOffset = bds.getOffset();
    1423                        if (is == 1) {
    1424                                if (useLong) {
    1425                                        final long lb = bds.getElementLongAbs(bOffset);
    1426                                        while (it.hasNext()) {
    1427                                                for (int i = 0; i < isize; i++) {
    1428                                                        data[it.index + i] += lb;
    1429                                                }
    1430                                        }
    1431                                } else {
    1432                                        final double db = bds.getElementDoubleAbs(bOffset);
    1433                                        while (it.hasNext()) {
    1434                                                for (int i = 0; i < isize; i++) {
    1435                                                        data[it.index + i] += db;
    1436                                                }
    1437                                        }
    1438                                }
    1439                        } else if (is == isize) {
    1440                                if (useLong) {
    1441                                        while (it.hasNext()) {
    1442                                                for (int i = 0; i < isize; i++) {
    1443                                                        data[it.index + i] += bds.getElementLongAbs(i);
    1444                                                }
    1445                                        }
    1446                                } else {
    1447                                        while (it.hasNext()) {
    1448                                                for (int i = 0; i < isize; i++) {
    1449                                                        data[it.index + i] += bds.getElementDoubleAbs(i);
    1450                                                }
    1451                                        }
    1452                                }
    1453                        } else {
    1454                                throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1455                        }
    1456                } else {
    1457                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1458                        it.setOutputDouble(!useLong);
    1459                        if (is == 1) {
    1460                                if (useLong) {
    1461                                        while (it.hasNext()) {
    1462                                                final long lb = it.bLong;
    1463                                                data[it.aIndex] += lb;
    1464                                                for (int i = 1; i < isize; i++) {
    1465                                                        data[it.aIndex + i] += lb;
    1466                                                }
    1467                                        }
    1468                                } else {
    1469                                        while (it.hasNext()) {
    1470                                                final double db = it.bDouble;
    1471                                                data[it.aIndex] += db;
    1472                                                for (int i = 1; i < isize; i++) {
    1473                                                        data[it.aIndex + i] += db;
    1474                                                }
    1475                                        }
    1476                                }
    1477                        } else if (is == isize) {
    1478                                if (useLong) {
    1479                                        while (it.hasNext()) {
    1480                                                data[it.aIndex] += it.bLong;
    1481                                                for (int i = 1; i < isize; i++) {
    1482                                                        data[it.aIndex + i] += bds.getElementLongAbs(it.bIndex + i);
    1483                                                }
    1484                                        }
    1485                                } else {
    1486                                        while (it.hasNext()) {
    1487                                                data[it.aIndex] += it.bDouble;
    1488                                                for (int i = 1; i < isize; i++) {
    1489                                                        data[it.aIndex + i] += bds.getElementDoubleAbs(it.bIndex + i);
    1490                                                }
    1491                                        }
    1492                                }
    1493                        } else {
    1494                                throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1495                        }
    1496                }
    1497                return this;
    1498        }
    1499
    1500        @Override
    1501        public CompoundIntegerDataset isubtract(final Object b) {
    1502                setDirty();
    1503                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1504                boolean useLong = bds.getElementClass().equals(Long.class);
    1505                int is = bds.getElementsPerItem();
    1506                if (bds.getSize() == 1) {
    1507                        final IndexIterator it = getIterator();
    1508                        final int bOffset = bds.getOffset();
    1509                        if (is == 1) {
    1510                                if (useLong) {
    1511                                        final long lb = bds.getElementLongAbs(bOffset);
    1512                                        while (it.hasNext()) {
    1513                                                for (int i = 0; i < isize; i++) {
    1514                                                        data[it.index + i] -= lb;
    1515                                                }
    1516                                        }
    1517                                } else {
    1518                                        final double db = bds.getElementDoubleAbs(bOffset);
    1519                                        while (it.hasNext()) {
    1520                                                for (int i = 0; i < isize; i++) {
    1521                                                        data[it.index + i] -= db;
    1522                                                }
    1523                                        }
    1524                                }
    1525                        } else if (is == isize) {
    1526                                if (useLong) {
    1527                                        while (it.hasNext()) {
    1528                                                for (int i = 0; i < isize; i++) {
    1529                                                        data[it.index + i] -= bds.getElementLongAbs(i);
    1530                                                }
    1531                                        }
    1532                                } else {
    1533                                        while (it.hasNext()) {
    1534                                                for (int i = 0; i < isize; i++) {
    1535                                                        data[it.index + i] -= bds.getElementDoubleAbs(i);
    1536                                                }
    1537                                        }
    1538                                }
    1539                        } else {
    1540                                throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1541                        }
    1542                } else {
    1543                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1544                        it.setOutputDouble(!useLong);
    1545                        if (is == 1) {
    1546                                if (useLong) {
    1547                                        while (it.hasNext()) {
    1548                                                final long lb = it.bLong;
    1549                                                data[it.aIndex] += lb;
    1550                                                for (int i = 1; i < isize; i++) {
    1551                                                        data[it.aIndex + i] -= lb;
    1552                                                }
    1553                                        }
    1554                                } else {
    1555                                        while (it.hasNext()) {
    1556                                                final double db = it.bDouble;
    1557                                                data[it.aIndex] += db;
    1558                                                for (int i = 1; i < isize; i++) {
    1559                                                        data[it.aIndex + i] -= db;
    1560                                                }
    1561                                        }
    1562                                }
    1563                        } else if (is == isize) {
    1564                                if (useLong) {
    1565                                        while (it.hasNext()) {
    1566                                                data[it.aIndex] += it.bLong;
    1567                                                for (int i = 1; i < isize; i++) {
    1568                                                        data[it.aIndex + i] -= bds.getElementLongAbs(it.bIndex + i);
    1569                                                }
    1570                                        }
    1571                                } else {
    1572                                        while (it.hasNext()) {
    1573                                                data[it.aIndex] += it.bDouble;
    1574                                                for (int i = 1; i < isize; i++) {
    1575                                                        data[it.aIndex + i] -= bds.getElementDoubleAbs(it.bIndex + i);
    1576                                                }
    1577                                        }
    1578                                }
    1579                        } else {
    1580                                throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1581                        }
    1582                }
    1583                return this;
    1584        }
    1585
    1586        @Override
    1587        public CompoundIntegerDataset imultiply(final Object b) {
    1588                setDirty();
    1589                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1590                boolean useLong = bds.getElementClass().equals(Long.class);
    1591                int is = bds.getElementsPerItem();
    1592                if (bds.getSize() == 1) {
    1593                        final IndexIterator it = getIterator();
    1594                        final int bOffset = bds.getOffset();
    1595                        if (useLong) {
    1596                                if (is == 1) {
    1597                                        final long lb = bds.getElementLongAbs(bOffset);
    1598                                        while (it.hasNext()) {
    1599                                                for (int i = 0; i < isize; i++) {
    1600                                                        data[it.index + i] *= lb;
    1601                                                }
    1602                                        }
    1603                                } else if (is == isize) {
    1604                                        while (it.hasNext()) {
    1605                                                for (int i = 0; i < isize; i++) {
    1606                                                        data[it.index + i] *= bds.getElementLongAbs(i);
    1607                                                }
    1608                                        }
    1609                                } else {
    1610                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1611                                }
    1612                        } else {
    1613                                if (is == 1) {
    1614                                        final double db = bds.getElementDoubleAbs(bOffset);
    1615                                        while (it.hasNext()) {
    1616                                                for (int i = 0; i < isize; i++) {
    1617                                                        data[it.index + i] *= db;
    1618                                                }
    1619                                        }
    1620                                } else if (is == isize) {
    1621                                        while (it.hasNext()) {
    1622                                                for (int i = 0; i < isize; i++) {
    1623                                                        data[it.index + i] *= bds.getElementDoubleAbs(i);
    1624                                                }
    1625                                        }
    1626                                } else {
    1627                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1628                                }
    1629                        }
    1630                } else {
    1631                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1632                        it.setOutputDouble(!useLong);
    1633                        if (useLong) {
    1634                                if (is == 1) {
    1635                                        while (it.hasNext()) {
    1636                                                final double lb = it.bLong;
    1637                                                for (int i = 0; i < isize; i++) {
    1638                                                        data[it.aIndex + i] *= lb;
    1639                                                }
    1640                                        }
    1641                                } else if (is == isize) {
    1642                                        while (it.hasNext()) {
    1643                                                data[it.aIndex] *= it.bLong;
    1644                                                for (int i = 1; i < isize; i++) {
    1645                                                        data[it.aIndex + i] *= bds.getElementLongAbs(it.bIndex + i);
    1646                                                }
    1647                                        }
    1648                                } else {
    1649                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1650                                }
    1651                        } else {
    1652                                if (is == 1) {
    1653                                        while (it.hasNext()) {
    1654                                                final double db = it.bDouble;
    1655                                                for (int i = 0; i < isize; i++) {
    1656                                                        data[it.aIndex + i] *= db;
    1657                                                }
    1658                                        }
    1659                                } else if (is == isize) {
    1660                                        while (it.hasNext()) {
    1661                                                data[it.aIndex] *= it.bDouble;
    1662                                                for (int i = 1; i < isize; i++) {
    1663                                                        data[it.aIndex + i] *= bds.getElementDoubleAbs(it.bIndex + i);
    1664                                                }
    1665                                        }
    1666                                } else {
    1667                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1668                                }
    1669                        }
    1670                }
    1671                return this;
    1672        }
    1673
    1674        @Override
    1675        public CompoundIntegerDataset idivide(final Object b) {
    1676                setDirty();
    1677                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1678                boolean useLong = bds.getElementClass().equals(Long.class);
    1679                int is = bds.getElementsPerItem();
    1680                if (bds.getSize() == 1) {
    1681                        final IndexIterator it = getIterator();
    1682                        final int bOffset = bds.getOffset();
    1683                        if (useLong) {
    1684                                if (is == 1) {
    1685                                        final long lb = bds.getElementLongAbs(bOffset);
    1686                                        if (lb == 0) { // INT_USE
    1687                                                fill(0); // INT_USE
    1688                                        } else { // INT_USE
    1689                                        while (it.hasNext()) {
    1690                                                for (int i = 0; i < isize; i++) {
    1691                                                        data[it.index + i] /= lb;
    1692                                                }
    1693                                        }
    1694                                        } // INT_USE
    1695                                } else if (is == isize) {
    1696                                        while (it.hasNext()) {
    1697                                                for (int i = 0; i < isize; i++) {
    1698                                                        final long lb = bds.getElementLongAbs(i);
    1699                                try {
    1700                                                                data[it.index + i] /= lb; // INT_EXCEPTION
    1701                                } catch (ArithmeticException e) {
    1702                                        data[it.index + i] = 0;
    1703                                }
    1704                                                }
    1705                                        }
    1706                                } else {
    1707                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1708                                }
    1709                        } else {
    1710                                if (is == 1) {
    1711                                        final double db = bds.getElementDoubleAbs(bOffset);
    1712                                        if (db == 0) { // INT_USE
    1713                                                fill(0); // INT_USE
    1714                                        } else { // INT_USE
    1715                                        while (it.hasNext()) {
    1716                                                for (int i = 0; i < isize; i++) {
    1717                                                        data[it.index + i] /= db;
    1718                                                }
    1719                                        }
    1720                                        } // INT_USE
    1721                                } else if (is == isize) {
    1722                                        while (it.hasNext()) {
    1723                                                for (int i = 0; i < isize; i++) {
    1724                                                        final double db = bds.getElementDoubleAbs(i);
    1725                                try {
    1726                                                                data[it.index + i] /= db; // INT_EXCEPTION
    1727                                } catch (ArithmeticException e) {
    1728                                        data[it.index + i] = 0;
    1729                                }
    1730                                                }
    1731                                        }
    1732                                } else {
    1733                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1734                                }
    1735                        }
    1736                } else {
    1737                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1738                        it.setOutputDouble(!useLong);
    1739                        if (useLong) {
    1740                                if (is == 1) {
    1741                                        while (it.hasNext()) {
    1742                                                final long lb = it.bLong;
    1743                                                if (lb == 0) { // INT_USE
    1744                                                        for (int i = 0; i < isize; i++) { // INT_USE
    1745                                                                data[it.aIndex + i] = 0; // INT_USE
    1746                                                        }// INT_USE
    1747                                                } else { // INT_USE
    1748                                                for (int i = 0; i < isize; i++) {
    1749                                                        data[it.aIndex + i] /= lb;
    1750                                                }
    1751                                                } // INT_USE
    1752                                        }
    1753                                } else if (is == isize) {
    1754                                        while (it.hasNext()) {
    1755                                                for (int i = 0; i < isize; i++) {
    1756                                                        final long lb = bds.getElementLongAbs(it.bIndex + i);
    1757                                try {
    1758                                                                data[it.aIndex + i] /= lb; // INT_EXCEPTION
    1759                                } catch (ArithmeticException e) {
    1760                                        data[it.aIndex + i] = 0;
    1761                                }
    1762                                                }
    1763                                        }
    1764                                } else {
    1765                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1766                                }
    1767                        } else {
    1768                                if (is == 1) {
    1769                                        while (it.hasNext()) {
    1770                                                final double db = it.bDouble;
    1771                                                if (db == 0) { // INT_USE
    1772                                                        for (int i = 0; i < isize; i++) { // INT_USE
    1773                                                                data[it.aIndex + i] = 0; // INT_USE
    1774                                                        }// INT_USE
    1775                                                } else { // INT_USE
    1776                                                for (int i = 0; i < isize; i++) {
    1777                                                        data[it.aIndex + i] /= db;
    1778                                                }
    1779                                                } // INT_USE
    1780                                        }
    1781                                } else if (is == isize) {
    1782                                        while (it.hasNext()) {
    1783                                                for (int i = 0; i < isize; i++) {
    1784                                                        final double db = bds.getElementDoubleAbs(it.bIndex + i);
    1785                                try {
    1786                                                                data[it.aIndex + i] /= db; // INT_EXCEPTION
    1787                                } catch (ArithmeticException e) {
    1788                                        data[it.aIndex + i] = 0;
    1789                                }
    1790                                                }
    1791                                        }
    1792                                } else {
    1793                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1794                                }
    1795                        }
    1796                }
    1797                return this;
    1798        }
    1799
    1800        @Override
    1801        public CompoundIntegerDataset ifloor() {
    1802                return this;
    1803        }
    1804
    1805        @Override
    1806        public CompoundIntegerDataset iremainder(final Object b) {
    1807                setDirty();
    1808                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1809                boolean useLong = bds.getElementClass().equals(Long.class);
    1810                int is = bds.getElementsPerItem();
    1811                if (bds.getSize() == 1) {
    1812                        final IndexIterator it = getIterator();
    1813                        final int bOffset = bds.getOffset();
    1814                        if (useLong) {
    1815                                if (is == 1) {
    1816                                        final long lb = bds.getElementLongAbs(bOffset);
    1817                                        if (lb == 0) { // INT_USE
    1818                                                fill(0); // INT_USE
    1819                                        } else { // INT_USE
    1820                                        while (it.hasNext()) {
    1821                                                for (int i = 0; i < isize; i++) {
    1822                                                        data[it.index + i] %= lb;
    1823                                                }
    1824                                        }
    1825                                        } // INT_USE
    1826                                } else if (is == isize) {
    1827                                        while (it.hasNext()) {
    1828                                                for (int i = 0; i < isize; i++) {
    1829                                try {
    1830                                                                data[it.index + i] %= bds.getElementLongAbs(i); // INT_EXCEPTION
    1831                                } catch (ArithmeticException e) {
    1832                                        data[it.index + i] = 0;
    1833                                }
    1834                                                }
    1835                                        }
    1836                                } else {
    1837                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1838                                }
    1839                        } else {
    1840                                if (is == 1) {
    1841                                        final double db = bds.getElementDoubleAbs(bOffset);
    1842                                        if (db == 0) { // INT_USE
    1843                                                fill(0); // INT_USE
    1844                                        } else { // INT_USE
    1845                                        while (it.hasNext()) {
    1846                                                for (int i = 0; i < isize; i++) {
    1847                                                        data[it.index + i] %= db;
    1848                                                }
    1849                                        }
    1850                                        } // INT_USE
    1851                                } else if (is == isize) {
    1852                                        while (it.hasNext()) {
    1853                                                for (int i = 0; i < isize; i++) {
    1854                                try {
    1855                                                                data[it.index + i] %= bds.getElementDoubleAbs(i); // INT_EXCEPTION
    1856                                } catch (ArithmeticException e) {
    1857                                        data[it.index + i] = 0;
    1858                                }
    1859                                                }
    1860                                        }
    1861                                } else {
    1862                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1863                                }
    1864                        }
    1865                } else {
    1866                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1867                        it.setOutputDouble(!useLong);
    1868                        if (useLong) {
    1869                                if (is == 1) {
    1870                                        while (it.hasNext()) {
    1871                                                final long lb = it.bLong;
    1872                                                if (lb == 0) { // INT_USE
    1873                                                        for (int i = 0; i < isize; i++) // INT_USE
    1874                                                                data[it.aIndex + i] = 0; // INT_USE
    1875                                                } else { // INT_USE
    1876                                                for (int i = 0; i < isize; i++)
    1877                                                        data[it.aIndex + i] %= lb;
    1878                                                } // INT_USE
    1879                                        }
    1880                                } else if (is == isize) {
    1881                                        while (it.hasNext()) {
    1882                                                for (int i = 0; i < isize; i++) {
    1883                                                        final long lb = bds.getElementLongAbs(it.bIndex + i);
    1884                                try {
    1885                                                                data[it.aIndex + i] %= lb; // INT_EXCEPTION
    1886                                } catch (ArithmeticException e) {
    1887                                        data[it.aIndex + i] = 0;
    1888                                }
    1889                                                }
    1890                                        }
    1891                                } else {
    1892                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1893                                }
    1894                        } else {
    1895                                if (is == 1) {
    1896                                        while (it.hasNext()) {
    1897                                                final double db = it.bDouble;
    1898                                                if (db == 0) { // INT_USE
    1899                                                        for (int i = 0; i < isize; i++) // INT_USE
    1900                                                                data[it.aIndex + i] = 0; // INT_USE
    1901                                                } else { // INT_USE
    1902                                                for (int i = 0; i < isize; i++) {
    1903                                                        data[it.aIndex + i] %= db;
    1904                                                }
    1905                                                } // INT_USE
    1906                                        }
    1907                                } else if (is == isize) {
    1908                                        while (it.hasNext()) {
    1909                                                for (int i = 0; i < isize; i++) {
    1910                                                        final double db = bds.getElementDoubleAbs(it.bIndex + i);
    1911                                try {
    1912                                                                data[it.aIndex + i] %= db; // INT_EXCEPTION
    1913                                } catch (ArithmeticException e) {
    1914                                        data[it.aIndex + i] = 0;
    1915                                }
    1916                                                }
    1917                                        }
    1918                                } else {
    1919                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1920                                }
    1921                        }
    1922                }
    1923                return this;
    1924        }
    1925
    1926        @Override
    1927        public CompoundIntegerDataset ipower(final Object b) {
    1928                setDirty();
    1929                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1930                final int is = bds.getElementsPerItem();
    1931                if (bds.getSize() == 1) {
    1932                        final int bOffset = bds.getOffset();
    1933                        final double vr = bds.getElementDoubleAbs(bOffset);
    1934                        final IndexIterator it = getIterator();
    1935                        if (bds.isComplex()) {
    1936                                final double vi = bds.getElementDoubleAbs(bOffset + 1);
    1937                                if (vi == 0) {
    1938                                        while (it.hasNext()) {
    1939                                                for (int i = 0; i < isize; i++) {
    1940                                                        final double v = Math.pow(data[it.index + i], vr);
    1941                                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1942                                                                data[it.index + i] = 0; // INT_USE
    1943                                                        } else { // INT_USE
    1944                                                        data[it.index + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1945                                                        } // INT_USE
    1946                                                }
    1947                                        }
    1948                                } else {
    1949                                        final Complex zv = new Complex(vr, vi);
    1950                                        while (it.hasNext()) {
    1951                                                for (int i = 0; i < isize; i++) {
    1952                                                        Complex zd = new Complex(data[it.index + i], 0);
    1953                                                        final double v = zd.pow(zv).getReal();
    1954                                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1955                                                                data[it.index + i] = 0; // INT_USE
    1956                                                        } else { // INT_USE
    1957                                                        data[it.index + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1958                                                        } // INT_USE
    1959                                                }
    1960                                        }
    1961                                }
    1962                        } else if (is == 1) {
    1963                                while (it.hasNext()) {
    1964                                        for (int i = 0; i < isize; i++) {
    1965                                                final double v = Math.pow(data[it.index + i], vr);
    1966                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1967                                                        data[it.index + i] = 0; // INT_USE
    1968                                                } else { // INT_USE
    1969                                                data[it.index + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1970                                                } // INT_USE
    1971                                        }
    1972                                }
    1973                        } else if (is == isize) {
    1974                                while (it.hasNext()) {
    1975                                        for (int i = 0; i < isize; i++) {
    1976                                                final double v = Math.pow(data[it.index + i], bds.getElementDoubleAbs(i));
    1977                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1978                                                        data[it.index + i] = 0; // INT_USE
    1979                                                } else { // INT_USE
    1980                                                data[it.index + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1981                                                } // INT_USE
    1982                                        }
    1983                                }
    1984                        }
    1985                } else {
    1986                        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
    1987                        it.setOutputDouble(true);
    1988                        if (bds.isComplex()) {
    1989                                while (it.hasNext()) {
    1990                                        final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1));
    1991                                        double v = new Complex(it.aDouble, 0).pow(zv).getReal();
    1992                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1993                                                data[it.aIndex] = 0; // INT_USE
    1994                                        } else { // INT_USE
    1995                                        data[it.aIndex] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1996                                        } // INT_USE
    1997                                        for (int i = 1; i < isize; i++) {
    1998                                                v = new Complex(data[it.aIndex + i], 0).pow(zv).getReal();
    1999                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    2000                                                        data[it.aIndex + i] = 0; // INT_USE
    2001                                                } else { // INT_USE
    2002                                                data[it.aIndex + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    2003                                                } // INT_USE
    2004                                        }
    2005                                }
    2006                        } else {
    2007                                while (it.hasNext()) {
    2008                                        double v = Math.pow(it.aDouble, it.bDouble);
    2009                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    2010                                                data[it.aIndex] = 0; // INT_USE
    2011                                        } else { // INT_USE
    2012                                        data[it.aIndex] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    2013                                        } // INT_USE
    2014                                        for (int i = 1; i < isize; i++) {
    2015                                                v = Math.pow(data[it.aIndex + i], bds.getElementDoubleAbs(it.bIndex + i));
    2016                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    2017                                                        data[it.aIndex + i] = 0; // INT_USE
    2018                                                } else { // INT_USE
    2019                                                data[it.aIndex + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    2020                                                } // INT_USE
    2021                                        }
    2022                                }
    2023                        }
    2024                }
    2025                return this;
    2026        }
    2027
    2028        @Override
    2029        public double residual(final Object b, final Dataset w, boolean ignoreNaNs) {
    2030                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    2031                final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
    2032                it.setOutputDouble(true);
    2033                double sum = 0;
    2034                double comp = 0;
    2035                final int bis = bds.getElementsPerItem();
    2036
    2037                if (bis == 1) {
    2038                        if (w == null) {
    2039                                while (it.hasNext()) {
    2040                                        final double db = it.bDouble;
    2041                                        double diff = it.aDouble - db;
    2042                                        double err = diff * diff - comp;
    2043                                        double temp = sum + err;
    2044                                        comp = (temp - sum) - err;
    2045                                        sum = temp;
    2046                                        for (int i = 1; i < isize; i++) {
    2047                                                diff = data[it.aIndex + i] - db;
    2048                                                err = diff * diff - comp;
    2049                                                temp = sum + err;
    2050                                                comp = (temp - sum) - err;
    2051                                                sum = temp;
    2052                                        }
    2053                                }
    2054                        } else {
    2055                                IndexIterator itw = w.getIterator();
    2056                                while (it.hasNext() && itw.hasNext()) {
    2057                                        final double db = it.bDouble;
    2058                                        double diff = it.aDouble - db;
    2059                                        final double dw = w.getElementDoubleAbs(itw.index);
    2060                                        double err = diff * diff * dw - comp;
    2061                                        double temp = sum + err;
    2062                                        comp = (temp - sum) - err;
    2063                                        sum = temp;
    2064                                        for (int i = 1; i < isize; i++) {
    2065                                                diff = data[it.aIndex + i] - db;
    2066                                                err = diff * diff * dw - comp;
    2067                                                temp = sum + err;
    2068                                                comp = (temp - sum) - err;
    2069                                                sum = temp;
    2070                                        }
    2071                                }
    2072                        }
    2073                } else {
    2074                        if (w == null) {
    2075                                while (it.hasNext()) {
    2076                                        double diff = it.aDouble - it.bDouble;
    2077                                        double err = diff * diff - comp;
    2078                                        double temp = sum + err;
    2079                                        comp = (temp - sum) - err;
    2080                                        sum = temp;
    2081                                        for (int i = 1; i < isize; i++) {
    2082                                                diff = data[it.aIndex + i] - bds.getElementDoubleAbs(it.bIndex + i);
    2083                                                err = diff * diff - comp;
    2084                                                temp = sum + err;
    2085                                                comp = (temp - sum) - err;
    2086                                                sum = temp;
    2087                                        }
    2088                                }
    2089                        } else {
    2090                                IndexIterator itw = w.getIterator();
    2091                                while (it.hasNext() && itw.hasNext()) {
    2092                                        double diff = it.aDouble - it.bDouble;
    2093                                        final double dw = w.getElementDoubleAbs(itw.index);
    2094                                        double err = diff * diff * dw - comp;
    2095                                        double temp = sum + err;
    2096                                        comp = (temp - sum) - err;
    2097                                        sum = temp;
    2098                                        for (int i = 1; i < isize; i++) {
    2099                                                diff = data[it.aIndex + i] - bds.getElementDoubleAbs(it.bIndex + i);
    2100                                                err = diff * diff * dw - comp;
    2101                                                temp = sum + err;
    2102                                                comp = (temp - sum) - err;
    2103                                                sum = temp;
    2104                                        }
    2105                                }
    2106                        }
    2107                }
    2108                return sum;
    2109        }
    2110}