    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 DoubleDataset.java by fromdouble.py
    014
    015package org.eclipse.january.dataset;
    016
    017import java.util.ArrayList;
    018import java.util.Arrays;
    019import java.util.List;
    020import java.util.Set;
    021import java.util.TreeSet;
    022
    023import org.apache.commons.math3.complex.Complex;
    024import org.eclipse.january.metadata.StatisticsMetadata;
    025
    026
    027/**
    028 * Extend dataset for short values // PRIM_TYPE
    029 */
    030public class ShortDataset extends AbstractDataset {
    031        // pin UID to base class
    032        private static final long serialVersionUID = Dataset.serialVersionUID;
    033
    034        protected short[] data; // subclass alias // PRIM_TYPE
    035
    036        @Override
    037        protected void setData() {
    038                data = (short[]) odata; // PRIM_TYPE
    039        }
    040
    041        protected static short[] createArray(final int size) { // PRIM_TYPE
    042                short[] array = null; // PRIM_TYPE
    043
    044                try {
    045                        array = new short[size]; // PRIM_TYPE
    046                } catch (OutOfMemoryError e) {
    047                        logger.error("The size of the dataset ({}) that is being created is too large "
    048                                        + "and there is not enough memory to hold it.", size);
    049                        throw new OutOfMemoryError("The dimensions given are too large, and there is "
    050                                        + "not enough memory available in the Java Virtual Machine");
    051                }
    052                return array;
    053        }
    054
    055        @Override
    056        public int getDType() {
    057                return INT16; // DATA_TYPE
    058        }
    059
    060        /**
    061         * Create a null dataset
    062         */
    063        ShortDataset() {
    064        }
    065
    066        /**
    067         * Create a zero-filled dataset of given shape
    068         * @param shape
    069         */
    070        ShortDataset(final int... shape) {
    071                if (shape != null) {
    072                        size = ShapeUtils.calcSize(shape);
    073                        this.shape = shape.clone();
    074
    075                        try {
    076                                odata = data = createArray(size);
    077                        } catch (Throwable t) {
    078                                logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    079                                throw new IllegalArgumentException(t);
    080                        }
    081                }
    082        }
    083
    084        /**
    085         * Create a dataset using given data
    086         * @param data
    087         * @param shape
    088         *            (can be null to create 1D dataset)
    089         */
    090        ShortDataset(final short[] data, int... shape) { // PRIM_TYPE
    091                if (data == null) {
    092                        throw new IllegalArgumentException("Data must not be null");
    093                }
    094                if (shape == null || shape.length == 0) {
    095                        shape = new int[] { data.length };
    096                }
    097                size = ShapeUtils.calcSize(shape);
    098                if (size != data.length) {
    099                        throw new IllegalArgumentException(String.format("Shape %s is not compatible with size of data array, %d",
    100                                        Arrays.toString(shape), data.length));
    101                }
    102                this.shape = size == 0 ? null : shape.clone();
    103
    104                odata = this.data = data;
    105        }
    106
    107        /**
    108         * Copy a dataset
    109         * @param dataset
    110         */
    111        ShortDataset(final ShortDataset dataset) {
    112                copyToView(dataset, this, true, true);
    113
    114                try {
    115                        if (dataset.stride == null) {
    116                                if (dataset.data != null) {
    117                                        odata = data = dataset.data.clone();
    118                                }
    119                        } else {
    120                                offset = 0;
    121                                stride = null;
    122                                base = null;
    123                                odata = data = createArray(size);
    124
    125                                IndexIterator iter = dataset.getIterator();
    126                                for (int i = 0; iter.hasNext(); i++) {
    127                                        data[i] = dataset.data[iter.index];
    128                                }
    129                        }
    130                } catch (Throwable t) {
    131                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    132                        throw new IllegalArgumentException(t);
    133                }
    134        }
    135
    136        /**
    137         * Copy and cast a dataset to this class type
    138         * @param dataset
    139         */
    140        ShortDataset(final Dataset dataset) {
    141                copyToView(dataset, this, true, false);
    142                offset = 0;
    143                stride = null;
    144                base = null;
    145                try {
    146                        odata = data = createArray(size);
    147                } catch (Throwable t) {
    148                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    149                        throw new IllegalArgumentException(t);
    150                }
    151                IndexIterator iter = dataset.getIterator();
    152                for (int i = 0; iter.hasNext(); i++) {
    153                        data[i] = (short) dataset.getElementLongAbs(iter.index); // GET_ELEMENT_WITH_CAST
    154                }
    155        }
    156
    157        @Override
    158        public boolean equals(Object obj) {
    159                if (this == obj) {
    160                        return true;
    161                }
    162
    163                if (obj == null) {
    164                        return false;
    165                }
    166
    167                if (!getClass().equals(obj.getClass())) {
    168                        if (getRank() == 0) { // for zero-rank datasets
    169                                return obj.equals(getObjectAbs(offset));
    170                        }
    171                        return false;
    172                }
    173
    174                ShortDataset other = (ShortDataset) obj;
    175                if (size != other.size) {
    176                        return false;
    177                }
    178                if (!Arrays.equals(shape, other.shape)) {
    179                        return false;
    180                }
    181                if (data == other.data && stride == null && other.stride == null) {
    182                        return true;
    183                }
    184
    185                IndexIterator iter = getIterator();
    186                IndexIterator oiter = other.getIterator();
    187                while (iter.hasNext() && oiter.hasNext()) {
    188                        if (data[iter.index] != other.data[oiter.index]) { // OBJECT_UNEQUAL
    189                                return false;
    190                        }
    191                }
    192                return true;
    193        }
    194
    195        @Override
    196        public int hashCode() {
    197                return super.hashCode();
    198        }
    199
    200        @Override
    201        public ShortDataset clone() {
    202                return new ShortDataset(this);
    203        }
    204
    205        /**
    206         * Create a dataset from an object which could be a Java list, array (of arrays...) or Number. Ragged
    207         * sequences or arrays are padded with zeros.
    208         *
    209         * @param obj
    210         * @return dataset with contents given by input
    211         */
    212        static ShortDataset createFromObject(final Object obj) {
    213                ShortDataset result = new ShortDataset();
    214
    215                if (obj != null) {
    216                        result.shape = ShapeUtils.getShapeFromObject(obj);
    217                        result.size = ShapeUtils.calcSize(result.shape);
    218
    219                        try {
    220                                result.odata = result.data = createArray(result.size);
    221                        } catch (Throwable t) {
    222                                logger.error("Could not create a dataset of shape {}", Arrays.toString(result.shape), t);
    223                                throw new IllegalArgumentException(t);
    224                        }
    225
    226                        int[] pos = new int[result.shape.length];
    227                        result.fillData(obj, 0, pos);
    228                }
    229
    230                return result;
    231        }
    232
    233        /**
    234         *
    235         * @param stop
    236         * @return a new 1D dataset, filled with values determined by parameters
    237         */
    238        static ShortDataset createRange(final double stop) {
    239                return createRange(0, stop, 1);
    240        }
    241
    242        /**
    243         *
    244         * @param start
    245         * @param stop
    246         * @param step
    247         * @return a new 1D dataset, filled with values determined by parameters
    248         */
    249        static ShortDataset createRange(final double start, final double stop, final double step) {
    250                int size = calcSteps(start, stop, step);
    251                ShortDataset result = new ShortDataset(size);
    252                for (int i = 0; i < size; i++) {
    253                        result.data[i] = (short) (start + i * step); // PRIM_TYPE // ADD_CAST
    254                }
    255                return result;
    256        }
    257
    258        /**
    259         * @param shape
    260         * @return a dataset filled with ones
    261         */
    262        static ShortDataset ones(final int... shape) {
    263                return new ShortDataset(shape).fill(1);
    264        }
    265
    266        @Override
    267        public ShortDataset fill(final Object obj) {
    268                setDirty();
    269                short dv = (short) DTypeUtils.toLong(obj); // PRIM_TYPE // FROM_OBJECT
    270                IndexIterator iter = getIterator();
    271                while (iter.hasNext()) {
    272                        data[iter.index] = dv;
    273                }
    274
    275                return this;
    276        }
    277
    278        /**
    279         * This is a typed version of {@link #getBuffer()}
    280         * @return data buffer as linear array
    281         */
    282        public short[] getData() { // PRIM_TYPE
    283                return data;
    284        }
    285
    286        @Override
    287        protected int getBufferLength() {
    288                if (data == null)
    289                        return 0;
    290                return data.length;
    291        }
    292
    293        @Override
    294        public ShortDataset getView(boolean deepCopyMetadata) {
    295                ShortDataset view = new ShortDataset();
    296                copyToView(this, view, true, deepCopyMetadata);
    297                view.setData();
    298                return view;
    299        }
    300
    301        /**
    302         * Get a value from an absolute index of the internal array. This is an internal method with no checks so can be
    303         * dangerous. Use with care or ideally with an iterator.
    304         *
    305         * @param index
    306         *            absolute index
    307         * @return value
    308         */
    309        public short getAbs(final int index) { // PRIM_TYPE
    310                return data[index];
    311        }
    312
    313        @Override
    314        public boolean getElementBooleanAbs(final int index) {
    315                return data[index] != 0; // BOOLEAN_FALSE
    316        }
    317
    318        @Override
    319        public double getElementDoubleAbs(final int index) {
    320                return data[index]; // BOOLEAN_ZERO
    321        }
    322
    323        @Override
    324        public long getElementLongAbs(final int index) {
    325                return data[index]; // BOOLEAN_ZERO // OMIT_TOLONG_INT
    326        }
    327
    328        @Override
    329        public Object getObjectAbs(final int index) {
    330                return data[index];
    331        }
    332
    333        @Override
    334        public String getStringAbs(final int index) {
    335                return stringFormat == null ? String.format("%d", data[index]) : // FORMAT_STRING
    336                        stringFormat.format(data[index]);
    337        }
    338
    339        /**
    340         * Set a value at absolute index in the internal array. This is an internal method with no checks so can be
    341         * dangerous. Use with care or ideally with an iterator.
    342         *
    343         * @param index
    344         *            absolute index
    345         * @param val
    346         *            new value
    347         */
    348        public void setAbs(final int index, final short val) { // PRIM_TYPE
    349                setDirty();
    350                data[index] = val;
    351        }
    352
    353        @Override
    354        protected void setItemDirect(final int dindex, final int sindex, final Object src) {
    355                setDirty();
    356                short[] dsrc = (short[]) src; // PRIM_TYPE
    357                data[dindex] = dsrc[sindex];
    358        }
    359
    360        @Override
    361        public void setObjectAbs(final int index, final Object obj) {
    362                if (index < 0 || index > data.length) {
    363                        throw new IndexOutOfBoundsException("Index given is outside dataset");
    364                }
    365
    366                setAbs(index, (short) DTypeUtils.toLong(obj)); // FROM_OBJECT
    367        }
    368
    369        /**
    370         * @return item in first position
    371         * @since 2.0
    372         */
    373        public short get() { // PRIM_TYPE
    374                return data[getFirst1DIndex()];
    375        }
    376
    377        /**
    378         * @param i
    379         * @return item in given position
    380         */
    381        public short get(final int i) { // PRIM_TYPE
    382                return data[get1DIndex(i)];
    383        }
    384
    385        /**
    386         * @param i
    387         * @param j
    388         * @return item in given position
    389         */
    390        public short get(final int i, final int j) { // PRIM_TYPE
    391                return data[get1DIndex(i, j)];
    392        }
    393
    394        /**
    395         * @param pos
    396         * @return item in given position
    397         */
    398        public short get(final int... pos) { // PRIM_TYPE
    399                return data[get1DIndex(pos)];
    400        }
    401
    402        @Override
    403        public Object getObject() {
    404                return Short.valueOf(get()); // CLASS_TYPE
    405        }
    406
    407        @Override
    408        public Object getObject(final int i) {
    409                return Short.valueOf(get(i)); // CLASS_TYPE
    410        }
    411
    412        @Override
    413        public Object getObject(final int i, final int j) {
    414                return Short.valueOf(get(i, j)); // CLASS_TYPE
    415        }
    416
    417        @Override
    418        public Object getObject(final int... pos) {
    419                return Short.valueOf(get(pos)); // CLASS_TYPE
    420        }
    421
    422        @Override
    423        public String getString() {
    424                return getStringAbs(getFirst1DIndex());
    425        }
    426
    427        @Override
    428        public String getString(final int i) {
    429                return getStringAbs(get1DIndex(i));
    430        }
    431
    432        @Override
    433        public String getString(final int i, final int j) {
    434                return getStringAbs(get1DIndex(i, j));
    435        }
    436
    437        @Override
    438        public String getString(final int... pos) {
    439                return getStringAbs(get1DIndex(pos));
    440        }
    441
    442        @Override
    443        public double getDouble() {
    444                return get(); // BOOLEAN_ZERO
    445        }
    446
    447        @Override
    448        public double getDouble(final int i) {
    449                return get(i); // BOOLEAN_ZERO
    450        }
    451
    452        @Override
    453        public double getDouble(final int i, final int j) {
    454                return get(i, j); // BOOLEAN_ZERO
    455        }
    456
    457        @Override
    458        public double getDouble(final int... pos) {
    459                return get(pos); // BOOLEAN_ZERO
    460        }
    461
    462        @Override
    463        public float getFloat() {
    464                return get(); // BOOLEAN_ZERO // OMIT_REAL_CAST
    465        }
    466
    467        @Override
    468        public float getFloat(final int i) {
    469                return get(i); // BOOLEAN_ZERO // OMIT_REAL_CAST
    470        }
    471
    472        @Override
    473        public float getFloat(final int i, final int j) {
    474                return get(i, j); // BOOLEAN_ZERO // OMIT_REAL_CAST
    475        }
    476
    477        @Override
    478        public float getFloat(final int... pos) {
    479                return get(pos); // BOOLEAN_ZERO // OMIT_REAL_CAST
    480        }
    481
    482        @Override
    483        public long getLong() {
    484                return get(); // BOOLEAN_ZERO // OMIT_UPCAST
    485        }
    486
    487        @Override
    488        public long getLong(final int i) {
    489                return get(i); // BOOLEAN_ZERO // OMIT_UPCAST
    490        }
    491
    492        @Override
    493        public long getLong(final int i, final int j) {
    494                return get(i, j); // BOOLEAN_ZERO // OMIT_UPCAST
    495        }
    496
    497        @Override
    498        public long getLong(final int... pos) {
    499                return get(pos); // BOOLEAN_ZERO // OMIT_UPCAST
    500        }
    501
    502        @Override
    503        public int getInt() {
    504                return get(); // BOOLEAN_ZERO // OMIT_UPCAST
    505        }
    506
    507        @Override
    508        public int getInt(final int i) {
    509                return get(i); // BOOLEAN_ZERO // OMIT_UPCAST
    510        }
    511
    512        @Override
    513        public int getInt(final int i, final int j) {
    514                return get(i, j); // BOOLEAN_ZERO // OMIT_UPCAST
    515        }
    516
    517        @Override
    518        public int getInt(final int... pos) {
    519                return get(pos); // BOOLEAN_ZERO // OMIT_UPCAST
    520        }
    521
    522        @Override
    523        public short getShort() {
    524                return get(); // BOOLEAN_ZERO // OMIT_UPCAST
    525        }
    526
    527        @Override
    528        public short getShort(final int i) {
    529                return get(i); // BOOLEAN_ZERO // OMIT_UPCAST
    530        }
    531
    532        @Override
    533        public short getShort(final int i, final int j) {
    534                return get(i, j); // BOOLEAN_ZERO // OMIT_UPCAST
    535        }
    536
    537        @Override
    538        public short getShort(final int... pos) {
    539                return get(pos); // BOOLEAN_ZERO // OMIT_UPCAST
    540        }
    541
    542        @Override
    543        public byte getByte() {
    544                return (byte) get(); // BOOLEAN_ZERO // OMIT_UPCAST
    545        }
    546
    547        @Override
    548        public byte getByte(final int i) {
    549                return (byte) get(i); // BOOLEAN_ZERO // OMIT_UPCAST
    550        }
    551
    552        @Override
    553        public byte getByte(final int i, final int j) {
    554                return (byte) get(i, j); // BOOLEAN_ZERO // OMIT_UPCAST
    555        }
    556
    557        @Override
    558        public byte getByte(final int... pos) {
    559                return (byte) get(pos); // BOOLEAN_ZERO // OMIT_UPCAST
    560        }
    561
    562        @Override
    563        public boolean getBoolean() {
    564                return get() != 0; // BOOLEAN_FALSE
    565        }
    566
    567        @Override
    568        public boolean getBoolean(final int i) {
    569                return get(i) != 0; // BOOLEAN_FALSE
    570        }
    571
    572        @Override
    573        public boolean getBoolean(final int i, final int j) {
    574                return get(i, j) != 0; // BOOLEAN_FALSE
    575        }
    576
    577        @Override
    578        public boolean getBoolean(final int... pos) {
    579                return get(pos) != 0; // BOOLEAN_FALSE
    580        }
    581
    582        /**
    583         * Sets the value at first point to the passed value. The dataset must not be null
    584         *
    585         * @param value
    586         * @since 2.0
    587         */
    588        public void setItem(final short value) { // PRIM_TYPE
    589                setAbs(getFirst1DIndex(), value);
    590        }
    591
    592        /**
    593         * Sets the value at a particular point to the passed value. The dataset must be 1D
    594         *
    595         * @param value
    596         * @param i
    597         */
    598        public void setItem(final short value, final int i) { // PRIM_TYPE
    599                setAbs(get1DIndex(i), value);
    600        }
    601
    602        /**
    603         * Sets the value at a particular point to the passed value. The dataset must be 2D
    604         *
    605         * @param value
    606         * @param i
    607         * @param j
    608         */
    609        public void setItem(final short value, final int i, final int j) { // PRIM_TYPE
    610                setAbs(get1DIndex(i, j), value);
    611        }
    612
    613        /**
    614         * Sets the value at a particular point to the passed value
    615         *
    616         * @param value
    617         * @param pos
    618         */
    619        public void setItem(final short value, final int... pos) { // PRIM_TYPE
    620                setAbs(get1DIndex(pos), value);
    621        }
    622
    623        @Override
    624        public void set(final Object obj) {
    625                setItem((short) DTypeUtils.toLong(obj)); // FROM_OBJECT
    626        }
    627
    628        @Override
    629        public void set(final Object obj, final int i) {
    630                setItem((short) DTypeUtils.toLong(obj), i); // FROM_OBJECT
    631        }
    632
    633        @Override
    634        public void set(final Object obj, final int i, final int j) {
    635                setItem((short) DTypeUtils.toLong(obj), i, j); // FROM_OBJECT
    636        }
    637
    638        @Override
    639        public void set(final Object obj, int... pos) {
    640                if (pos == null || (pos.length == 0 && shape.length > 0)) {
    641                        pos = new int[shape.length];
    642                }
    643
    644                setItem((short) DTypeUtils.toLong(obj), pos); // FROM_OBJECT
    645        }
    646
    647        @Override
    648        public void resize(int... newShape) {
    649                setDirty();
    650                final IndexIterator iter = getIterator();
    651                final int nsize = ShapeUtils.calcSize(newShape);
    652                final short[] ndata; // PRIM_TYPE
    653                try {
    654                        ndata = createArray(nsize);
    655                } catch (Throwable t) {
    656                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    657                        throw new IllegalArgumentException(t);
    658                }
    659                for (int i = 0; iter.hasNext() && i < nsize; i++) {
    660                        ndata[i] = data[iter.index];
    661                }
    662
    663                odata = data = ndata;
    664                size = nsize;
    665                shape = newShape;
    666                stride = null;
    667                offset = 0;
    668                base = null;
    669        }
    670
    671        @Override
    672        public ShortDataset sort(Integer axis) {
    673                setDirty();
    674                if (axis == null) {
    675                        if (stride == null) {
    676                                Arrays.sort(data);
    677                        } else {
    678                                ShortDataset ads = clone().sort(null);
    679                                setSlicedView(getView(false), ads);
    680                        }
    681                } else {
    682                        axis = checkAxis(axis);
    683
    684                        ShortDataset ads = new ShortDataset(shape[axis]);
    685                        PositionIterator pi = getPositionIterator(axis);
    686                        int[] pos = pi.getPos();
    687                        boolean[] hit = pi.getOmit();
    688                        while (pi.hasNext()) {
    689                                copyItemsFromAxes(pos, hit, ads);
    690                                Arrays.sort(ads.data);
    691                                setItemsOnAxes(pos, hit, ads.data);
    692                        }
    693                }
    694                return this;
    695                // throw new UnsupportedOperationException("Cannot sort dataset"); // BOOLEAN_USE
    696        }
    697
    698        @Override
    699        public ShortDataset getUniqueItems() {
    700                Set<Short> set = new TreeSet<Short>(); // CLASS_TYPE
    701                IndexIterator it = getIterator();
    702                while (it.hasNext()) {
    703                        set.add(data[it.index]);
    704                }
    705
    706                ShortDataset u = new ShortDataset(set.size()); // CLASS_TYPE
    707                int i = 0;
    708                short[] udata = u.getData(); // PRIM_TYPE
    709                for (Short v : set) { // CLASS_TYPE
    710                        udata[i++] = v;
    711                }
    712                return u;
    713        }
    714
    715        @Override
    716        public ShortDataset getSlice(final SliceIterator siter) {
    717                ShortDataset result = new ShortDataset(siter.getShape());
    718                short[] rdata = result.data; // PRIM_TYPE
    719
    720                for (int i = 0; siter.hasNext(); i++)
    721                        rdata[i] = data[siter.index];
    722
    723                result.setName(name + BLOCK_OPEN + Slice.createString(siter.shape, siter.start, siter.stop, siter.step) + BLOCK_CLOSE);
    724                return result;
    725        }
    726
    727        @Override
    728        public void fillDataset(Dataset result, IndexIterator iter) {
    729                IndexIterator riter = result.getIterator();
    730                result.setDirty();
    731
    732                short[] rdata = ((ShortDataset) result).data; // PRIM_TYPE
    733
    734                while (riter.hasNext() && iter.hasNext()) {
    735                        rdata[riter.index] = data[iter.index];
    736                }
    737        }
    738
    739        @Override
    740        public ShortDataset setByBoolean(final Object obj, Dataset selection) {
    741                setDirty();
    742                if (obj instanceof Dataset) {
    743                        final Dataset ds = (Dataset) obj;
    744                        final int length = ((Number) selection.sum()).intValue();
    745                        if (length != ds.getSize()) {
    746                                throw new IllegalArgumentException(
    747                                                "Number of true items in selection does not match number of items in dataset");
    748                        }
    749
    750                        final IndexIterator oiter = ds.getIterator();
    751                        final BooleanIterator biter = getBooleanIterator(selection);
    752
    753                        while (biter.hasNext() && oiter.hasNext()) {
    754                                data[biter.index] = (short) ds.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    755                        }
    756                } else {
    757                        final short dv = (short) DTypeUtils.toLong(obj); // PRIM_TYPE // FROM_OBJECT
    758                        final BooleanIterator biter = getBooleanIterator(selection);
    759
    760                        while (biter.hasNext()) {
    761                                data[biter.index] = dv;
    762                        }
    763                }
    764                return this;
    765        }
    766
    767        @Override
    768        public ShortDataset setBy1DIndex(final Object obj, final Dataset index) {
    769                setDirty();
    770                if (obj instanceof Dataset) {
    771                        final Dataset ds = (Dataset) obj;
    772                        if (index.getSize() != ds.getSize()) {
    773                                throw new IllegalArgumentException(
    774                                                "Number of items in index dataset does not match number of items in dataset");
    775                        }
    776
    777                        final IndexIterator oiter = ds.getIterator();
    778                        final IntegerIterator iter = new IntegerIterator(index, size);
    779
    780                        while (iter.hasNext() && oiter.hasNext()) {
    781                                data[iter.index] = (short) ds.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    782                        }
    783                } else {
    784                        final short dv = (short) DTypeUtils.toLong(obj); // PRIM_TYPE // FROM_OBJECT
    785                        IntegerIterator iter = new IntegerIterator(index, size);
    786
    787                        while (iter.hasNext()) {
    788                                data[iter.index] = dv;
    789                        }
    790                }
    791                return this;
    792        }
    793
    794        @Override
    795        public ShortDataset setByIndexes(final Object obj, final Object... indexes) {
    796                setDirty();
    797                final IntegersIterator iter = new IntegersIterator(shape, indexes);
    798                final int[] pos = iter.getPos();
    799
    800                if (obj instanceof Dataset) {
    801                        final Dataset ds = (Dataset) obj;
    802                        if (ShapeUtils.calcSize(iter.getShape()) != ds.getSize()) {
    803                                throw new IllegalArgumentException(
    804                                                "Number of items in index datasets does not match number of items in dataset");
    805                        }
    806
    807                        final IndexIterator oiter = ds.getIterator();
    808
    809                        while (iter.hasNext() && oiter.hasNext()) {
    810                                setItem((short) ds.getElementLongAbs(oiter.index), pos); // GET_ELEMENT_WITH_CAST
    811                        }
    812                } else {
    813                        final short dv = (short) DTypeUtils.toLong(obj); // PRIM_TYPE // FROM_OBJECT
    814
    815                        while (iter.hasNext()) {
    816                                setItem(dv, pos);
    817                        }
    818                }
    819                return this;
    820        }
    821
    822        @Override
    823        ShortDataset setSlicedView(Dataset view, Dataset d) {
    824                setDirty();
    825                final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(view, d);
    826
    827                while (it.hasNext()) {
    828                        data[it.aIndex] = (short) it.bLong; // BCAST_WITH_CAST d.getElementLongAbs(it.bIndex);
    829                }
    830                return this;
    831        }
    832
    833        @Override
    834        public ShortDataset setSlice(final Object obj, final IndexIterator siter) {
    835                setDirty();
    836
    837                if (obj instanceof IDataset) {
    838                        final IDataset ds = (IDataset) obj;
    839                        final int[] oshape = ds.getShape();
    840
    841                        if (!ShapeUtils.areShapesCompatible(siter.getShape(), oshape)) {
    842                                throw new IllegalArgumentException(String.format(
    843                                                "Input dataset is not compatible with slice: %s cf %s", Arrays.toString(oshape),
    844                                                Arrays.toString(siter.getShape())));
    845                        }
    846
    847                        if (ds instanceof Dataset) {
    848                                final Dataset ads = (Dataset) ds;
    849                                final IndexIterator oiter = ads.getIterator();
    850
    851                                while (siter.hasNext() && oiter.hasNext())
    852                                        data[siter.index] = (short) ads.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    853                        } else {
    854                                final IndexIterator oiter = new PositionIterator(oshape);
    855                                final int[] pos = oiter.getPos();
    856
    857                                while (siter.hasNext() && oiter.hasNext())
    858                                        data[siter.index] = ds.getShort(pos); // PRIM_TYPE
    859                        }
    860                } else {
    861                        try {
    862                                short v = (short) DTypeUtils.toLong(obj); // PRIM_TYPE // FROM_OBJECT
    863
    864                                while (siter.hasNext())
    865                                        data[siter.index] = v;
    866                        } catch (IllegalArgumentException e) {
    867                                throw new IllegalArgumentException("Object for setting slice is not a dataset or number");
    868                        }
    869                }
    870                return this;
    871        }
    872
    873        @Override
    874        public void copyItemsFromAxes(final int[] pos, final boolean[] axes, final Dataset dest) {
    875                short[] ddata = (short[]) dest.getBuffer(); // PRIM_TYPE
    876
    877                SliceIterator siter = getSliceIteratorFromAxes(pos, axes);
    878                int[] sshape = ShapeUtils.squeezeShape(siter.getShape(), false);
    879
    880                IndexIterator diter = dest.getSliceIterator(null, sshape, null);
    881
    882                if (ddata.length < ShapeUtils.calcSize(sshape)) {
    883                        throw new IllegalArgumentException("destination array is not large enough");
    884                }
    885
    886                dest.setDirty();
    887                while (siter.hasNext() && diter.hasNext()) {
    888                        ddata[diter.index] = data[siter.index];
    889                }
    890        }
    891
    892        @Override
    893        public void setItemsOnAxes(final int[] pos, final boolean[] axes, final Object src) {
    894                setDirty();
    895                short[] sdata = (short[]) src; // PRIM_TYPE
    896
    897                SliceIterator siter = getSliceIteratorFromAxes(pos, axes);
    898
    899                if (sdata.length < ShapeUtils.calcSize(siter.getShape())) {
    900                        throw new IllegalArgumentException("destination array is not large enough");
    901                }
    902
    903                for (int i = 0; siter.hasNext(); i++) {
    904                        data[siter.index] = sdata[i];
    905                }
    906        }
    907
    908        private List<int[]> findPositions(final short value) { // PRIM_TYPE
    909                IndexIterator iter = getIterator(true);
    910                List<int[]> posns = new ArrayList<int[]>();
    911                int[] pos = iter.getPos();
    912
    913                {
    914                        while (iter.hasNext()) {
    915                                if (data[iter.index] == value) {
    916                                        posns.add(pos.clone());
    917                                }
    918                        }
    919                }
    920                return posns;
    921        }
    922
    923        @Override
    924        public int[] maxPos(boolean... ignoreInvalids) {
    925                StatisticsMetadata<Number> md = getStats(); // PRIM_TYPE
    926                // StatisticsMetadata<Number> md = getStats(); // BOOLEAN_USE
    927                // StatisticsMetadata<String> md = getStringStats(); // OBJECT_USE
    928                List<int[]> max = md.getMaximumPositions(ignoreInvalids);
    929
    930                if (max == null) {
    931                        max = findPositions(md.getMaximum(ignoreInvalids).shortValue()); // PRIM_TYPE
    932                        // max = findPositions(md.getMaximum(ignoreInvalids).intValue() != 0); // BOOLEAN_USE
    933                        // max = findPositions(md.getMaximum(ignoreInvalids).toString()); // OBJECT_USE
    934
    935                        md.setMaximumPositions(max);
    936                }
    937
    938                return max.get(0); // first maximum
    939        }
    940
    941        @Override
    942        public int[] minPos(boolean... ignoreInvalids) {
    943                StatisticsMetadata<Number> md = getStats(); // PRIM_TYPE
    944                // StatisticsMetadata<Number> md = getStats(); // BOOLEAN_USE
    945                // StatisticsMetadata<String> md = getStringStats(); // OBJECT_USE
    946                List<int[]> min = md.getMinimumPositions(ignoreInvalids);
    947
    948                if (min == null) {
    949                        min = findPositions(md.getMinimum(ignoreInvalids).shortValue()); // PRIM_TYPE
    950                        // min = findPositions(md.getMinimum(ignoreInvalids).intValue() != 0); // BOOLEAN_USE
    951                        // min = findPositions(md.getMinimum(ignoreInvalids).toString()); // OBJECT_USE
    952
    953                        md.setMinimumPositions(min);
    954                }
    955
    956                return min.get(0); // first minimum
    957        }
    958
    959        @Override
    960        public boolean containsNans() {
    961                return false;
    962        }
    963
    964        @Override
    965        public boolean containsInfs() {
    966                return false;
    967        }
    968
    969        @Override
    970        public boolean containsInvalidNumbers() {
    971                return false;
    972        }
    973
    974        @Override
    975        public ShortDataset iadd(final Object b) {
    976                setDirty();
    977                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    978                boolean useLong = bds.getElementClass().equals(Long.class);
    979                if (bds.getSize() == 1) {
    980                        final IndexIterator it = getIterator();
    981                        final int bOffset = bds.getOffset();
    982                        if (useLong) {
    983                                final long lb = bds.getElementLongAbs(bOffset);
    984                                while (it.hasNext()) {
    985                                        data[it.index] += lb;
    986                                }
    987                        } else {
    988                                final double db = bds.getElementDoubleAbs(bOffset);
    989                                while (it.hasNext()) {
    990                                        data[it.index] += db;
    991                                }
    992                        }
    993                } else {
    994                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    995                        it.setOutputDouble(!useLong);
    996                        if (useLong) {
    997                                while (it.hasNext()) {
    998                                        data[it.aIndex] += it.bLong;
    999                                }
    1000                        } else {
    1001                                while (it.hasNext()) {
    1002                                        data[it.aIndex] += it.bDouble;
    1003                                }
    1004                        }
    1005                }
    1006                return this;
    1007        }
    1008
    1009        @Override
    1010        public ShortDataset isubtract(final Object b) {
    1011                setDirty();
    1012                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1013                boolean useLong = bds.getElementClass().equals(Long.class);
    1014                if (bds.getSize() == 1) {
    1015                        final IndexIterator it = getIterator();
    1016                        final int bOffset = bds.getOffset();
    1017                        if (useLong) {
    1018                                final long lb = bds.getElementLongAbs(bOffset);
    1019                                while (it.hasNext()) {
    1020                                        data[it.index] -= lb;
    1021                                }
    1022                        } else {
    1023                                final double db = bds.getElementDoubleAbs(bOffset);
    1024                                while (it.hasNext()) {
    1025                                        data[it.index] -= db;
    1026                                }
    1027                        }
    1028                } else {
    1029                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1030                        if (useLong) {
    1031                                it.setOutputDouble(false);
    1032                                while (it.hasNext()) {
    1033                                        data[it.aIndex] -= it.bLong;
    1034                                }
    1035                        } else {
    1036                                it.setOutputDouble(true);
    1037                                while (it.hasNext()) {
    1038                                        data[it.aIndex] -= it.bDouble;
    1039                                }
    1040                        }
    1041                }
    1042                return this;
    1043        }
    1044
    1045        @Override
    1046        public ShortDataset imultiply(final Object b) {
    1047                setDirty();
    1048                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1049                boolean useLong = bds.getElementClass().equals(Long.class);
    1050                if (bds.getSize() == 1) {
    1051                        final IndexIterator it = getIterator();
    1052                        final int bOffset = bds.getOffset();
    1053                        if (useLong) {
    1054                                final long lb = bds.getElementLongAbs(bOffset);
    1055                                while (it.hasNext()) {
    1056                                        data[it.index] *= lb;
    1057                                }
    1058                        } else {
    1059                                final double db = bds.getElementDoubleAbs(bOffset);
    1060                                while (it.hasNext()) {
    1061                                        data[it.index] *= db;
    1062                                }
    1063                        }
    1064                } else {
    1065                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1066                        it.setOutputDouble(!useLong);
    1067                        if (useLong) {
    1068                                while (it.hasNext()) {
    1069                                        data[it.aIndex] *= it.bLong;
    1070                                }
    1071                        } else {
    1072                                while (it.hasNext()) {
    1073                                        data[it.aIndex] *= it.bDouble;
    1074                                }
    1075                        }
    1076                }
    1077                return this;
    1078        }
    1079
    1080        @Override
    1081        public ShortDataset idivide(final Object b) {
    1082                setDirty();
    1083                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1084                boolean useLong = bds.getElementClass().equals(Long.class);
    1085                if (bds.getSize() == 1) {
    1086                        final int bOffset = bds.getOffset();
    1087                        if (useLong) {
    1088                                final long lb = bds.getElementLongAbs(bOffset);
    1089                                if (lb == 0) { // INT_USE
    1090                                        fill(0); // INT_USE
    1091                                } else { // INT_USE
    1092                                final IndexIterator it = getIterator();
    1093                                while (it.hasNext()) {
    1094                                        data[it.index] /= lb;
    1095                                }
    1096                                } // INT_USE
    1097                        } else {
    1098                                final double db = bds.getElementDoubleAbs(bOffset);
    1099                                if (db == 0) { // INT_USE
    1100                                        fill(0); // INT_USE
    1101                                } else { // INT_USE
    1102                                final IndexIterator it = getIterator();
    1103                                while (it.hasNext()) {
    1104                                        data[it.index] /= db;
    1105                                }
    1106                                } // INT_USE
    1107                        }
    1108                } else {
    1109                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1110                        it.setOutputDouble(!useLong);
    1111                        if (useLong) {
    1112                                while (it.hasNext()) {
    1113                                        if (it.bLong == 0) { // INT_USE
    1114                                                data[it.aIndex] = 0; // INT_USE
    1115                                        } else { // INT_USE
    1116                                        data[it.aIndex] /= it.bLong;
    1117                                        } // INT_USE
    1118                                }
    1119                        } else {
    1120                                while (it.hasNext()) {
    1121                                        if (it.bDouble == 0) { // INT_USE
    1122                                                data[it.aIndex] = 0; // INT_USE
    1123                                        } else { // INT_USE
    1124                                        data[it.aIndex] /= it.bDouble;
    1125                                        } // INT_USE
    1126                                }
    1127                        }
    1128                }
    1129                return this;
    1130        }
    1131
    1132        @Override
    1133        public ShortDataset ifloor() {
    1134                return this;
    1135        }
    1136
    1137        @Override
    1138        public ShortDataset iremainder(final Object b) {
    1139                setDirty();
    1140                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1141                boolean useLong = bds.getElementClass().equals(Long.class);
    1142                if (bds.getSize() == 1) {
    1143                        final int bOffset = bds.getOffset();
    1144                        if (useLong) {
    1145                                final long lb = bds.getElementLongAbs(bOffset);
    1146                                if (lb == 0) { // INT_USE
    1147                                        fill(0); // INT_USE
    1148                                } else { // INT_USE
    1149                                final IndexIterator it = getIterator();
    1150                                while (it.hasNext()) {
    1151                                        data[it.index] %= lb;
    1152                                }
    1153                                } // INT_USE
    1154                        } else {
    1155                                final long lb = bds.getElementLongAbs(bOffset);
    1156                                if (lb == 0) { // INT_USE
    1157                                        fill(0); // INT_USE
    1158                                } else { // INT_USE
    1159                                final IndexIterator it = getIterator();
    1160                                while (it.hasNext()) {
    1161                                        data[it.index] %= lb;
    1162                                }
    1163                                } // INT_USE
    1164                        }
    1165                } else {
    1166                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1167                        it.setOutputDouble(!useLong);
    1168                        if (useLong) {
    1169                                while (it.hasNext()) {
    1170                                try {
    1171                                                data[it.aIndex] %= it.bLong; // INT_EXCEPTION
    1172                                } catch (ArithmeticException e) {
    1173                                        data[it.aIndex] = 0;
    1174                                }
    1175                                }
    1176                        } else {
    1177                                while (it.hasNext()) {
    1178                                        data[it.aIndex] %= it.bDouble;
    1179                                }
    1180                        }
    1181                }
    1182                return this;
    1183        }
    1184
    1185        @Override
    1186        public ShortDataset ipower(final Object b) {
    1187                setDirty();
    1188                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1189                if (bds.getSize() == 1) {
    1190                        final int bOffset = bds.getOffset();
    1191                        final double vr = bds.getElementDoubleAbs(bOffset);
    1192                        final IndexIterator it = getIterator();
    1193                        if (bds.isComplex()) {
    1194                                final double vi = bds.getElementDoubleAbs(bOffset + 1);
    1195                                if (vi == 0) {
    1196                                        while (it.hasNext()) {
    1197                                                final double v = Math.pow(data[it.index], vr);
    1198                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1199                                                        data[it.index] = 0; // INT_USE
    1200                                                } else { // INT_USE
    1201                                                data[it.index] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1202                                                } // INT_USE
    1203                                        }
    1204                                } else {
    1205                                        final Complex zv = new Complex(vr, vi);
    1206                                        while (it.hasNext()) {
    1207                                                Complex zd = new Complex(data[it.index], 0);
    1208                                                final double v = zd.pow(zv).getReal();
    1209                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1210                                                        data[it.index] = 0; // INT_USE
    1211                                                } else { // INT_USE
    1212                                                data[it.index] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1213                                                } // INT_USE
    1214                                        }
    1215                                }
    1216                        } else {// NAN_OMIT
    1217                                while (it.hasNext()) {
    1218                                        final double v = Math.pow(data[it.index], vr);
    1219                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1220                                                data[it.index] = 0; // INT_USE
    1221                                        } else { // INT_USE
    1222                                        data[it.index] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1223                                        } // INT_USE
    1224                                }
    1225                        }
    1226                } else {
    1227                        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
    1228                        it.setOutputDouble(true);
    1229                        if (bds.isComplex()) {
    1230                                while (it.hasNext()) {
    1231                                        final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1));
    1232                                        final double v = new Complex(it.aDouble, 0).pow(zv).getReal();
    1233                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1234                                                data[it.aIndex] = 0; // INT_USE
    1235                                        } else { // INT_USE
    1236                                        data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1237                                        } // INT_USE
    1238                                }
    1239                        } else {// NAN_OMIT
    1240                                while (it.hasNext()) {
    1241                                        final double v = Math.pow(it.aDouble, it.bDouble);
    1242                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1243                                                data[it.aIndex] = 0; // INT_USE
    1244                                        } else { // INT_USE
    1245                                        data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1246                                        } // INT_USE
    1247                                }
    1248                        }
    1249                }
    1250                return this;
    1251        }
    1252
    1253        @Override
    1254        public double residual(final Object b, final Dataset w, boolean ignoreNaNs) {
    1255                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1256                final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
    1257                it.setOutputDouble(true);
    1258                double sum = 0;
    1259                double comp = 0;
    1260                {
    1261                        if (w == null) {
    1262                                while (it.hasNext()) {
    1263                                        final double diff = it.aDouble - it.bDouble;
    1264                                        final double err = diff * diff - comp;
    1265                                        final double temp = sum + err;
    1266                                        comp = (temp - sum) - err;
    1267                                        sum = temp;
    1268                                }
    1269                        } else {
    1270                                IndexIterator itw = w.getIterator();
    1271                                while (it.hasNext() && itw.hasNext()) {
    1272                                        final double diff = it.aDouble - it.bDouble;
    1273                                        final double err = diff * diff * w.getElementDoubleAbs(itw.index) - comp;
    1274                                        final double temp = sum + err;
    1275                                        comp = (temp - sum) - err;
    1276                                        sum = temp;
    1277                                }
    1278                        }
    1279                }
    1280                return sum;
    1281        }
    1282}