File: HDF5TimeDurationMDArray.java

package info (click to toggle)
libsis-jhdf5-java 19.04.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,668 kB
  • sloc: java: 79,644; ansic: 18,986; sh: 309; makefile: 49; xml: 12
file content (577 lines) | stat: -rw-r--r-- 15,683 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
 * Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.systemsx.cisd.hdf5;

import java.util.Iterator;

import ch.systemsx.cisd.base.mdarray.MDAbstractArray;
import ch.systemsx.cisd.base.mdarray.MDLongArray;

/**
 * A multi-dimensional array of time durations.
 * 
 * @author Bernd Rinn
 */
public class HDF5TimeDurationMDArray extends MDAbstractArray<Long>
{
    private static final long serialVersionUID = 1L;

    final MDLongArray timeDurations;

    final HDF5TimeUnit timeUnit;

    /**
     * Creates an array of <var>timeDurations</var> using a common <var>timeUnit</var>.
     */
    public HDF5TimeDurationMDArray(MDLongArray timeDurations, HDF5TimeUnit timeUnit)
    {
        super(timeDurations.dimensions(), timeDurations.getAsFlatArray().length, 0);
        this.timeDurations = timeDurations;
        this.timeUnit = timeUnit;
    }

    /**
     * Creates an array of <var>timeDurations</var> using a common <var>timeUnit</var>.
     */
    public HDF5TimeDurationMDArray(long[] timeDurations, int[] dimensions, HDF5TimeUnit timeUnit)
    {
        super(dimensions, timeDurations.length, 0);
        this.timeDurations = new MDLongArray(timeDurations, dimensions, true);
        this.timeUnit = timeUnit;
    }

    /**
     * Creates an array of dimension <var>dimensions</var> with <var>timeUnit</var>.
     */
    public HDF5TimeDurationMDArray(int[] dimensions, HDF5TimeUnit timeUnit)
    {
        this(new long[getLength(dimensions, 0)], dimensions, timeUnit);
    }

    /**
     * Creates an array of <var>timeDurations</var> using a common <var>timeUnit</var>.
     */
    public HDF5TimeDurationMDArray(HDF5TimeDuration[] timeDurations, int[] dimensions,
            HDF5TimeUnit timeUnit)
    {
        super(dimensions, timeDurations.length, 0);
        HDF5TimeUnit smallestTimeUnit = getSmallestUnit(timeDurations);
        final long[] durations = new long[timeDurations.length];
        if (timeUnit != smallestTimeUnit)
        {
            for (int i = 0; i < timeDurations.length; ++i)
            {
                durations[i] = timeUnit.convert(timeDurations[i]);
            }
        } else
        {
            for (int i = 0; i < timeDurations.length; ++i)
            {
                durations[i] = timeDurations[i].getValue();
            }
        }
        this.timeDurations = new MDLongArray(durations, dimensions, true);
        this.timeUnit = timeUnit;
    }

    /**
     * Creates an array of <var>timeDurations</var> using the smallest time unit.
     */
    public HDF5TimeDurationMDArray(HDF5TimeDuration[] timeDurations, int[] dimensions)
    {
        super(dimensions, timeDurations.length, 0);
        HDF5TimeUnit smallestTimeUnit = getSmallestUnit(timeDurations);
        final long[] durations = new long[timeDurations.length];
        for (int i = 0; i < timeDurations.length; ++i)
        {
            durations[i] = smallestTimeUnit.convert(timeDurations[i]);
        }
        this.timeDurations = new MDLongArray(durations, dimensions, true);
        this.timeUnit = smallestTimeUnit;
    }

    private static HDF5TimeUnit getSmallestUnit(HDF5TimeDuration[] timeDurations)
    {
        HDF5TimeUnit unit = timeDurations[0].getUnit();
        for (int i = 1; i < timeDurations.length; ++i)
        {
            final HDF5TimeUnit u = timeDurations[i].getUnit();
            if (u != unit)
            {
                if (u.ordinal() < unit.ordinal())
                {
                    unit = u;
                }
            }
        }
        return unit;
    }

    /**
     * Returns the time unit.
     */
    public HDF5TimeUnit getUnit()
    {
        return timeUnit;
    }

    /**
     * Returns the time duration values as a flat array.
     */
    @Override
    public long[] getAsFlatArray()
    {
        return timeDurations.getAsFlatArray();
    }

    /**
     * Returns the number of elements.
     */
    @Override
    public int[] dimensions()
    {
        return timeDurations.dimensions();
    }

    /**
     * Returns the number of elements.
     */
    @Override
    public long[] longDimensions()
    {
        return timeDurations.longDimensions();
    }

    /**
     * Returns the number of elements.
     */
    public int getLength()
    {
        return timeDurations.size();
    }

    /**
     * Returns the time duration values.
     */
    public MDLongArray getValues()
    {
        return timeDurations;
    }
    
    /**
     * Returns the time duration values in the given <var>targetUnit</var>.
     */
    public MDLongArray getValues(HDF5TimeUnit targetUnit)
    {
        if (targetUnit == timeUnit)
        {
            return timeDurations;
        }
        final long[] sourceDurations = timeDurations.getAsFlatArray();
        final long[] targetDurations = new long[sourceDurations.length];
        for (int i = 0; i < targetDurations.length; ++i)
        {
            targetDurations[i] = targetUnit.convert(sourceDurations[i], timeUnit);
        }
        return new MDLongArray(targetDurations, timeDurations.dimensions());
    }

    /**
     * Returns the time duration values as a flat array in the given <var>targetUnit</var>.
     */
    public long[] getAsFlatArray(HDF5TimeUnit targetUnit)
    {
        return getValues(targetUnit).getAsFlatArray();
    }

    /**
     * Returns the value of a one-dimensional array at the position defined by <var>index</var> in
     * the given <var>targetUnit</var>.
     * <p>
     * <b>Do not call for arrays other than one-dimensional!</b>
     */
    public HDF5TimeDuration get(HDF5TimeUnit targetUnit, int index)
    {
        if (targetUnit == timeUnit)
        {
            return new HDF5TimeDuration(timeDurations.get(index), timeUnit);
        } else
        {
            return new HDF5TimeDuration(targetUnit.convert(timeDurations.get(index), timeUnit),
                    targetUnit);
        }
    }

    /**
     * Returns the value of a two-dimensional array at the position defined by <var>indexX</var> and
     * <var>indexY</var> in the given <var>targetUnit</var>.
     * <p>
     * <b>Do not call for arrays other than two-dimensional!</b>
     */
    public HDF5TimeDuration get(HDF5TimeUnit targetUnit, int indexX, int indexY)
    {
        if (targetUnit == timeUnit)
        {
            return new HDF5TimeDuration(timeDurations.get(indexX, indexY), timeUnit);
        } else
        {
            return new HDF5TimeDuration(targetUnit.convert(timeDurations.get(indexX, indexY),
                    timeUnit), targetUnit);
        }
    }

    /**
     * Returns the value of a three-dimensional array at the position defined by <var>indexX</var>,
     * <var>indexY</var> and <var>indexZ</var> in the given <var>targetUnit</var>.
     * <p>
     * <b>Do not call for arrays other than three-dimensional!</b>
     */
    public HDF5TimeDuration get(HDF5TimeUnit targetUnit, int indexX, int indexY, int indexZ)
    {
        if (targetUnit == timeUnit)
        {
            return new HDF5TimeDuration(timeDurations.get(indexX, indexY, indexZ), timeUnit);
        } else
        {
            return new HDF5TimeDuration(targetUnit.convert(
                    timeDurations.get(indexX, indexY, indexZ), timeUnit), targetUnit);
        }
    }

    /**
     * Returns the value of array at the position defined by <var>indices</var> in the given
     * <var>targetUnit</var>.
     */
    public HDF5TimeDuration get(HDF5TimeUnit targetUnit, int... indices)
    {
        if (targetUnit == timeUnit)
        {
            return new HDF5TimeDuration(timeDurations.get(indices), timeUnit);
        } else
        {
            return new HDF5TimeDuration(targetUnit.convert(timeDurations.get(indices), timeUnit),
                    targetUnit);
        }
    }

    /**
     * Returns the value element <var>index</var>.
     * <p>
     * <b>Do not call for arrays other than one-dimensional!</b>
     */
    public long getValue(int index)
    {
        return timeDurations.get(index);
    }

    /**
     * Returns the value element <var>(indexX,indexY)</var>.
     * <p>
     * <b>Do not call for arrays other than two-dimensional!</b>
     */
    public long getValue(int indexX, int indexY)
    {
        return timeDurations.get(indexX, indexY);
    }

    /**
     * Returns the value element <var>(indexX,indexY,indexZ)</var>.
     * <p>
     * <b>Do not call for arrays other than three-dimensional!</b>
     */
    public long getValue(int indexX, int indexY, int indexZ)
    {
        return timeDurations.get(indexX, indexY, indexZ);
    }

    /**
     * Returns the value element <var>indices</var>.
     */
    public long getValue(int... indices)
    {
        return timeDurations.get(indices);
    }

    /**
     * Returns the value element <var>index</var> in the given <var>targetUnit</var>.
     * <p>
     * <b>Do not call for arrays other than one-dimensional!</b>
     */
    public long getValue(HDF5TimeUnit targetUnit, int index)
    {
        return (targetUnit == timeUnit) ? timeDurations.get(index) : targetUnit.convert(
                timeDurations.get(index), timeUnit);
    }

    /**
     * Returns the value element <var>index</var> in the given <var>targetUnit</var>.
     * <p>
     * <b>Do not call for arrays other than one-dimensional!</b>
     */
    public long getValue(HDF5TimeUnit targetUnit, int indexX, int indexY)
    {
        return (targetUnit == timeUnit) ? timeDurations.get(indexX, indexY) : targetUnit.convert(
                timeDurations.get(indexX, indexY), timeUnit);
    }

    /**
     * Returns the value element <var>index</var> in the given <var>targetUnit</var>.
     * <p>
     * <b>Do not call for arrays other than one-dimensional!</b>
     */
    public long getValue(HDF5TimeUnit targetUnit, int indexX, int indexY, int indexZ)
    {
        return (targetUnit == timeUnit) ? timeDurations.get(indexX, indexY, indexZ) : targetUnit
                .convert(timeDurations.get(indexX, indexY, indexZ), timeUnit);
    }

    /**
     * Returns the value element <var>index</var> in the given <var>targetUnit</var>.
     * <p>
     * <b>Do not call for arrays other than one-dimensional!</b>
     */
    public long getValue(HDF5TimeUnit targetUnit, int... indices)
    {
        return (targetUnit == timeUnit) ? timeDurations.get(indices) : targetUnit.convert(
                timeDurations.get(indices), timeUnit);
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + timeDurations.hashCode();
        result = prime * result + ((timeUnit == null) ? 0 : timeUnit.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        final HDF5TimeDurationMDArray other = (HDF5TimeDurationMDArray) obj;
        if (timeDurations.equals(other.timeDurations) == false)
        {
            return false;
        }
        if (timeUnit != other.timeUnit)
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "HDF5TimeDurationArray [timeDurations=" + timeDurations.toString() + ", timeUnit="
                + timeUnit + "]";
    }

    @Override
    public Long getAsObject(int linearIndex)
    {
        return timeDurations.getAsObject(linearIndex);
    }

    @Override
    public Long getAsObject(int... indices)
    {
        return timeDurations.getAsObject(indices);
    }

    @Override
    public void setToObject(Long value, int... indices)
    {
        timeDurations.setToObject(value, indices);
    }

    @Override
    public void setToObject(Long value, int linearIndex)
    {
        timeDurations.setToObject(value, linearIndex);
    }

    @Override
    public long[] getCopyAsFlatArray()
    {
        return timeDurations.getCopyAsFlatArray();
    }

    @Override
    protected void adaptCapacityHyperRows()
    {
        // Noop
    }

    @Override
    public int capacity()
    {
        return timeDurations.capacity();
    }

    @Override
    public int incNumberOfHyperRows(int count)
    {
        return timeDurations.incNumberOfHyperRows(count);
    }

    @Override
    public int rank()
    {
        return timeDurations.rank();
    }

    @Override
    public int size(int dim)
    {
        return timeDurations.size(dim);
    }

    @Override
    public int size()
    {
        return timeDurations.size();
    }

    @Override
    public int numberOfHyperRows()
    {
        return timeDurations.numberOfHyperRows();
    }

    @Override
    public int decNumberOfHyperRows(int count)
    {
        return timeDurations.decNumberOfHyperRows(count);
    }

    @Override
    public int computeIndex(int... indices)
    {
        return timeDurations.computeIndex(indices);
    }

    @Override
    public int[] computeReverseIndex(int linearIndex)
    {
        return timeDurations.computeReverseIndex(linearIndex);
    }

    @Override
    public int computeIndex(int indexX, int indexY)
    {
        return timeDurations.computeIndex(indexX, indexY);
    }

    @Override
    public int computeIndex(int indexX, int indexY, int indexZ)
    {
        return timeDurations.computeIndex(indexX, indexY, indexZ);
    }

    @Override
    public Iterator<ch.systemsx.cisd.base.mdarray.MDAbstractArray<Long>.ArrayEntry> iterator()
    {
        return timeDurations.iterator();
    }

    /**
     * @see MDLongArray#get(int)
     */
    public long get(int index)
    {
        return timeDurations.get(index);
    }

    /**
     * @see MDLongArray#get(int, int)
     */
    public long get(int indexX, int indexY)
    {
        return timeDurations.get(indexX, indexY);
    }

    /**
     * @see MDLongArray#get(int, int, int)
     */
    public long get(int indexX, int indexY, int indexZ)
    {
        return timeDurations.get(indexX, indexY, indexZ);
    }

    /**
     * @see MDLongArray#get(int[])
     */
    public long get(int... indices)
    {
        return timeDurations.get(indices);
    }

    /**
     * @see MDLongArray#set(long, int)
     */
    public void set(long value, int index)
    {
        timeDurations.set(value, index);
    }

    /**
     * @see MDLongArray#set(long, int, int)
     */
    public void set(long value, int indexX, int indexY)
    {
        timeDurations.set(value, indexX, indexY);
    }

    /**
     * @see MDLongArray#set(long, int, int, int)
     */
    public void set(long value, int indexX, int indexY, int indexZ)
    {
        timeDurations.set(value, indexX, indexY, indexZ);
    }

    /**
     * @see MDLongArray#set(long, int[])
     */
    public void set(long value, int... indices)
    {
        timeDurations.set(value, indices);
    }

    /**
     * @see MDLongArray#toMatrix()
     */
    public long[][] toMatrix()
    {
        return timeDurations.toMatrix();
    }

}