|
| 1 | +package com.arrayfire; |
| 2 | + |
| 3 | +import static com.arrayfire.Util.*; |
| 4 | +import static com.arrayfire.ArrayFire.range; |
| 5 | +import static com.arrayfire.ArrayFire.add; |
| 6 | +import static com.arrayfire.ArrayFire.mul; |
| 7 | + |
| 8 | +public class Seq extends AFLibLoader { |
| 9 | + // Start position of the sequence |
| 10 | + private double begin; |
| 11 | + // End position of the sequence (inclusive) |
| 12 | + private double end; |
| 13 | + // Step size between sequence values |
| 14 | + private double step; |
| 15 | + |
| 16 | + private long size; |
| 17 | + private boolean gfor; // always false until implemented |
| 18 | + |
| 19 | + public Seq() { |
| 20 | + this(0); |
| 21 | + } |
| 22 | + |
| 23 | + public Seq(double length) { |
| 24 | + if (length < 0) { |
| 25 | + init(0, length, 1); |
| 26 | + } else { |
| 27 | + init(0, length - 1, 1); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public Seq(double begin, double end) throws Exception { |
| 32 | + this(begin, end, 1); |
| 33 | + } |
| 34 | + |
| 35 | + public Seq(double begin, double end, double step) throws Exception { |
| 36 | + if (step == 0 && begin != end) { // Span |
| 37 | + throw new IllegalArgumentException("Invalid step size."); |
| 38 | + } |
| 39 | + if ((signbit(end) == signbit(begin)) && (signbit(end - begin) != signbit(step))) { |
| 40 | + throw new Exception("Sequence is invalid"); |
| 41 | + } |
| 42 | + init(begin, end, step); |
| 43 | + } |
| 44 | + |
| 45 | + public Seq(Seq other) { |
| 46 | + set(other); |
| 47 | + } |
| 48 | + |
| 49 | + public void set(Seq other) { |
| 50 | + init(other.begin, other.end, other.step); |
| 51 | + } |
| 52 | + |
| 53 | + public Seq negate() throws Exception { |
| 54 | + return new Seq(-begin, -end, -step); |
| 55 | + } |
| 56 | + |
| 57 | + public Seq addOffset(double offset) throws Exception { |
| 58 | + return new Seq(begin + offset, end + offset, step); |
| 59 | + } |
| 60 | + |
| 61 | + public Seq subtractOffset(double offset) throws Exception { |
| 62 | + return new Seq(begin - offset, end - offset, step); |
| 63 | + } |
| 64 | + |
| 65 | + public Seq spaceBy(double factor) throws Exception { |
| 66 | + return new Seq(begin * factor, end * factor, step * factor); |
| 67 | + } |
| 68 | + |
| 69 | + public Array toArray() throws Exception { |
| 70 | + double diff = end - begin; |
| 71 | + int len = (int)((diff + Math.abs(step) * (signbit(diff) == 0 ? 1 : -1)) / step); |
| 72 | + Array tmp = range(len); |
| 73 | + Array res = new Array(); |
| 74 | + mul(res, (float) step, tmp); |
| 75 | + add(res, (float)begin, res); |
| 76 | + return res; |
| 77 | + } |
| 78 | + |
| 79 | + private void init(double begin, double end, double step) { |
| 80 | + this.begin = begin; |
| 81 | + this.end = end; |
| 82 | + this.step = step; |
| 83 | + this.gfor = false; |
| 84 | + this.size = (long)(step != 0 ? Math.abs((end - begin) / step) + 1 : 0); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean equals(Object obj) { |
| 89 | + if (obj == this) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + if (obj == null || obj.getClass() != this.getClass()) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + Seq seq = (Seq) obj; |
| 96 | + return this.begin == seq.begin && this.end == seq.end && |
| 97 | + this.step == seq.step && size == seq.size; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public int hashCode() { |
| 102 | + final int prime = 31; |
| 103 | + int result = 1; |
| 104 | + result = prime * result + (int) ((int)begin ^ ((int)begin >>> 32)); |
| 105 | + result = prime * result + (int) ((int) end ^ ((int) end >>> 32)); |
| 106 | + result = prime * result + (int) ((int) step ^ ((int) step >>> 32)); |
| 107 | + result = prime * result + (int) ((int) size ^ ((int) size >>> 32)); |
| 108 | + return result; |
| 109 | + } |
| 110 | +} |
0 commit comments