index int64 | repo_id string | file_path string | content string |
|---|---|---|---|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/PointZ3Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
final class PointZ3Form extends Z3Form<PointZ3> {
@Override
public String tag() {
return "point";
}
@Override
public PointZ3 unit() {
return PointZ3.origin();
}
@Override
public Class<?> type() {
return PointZ3.class;
}
@Override
public long getXMin(PointZ3 point) {
return point.x;
}
@Override
public long getYMin(PointZ3 point) {
return point.y;
}
@Override
public long getZMin(PointZ3 point) {
return point.z;
}
@Override
public long getXMax(PointZ3 point) {
return point.x;
}
@Override
public long getYMax(PointZ3 point) {
return point.y;
}
@Override
public long getZMax(PointZ3 point) {
return point.z;
}
@Override
public boolean contains(PointZ3 outer, PointZ3 inner) {
return outer.contains(inner);
}
@Override
public boolean intersects(PointZ3 s, PointZ3 t) {
return s.intersects(t);
}
@Override
public Item mold(PointZ3 point) {
if (point != null) {
return Record.create(1).attr(tag(), Record.create(3)
.item(point.x).item(point.y).item(point.z));
} else {
return Item.extant();
}
}
@Override
public PointZ3 cast(Item item) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final long x = header.getItem(0).longValue(0L);
final long y = header.getItem(1).longValue(0L);
final long z = header.getItem(2).longValue(0L);
return new PointZ3(x, y, z);
} else {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Precision.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Form;
import swim.structure.Kind;
import swim.structure.Value;
import swim.util.Murmur3;
public final class Precision implements Debug {
final int bits;
Precision(int bits) {
this.bits = bits;
}
public boolean isDefined() {
return this.bits != 0;
}
public boolean isHalf() {
return this.bits == 16;
}
public boolean isSingle() {
return this.bits == 32;
}
public boolean isDouble() {
return this.bits == 64;
}
public int bits() {
return this.bits;
}
public int bytes() {
return (this.bits + 7) / 8;
}
public Precision min(Precision that) {
return fromBits(Math.min(this.bits, that.bits));
}
public Precision max(Precision that) {
return fromBits(Math.max(this.bits, that.bits));
}
public Value toValue() {
return form().mold(this).toValue();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Precision) {
final Precision that = (Precision) other;
return this.bits == that.bits;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(Precision.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.bits));
}
@Override
public void debug(Output<?> output) {
output = output.write("Precision").write('.');
if (this.bits == 0) {
output = output.write("undefined").write('(').write(')');
} else if (this.bits == 16) {
output = output.write("f16").write('(').write(')');
} else if (this.bits == 32) {
output = output.write("f32").write('(').write(')');
} else if (this.bits == 64) {
output = output.write("f64").write('(').write(')');
} else {
output = output.write("fromBits").write('(').debug(this.bits).write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static Precision undefined;
private static Precision f16;
private static Precision f32;
private static Precision f64;
private static Form<Precision> form;
public static Precision undefined() {
if (undefined == null) {
undefined = new Precision(0);
}
return undefined;
}
public static Precision f16() {
if (f16 == null) {
f16 = new Precision(16);
}
return f16;
}
public static Precision f32() {
if (f32 == null) {
f32 = new Precision(32);
}
return f32;
}
public static Precision f64() {
if (f64 == null) {
f64 = new Precision(64);
}
return f64;
}
public static Precision fromBits(int bits) {
if (bits < 0) {
throw new IllegalArgumentException();
} else if (bits == 0) {
return undefined();
} else if (bits == 16) {
return f16();
} else if (bits == 32) {
return f32();
} else if (bits == 64) {
return f64();
} else {
return new Precision(bits);
}
}
@Kind
public static Form<Precision> form() {
if (form == null) {
form = new PrecisionForm();
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/PrecisionForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Record;
import swim.structure.Value;
final class PrecisionForm extends Form<Precision> {
@Override
public String tag() {
return "precision";
}
@Override
public Precision unit() {
return Precision.undefined();
}
@Override
public Class<?> type() {
return Precision.class;
}
@Override
public Item mold(Precision precision) {
if (precision != null) {
return Record.create(1).attr(tag(), Record.create(1)
.slot("bits", precision.bits));
} else {
return Item.extant();
}
}
@Override
public Precision cast(Item item) {
final Value header = item.toValue().getAttr(tag());
if (header.isDefined()) {
final Value bits = header.get("bits");
if (bits instanceof Num) {
return Precision.fromBits(bits.intValue());
}
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
public class R implements AffineSpace<Double, Double, Double>, VectorSpace<Double, Double>, RealField<Double>, Debug {
protected R() {
// stub
}
@Override
public R vector() {
return this;
}
@Override
public R scalar() {
return this;
}
@Override
public TensorDims dimensions() {
return TensorDims.d1();
}
@Override
public final Double origin() {
return 0.0;
}
@Override
public final Double zero() {
return 0.0;
}
@Override
public final Double unit() {
return 1.0;
}
@Override
public final Double add(Double a, Double b) {
return a + b;
}
@Override
public final Double opposite(Double a) {
return -a;
}
@Override
public final Double subtract(Double a, Double b) {
return a - b;
}
@Override
public final Double multiply(Double a, Double b) {
return a * b;
}
@Override
public final Double inverse(Double a) {
return 1.0 / a;
}
@Override
public final Double divide(Double a, Double b) {
return a / b;
}
@Override
public Double combine(Double a, Double u, Double b, Double v) {
return a * u + b * v;
}
@Override
public final Double translate(Double p, Double v) {
return p + v;
}
@Override
public final Double difference(Double p, Double q) {
return p - q;
}
@Override
public final Double pow(Double b, Double e) {
return Math.pow(b, e);
}
@Override
public final Double exp(Double a) {
return Math.exp(a);
}
@Override
public final Double log(Double a) {
return Math.log(a);
}
@Override
public final Double sqrt(Double a) {
return Math.sqrt(a);
}
@Override
public final Double hypot(Double x, Double y) {
return Math.hypot(x, y);
}
@Override
public final Double sin(Double a) {
return Math.sin(a);
}
@Override
public final Double cos(Double a) {
return Math.cos(a);
}
@Override
public final Double tan(Double a) {
return Math.tan(a);
}
@Override
public final Double asin(Double a) {
return Math.asin(a);
}
@Override
public final Double acos(Double a) {
return Math.acos(a);
}
@Override
public final Double atan(Double a) {
return Math.atan(a);
}
@Override
public final Double atan2(Double y, Double x) {
return Math.atan2(y, x);
}
@Override
public final Double sinh(Double x) {
return Math.sinh(x);
}
@Override
public final Double cosh(Double x) {
return Math.cosh(x);
}
@Override
public final Double tanh(Double x) {
return Math.tanh(x);
}
@Override
public final Double sigmoid(Double x) {
return 1.0 / (1.0 + Math.exp(-x));
}
@Override
public final Double rectify(Double x) {
return Math.max(0.0, x);
}
@Override
public final Double abs(Double a) {
return Math.abs(a);
}
@Override
public final Double ceil(Double a) {
return Math.ceil(a);
}
@Override
public final Double floor(Double a) {
return Math.floor(a);
}
@Override
public final Double round(Double a) {
return Math.rint(a);
}
@Override
public final Double min(Double a, Double b) {
return Math.min(a, b);
}
@Override
public final Double max(Double a, Double b) {
return Math.max(a, b);
}
@Override
public final int compare(Double a, Double b) {
return Double.compare(a, b);
}
@Override
public void debug(Output<?> output) {
output.write('R').write('.').write("field").write('(').write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static R field;
public static R field() {
if (field == null) {
field = new R();
}
return field;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R2.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
public class R2 implements AffineSpace<PointR2, VectorR2, Double>, VectorSpace<VectorR2, Double>, F2<VectorR2, Double>, Debug {
protected R2() {
// stub
}
@Override
public final R2 vector() {
return this;
}
@Override
public final R scalar() {
return R.field();
}
@Override
public TensorDims dimensions() {
return TensorDims.d2();
}
@Override
public final PointR2 origin() {
return PointR2.origin();
}
@Override
public final VectorR2 zero() {
return VectorR2.zero();
}
@Override
public final VectorR2 of(Double x, Double y) {
return VectorR2.of(x, y);
}
@Override
public final Double getX(VectorR2 v) {
return v.x;
}
@Override
public final Double getY(VectorR2 v) {
return v.y;
}
@Override
public final VectorR2 add(VectorR2 u, VectorR2 v) {
return u.plus(v);
}
@Override
public final VectorR2 opposite(VectorR2 v) {
return v.opposite();
}
@Override
public final VectorR2 subtract(VectorR2 u, VectorR2 v) {
return u.minus(v);
}
@Override
public final VectorR2 multiply(VectorR2 u, Double a) {
return u.times(a);
}
@Override
public final VectorR2 combine(Double a, VectorR2 u, Double b, VectorR2 v) {
return new VectorR2(a * u.x + b * v.x, a * u.y + b * v.y);
}
@Override
public final PointR2 translate(PointR2 p, VectorR2 v) {
return p.plus(v);
}
@Override
public final VectorR2 difference(PointR2 p, PointR2 q) {
return p.minus(q);
}
@Override
public void debug(Output<?> output) {
output.write("R2").write('.').write("space").write('(').write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static R2 space;
public static R2 space() {
if (space == null) {
space = new R2();
}
return space;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R2Boundary.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface R2Boundary<T> extends Boundary<T> {
double getXMin(T object);
double getYMin(T object);
double getXMax(T object);
double getYMax(T object);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R2Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Form;
public abstract class R2Form<T> extends Form<T> implements R2Boundary<T> {
@Override
public abstract double getXMin(T object);
@Override
public abstract double getYMin(T object);
@Override
public abstract double getXMax(T object);
@Override
public abstract double getYMax(T object);
@Override
public abstract boolean contains(T outer, T inner);
@Override
public abstract boolean intersects(T s, T t);
public static <T> Z2Form<T> transformed(R2Form<T> form, R2ToZ2Function function) {
return new R2ToZ2Form<T>(form, function);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R2Shape.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Kind;
public abstract class R2Shape implements Shape {
public abstract double xMin();
public abstract double yMin();
public abstract double xMax();
public abstract double yMax();
@Override
public boolean contains(Shape shape) {
if (shape instanceof R2Shape) {
return contains((R2Shape) shape);
} else {
return false;
}
}
@Override
public boolean intersects(Shape shape) {
if (shape instanceof R2Shape) {
return intersects((R2Shape) shape);
} else {
return false;
}
}
public abstract boolean contains(R2Shape shape);
public abstract boolean intersects(R2Shape shape);
public abstract Z2Shape transform(R2ToZ2Function f);
private static R2Form<R2Shape> shapeForm;
@Kind
public static R2Form<R2Shape> shapeForm() {
if (shapeForm == null) {
shapeForm = new R2ShapeForm();
}
return shapeForm;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R2ShapeForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Value;
final class R2ShapeForm extends R2Form<R2Shape> {
@Override
public Class<?> type() {
return R2Shape.class;
}
@Override
public double getXMin(R2Shape shape) {
return shape.xMin();
}
@Override
public double getYMin(R2Shape shape) {
return shape.yMin();
}
@Override
public double getXMax(R2Shape shape) {
return shape.xMax();
}
@Override
public double getYMax(R2Shape shape) {
return shape.yMax();
}
@Override
public boolean contains(R2Shape outer, R2Shape inner) {
return outer.contains(inner);
}
@Override
public boolean intersects(R2Shape s, R2Shape t) {
return s.intersects(t);
}
@Override
public Item mold(R2Shape shape) {
if (shape != null) {
return shape.toValue();
} else {
return Item.extant();
}
}
@Override
public R2Shape cast(Item item) {
final Value value = item.toValue();
final String tag = value.tag();
if ("point".equals(tag)) {
return PointR2.form().cast(value);
} else if ("box".equals(tag)) {
return BoxR2.form().cast(value);
} else if ("circle".equals(tag)) {
return CircleR2.form().cast(value);
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R2ToZ2Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
final class R2ToZ2Form<T> extends Z2Form<T> {
final R2Form<T> form;
final R2ToZ2Function function;
R2ToZ2Form(R2Form<T> form, R2ToZ2Function function) {
this.form = form;
this.function = function;
}
@Override
public String tag() {
return this.form.tag();
}
@Override
public T unit() {
return this.form.unit();
}
@Override
public Class<?> type() {
return this.form.type();
}
@Override
public long getXMin(T object) {
return this.function.transformX(this.form.getXMin(object), this.form.getYMin(object));
}
@Override
public long getYMin(T object) {
return this.function.transformY(this.form.getXMin(object), this.form.getYMin(object));
}
@Override
public long getXMax(T object) {
return this.function.transformX(this.form.getXMax(object), this.form.getYMax(object));
}
@Override
public long getYMax(T object) {
return this.function.transformY(this.form.getXMax(object), this.form.getYMax(object));
}
@Override
public boolean contains(T outer, T inner) {
return this.form.contains(outer, inner);
}
@Override
public boolean intersects(T s, T t) {
return this.form.intersects(s, t);
}
@Override
public Item mold(T object) {
return this.form.mold(object);
}
@Override
public T cast(Item item) {
return this.form.cast(item);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R2ToZ2Function.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface R2ToZ2Function {
long transformX(double x, double y);
long transformY(double x, double y);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R2ToZ2Operator.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface R2ToZ2Operator extends R2ToZ2Function {
Z2ToR2Operator inverse();
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R3.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
public class R3 implements AffineSpace<PointR3, VectorR3, Double>, VectorSpace<VectorR3, Double>, F3<VectorR3, Double>, Debug {
protected R3() {
// stub
}
@Override
public final R3 vector() {
return this;
}
@Override
public final R scalar() {
return R.field();
}
@Override
public TensorDims dimensions() {
return TensorDims.d3();
}
@Override
public final PointR3 origin() {
return PointR3.origin();
}
@Override
public final VectorR3 zero() {
return VectorR3.zero();
}
@Override
public final VectorR3 of(Double x, Double y, Double z) {
return VectorR3.of(x, y, z);
}
@Override
public final Double getX(VectorR3 v) {
return v.x;
}
@Override
public final Double getY(VectorR3 v) {
return v.y;
}
@Override
public final Double getZ(VectorR3 v) {
return v.z;
}
@Override
public final VectorR3 add(VectorR3 u, VectorR3 v) {
return u.plus(v);
}
@Override
public final VectorR3 opposite(VectorR3 v) {
return v.opposite();
}
@Override
public final VectorR3 subtract(VectorR3 u, VectorR3 v) {
return u.minus(v);
}
@Override
public final VectorR3 multiply(VectorR3 u, Double a) {
return u.times(a);
}
@Override
public final VectorR3 combine(Double a, VectorR3 u, Double b, VectorR3 v) {
return new VectorR3(a * u.x + b * v.x, a * u.y + b * v.y, a * u.z + b * v.z);
}
@Override
public final PointR3 translate(PointR3 p, VectorR3 v) {
return p.plus(v);
}
@Override
public final VectorR3 difference(PointR3 p, PointR3 q) {
return p.minus(q);
}
@Override
public void debug(Output<?> output) {
output.write("R3").write('.').write("space").write('(').write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static R3 space;
public static R3 space() {
if (space == null) {
space = new R3();
}
return space;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R3Boundary.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface R3Boundary<T> extends Boundary<T> {
double getXMin(T object);
double getYMin(T object);
double getZMin(T objct);
double getXMax(T object);
double getYMax(T object);
double getZMax(T object);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R3Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Form;
public abstract class R3Form<T> extends Form<T> implements R3Boundary<T> {
@Override
public abstract double getXMin(T object);
@Override
public abstract double getYMin(T object);
@Override
public abstract double getZMin(T object);
@Override
public abstract double getXMax(T object);
@Override
public abstract double getYMax(T object);
@Override
public abstract double getZMax(T object);
@Override
public abstract boolean contains(T outer, T inner);
@Override
public abstract boolean intersects(T s, T t);
//public static <T> Z3Form<T> transformed(R3Form<T> form, R3ToZ3Function function) {
// return new R3ToZ3Form<T>(form, function);
//}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R3Shape.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Kind;
public abstract class R3Shape implements Shape {
public abstract double xMin();
public abstract double yMin();
public abstract double zMin();
public abstract double xMax();
public abstract double yMax();
public abstract double zMax();
@Override
public boolean contains(Shape shape) {
if (shape instanceof R3Shape) {
return contains((R3Shape) shape);
} else {
return false;
}
}
@Override
public boolean intersects(Shape shape) {
if (shape instanceof R3Shape) {
return intersects((R3Shape) shape);
} else {
return false;
}
}
public abstract boolean contains(R3Shape shape);
public abstract boolean intersects(R3Shape shape);
public abstract Z3Shape transform(R3ToZ3Function f);
private static R3Form<R3Shape> shapeForm;
@Kind
public static R3Form<R3Shape> shapeForm() {
if (shapeForm == null) {
shapeForm = new R3ShapeForm();
}
return shapeForm;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R3ShapeForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Value;
final class R3ShapeForm extends R3Form<R3Shape> {
@Override
public Class<?> type() {
return R3Shape.class;
}
@Override
public double getXMin(R3Shape shape) {
return shape.xMin();
}
@Override
public double getYMin(R3Shape shape) {
return shape.yMin();
}
@Override
public double getZMin(R3Shape shape) {
return shape.zMin();
}
@Override
public double getXMax(R3Shape shape) {
return shape.xMax();
}
@Override
public double getYMax(R3Shape shape) {
return shape.yMax();
}
@Override
public double getZMax(R3Shape shape) {
return shape.zMax();
}
@Override
public boolean contains(R3Shape outer, R3Shape inner) {
return outer.contains(inner);
}
@Override
public boolean intersects(R3Shape s, R3Shape t) {
return s.intersects(t);
}
@Override
public Item mold(R3Shape shape) {
if (shape != null) {
return shape.toValue();
} else {
return Item.extant();
}
}
@Override
public R3Shape cast(Item item) {
final Value value = item.toValue();
final String tag = value.tag();
if ("point".equals(tag)) {
return PointR3.form().cast(value);
} else if ("box".equals(tag)) {
return BoxR3.form().cast(value);
} else if ("sphere".equals(tag)) {
return SphereR3.form().cast(value);
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R3ToZ3Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
final class R3ToZ3Form<T> extends Z3Form<T> {
final R3Form<T> form;
final R3ToZ3Function function;
R3ToZ3Form(R3Form<T> form, R3ToZ3Function function) {
this.form = form;
this.function = function;
}
@Override
public String tag() {
return this.form.tag();
}
@Override
public T unit() {
return this.form.unit();
}
@Override
public Class<?> type() {
return this.form.type();
}
@Override
public long getXMin(T object) {
return this.function.transformX(this.form.getXMin(object), this.form.getYMin(object), this.form.getZMin(object));
}
@Override
public long getYMin(T object) {
return this.function.transformY(this.form.getXMin(object), this.form.getYMin(object), this.form.getZMin(object));
}
@Override
public long getZMin(T object) {
return this.function.transformZ(this.form.getXMin(object), this.form.getYMin(object), this.form.getZMin(object));
}
@Override
public long getXMax(T object) {
return this.function.transformX(this.form.getXMax(object), this.form.getYMax(object), this.form.getZMax(object));
}
@Override
public long getYMax(T object) {
return this.function.transformY(this.form.getXMax(object), this.form.getYMax(object), this.form.getZMax(object));
}
@Override
public long getZMax(T object) {
return this.function.transformZ(this.form.getXMax(object), this.form.getYMax(object), this.form.getZMax(object));
}
@Override
public boolean contains(T outer, T inner) {
return this.form.contains(outer, inner);
}
@Override
public boolean intersects(T s, T t) {
return this.form.intersects(s, t);
}
@Override
public Item mold(T object) {
return this.form.mold(object);
}
@Override
public T cast(Item item) {
return this.form.cast(item);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R3ToZ3Function.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface R3ToZ3Function {
long transformX(double x, double y, double z);
long transformY(double x, double y, double z);
long transformZ(double x, double y, double z);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/R3ToZ3Operator.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface R3ToZ3Operator extends R3ToZ3Function {
Z3ToR3Operator inverse();
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/RN.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
public class RN implements VectorSpace<VectorRN, Double>, FN<VectorRN, Double>, Debug {
protected final TensorDims dims;
protected RN(TensorDims dims) {
this.dims = dims;
}
@Override
public final R scalar() {
return R.field();
}
@Override
public final TensorDims dimensions() {
return this.dims;
}
@Override
public final int size() {
return this.dims.size;
}
@Override
public VectorRN zero() {
return new VectorRN(new double[this.dims.size]);
}
@Override
public VectorRN of(Object... array) {
final int n = array.length;
if (n != dims.size) {
throw new DimensionException();
}
final double[] us = new double[n];
for (int i = 0; i < n; i += 1) {
us[i] = (Double) array[i];
}
return new VectorRN(us);
}
public VectorRN of(double... array) {
return VectorRN.of(array);
}
@Override
public final Double get(VectorRN v, int i) {
return v.get(i);
}
@Override
public final VectorRN add(VectorRN u, VectorRN v) {
return u.plus(v);
}
@Override
public final VectorRN opposite(VectorRN v) {
return v.opposite();
}
@Override
public final VectorRN subtract(VectorRN u, VectorRN v) {
return u.minus(v);
}
@Override
public final VectorRN multiply(VectorRN u, Double a) {
return u.times(a);
}
@Override
public final VectorRN combine(Double a, VectorRN u, Double b, VectorRN v) {
final double[] us = u.array;
final double[] vs = v.array;
final int n = this.dims.size;
if (us.length != n || vs.length != n) {
throw new DimensionException();
}
final double[] ws = new double[n];
for (int i = 0; i < n; i += 1) {
ws[i] = a * us[i] + b * vs[i];
}
return new VectorRN(ws);
}
@Override
public void debug(Output<?> output) {
output.write("RN").write('.').write("space").write('(').debug(this.dims).write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
public static RN space(TensorDims dims) {
return new RN(dims);
}
public static RN space(int n) {
return new RN(TensorDims.of(n));
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Random.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public abstract class Random {
public abstract byte nextByte();
public abstract short nextShort();
public abstract int nextInt();
public abstract long nextLong();
public abstract float nextFloat();
public abstract double nextDouble();
public abstract boolean nextBoolean();
private static final ThreadLocal<Random> RANDOM = new ThreadLocal<Random>();
public static Random get() {
Random random = RANDOM.get();
if (random == null) {
random = new MersenneTwister64();
RANDOM.set(random);
}
return random;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/RealField.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface RealField<S> extends OrderedField<S>, CompleteField<S> {
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Ring.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface Ring<S> {
S zero();
S unit();
S add(S a, S b);
S opposite(S a);
S subtract(S a, S b);
S multiply(S a, S b);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Shape.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Value;
public interface Shape {
boolean contains(Shape shape);
boolean intersects(Shape shape);
Value toValue();
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/SphereR3.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Kind;
import swim.structure.Value;
import swim.util.Murmur3;
public class SphereR3 extends R3Shape implements Debug {
public final double cx;
public final double cy;
public final double cz;
public final double r;
public SphereR3(double cx, double cy, double cz, double r) {
this.cx = cx;
this.cy = cy;
this.cz = cz;
this.r = r;
}
@Override
public final double xMin() {
return this.cx - this.r;
}
@Override
public final double yMin() {
return this.cy - this.r;
}
@Override
public final double zMin() {
return this.cz - this.r;
}
@Override
public final double xMax() {
return this.cx + this.r;
}
@Override
public final double yMax() {
return this.cy + this.r;
}
@Override
public final double zMax() {
return this.cz + this.r;
}
@Override
public boolean contains(R3Shape shape) {
if (shape instanceof PointR3) {
return contains((PointR3) shape);
} else if (shape instanceof BoxR3) {
return contains((BoxR3) shape);
} else if (shape instanceof SphereR3) {
return contains((SphereR3) shape);
} else {
return false;
}
}
public boolean contains(PointR3 point) {
final double dx = point.x - this.cx;
final double dy = point.y - this.cy;
final double dz = point.z - this.cz;
return dx * dx + dy * dy + dz * dz <= this.r * this.r;
}
public boolean contains(BoxR3 box) {
final double dxMin = box.xMin - this.cx;
final double dyMin = box.yMin - this.cy;
final double dzMin = box.zMin - this.cz;
final double dxMax = box.xMax - this.cx;
final double dyMax = box.yMax - this.cy;
final double dzMax = box.zMax - this.cz;
final double r2 = this.r * this.r;
return dxMin * dxMin + dyMin * dyMin + dzMin * dzMin <= r2
&& dxMin * dxMin + dyMin * dyMin + dzMax * dzMax <= r2
&& dxMin * dxMin + dyMax * dyMax + dzMin * dzMin <= r2
&& dxMin * dxMin + dyMax * dyMax + dzMax * dzMax <= r2
&& dxMax * dxMax + dyMin * dyMin + dzMin * dzMin <= r2
&& dxMax * dxMax + dyMin * dyMin + dzMax * dzMax <= r2
&& dxMax * dxMax + dyMax * dyMax + dzMin * dzMin <= r2
&& dxMax * dxMax + dyMax * dyMax + dzMax * dzMax <= r2;
}
public boolean contains(SphereR3 sphere) {
final double dx = sphere.cx - this.cx;
final double dy = sphere.cy - this.cy;
final double dz = sphere.cz - this.cz;
return dx * dx + dy * dy + dz * dz + sphere.r * sphere.r <= this.r * this.r;
}
@Override
public boolean intersects(R3Shape shape) {
if (shape instanceof PointR3) {
return intersects((PointR3) shape);
} else if (shape instanceof BoxR3) {
return intersects((BoxR3) shape);
} else if (shape instanceof SphereR3) {
return intersects((SphereR3) shape);
} else {
return shape.intersects(this);
}
}
public boolean intersects(PointR3 point) {
final double dx = point.x - this.cx;
final double dy = point.y - this.cy;
final double dz = point.z - this.cz;
return dx * dx + dy * dy + dz * dz <= this.r * this.r;
}
public boolean intersects(BoxR3 box) {
final double dx = (this.cx < box.xMin ? box.xMin : box.xMax < this.cx ? box.xMax : this.cx) - this.cx;
final double dy = (this.cy < box.yMin ? box.yMin : box.yMax < this.cy ? box.yMax : this.cy) - this.cy;
final double dz = (this.cz < box.zMin ? box.zMin : box.zMax < this.cz ? box.xMax : this.cz) - this.cz;
return dx * dx + dy * dy + dz * dz <= this.r * this.r;
}
public boolean intersects(SphereR3 sphere) {
final double dx = sphere.cx - this.cx;
final double dy = sphere.cy - this.cy;
final double dz = sphere.cz - this.cz;
final double rr = this.r + sphere.r;
return dx * dx + dy * dy + dz * dz <= rr * rr;
}
@Override
public BoxZ3 transform(R3ToZ3Function f) {
final double xMin = this.cx - this.r;
final double yMin = this.cy - this.r;
final double zMin = this.cz - this.r;
final double xMax = this.cx + this.r;
final double yMax = this.cy + this.r;
final double zMax = this.cz + this.r;
return new BoxZ3(f.transformX(xMin, yMin, zMin),
f.transformY(xMin, yMin, zMin),
f.transformZ(xMin, yMin, zMin),
f.transformX(xMax, yMax, xMax),
f.transformY(xMax, yMax, xMax),
f.transformZ(xMax, yMax, xMax));
}
@Override
public Value toValue() {
return form().mold(this).toValue();
}
protected boolean canEqual(SphereR3 that) {
return true;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof SphereR3) {
final SphereR3 that = (SphereR3) other;
return that.canEqual(this) && this.cx == that.cx && this.cy == that.cy
&& this.cz == that.cz && this.r == that.r;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(SphereR3.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed,
Murmur3.hash(this.cx)), Murmur3.hash(this.cy)),
Murmur3.hash(this.cz)), Murmur3.hash(this.r)));
}
@Override
public void debug(Output<?> output) {
output.write("SphereR3").write('.').write("of").write('(')
.debug(this.cx).write(", ").debug(this.cy).write(", ")
.debug(this.cz).write(", ").debug(this.r).write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static R3Form<SphereR3> form;
public static SphereR3 of(double cx, double cy, double cz, double r) {
return new SphereR3(cx, cy, cz, r);
}
@Kind
public static R3Form<SphereR3> form() {
if (form == null) {
form = new SphereR3Form();
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/SphereR3Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
final class SphereR3Form extends R3Form<SphereR3> {
@Override
public String tag() {
return "sphere";
}
@Override
public Class<?> type() {
return SphereR3.class;
}
@Override
public double getXMin(SphereR3 sphere) {
return sphere.xMin();
}
@Override
public double getYMin(SphereR3 sphere) {
return sphere.yMin();
}
@Override
public double getZMin(SphereR3 sphere) {
return sphere.zMin();
}
@Override
public double getXMax(SphereR3 sphere) {
return sphere.xMax();
}
@Override
public double getYMax(SphereR3 sphere) {
return sphere.yMax();
}
@Override
public double getZMax(SphereR3 sphere) {
return sphere.zMax();
}
@Override
public boolean contains(SphereR3 outer, SphereR3 inner) {
return outer.contains(inner);
}
@Override
public boolean intersects(SphereR3 s, SphereR3 t) {
return s.intersects(t);
}
@Override
public Item mold(SphereR3 sphere) {
if (sphere != null) {
return Record.create(1).attr(tag(), Record.create(4)
.item(sphere.cx).item(sphere.cy).item(sphere.cz).item(sphere.r));
} else {
return Item.extant();
}
}
@Override
public SphereR3 cast(Item item) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final double cx = header.getItem(0).doubleValue(0.0);
final double cy = header.getItem(1).doubleValue(0.0);
final double cz = header.getItem(2).doubleValue(0.0);
final double r = header.getItem(3).doubleValue(0.0);
return new SphereR3(cx, cy, cz, r);
} else {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Tensor.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import java.util.Arrays;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
import swim.util.Murmur3;
public class Tensor implements Debug {
public final TensorDims dims;
public final Object array;
public final int offset;
protected Tensor(TensorDims dims, Object array, int offset) {
this.dims = dims;
this.array = array;
this.offset = offset;
}
public Tensor(TensorDims dims, double[] array, int offset) {
this.dims = dims;
this.array = array;
this.offset = offset;
}
public Tensor(TensorDims dims, float[] array, int offset) {
this.dims = dims;
this.array = array;
this.offset = offset;
}
public Tensor(TensorDims dims, double... array) {
this.dims = dims;
this.array = array;
this.offset = 0;
}
public Tensor(TensorDims dims, float... array) {
this.dims = dims;
this.array = array;
this.offset = 0;
}
public final TensorDims dimensions() {
return this.dims;
}
public final Precision precision() {
if (this.array instanceof double[]) {
return Precision.f64();
} else if (this.array instanceof float[]) {
return Precision.f32();
} else {
throw new AssertionError();
}
}
protected static int getOffset(TensorDims dim, int[] coords, int offset) {
int i = 0;
do {
final int k = coords[i];
if (k < 0 || k >= dim.size) {
throw new IndexOutOfBoundsException(Arrays.toString(coords));
}
offset += k * dim.stride;
dim = dim.next;
i += 1;
} while (dim != null);
return offset;
}
public final double getDouble(int... coords) {
final Object us = this.array;
if (us instanceof double[]) {
return ((double[]) us)[getOffset(dims, coords, 0)];
} else if (us instanceof float[]) {
return (double) ((float[]) us)[getOffset(dims, coords, 0)];
} else {
throw new AssertionError();
}
}
public final float getFloat(int... coords) {
final Object us = this.array;
if (us instanceof float[]) {
return ((float[]) us)[getOffset(dims, coords, 0)];
} else if (us instanceof double[]) {
return (float) ((double[]) us)[getOffset(dims, coords, 0)];
} else {
throw new AssertionError();
}
}
public final Tensor plus(Tensor that) {
return add(this, that);
}
public static Tensor add(Tensor u, Tensor v) {
return add(u, v, u.dims, u.precision().max(v.precision()));
}
public static void add(Tensor u, Tensor v, MutableTensor w) {
add(u.dims, u.array, u.offset, v.dims, v.array, v.offset, w.dims, w.array, w.offset);
}
public static Tensor add(Tensor u, Tensor v, TensorDims wd, Precision wp) {
final Object ws;
if (wp.isDouble()) {
ws = new double[wd.size * wd.stride];
} else if (wp.isSingle()) {
ws = new float[wd.size * wd.stride];
} else {
throw new AssertionError();
}
add(u.dims, u.array, u.offset, v.dims, v.array, v.offset, wd, ws, 0);
return new Tensor(wd, ws, 0);
}
public static void add(TensorDims ud, Object us, int ui,
TensorDims vd, Object vs, int vi,
TensorDims wd, Object ws, int wi) {
if (us instanceof double[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
add(ud, (double[]) us, ui, vd, (double[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
add(ud, (double[]) us, ui, vd, (double[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
add(ud, (double[]) us, ui, vd, (float[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
add(ud, (double[]) us, ui, vd, (float[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else if (us instanceof float[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
add(ud, (float[]) us, ui, vd, (double[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
add(ud, (float[]) us, ui, vd, (double[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
add(ud, (float[]) us, ui, vd, (float[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
add(ud, (float[]) us, ui, vd, (float[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
}
public static void add(TensorDims ud, double[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
add(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = us[ui] + vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void add(TensorDims ud, double[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
add(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (us[ui] + vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void add(TensorDims ud, double[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
add(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = us[ui] + (double) vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void add(TensorDims ud, double[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
add(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (us[ui] + (double) vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void add(TensorDims ud, float[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
add(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (double) us[ui] + vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void add(TensorDims ud, float[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
add(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) ((double) us[ui] + vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void add(TensorDims ud, float[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
add(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (double) us[ui] + (double) vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void add(TensorDims ud, float[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
add(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) ((double) us[ui] + (double) vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public final Tensor opposite() {
return opposite(this);
}
public static Tensor opposite(Tensor u) {
return opposite(u, u.dims, u.precision());
}
public static void opposite(Tensor u, MutableTensor w) {
opposite(u.dims, u.array, u.offset, w.dims, w.array, w.offset);
}
public static Tensor opposite(Tensor u, TensorDims wd, Precision wp) {
final Object ws;
if (wp.isDouble()) {
ws = new double[wd.size * wd.stride];
} else if (wp.isSingle()) {
ws = new float[wd.size * wd.stride];
} else {
throw new AssertionError();
}
opposite(u.dims, u.array, u.offset, wd, ws, 0);
return new Tensor(wd, ws, 0);
}
public static void opposite(TensorDims ud, Object us, int ui,
TensorDims wd, Object ws, int wi) {
if (us instanceof double[]) {
if (ws instanceof double[]) {
opposite(ud, (double[]) us, ui, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
opposite(ud, (double[]) us, ui, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (us instanceof float[]) {
if (ws instanceof double[]) {
opposite(ud, (float[]) us, ui, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
opposite(ud, (float[]) us, ui, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
}
public static void opposite(TensorDims ud, double[] us, int ui,
TensorDims wd, double[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null) {
throw new DimensionException();
}
while (wi < wn) {
opposite(ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (ud.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = -us[ui];
ui += ud.stride;
wi += wd.stride;
}
}
}
public static void opposite(TensorDims ud, double[] us, int ui,
TensorDims wd, float[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null) {
throw new DimensionException();
}
while (wi < wn) {
opposite(ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (ud.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) -us[ui];
ui += ud.stride;
wi += wd.stride;
}
}
}
public static void opposite(TensorDims ud, float[] us, int ui,
TensorDims wd, double[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null) {
throw new DimensionException();
}
while (wi < wn) {
opposite(ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (ud.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = -((double) us[ui]);
ui += ud.stride;
wi += wd.stride;
}
}
}
public static void opposite(TensorDims ud, float[] us, int ui,
TensorDims wd, float[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null) {
throw new DimensionException();
}
while (wi < wn) {
opposite(ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (ud.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = -us[ui];
ui += ud.stride;
wi += wd.stride;
}
}
}
public final Tensor minus(Tensor that) {
return subtract(this, that);
}
public static Tensor subtract(Tensor u, Tensor v) {
return subtract(u, v, u.dims, u.precision().max(v.precision()));
}
public static void subtract(Tensor u, Tensor v, MutableTensor w) {
subtract(u.dims, u.array, u.offset, v.dims, v.array, v.offset, w.dims, w.array, w.offset);
}
public static Tensor subtract(Tensor u, Tensor v, TensorDims wd, Precision wp) {
final Object ws;
if (wp.isDouble()) {
ws = new double[wd.size * wd.stride];
} else if (wp.isSingle()) {
ws = new float[wd.size * wd.stride];
} else {
throw new AssertionError();
}
subtract(u.dims, u.array, u.offset, v.dims, v.array, v.offset, wd, ws, 0);
return new Tensor(wd, ws, 0);
}
public static void subtract(TensorDims ud, Object us, int ui,
TensorDims vd, Object vs, int vi,
TensorDims wd, Object ws, int wi) {
if (us instanceof double[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
subtract(ud, (double[]) us, ui, vd, (double[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
subtract(ud, (double[]) us, ui, vd, (double[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
subtract(ud, (double[]) us, ui, vd, (float[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
subtract(ud, (double[]) us, ui, vd, (float[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else if (us instanceof float[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
subtract(ud, (float[]) us, ui, vd, (double[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
subtract(ud, (float[]) us, ui, vd, (double[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
subtract(ud, (float[]) us, ui, vd, (float[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
subtract(ud, (float[]) us, ui, vd, (float[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
}
public static void subtract(TensorDims ud, double[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
subtract(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = us[ui] - vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void subtract(TensorDims ud, double[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
subtract(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (us[ui] - vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void subtract(TensorDims ud, double[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
subtract(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = us[ui] - (double) vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void subtract(TensorDims ud, double[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
subtract(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (us[ui] - (double) vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void subtract(TensorDims ud, float[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
subtract(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (double) us[ui] - vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void subtract(TensorDims ud, float[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
subtract(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) ((double) us[ui] - vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void subtract(TensorDims ud, float[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
subtract(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (double) us[ui] - (double) vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void subtract(TensorDims ud, float[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
subtract(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) ((double) us[ui] - (double) vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public final Tensor times(double scalar) {
return multiply(scalar, this);
}
public static Tensor multiply(double a, Tensor u) {
return multiply(a, u, u.dims, u.precision());
}
public static void multiply(double a, Tensor u, MutableTensor w) {
multiply(a, u.dims, u.array, u.offset, w.dims, w.array, w.offset);
}
public static Tensor multiply(double a, Tensor u, TensorDims wd, Precision wp) {
final Object ws;
if (wp.isDouble()) {
ws = new double[wd.size * wd.stride];
} else if (wp.isSingle()) {
ws = new float[wd.size * wd.stride];
} else {
throw new AssertionError();
}
multiply(a, u.dims, u.array, u.offset, wd, ws, 0);
return new Tensor(wd, ws, 0);
}
public static void multiply(double a, TensorDims ud, Object us, int ui,
TensorDims wd, Object ws, int wi) {
if (us instanceof double[]) {
if (ws instanceof double[]) {
multiply(a, ud, (double[]) us, ui, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
multiply(a, ud, (double[]) us, ui, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (us instanceof float[]) {
if (ws instanceof double[]) {
multiply(a, ud, (float[]) us, ui, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
multiply(a, ud, (float[]) us, ui, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
}
public static void multiply(double a, TensorDims ud, double[] us, int ui,
TensorDims wd, double[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(a, ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (ud.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = a * us[ui];
ui += ud.stride;
wi += wd.stride;
}
}
}
public static void multiply(double a, TensorDims ud, double[] us, int ui,
TensorDims wd, float[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(a, ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (ud.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (a * us[ui]);
ui += ud.stride;
wi += wd.stride;
}
}
}
public static void multiply(double a, TensorDims ud, float[] us, int ui,
TensorDims wd, double[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(a, ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (ud.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (a * (double) us[ui]);
ui += ud.stride;
wi += wd.stride;
}
}
}
public static void multiply(double a, TensorDims ud, float[] us, int ui,
TensorDims wd, float[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(a, ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (ud.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (a * (double) us[ui]);
ui += ud.stride;
wi += wd.stride;
}
}
}
public final Tensor times(Tensor that) {
return multiply(this, that);
}
public static Tensor multiply(Tensor u, Tensor v) {
return multiply(u, v, u.dims, u.precision().max(v.precision()));
}
public static void multiply(Tensor u, Tensor v, MutableTensor w) {
multiply(u.dims, u.array, u.offset, v.dims, v.array, v.offset, w.dims, w.array, w.offset);
}
public static Tensor multiply(Tensor u, Tensor v, TensorDims wd, Precision wp) {
final Object ws;
if (wp.isDouble()) {
ws = new double[wd.size * wd.stride];
} else if (wp.isSingle()) {
ws = new float[wd.size * wd.stride];
} else {
throw new AssertionError();
}
multiply(u.dims, u.array, u.offset, v.dims, v.array, v.offset, wd, ws, 0);
return new Tensor(wd, ws, 0);
}
public static void multiply(TensorDims ud, Object us, int ui,
TensorDims vd, Object vs, int vi,
TensorDims wd, Object ws, int wi) {
if (us instanceof double[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
multiply(ud, (double[]) us, ui, vd, (double[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
multiply(ud, (double[]) us, ui, vd, (double[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
multiply(ud, (double[]) us, ui, vd, (float[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
multiply(ud, (double[]) us, ui, vd, (float[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else if (us instanceof float[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
multiply(ud, (float[]) us, ui, vd, (double[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
multiply(ud, (float[]) us, ui, vd, (double[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
multiply(ud, (float[]) us, ui, vd, (float[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
multiply(ud, (float[]) us, ui, vd, (float[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
}
public static void multiply(TensorDims ud, double[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = us[ui] * vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void multiply(TensorDims ud, double[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (us[ui] * vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void multiply(TensorDims ud, double[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = us[ui] * (double) vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void multiply(TensorDims ud, double[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (us[ui] * (double) vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void multiply(TensorDims ud, float[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (double) us[ui] * vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void multiply(TensorDims ud, float[] us, int ui,
TensorDims vd, double[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) ((double) us[ui] * vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void multiply(TensorDims ud, float[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (double) us[ui] * (double) vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public static void multiply(TensorDims ud, float[] us, int ui,
TensorDims vd, float[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
multiply(ud.next, us, ui, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) ((double) us[ui] * (double) vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += wd.stride;
}
}
}
public final Tensor timesMatrix(Tensor that) {
return multiplyMatrix(this, that);
}
public static Tensor multiplyMatrix(Tensor u, Tensor v) {
return multiplyMatrix(u, v, u.dims, u.precision().max(v.precision()));
}
public static Tensor multiplyMatrix(Tensor u, Tensor v, TensorDims wd, Precision wp) {
return multiplyMatrix(u, false, v, false, wd, wp, false);
}
public static void multiplyMatrix(Tensor u, boolean ut, Tensor v, boolean vt, MutableTensor w, boolean wt) {
multiplyMatrix(u.dims, u.array, u.offset, ut, v.dims, v.array, v.offset, vt, w.dims, w.array, w.offset, wt);
}
public static Tensor multiplyMatrix(Tensor u, boolean ut, Tensor v, boolean vt, TensorDims wd, Precision wp, boolean wt) {
final Object ws;
if (wp.isDouble()) {
ws = new double[wd.size * wd.stride];
} else if (wp.isSingle()) {
ws = new float[wd.size * wd.stride];
} else {
throw new AssertionError();
}
multiplyMatrix(u.dims, u.array, u.offset, ut, v.dims, v.array, v.offset, vt, wd, ws, 0, wt);
return new Tensor(wd, ws, 0);
}
public static void multiplyMatrix(TensorDims ud, Object us, int ui, boolean ut,
TensorDims vd, Object vs, int vi, boolean vt,
TensorDims wd, Object ws, int wi, boolean wt) {
if (us instanceof double[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
multiplyMatrix(ud, (double[]) us, ui, ut, vd, (double[]) vs, vi, vt, wd, (double[]) ws, wi, wt);
} else if (ws instanceof float[]) {
multiplyMatrix(ud, (double[]) us, ui, ut, vd, (double[]) vs, vi, vt, wd, (float[]) ws, wi, wt);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
multiplyMatrix(ud, (double[]) us, ui, ut, vd, (float[]) vs, vi, vt, wd, (double[]) ws, wi, wt);
} else if (ws instanceof float[]) {
multiplyMatrix(ud, (double[]) us, ui, ut, vd, (float[]) vs, vi, vt, wd, (float[]) ws, wi, wt);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else if (us instanceof float[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
multiplyMatrix(ud, (float[]) us, ui, ut, vd, (double[]) vs, vi, vt, wd, (double[]) ws, wi, wt);
} else if (ws instanceof float[]) {
multiplyMatrix(ud, (float[]) us, ui, ut, vd, (double[]) vs, vi, vt, wd, (float[]) ws, wi, wt);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
multiplyMatrix(ud, (float[]) us, ui, ut, vd, (float[]) vs, vi, vt, wd, (double[]) ws, wi, wt);
} else if (ws instanceof float[]) {
multiplyMatrix(ud, (float[]) us, ui, ut, vd, (float[]) vs, vi, vt, wd, (float[]) ws, wi, wt);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
}
public static void multiplyMatrix(TensorDims ud, double[] us, int ui, boolean ut,
TensorDims vd, double[] vs, int vi, boolean vt,
TensorDims wd, double[] ws, int wi, boolean wt) {
if (ud.rank() != 2 || vd.rank() != 2 || wd.rank() != 2) {
throw new DimensionException();
}
final int m = ut ? ud.size : ud.next.size;
final int n = ut ? ud.next.size : ud.size;
final int p = vt ? vd.next.size : vd.size;
if ((vt ? vd.size : vd.next.size) != n
|| (wt ? wd.next.size : wd.size) != m
|| (wt ? wd.next.size : wd.size) != p) {
throw new DimensionException();
}
final int uc = ut ? ud.next.stride : ud.stride;
final int ur = ut ? ud.stride : ud.next.stride;
final int vc = vt ? vd.next.stride : vd.stride;
final int vr = vt ? vd.stride : vd.next.stride;
final int wc = wt ? wd.next.stride : wd.stride;
final int wr = wt ? wd.stride : wd.next.stride;
final int ui0 = ui;
final int vi0 = vi;
final int wi0 = wi;
for (int i = 0; i < m; i += 1) {
wi = wi0 + wr * i;
for (int j = 0; j < p; j += 1) {
ui = ui0 + ur * i;
vi = vi0 + vc * i;
double dp = 0.0;
for (int d = 0; d < n; d += 1) {
dp += us[ui] * vs[vi];
ui += uc;
vi += vr;
}
ws[wi] = dp;
wi += wc;
}
}
}
public static void multiplyMatrix(TensorDims ud, double[] us, int ui, boolean ut,
TensorDims vd, double[] vs, int vi, boolean vt,
TensorDims wd, float[] ws, int wi, boolean wt) {
if (ud.rank() != 2 || vd.rank() != 2 || wd.rank() != 2) {
throw new DimensionException();
}
final int m = ut ? ud.size : ud.next.size;
final int n = ut ? ud.next.size : ud.size;
final int p = vt ? vd.next.size : vd.size;
if ((vt ? vd.size : vd.next.size) != n
|| (wt ? wd.next.size : wd.size) != m
|| (wt ? wd.next.size : wd.size) != p) {
throw new DimensionException();
}
final int uc = ut ? ud.next.stride : ud.stride;
final int ur = ut ? ud.stride : ud.next.stride;
final int vc = vt ? vd.next.stride : vd.stride;
final int vr = vt ? vd.stride : vd.next.stride;
final int wc = wt ? wd.next.stride : wd.stride;
final int wr = wt ? wd.stride : wd.next.stride;
final int ui0 = ui;
final int vi0 = vi;
final int wi0 = wi;
for (int i = 0; i < m; i += 1) {
wi = wi0 + wr * i;
for (int j = 0; j < p; j += 1) {
ui = ui0 + ur * i;
vi = vi0 + vc * i;
double dp = 0.0;
for (int d = 0; d < n; d += 1) {
dp += us[ui] * vs[vi];
ui += uc;
vi += vr;
}
ws[wi] = (float) dp;
wi += wc;
}
}
}
public static void multiplyMatrix(TensorDims ud, double[] us, int ui, boolean ut,
TensorDims vd, float[] vs, int vi, boolean vt,
TensorDims wd, double[] ws, int wi, boolean wt) {
if (ud.rank() != 2 || vd.rank() != 2 || wd.rank() != 2) {
throw new DimensionException();
}
final int m = ut ? ud.size : ud.next.size;
final int n = ut ? ud.next.size : ud.size;
final int p = vt ? vd.next.size : vd.size;
if ((vt ? vd.size : vd.next.size) != n
|| (wt ? wd.next.size : wd.size) != m
|| (wt ? wd.next.size : wd.size) != p) {
throw new DimensionException();
}
final int uc = ut ? ud.next.stride : ud.stride;
final int ur = ut ? ud.stride : ud.next.stride;
final int vc = vt ? vd.next.stride : vd.stride;
final int vr = vt ? vd.stride : vd.next.stride;
final int wc = wt ? wd.next.stride : wd.stride;
final int wr = wt ? wd.stride : wd.next.stride;
final int ui0 = ui;
final int vi0 = vi;
final int wi0 = wi;
for (int i = 0; i < m; i += 1) {
wi = wi0 + wr * i;
for (int j = 0; j < p; j += 1) {
ui = ui0 + ur * i;
vi = vi0 + vc * i;
double dp = 0.0;
for (int d = 0; d < n; d += 1) {
dp += us[ui] * (double) vs[vi];
ui += uc;
vi += vr;
}
ws[wi] = dp;
wi += wc;
}
}
}
public static void multiplyMatrix(TensorDims ud, double[] us, int ui, boolean ut,
TensorDims vd, float[] vs, int vi, boolean vt,
TensorDims wd, float[] ws, int wi, boolean wt) {
if (ud.rank() != 2 || vd.rank() != 2 || wd.rank() != 2) {
throw new DimensionException();
}
final int m = ut ? ud.size : ud.next.size;
final int n = ut ? ud.next.size : ud.size;
final int p = vt ? vd.next.size : vd.size;
if ((vt ? vd.size : vd.next.size) != n
|| (wt ? wd.next.size : wd.size) != m
|| (wt ? wd.next.size : wd.size) != p) {
throw new DimensionException();
}
final int uc = ut ? ud.next.stride : ud.stride;
final int ur = ut ? ud.stride : ud.next.stride;
final int vc = vt ? vd.next.stride : vd.stride;
final int vr = vt ? vd.stride : vd.next.stride;
final int wc = wt ? wd.next.stride : wd.stride;
final int wr = wt ? wd.stride : wd.next.stride;
final int ui0 = ui;
final int vi0 = vi;
final int wi0 = wi;
for (int i = 0; i < m; i += 1) {
wi = wi0 + wr * i;
for (int j = 0; j < p; j += 1) {
ui = ui0 + ur * i;
vi = vi0 + vc * i;
double dp = 0.0;
for (int d = 0; d < n; d += 1) {
dp += us[ui] * (double) vs[vi];
ui += uc;
vi += vr;
}
ws[wi] = (float) dp;
wi += wc;
}
}
}
public static void multiplyMatrix(TensorDims ud, float[] us, int ui, boolean ut,
TensorDims vd, double[] vs, int vi, boolean vt,
TensorDims wd, double[] ws, int wi, boolean wt) {
if (ud.rank() != 2 || vd.rank() != 2 || wd.rank() != 2) {
throw new DimensionException();
}
final int m = ut ? ud.size : ud.next.size;
final int n = ut ? ud.next.size : ud.size;
final int p = vt ? vd.next.size : vd.size;
if ((vt ? vd.size : vd.next.size) != n
|| (wt ? wd.next.size : wd.size) != m
|| (wt ? wd.next.size : wd.size) != p) {
throw new DimensionException();
}
final int uc = ut ? ud.next.stride : ud.stride;
final int ur = ut ? ud.stride : ud.next.stride;
final int vc = vt ? vd.next.stride : vd.stride;
final int vr = vt ? vd.stride : vd.next.stride;
final int wc = wt ? wd.next.stride : wd.stride;
final int wr = wt ? wd.stride : wd.next.stride;
final int ui0 = ui;
final int vi0 = vi;
final int wi0 = wi;
for (int i = 0; i < m; i += 1) {
wi = wi0 + wr * i;
for (int j = 0; j < p; j += 1) {
ui = ui0 + ur * i;
vi = vi0 + vc * i;
double dp = 0.0;
for (int d = 0; d < n; d += 1) {
dp += (double) us[ui] * vs[vi];
ui += uc;
vi += vr;
}
ws[wi] = dp;
wi += wc;
}
}
}
public static void multiplyMatrix(TensorDims ud, float[] us, int ui, boolean ut,
TensorDims vd, double[] vs, int vi, boolean vt,
TensorDims wd, float[] ws, int wi, boolean wt) {
if (ud.rank() != 2 || vd.rank() != 2 || wd.rank() != 2) {
throw new DimensionException();
}
final int m = ut ? ud.size : ud.next.size;
final int n = ut ? ud.next.size : ud.size;
final int p = vt ? vd.next.size : vd.size;
if ((vt ? vd.size : vd.next.size) != n
|| (wt ? wd.next.size : wd.size) != m
|| (wt ? wd.next.size : wd.size) != p) {
throw new DimensionException();
}
final int uc = ut ? ud.next.stride : ud.stride;
final int ur = ut ? ud.stride : ud.next.stride;
final int vc = vt ? vd.next.stride : vd.stride;
final int vr = vt ? vd.stride : vd.next.stride;
final int wc = wt ? wd.next.stride : wd.stride;
final int wr = wt ? wd.stride : wd.next.stride;
final int ui0 = ui;
final int vi0 = vi;
final int wi0 = wi;
for (int i = 0; i < m; i += 1) {
wi = wi0 + wr * i;
for (int j = 0; j < p; j += 1) {
ui = ui0 + ur * i;
vi = vi0 + vc * i;
double dp = 0.0;
for (int d = 0; d < n; d += 1) {
dp += (double) us[ui] * vs[vi];
ui += uc;
vi += vr;
}
ws[wi] = (float) dp;
wi += wc;
}
}
}
public static void multiplyMatrix(TensorDims ud, float[] us, int ui, boolean ut,
TensorDims vd, float[] vs, int vi, boolean vt,
TensorDims wd, double[] ws, int wi, boolean wt) {
if (ud.rank() != 2 || vd.rank() != 2 || wd.rank() != 2) {
throw new DimensionException();
}
final int m = ut ? ud.size : ud.next.size;
final int n = ut ? ud.next.size : ud.size;
final int p = vt ? vd.next.size : vd.size;
if ((vt ? vd.size : vd.next.size) != n
|| (wt ? wd.next.size : wd.size) != m
|| (wt ? wd.next.size : wd.size) != p) {
throw new DimensionException();
}
final int uc = ut ? ud.next.stride : ud.stride;
final int ur = ut ? ud.stride : ud.next.stride;
final int vc = vt ? vd.next.stride : vd.stride;
final int vr = vt ? vd.stride : vd.next.stride;
final int wc = wt ? wd.next.stride : wd.stride;
final int wr = wt ? wd.stride : wd.next.stride;
final int ui0 = ui;
final int vi0 = vi;
final int wi0 = wi;
for (int i = 0; i < m; i += 1) {
wi = wi0 + wr * i;
for (int j = 0; j < p; j += 1) {
ui = ui0 + ur * i;
vi = vi0 + vc * i;
double dp = 0.0;
for (int d = 0; d < n; d += 1) {
dp += (double) us[ui] * (double) vs[vi];
ui += uc;
vi += vr;
}
ws[wi] = dp;
wi += wc;
}
}
}
public static void multiplyMatrix(TensorDims ud, float[] us, int ui, boolean ut,
TensorDims vd, float[] vs, int vi, boolean vt,
TensorDims wd, float[] ws, int wi, boolean wt) {
if (ud.rank() != 2 || vd.rank() != 2 || wd.rank() != 2) {
throw new DimensionException();
}
final int m = ut ? ud.size : ud.next.size;
final int n = ut ? ud.next.size : ud.size;
final int p = vt ? vd.next.size : vd.size;
if ((vt ? vd.size : vd.next.size) != n
|| (wt ? wd.next.size : wd.size) != m
|| (wt ? wd.next.size : wd.size) != p) {
throw new DimensionException();
}
final int uc = ut ? ud.next.stride : ud.stride;
final int ur = ut ? ud.stride : ud.next.stride;
final int vc = vt ? vd.next.stride : vd.stride;
final int vr = vt ? vd.stride : vd.next.stride;
final int wc = wt ? wd.next.stride : wd.stride;
final int wr = wt ? wd.stride : wd.next.stride;
final int ui0 = ui;
final int vi0 = vi;
final int wi0 = wi;
for (int i = 0; i < m; i += 1) {
wi = wi0 + wr * i;
for (int j = 0; j < p; j += 1) {
ui = ui0 + ur * i;
vi = vi0 + vc * j;
double dp = 0.0;
for (int d = 0; d < n; d += 1) {
dp += (double) us[ui] * (double) vs[vi];
ui += uc;
vi += vr;
}
ws[wi] = (float) dp;
wi += wc;
}
}
}
public static Tensor combine(double a, Tensor u, double b, Tensor v) {
return combine(a, u, b, v, u.dims, u.precision().max(v.precision()));
}
public static void combine(double a, Tensor u, double b, Tensor v, MutableTensor w) {
combine(a, u.dims, u.array, u.offset, b, v.dims, v.array, v.offset, w.dims, w.array, w.offset);
}
public static Tensor combine(double a, Tensor u, double b, Tensor v, TensorDims wd, Precision wp) {
final Object ws;
if (wp.isDouble()) {
ws = new double[wd.size * wd.stride];
} else if (wp.isSingle()) {
ws = new float[wd.size * wd.stride];
} else {
throw new AssertionError();
}
combine(a, u.dims, u.array, u.offset, b, v.dims, v.array, v.offset, wd, ws, 0);
return new Tensor(wd, ws, 0);
}
public static void combine(double a, TensorDims ud, Object us, int ui,
double b, TensorDims vd, Object vs, int vi,
TensorDims wd, Object ws, int wi) {
if (us instanceof double[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
combine(a, ud, (double[]) us, ui, b, vd, (double[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
combine(a, ud, (double[]) us, ui, b, vd, (double[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
combine(a, ud, (double[]) us, ui, b, vd, (float[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
combine(a, ud, (double[]) us, ui, b, vd, (float[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else if (us instanceof float[]) {
if (vs instanceof double[]) {
if (ws instanceof double[]) {
combine(a, ud, (float[]) us, ui, b, vd, (double[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
combine(a, ud, (float[]) us, ui, b, vd, (double[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else if (vs instanceof float[]) {
if (ws instanceof double[]) {
combine(a, ud, (float[]) us, ui, b, vd, (float[]) vs, vi, wd, (double[]) ws, wi);
} else if (ws instanceof float[]) {
combine(a, ud, (float[]) us, ui, b, vd, (float[]) vs, vi, wd, (float[]) ws, wi);
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
}
public static void combine(double a, TensorDims ud, double[] us, int ui,
double b, TensorDims vd, double[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
combine(a, ud.next, us, ui, b, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = a * us[ui] + b * vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
}
}
public static void combine(double a, TensorDims ud, double[] us, int ui,
double b, TensorDims vd, double[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
combine(a, ud.next, us, ui, b, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (a * us[ui] + b * vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
}
}
public static void combine(double a, TensorDims ud, double[] us, int ui,
double b, TensorDims vd, float[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
combine(a, ud.next, us, ui, b, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = a * us[ui] + b * (double) vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
}
}
public static void combine(double a, TensorDims ud, double[] us, int ui,
double b, TensorDims vd, float[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
combine(a, ud.next, us, ui, b, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (a * us[ui] + b * (double) vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
}
}
public static void combine(double a, TensorDims ud, float[] us, int ui,
double b, TensorDims vd, double[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
combine(a, ud.next, us, ui, b, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = a * (double) us[ui] + b * vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
}
}
public static void combine(double a, TensorDims ud, float[] us, int ui,
double b, TensorDims vd, double[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
combine(a, ud.next, us, ui, b, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (a * (double) us[ui] + b * vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
}
}
public static void combine(double a, TensorDims ud, float[] us, int ui,
double b, TensorDims vd, float[] vs, int vi,
TensorDims wd, double[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
combine(a, ud.next, us, ui, b, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = a * (double) us[ui] + b * (double) vs[vi];
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
}
}
public static void combine(double a, TensorDims ud, float[] us, int ui,
double b, TensorDims vd, float[] vs, int vi,
TensorDims wd, float[] ws, int wi) {
if (ud.size != vd.size || ud.size != wd.size || vd.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (wd.next != null) {
if (ud.next == null || vd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
combine(a, ud.next, us, ui, b, vd.next, vs, vi, wd.next, ws, wi);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
} else {
if (ud.next != null || vd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) (a * (double) us[ui] + b * (double) vs[vi]);
ui += ud.stride;
vi += vd.stride;
wi += ud.stride;
}
}
}
public final double[] getDoubleArray() {
if (array instanceof double[]) {
return (double[]) this.array;
} else {
return null;
}
}
public final float[] getFloatArray() {
if (array instanceof float[]) {
return (float[]) this.array;
} else {
return null;
}
}
public final int getArrayOffset() {
return this.offset;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Tensor) {
final Tensor that = (Tensor) other;
final Object us = this.array;
final Object vs = that.array;
if (us instanceof double[] && vs instanceof double[]) {
return equals(dims, (double[]) us, this.offset, that.dims, (double[]) vs, that.offset);
} else if (us instanceof float[] && vs instanceof float[]) {
return equals(dims, (float[]) us, this.offset, that.dims, (float[]) vs, that.offset);
}
}
return false;
}
static boolean equals(TensorDims ud, double[] us, int ui,
TensorDims vd, double[] vs, int vi) {
if (ud.size != vd.size) {
return false;
}
final int un = ui + ud.size * ud.stride;
if (ud.next != null) {
if (vd == null) {
return false;
}
while (ui < un) {
if (!equals(ud.next, us, ui, vd.next, vs, vi)) {
return false;
}
ui += ud.stride;
vi += vd.stride;
}
} else {
if (vd.next != null) {
return false;
}
while (ui < un) {
if (us[ui] != vs[vi]) {
return false;
}
ui += ud.stride;
vi += vd.stride;
}
}
return true;
}
static boolean equals(TensorDims ud, float[] us, int ui,
TensorDims vd, float[] vs, int vi) {
if (ud.size != vd.size) {
return false;
}
final int un = ui + ud.size * ud.stride;
if (ud.next != null) {
if (vd == null) {
return false;
}
while (ui < un) {
if (!equals(ud.next, us, ui, vd.next, vs, vi)) {
return false;
}
ui += ud.stride;
vi += vd.stride;
}
} else {
if (vd.next != null) {
return false;
}
while (ui < un) {
if (us[ui] != vs[vi]) {
return false;
}
ui += ud.stride;
vi += vd.stride;
}
}
return true;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(Tensor.class);
}
int code = hashSeed;
final Object us = this.array;
if (us instanceof double[]) {
code = hash(code, this.dims, (double[]) us, this.offset);
} else if (us instanceof float[]) {
code = hash(code, this.dims, (float[]) us, this.offset);
} else {
throw new AssertionError();
}
return Murmur3.mash(code);
}
static int hash(int code, TensorDims ud, double[] us, int ui) {
final int limit = ui + ud.size * ud.stride;
if (ud.next != null) {
while (ui < limit) {
hash(code, ud.next, us, ui);
ui += ud.stride;
}
} else {
while (ui < limit) {
code = Murmur3.mix(code, Murmur3.hash(us[ui]));
ui += ud.stride;
}
}
return code;
}
static int hash(int code, TensorDims ud, float[] us, int ui) {
final int limit = ui + ud.size * ud.stride;
if (ud.next != null) {
while (ui < limit) {
hash(code, ud.next, us, ui);
ui += ud.stride;
}
} else {
while (ui < limit) {
code = Murmur3.mix(code, Murmur3.hash(us[ui]));
ui += ud.stride;
}
}
return code;
}
@Override
public void debug(Output<?> output) {
output = output.write("Tensor").write('.').write("of").write('(')
.debug(this.dims).write(", ").debug(this.offset);
final Object us = this.array;
if (us instanceof double[]) {
debug(output, (double[]) us);
} else if (us instanceof float[]) {
debug(output, (float[]) us);
} else {
throw new AssertionError();
}
output = output.write(')');
}
static void debug(Output<?> output, double[] us) {
for (int i = 0, n = us.length; i < n; i += 1) {
output = output.write(", ").debug(us[i]);
}
}
static void debug(Output<?> output, float[] us) {
for (int i = 0, n = us.length; i < n; i += 1) {
output = output.write(", ").debug(us[i]);
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
public static Tensor zero(TensorDims dims) {
return new Tensor(dims, new float[dims.size * dims.stride]);
}
public static Tensor of(TensorDims dims, int offset, double... array) {
return new Tensor(dims, array, offset);
}
public static Tensor of(TensorDims dims, int offset, float... array) {
return new Tensor(dims, array, offset);
}
public static TensorSpace<Tensor, Double> space(TensorSpace<Tensor, Double> next, TensorDims dims) {
return new TensorObjectSpace(next, dims);
}
public static TensorSpace<Tensor, Double> space(TensorSpace<Tensor, Double> next, int n) {
return new TensorObjectSpace(next, next.dimensions().by(n));
}
public static TensorSpace<Tensor, Double> space(TensorDims dims) {
if (dims.next != null) {
throw new DimensionException();
}
return new TensorObjectSpace(null, dims);
}
public static TensorSpace<Tensor, Double> space(int n) {
return new TensorObjectSpace(null, TensorDims.of(n));
}
public static TensorForm<Tensor> form(TensorDims dims, Precision prec) {
return new TensorObjectForm(dims, prec);
}
public static TensorForm<Tensor> form(TensorDims dims) {
return new TensorObjectForm(dims, Precision.f64());
}
public static Item mold(String tag, Tensor u) {
final Object us = u.array;
if (us instanceof double[]) {
return mold(tag, u.dims, (double[]) us, u.offset);
} else if (us instanceof float[]) {
return mold(tag, u.dims, (float[]) us, u.offset);
} else {
throw new AssertionError();
}
}
public static Item mold(String tag, TensorDims ud, double[] us, int ui) {
final int un = ui + ud.size * ud.stride;
final Record header = Record.create(ud.size);
if (ud.next != null) {
while (ui < un) {
header.item(mold(tag, ud.next, us, ui));
ui += ud.stride;
}
} else {
while (ui < un) {
header.item(us[ui]);
ui += ud.stride;
}
}
return Record.create(1).attr(tag, header);
}
public static Item mold(String tag, TensorDims ud, float[] us, int ui) {
final int un = ui + ud.size * ud.stride;
final Record header = Record.create(ud.size);
if (ud.next != null) {
while (ui < un) {
header.item(mold(tag, ud.next, us, ui));
ui += ud.stride;
}
} else {
while (ui < un) {
header.item(us[ui]);
ui += ud.stride;
}
}
return Record.create(1).attr(tag, header);
}
public static Tensor cast(String tag, Item item, TensorDims wd, Precision wp) {
if (wp.isDouble()) {
final double[] ws = new double[wd.size * wd.stride];
cast(tag, item, wd, ws, 0);
return new Tensor(wd, ws);
} else if (wp.isSingle()) {
final float[] ws = new float[wd.size * wd.stride];
cast(tag, item, wd, ws, 0);
return new Tensor(wd, ws);
} else {
throw new AssertionError();
}
}
public static void cast(String tag, Item item, TensorDims wd, double[] ws, int wi) {
final Value header = item.toValue().header(tag);
if (!header.isDefined()) {
return;
}
if (wd.next != null) {
for (int i = 0; i < wd.size; i += 1) {
cast(tag, header.getItem(i), wd.next, ws, wi);
wi += wd.stride;
}
} else {
for (int i = 0; i < wd.size; i += 1) {
ws[wi] = header.getItem(i).doubleValue(0.0);
wi += wd.stride;
}
}
}
public static void cast(String tag, Item item, TensorDims wd, float[] ws, int wi) {
final Value header = item.toValue().header(tag);
if (!header.isDefined()) {
return;
}
if (wd.next != null) {
for (int i = 0; i < wd.size; i += 1) {
cast(tag, header.getItem(i), wd.next, ws, wi);
wi += wd.stride;
}
} else {
for (int i = 0; i < wd.size; i += 1) {
ws[wi] = header.getItem(i).floatValue(0.0f);
wi += wd.stride;
}
}
}
public static void copy(TensorDims ud, double[] us, int ui,
TensorDims wd, double[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn;
if (ud.next != null) {
if (wd.next == null) {
throw new DimensionException();
}
wn = wi + wd.size * wd.stride;
while (wi < wn) {
copy(ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (wd.next != null) {
throw new DimensionException();
}
if (ud.stride == 1 && wd.stride == 1) {
System.arraycopy(us, ui, ws, wi, ud.size);
} else {
wn = wi + wd.size * wd.stride;
while (wi < wn) {
ws[wi] = us[ui];
ui += ud.stride;
wi += wd.stride;
}
}
}
}
public static void copy(TensorDims ud, double[] us, int ui,
TensorDims wd, float[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (ud.next != null) {
if (wd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
copy(ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (wd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (float) us[ui];
ui += ud.stride;
wi += wd.stride;
}
}
}
public static void copy(TensorDims ud, float[] us, int ui,
TensorDims wd, double[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn = wi + wd.size * wd.stride;
if (ud.next != null) {
if (wd.next == null) {
throw new DimensionException();
}
while (wi < wn) {
copy(ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (wd.next != null) {
throw new DimensionException();
}
while (wi < wn) {
ws[wi] = (double) us[ui];
ui += ud.stride;
wi += wd.stride;
}
}
}
public static void copy(TensorDims ud, float[] us, int ui,
TensorDims wd, float[] ws, int wi) {
if (ud.size != wd.size) {
throw new DimensionException();
}
final int wn;
if (ud.next != null) {
if (wd.next == null) {
throw new DimensionException();
}
wn = wi + wd.size * wd.stride;
while (wi < wn) {
copy(ud.next, us, ui, wd.next, ws, wi);
ui += ud.stride;
wi += wd.stride;
}
} else {
if (wd.next != null) {
throw new DimensionException();
}
if (ud.stride == 1 && wd.stride == 1) {
System.arraycopy(us, ui, ws, wi, ud.size);
} else {
wn = wi + wd.size * wd.stride;
while (wi < wn) {
ws[wi] = us[ui];
ui += ud.stride;
wi += wd.stride;
}
}
}
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorArray.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.util.Murmur3;
public class TensorArray<V, S> implements Debug {
final TensorSpace<TensorArray<V, S>, S> space;
final Object[] array;
public TensorArray(TensorSpace<TensorArray<V, S>, S> space, Object... array) {
this.space = space;
this.array = array;
}
public final TensorSpace<TensorArray<V, S>, S> space() {
return this.space;
}
public final TensorDims dimensions() {
return this.space.dimensions();
}
@SuppressWarnings("unchecked")
public final V get(int i) {
return (V) this.array[i];
}
public TensorArray<V, S> plus(TensorArray<V, S> that) {
return this.space.add(this, that);
}
public TensorArray<V, S> opposite() {
return this.space.opposite(this);
}
public TensorArray<V, S> minus(TensorArray<V, S> that) {
return this.space.subtract(this, that);
}
public TensorArray<V, S> times(S scalar) {
return this.space.multiply(this, scalar);
}
protected boolean canEqual(TensorArray<?, ?> that) {
return true;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof TensorArray<?, ?>) {
final TensorArray<?, ?> that = (TensorArray<?, ?>) other;
if (that.canEqual(this)) {
final Object[] us = this.array;
final Object[] vs = that.array;
final int n = us.length;
if (n == vs.length) {
for (int i = 0; i < n; i += 1) {
final Object u = us[i];
final Object v = vs[i];
if (u == null ? v != null : !u.equals(v)) {
return false;
}
}
return true;
}
}
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(TensorArray.class);
}
int code = hashSeed;
final Object[] us = this.array;
for (int i = 0, n = us.length; i < n; i += 1) {
code = Murmur3.mix(code, Murmur3.hash(us[i]));
}
return Murmur3.mash(code);
}
@Override
public void debug(Output<?> output) {
output = output.debug(this.space).write('.').write("fromArray").write('(');
final Object[] us = this.array;
final int n = us.length;
if (n > 0) {
output = output.debug(us[0]);
for (int i = 1; i < n; i += 1) {
output = output.write(", ").debug(us[i]);
}
}
output = output.write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
public static <V, S> TensorArraySpace<TensorArray<V, S>, V, S> space(TensorSpace<V, S> next, TensorDims dims) {
return new TensorArrayObjectSpace<V, S>(next, dims);
}
public static <V, S> TensorArraySpace<TensorArray<V, S>, V, S> space(TensorSpace<V, S> next, int n) {
return new TensorArrayObjectSpace<V, S>(next, next.dimensions().by(n));
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorArrayForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import java.lang.reflect.Array;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
public abstract class TensorArrayForm<T, V> extends TensorForm<T> {
public abstract TensorForm<V> next();
public abstract T fromArray(Object... array);
public abstract Object[] toArray(T tensor);
protected Object[] newArray(int length) {
return (Object[]) Array.newInstance(next().type(), length);
}
@Override
public String tag() {
return "tensor";
}
@SuppressWarnings("unchecked")
@Override
public Item mold(T tensor) {
if (tensor != null) {
final Object[] us = toArray(tensor);
final Record header = Record.create(us.length);
final TensorForm<V> next = next();
for (int i = 0; i < us.length; i += 1) {
header.item(next.mold((V) us[i]));
}
return Record.create(1).attr(tag(), header);
} else {
return Item.extant();
}
}
@Override
public T cast(Item item) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final int n = header.length();
final Object[] us = newArray(n);
final TensorForm<V> next = next();
for (int i = 0; i < n; i += 1) {
V u = next.cast(header.getItem(i));
if (u == null) {
u = next.unit();
}
us[i] = u;
}
return fromArray(us);
} else {
return null;
}
}
@Override
public T fromTensor(TensorDims vd, float[] vs, int vi) {
final Object[] us = newArray(vd.size);
final TensorForm<V> next = next();
for (int i = 0; i < vd.size; i += 1) {
V u = next.fromTensor(vd.next, vs, vi);
if (u == null) {
u = next.unit();
}
us[i] = u;
vi += vd.stride;
}
return fromArray(us);
}
@Override
public T fromTensor(TensorDims vd, double[] vs, int vi) {
final Object[] us = newArray(vd.size);
final TensorForm<V> next = next();
for (int i = 0; i < vd.size; i += 1) {
V u = next.fromTensor(vd.next, vs, vi);
if (u == null) {
u = next.unit();
}
us[i] = u;
vi += vd.stride;
}
return fromArray(us);
}
@SuppressWarnings("unchecked")
@Override
public void toTensor(T u, TensorDims vd, float[] vs, int vi) {
final Object[] us = toArray(u);
if (us.length != vd.size) {
throw new DimensionException();
}
final TensorForm<V> next = next();
for (int i = 0; i < vd.size; i += 1) {
next.toTensor((V) us[i], vd.next, vs, vi);
vi += vd.stride;
}
}
@SuppressWarnings("unchecked")
@Override
public void toTensor(T u, TensorDims vd, double[] vs, int vi) {
final Object[] us = toArray(u);
if (us.length != vd.size) {
throw new DimensionException();
}
final TensorForm<V> next = next();
for (int i = 0; i < vd.size; i += 1) {
next.toTensor((V) us[i], vd.next, vs, vi);
vi += vd.stride;
}
}
@Override
public Item moldTensor(TensorDims vd, float[] vs, int vi) {
final Record header = Record.create(vd.size);
final TensorForm<V> next = next();
for (int i = 0; i < vd.size; i += 1) {
header.item(next.moldTensor(vd.next, vs, vi));
vi += vd.stride;
}
return Record.create(1).attr(tag(), header);
}
@Override
public Item moldTensor(TensorDims vd, double[] vs, int vi) {
final Record header = Record.create(vd.size);
final TensorForm<V> next = next();
for (int i = 0; i < vd.size; i += 1) {
header.item(next.moldTensor(vd.next, vs, vi));
vi += vd.stride;
}
return Record.create(1).attr(tag(), header);
}
@Override
public void castTensor(Item item, TensorDims vd, float[] vs, int vi) {
final Value header = item.toValue().header(tag());
if (!header.isDefined() || header.length() != vd.size) {
throw new DimensionException();
}
final TensorForm<V> next = next();
for (int i = 0; i < vd.size; i += 1) {
next.castTensor(header.getItem(i), vd.next, vs, vi);
vi += vd.stride;
}
}
@Override
public void castTensor(Item item, TensorDims vd, double[] vs, int vi) {
final Value header = item.toValue().header(tag());
if (!header.isDefined() || header.length() != vd.size) {
throw new DimensionException();
}
final TensorForm<V> next = next();
for (int i = 0; i < vd.size; i += 1) {
next.castTensor(header.getItem(i), vd.next, vs, vi);
vi += vd.stride;
}
}
public static <V> TensorArrayForm<V[], V> from(TensorForm<V> next) {
return new TensorArrayIdentityForm<V>(next);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorArrayIdentityForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import java.lang.reflect.Array;
final class TensorArrayIdentityForm<V> extends TensorArrayForm<V[], V> {
final TensorForm<V> next;
TensorArrayIdentityForm(TensorForm<V> next) {
this.next = next;
}
@Override
public Class<?> type() {
return (Class<?>) Array.newInstance(this.next.type(), 0).getClass();
}
@Override
public TensorForm<V> next() {
return this.next;
}
@SuppressWarnings("unchecked")
@Override
public V[] fromArray(Object... array) {
return (V[]) array;
}
@Override
public Object[] toArray(V[] tensor) {
return tensor;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorArrayIdentitySpace.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import java.lang.reflect.Array;
final class TensorArrayIdentitySpace<V, S> extends TensorArraySpace<V[], V, S> {
final Class<V> type;
final TensorSpace<V, S> next;
final TensorDims dims;
TensorArrayIdentitySpace(Class<V> type, TensorSpace<V, S> next, TensorDims dims) {
this.type = type;
this.next = next;
this.dims = dims;
}
@Override
public Field<S> scalar() {
return this.next.scalar();
}
@Override
public TensorDims dimensions() {
return this.dims;
}
@Override
public TensorSpace<V, S> next() {
return this.next;
}
@SuppressWarnings("unchecked")
@Override
public V[] of(Object... array) {
return (V[]) array;
}
@Override
public Object[] toArray(V[] tensor) {
return tensor;
}
@Override
public TensorForm<V[]> form(TensorForm<V> next) {
return TensorArrayForm.from(next);
}
@Override
protected Object[] newArray(int length) {
return (Object[]) Array.newInstance(type, length);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorArrayObjectSpace.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
final class TensorArrayObjectSpace<V, S> extends TensorArraySpace<TensorArray<V, S>, V, S> implements Debug {
final TensorSpace<V, S> next;
final TensorDims dims;
TensorArrayObjectSpace(TensorSpace<V, S> next, TensorDims dims) {
this.next = next;
this.dims = dims;
}
@Override
public Field<S> scalar() {
return this.next.scalar();
}
@Override
public TensorDims dimensions() {
return this.dims;
}
@Override
public TensorSpace<V, S> next() {
return this.next;
}
@Override
public TensorArray<V, S> of(Object... array) {
return new TensorArray<V, S>(this, array);
}
@Override
public Object[] toArray(TensorArray<V, S> tensor) {
return tensor.array;
}
@Override
public void debug(Output<?> output) {
output.write("TensorArray").write('.').write("space").write('(')
.debug(this.next).write(", ").debug(this.dims).write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorArraySpace.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public abstract class TensorArraySpace<T, V, S> implements TensorSpace<T, S> {
public abstract TensorSpace<V, S> next();
public abstract T of(Object... array);
public abstract Object[] toArray(T tensor);
public TensorForm<T> form(TensorForm<V> next) {
return new TensorArraySpaceForm<T, V, S>(this, next);
}
protected Object[] newArray(int length) {
return new Object[length];
}
@Override
public T zero() {
final int n = dimensions().size;
final Object[] ws = new Object[n];
final V zero = next().zero();
for (int i = 0; i < n; i += 1) {
ws[i] = zero;
}
return of(ws);
}
@SuppressWarnings("unchecked")
@Override
public T add(T u, T v) {
final Object[] us = toArray(u);
final Object[] vs = toArray(v);
final int n = us.length;
if (n != vs.length) {
throw new DimensionException();
}
final Object[] ws = newArray(n);
final TensorSpace<V, S> next = next();
for (int i = 0; i < n; i += 1) {
ws[i] = next.add((V) us[i], (V) vs[i]);
}
return of(ws);
}
@SuppressWarnings("unchecked")
@Override
public T opposite(T v) {
final Object[] vs = toArray(v);
final int n = vs.length;
final Object[] ws = newArray(n);
final TensorSpace<V, S> next = next();
for (int i = 0; i < n; i += 1) {
ws[i] = next.opposite((V) vs[i]);
}
return of(ws);
}
@SuppressWarnings("unchecked")
@Override
public T subtract(T u, T v) {
final Object[] us = toArray(u);
final Object[] vs = toArray(v);
final int n = us.length;
if (n != vs.length) {
throw new DimensionException();
}
final Object[] ws = newArray(n);
final TensorSpace<V, S> next = next();
for (int i = 0; i < n; i += 1) {
ws[i] = next.subtract((V) us[i], (V) vs[i]);
}
return of(ws);
}
@SuppressWarnings("unchecked")
@Override
public T multiply(T u, S a) {
final Object[] us = toArray(u);
final int n = us.length;
final Object[] ws = newArray(n);
final TensorSpace<V, S> next = next();
for (int i = 0; i < n; i += 1) {
ws[i] = next.multiply((V) us[i], a);
}
return of(ws);
}
@SuppressWarnings("unchecked")
@Override
public T combine(S a, T u, S b, T v) {
final Object[] us = toArray(u);
final Object[] vs = toArray(v);
final int n = us.length;
if (n != vs.length) {
throw new DimensionException();
}
final Object[] ws = newArray(n);
final TensorSpace<V, S> next = next();
for (int i = 0; i < n; i += 1) {
ws[i] = next.combine(a, (V) us[i], b, (V) vs[i]);
}
return of(ws);
}
public static <V, S> TensorArraySpace<V[], V, S> from(Class<V> type, TensorSpace<V, S> next, TensorDims dims) {
return new TensorArrayIdentitySpace<V, S>(type, next, dims);
}
public static <V, S> TensorArraySpace<V[], V, S> from(Class<V> type, TensorSpace<V, S> next, int n) {
return new TensorArrayIdentitySpace<V, S>(type, next, next.dimensions().by(n));
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorArraySpaceForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import java.lang.reflect.Array;
final class TensorArraySpaceForm<T, V, S> extends TensorArrayForm<T, V> {
final TensorArraySpace<T, V, S> space;
final TensorForm<V> next;
TensorArraySpaceForm(TensorArraySpace<T, V, S> space, TensorForm<V> next) {
this.space = space;
this.next = next;
}
@Override
public Class<?> type() {
return (Class<?>) Array.newInstance(this.next.type(), 0).getClass();
}
@Override
public TensorForm<V> next() {
return this.next;
}
@Override
public T fromArray(Object... array) {
return this.space.of(array);
}
@Override
public Object[] toArray(T tensor) {
return this.space.toArray(tensor);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorDims.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Form;
import swim.structure.Kind;
import swim.structure.Value;
import swim.util.Murmur3;
public final class TensorDims implements Debug {
public final int size;
public final int stride;
final TensorDims next;
TensorDims(int size, int stride, TensorDims next) {
this.size = size;
this.stride = stride;
this.next = next;
}
public boolean isDefined() {
return this.size != 0 || this.stride != 0 || this.next != null;
}
public int rank() {
TensorDims dim = this;
int n = 0;
do {
dim = dim.next;
n += 1;
} while (dim != null);
return n;
}
public int size() {
return this.size;
}
public int stride() {
return this.stride;
}
public TensorDims next() {
return this.next;
}
public boolean isPacked() {
return this.stride == (this.next != null ? this.next.size * this.next.stride : 1);
}
public boolean isFullyPacked() {
TensorDims dim = this;
do {
if (!dim.isPacked()) {
return false;
}
dim = dim.next;
} while (dim != null);
return true;
}
public TensorDims by(int size, int stride) {
if (!isDefined()) {
return of(size, stride);
} else if (stride == this.size * this.stride) {
return by(size);
} else {
return new TensorDims(size, stride, this);
}
}
public TensorDims by(int size) {
if (!isDefined()) {
return of(size);
} else if (size == 2 && this == d2()) {
return d2x2();
} else if (size == 3 && this == d3()) {
return d3x3();
} else if (size == 4 && this == d4()) {
return d4x4();
} else {
return new TensorDims(size, this.size * this.stride, this);
}
}
public TensorDims flattened() {
TensorDims dim = this;
int size = 1;
do {
if (dim.stride == (dim.next != null ? dim.next.size * dim.next.stride : 1)) {
size *= dim.size;
} else {
throw new DimensionException("flattened sparse dimensions");
}
dim = dim.next;
} while (dim != null);
return of(size);
}
public int[] toSizeArray(int[] sizes) {
TensorDims dim = this;
int i = 0;
do {
sizes[i] = dim.size;
dim = dim.next;
i += 1;
} while (dim != null);
return sizes;
}
public int[] toSizeArray() {
final int[] sizes = new int[rank()];
return toSizeArray(sizes);
}
public int[] toStrideArray(int[] strides) {
TensorDims dim = this;
int i = 0;
do {
strides[i] = dim.stride;
dim = dim.next;
i += 1;
} while (dim != null);
return strides;
}
public int[] toStrideArray() {
final int[] strides = new int[rank()];
return toStrideArray(strides);
}
public Value toValue() {
return form().mold(this).toValue();
}
public boolean conforms(TensorDims that) {
return conforms(this, that);
}
private static boolean conforms(TensorDims these, TensorDims those) {
do {
if (these.size != those.size) {
return false;
}
these = these.next;
those = those.next;
} while (these != null && those != null);
return these == null && those == null;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof TensorDims) {
return equals(this, (TensorDims) other);
}
return false;
}
static boolean equals(TensorDims these, TensorDims those) {
do {
if (these.size != those.size || these.stride != those.stride) {
return false;
}
these = these.next;
those = those.next;
} while (these != null && those != null);
return these == null && those == null;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(TensorDims.class);
}
return Murmur3.mash(hash(hashSeed, this));
}
static int hash(int code, TensorDims dim) {
do {
code = Murmur3.mix(Murmur3.mix(code, dim.size), dim.stride);
dim = dim.next;
} while (dim != null);
return code;
}
@Override
public void debug(Output<?> output) {
if (this.next != null) {
output = output.debug(this.next).write('.').write("by").write('(').debug(this.size);
if (!isPacked()) {
output = output.write(", ").debug(this.stride);
}
output = output.write(')');
} else {
output = output.write("TensorDims").write('.').write("of").write('(').debug(this.size);
if (!isPacked()) {
output = output.write(", ").debug(this.stride);
}
output = output.write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static TensorDims undefined;
private static TensorDims d1;
private static TensorDims d2;
private static TensorDims d3;
private static TensorDims d4;
private static TensorDims d2x2;
private static TensorDims d3x3;
private static TensorDims d4x4;
private static Form<TensorDims> form;
public static TensorDims undefined() {
if (undefined == null) {
undefined = new TensorDims(0, 0, null);
}
return undefined;
}
public static TensorDims d1() {
if (d1 == null) {
d1 = new TensorDims(1, 1, null);
}
return d1;
}
public static TensorDims d2() {
if (d2 == null) {
d2 = new TensorDims(2, 1, null);
}
return d2;
}
public static TensorDims d3() {
if (d3 == null) {
d3 = new TensorDims(3, 1, null);
}
return d3;
}
public static TensorDims d4() {
if (d4 == null) {
d4 = new TensorDims(4, 1, null);
}
return d4;
}
public static TensorDims d2x2() {
if (d2x2 == null) {
d2x2 = new TensorDims(2, 2, d2());
}
return d2x2;
}
public static TensorDims d3x3() {
if (d3x3 == null) {
d3x3 = new TensorDims(3, 3, d3());
}
return d3x3;
}
public static TensorDims d4x4() {
if (d4x4 == null) {
d4x4 = new TensorDims(4, 4, d4());
}
return d4x4;
}
public static TensorDims of(int size, int stride) {
if (size == 0 && stride == 0) {
return undefined();
} else if (stride == 1) {
return of(size);
} else {
return new TensorDims(size, stride, null);
}
}
public static TensorDims of(int size) {
if (size == 0) {
return undefined();
} else if (size == 1) {
return d1();
} else if (size == 2) {
return d2();
} else if (size == 3) {
return d3();
} else if (size == 4) {
return d4();
} else {
return new TensorDims(size, 1, null);
}
}
@Kind
public static Form<TensorDims> form() {
if (form == null) {
form = new TensorDimsForm();
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorDimsForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Attr;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Record;
import swim.structure.Value;
final class TensorDimsForm extends Form<TensorDims> {
@Override
public String tag() {
return "dim";
}
@Override
public TensorDims unit() {
return TensorDims.undefined();
}
@Override
public Class<?> type() {
return TensorDims.class;
}
@Override
public Item mold(TensorDims dim) {
final Record record = Record.create(dim.rank());
do {
final Record header = Record.create(2);
header.slot("size", dim.size);
if (!dim.isPacked()) {
header.slot("stride", dim.stride);
}
record.attr(tag(), header);
dim = dim.next;
} while (dim != null);
return record;
}
@Override
public TensorDims cast(Item item) {
final Value value = item.toValue();
TensorDims next = null;
for (int i = value.length() - 1; i >= 0; i -= 1) {
final Item member = value.getItem(i);
if (member instanceof Attr && tag().equals(member.key().stringValue(null))) {
final Value header = member.toValue();
final Value size = header.get("size");
if (size instanceof Num) {
final Value stride = header.get("stride");
if (stride instanceof Num) {
if (next != null) {
next = next.by(size.intValue(), stride.intValue());
} else {
next = TensorDims.of(size.intValue(), stride.intValue());
}
} else if (next != null) {
next = next.by(size.intValue());
} else {
next = TensorDims.of(size.intValue());
}
}
}
}
return next;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Form;
import swim.structure.Item;
public abstract class TensorForm<T> extends Form<T> {
public abstract T fromTensor(TensorDims dims, float[] tensor, int offset);
public abstract T fromTensor(TensorDims dims, double[] tensor, int offset);
public abstract void toTensor(T object, TensorDims dims, float[] tensor, int offset);
public abstract void toTensor(T object, TensorDims dims, double[] tensor, int offset);
public Item moldTensor(TensorDims dims, float[] tensor, int offset) {
return mold(fromTensor(dims, tensor, offset));
}
public Item moldTensor(TensorDims dims, double[] tensor, int offset) {
return mold(fromTensor(dims, tensor, offset));
}
public void castTensor(Item item, TensorDims dims, float[] tensor, int offset) {
toTensor(cast(item), dims, tensor, offset);
}
public void castTensor(Item item, TensorDims dims, double[] tensor, int offset) {
toTensor(cast(item), dims, tensor, offset);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorObjectForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
final class TensorObjectForm extends TensorForm<Tensor> {
final TensorDims dims;
final Precision prec;
TensorObjectForm(TensorDims dims, Precision prec) {
this.dims = dims;
this.prec = prec;
}
@Override
public String tag() {
return "tensor";
}
@Override
public Tensor unit() {
return Tensor.zero(this.dims);
}
@Override
public Class<?> type() {
return Tensor.class;
}
@Override
public Item mold(Tensor tensor) {
if (tensor != null) {
return Tensor.mold(tag(), tensor);
} else {
return Item.extant();
}
}
@Override
public Tensor cast(Item item) {
return Tensor.cast(tag(), item.toValue(), this.dims, this.prec);
}
@Override
public Tensor fromTensor(TensorDims ud, float[] us, int ui) {
if (this.prec.isDouble()) {
final double[] ws = new double[this.dims.size * this.dims.stride];
Tensor.copy(ud, us, ui, dims, ws, 0);
return new Tensor(this.dims, us);
} else if (this.prec.isSingle()) {
final float[] ws = new float[this.dims.size * this.dims.stride];
Tensor.copy(ud, us, ui, dims, ws, 0);
return new Tensor(this.dims, us);
} else {
throw new AssertionError();
}
}
@Override
public Tensor fromTensor(TensorDims ud, double[] us, int ui) {
if (this.prec.isDouble()) {
final double[] ws = new double[this.dims.size * this.dims.stride];
Tensor.copy(ud, us, ui, this.dims, ws, 0);
return new Tensor(this.dims, us);
} else if (this.prec.isSingle()) {
final float[] ws = new float[this.dims.size * this.dims.stride];
Tensor.copy(ud, us, ui, this.dims, ws, 0);
return new Tensor(this.dims, us);
} else {
throw new AssertionError();
}
}
public void toTensor(Tensor u, TensorDims wd, float[] ws, int wi) {
final Object us = u.array;
if (us instanceof double[]) {
Tensor.copy(u.dims, (double[]) us, 0, wd, ws, wi);
} else if (us instanceof float[]) {
Tensor.copy(u.dims, (float[]) us, 0, wd, ws, wi);
} else {
throw new AssertionError();
}
}
public void toTensor(Tensor u, TensorDims wd, double[] ws, int wi) {
final Object us = u.array;
if (us instanceof double[]) {
Tensor.copy(u.dims, (double[]) us, 0, wd, ws, wi);
} else if (us instanceof float[]) {
Tensor.copy(u.dims, (float[]) us, 0, wd, ws, wi);
} else {
throw new AssertionError();
}
}
@Override
public Item moldTensor(TensorDims ud, float[] us, int ui) {
return Tensor.mold(tag(), ud, us, ui);
}
@Override
public Item moldTensor(TensorDims ud, double[] us, int ui) {
return Tensor.mold(tag(), ud, us, ui);
}
@Override
public void castTensor(Item item, TensorDims wd, float[] ws, int wi) {
Tensor.cast(tag(), item.toValue(), wd, ws, wi);
}
@Override
public void castTensor(Item item, TensorDims wd, double[] ws, int wi) {
Tensor.cast(tag(), item.toValue(), wd, ws, wi);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorObjectSpace.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
final class TensorObjectSpace implements TensorSpace<Tensor, Double>, Debug {
final TensorSpace<Tensor, Double> next;
final TensorDims dims;
TensorObjectSpace(TensorSpace<Tensor, Double> next, TensorDims dims) {
this.next = next;
this.dims = dims;
}
TensorObjectSpace(TensorSpace<Tensor, Double> next, int n) {
this(next, next.dimensions().by(n));
}
@Override
public R scalar() {
return R.field();
}
@Override
public TensorDims dimensions() {
return this.dims;
}
public TensorForm<Tensor> form() {
return Tensor.form(this.dims);
}
@Override
public Tensor zero() {
return Tensor.zero(this.dims);
}
public Tensor of(double... array) {
return new Tensor(this.dims, array);
}
public Tensor of(float... array) {
return new Tensor(this.dims, array);
}
@Override
public Tensor add(Tensor u, Tensor v) {
return u.plus(v);
}
@Override
public Tensor opposite(Tensor v) {
return v.opposite();
}
@Override
public Tensor subtract(Tensor u, Tensor v) {
return u.minus(v);
}
@Override
public Tensor multiply(Tensor u, Double a) {
return u.times(a);
}
@Override
public Tensor combine(Double a, Tensor u, Double b, Tensor v) {
return Tensor.combine(a, u, b, v);
}
@Override
public void debug(Output<?> output) {
output.write("Tensor").write('.').write("space").write('(')
.debug(this.next).write(", ").debug(this.dims).write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/TensorSpace.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface TensorSpace<V, S> {
Field<S> scalar();
TensorDims dimensions();
V zero();
V add(V u, V v);
V opposite(V v);
V subtract(V u, V v);
V multiply(V u, S a);
V combine(S a, V u, S b, V v);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/UniformDistribution.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public class UniformDistribution extends Distribution {
final Random random;
final double lower;
final double upper;
public UniformDistribution(Random random, double lower, double upper) {
if (lower >= upper) {
throw new IllegalArgumentException(lower + " >= " + upper);
}
this.random = random;
this.lower = lower;
this.upper = upper;
}
public UniformDistribution(double lower, double upper) {
this(Random.get(), lower, upper);
}
@Override
public double density(double x) {
if (this.lower <= x && x <= this.upper) {
return 1.0 / (this.upper - this.lower);
} else {
return 0.0;
}
}
@Override
public double sample() {
final double u = this.random.nextDouble();
return u * this.upper + (1.0 - u) * this.lower;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorModule.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface VectorModule<V, S> {
Ring<S> scalar();
V zero();
V add(V u, V v);
V opposite(V v);
V subtract(V u, V v);
V multiply(V u, S a);
V combine(S a, V u, S b, V v);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorR2.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Kind;
import swim.structure.Value;
import swim.util.Murmur3;
public class VectorR2 implements Debug {
public final double x;
public final double y;
public VectorR2(double x, double y) {
this.x = x;
this.y = y;
}
public final VectorR2 plus(VectorR2 that) {
return new VectorR2(this.x + that.x, this.y + that.y);
}
public final VectorR2 opposite() {
return new VectorR2(-this.x, -this.y);
}
public final VectorR2 minus(VectorR2 that) {
return new VectorR2(this.x - that.x, this.y - that.y);
}
public final VectorR2 times(double scalar) {
return new VectorR2(this.x * scalar, this.y * scalar);
}
public VectorZ2 transform(R2ToZ2Function f) {
return new VectorZ2(f.transformX(this.x, this.y), f.transformY(this.x, this.y));
}
public Value toValue() {
return form().mold(this).toValue();
}
protected boolean canEqual(VectorR2 that) {
return true;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof VectorR2) {
final VectorR2 that = (VectorR2) other;
return that.canEqual(this) && this.x == that.x && this.y == that.y;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(VectorR2.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
Murmur3.hash(this.x)), Murmur3.hash(this.y)));
}
@Override
public void debug(Output<?> output) {
output.write("VectorR2").write('.').write("of").write('(')
.debug(this.x).write(", ").debug(this.y).write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static VectorR2 zero;
private static TensorForm<VectorR2> form;
public static VectorR2 zero() {
if (zero == null) {
zero = new VectorR2(0.0, 0.0);
}
return zero;
}
public static VectorR2 of(double x, double y) {
return new VectorR2(x, y);
}
@Kind
public static TensorForm<VectorR2> form() {
if (form == null) {
form = new VectorR2Form();
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorR2Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
final class VectorR2Form extends TensorForm<VectorR2> {
@Override
public String tag() {
return "vector";
}
@Override
public VectorR2 unit() {
return VectorR2.zero();
}
@Override
public Class<?> type() {
return VectorR2.class;
}
@Override
public Item mold(VectorR2 vector) {
if (vector != null) {
return Record.create(1).attr(tag(), Record.create(2)
.item(vector.x).item(vector.y));
} else {
return Item.extant();
}
}
@Override
public VectorR2 cast(Item item) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final double x = header.getItem(0).doubleValue(0.0);
final double y = header.getItem(1).doubleValue(0.0);
return new VectorR2(x, y);
} else {
return null;
}
}
@Override
public VectorR2 fromTensor(TensorDims dim, float[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final double x = (double) tensor[offset];
offset += dim.stride;
final double y = (double) tensor[offset];
return new VectorR2(x, y);
}
@Override
public VectorR2 fromTensor(TensorDims dim, double[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final double x = tensor[offset];
offset += dim.stride;
final double y = tensor[offset];
return new VectorR2(x, y);
}
public void toTensor(VectorR2 vector, TensorDims dim, float[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
tensor[offset] = (float) vector.x;
offset += dim.stride;
tensor[offset] = (float) vector.y;
}
public void toTensor(VectorR2 vector, TensorDims dim, double[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
tensor[offset] = vector.x;
offset += dim.stride;
tensor[offset] = vector.y;
}
@Override
public Item moldTensor(TensorDims dim, float[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final Record header = Record.create(2);
header.item(tensor[offset]);
offset += dim.stride;
header.item(tensor[offset]);
return Record.create(1).attr(tag(), header);
}
@Override
public Item moldTensor(TensorDims dim, double[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final Record header = Record.create(2);
header.item(tensor[offset]);
offset += dim.stride;
header.item(tensor[offset]);
return Record.create(1).attr(tag(), header);
}
@Override
public void castTensor(Item item, TensorDims dim, float[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
tensor[offset] = header.getItem(0).floatValue(0.0f);
offset += dim.stride;
tensor[offset] = header.getItem(1).floatValue(0.0f);
}
}
@Override
public void castTensor(Item item, TensorDims dim, double[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
tensor[offset] = header.getItem(0).doubleValue(0.0);
offset += dim.stride;
tensor[offset] = header.getItem(1).doubleValue(0.0);
}
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorR3.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Kind;
import swim.structure.Value;
import swim.util.Murmur3;
public class VectorR3 implements Debug {
public final double x;
public final double y;
public final double z;
public VectorR3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public final VectorR3 plus(VectorR3 that) {
return new VectorR3(this.x + that.x, this.y + that.y, this.z + that.z);
}
public final VectorR3 opposite() {
return new VectorR3(-this.x, -this.y, -this.z);
}
public final VectorR3 minus(VectorR3 that) {
return new VectorR3(this.x - that.x, this.y - that.y, this.z - that.z);
}
public final VectorR3 times(double scalar) {
return new VectorR3(this.x * scalar, this.y * scalar, this.z * scalar);
}
public VectorZ3 transform(R3ToZ3Function f) {
return new VectorZ3(f.transformX(this.x, this.y, this.z),
f.transformY(this.x, this.y, this.z),
f.transformZ(this.x, this.y, this.z));
}
public Value toValue() {
return form().mold(this).toValue();
}
protected boolean canEqual(VectorR3 that) {
return true;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof VectorR3) {
final VectorR3 that = (VectorR3) other;
return that.canEqual(this) && this.x == that.x && this.y == that.y && this.z == that.z;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(VectorR3.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed,
Murmur3.hash(this.x)), Murmur3.hash(this.y)), Murmur3.hash(this.z)));
}
@Override
public void debug(Output<?> output) {
output.write("VectorR3").write('.').write("of").write('(')
.debug(this.x).write(", ").debug(this.y).write(", ").debug(this.z).write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static VectorR3 zero;
private static TensorForm<VectorR3> form;
public static VectorR3 zero() {
if (zero == null) {
zero = new VectorR3(0.0, 0.0, 0.0);
}
return zero;
}
public static VectorR3 of(double x, double y, double z) {
return new VectorR3(x, y, z);
}
@Kind
public static TensorForm<VectorR3> form() {
if (form == null) {
form = new VectorR3Form();
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorR3Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
final class VectorR3Form extends TensorForm<VectorR3> {
@Override
public String tag() {
return "vector";
}
@Override
public VectorR3 unit() {
return VectorR3.zero();
}
@Override
public Class<?> type() {
return VectorR3.class;
}
@Override
public Item mold(VectorR3 vector) {
if (vector != null) {
return Record.create(1).attr(tag(), Record.create(3)
.item(vector.x).item(vector.y).item(vector.z));
} else {
return Item.extant();
}
}
@Override
public VectorR3 cast(Item item) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final double x = header.getItem(0).doubleValue(0.0);
final double y = header.getItem(1).doubleValue(0.0);
final double z = header.getItem(2).doubleValue(0.0);
return new VectorR3(x, y, z);
} else {
return null;
}
}
@Override
public VectorR3 fromTensor(TensorDims dim, float[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final double x = (double) tensor[offset];
offset += dim.stride;
final double y = (double) tensor[offset];
offset += dim.stride;
final double z = (double) tensor[offset];
return new VectorR3(x, y, z);
}
@Override
public VectorR3 fromTensor(TensorDims dim, double[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final double x = tensor[offset];
offset += dim.stride;
final double y = tensor[offset];
offset += dim.stride;
final double z = tensor[offset];
return new VectorR3(x, y, z);
}
public void toTensor(VectorR3 vector, TensorDims dim, float[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
tensor[offset] = (float) vector.x;
offset += dim.stride;
tensor[offset] = (float) vector.y;
offset += dim.stride;
tensor[offset] = (float) vector.z;
}
public void toTensor(VectorR3 vector, TensorDims dim, double[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
tensor[offset] = vector.x;
offset += dim.stride;
tensor[offset] = vector.y;
offset += dim.stride;
tensor[offset] = vector.z;
}
@Override
public Item moldTensor(TensorDims dim, float[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final Record header = Record.create(3);
header.item(tensor[offset]);
offset += dim.stride;
header.item(tensor[offset]);
offset += dim.stride;
header.item(tensor[offset]);
return Record.create(1).attr(tag(), header);
}
@Override
public Item moldTensor(TensorDims dim, double[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final Record header = Record.create(3);
header.item(tensor[offset]);
offset += dim.stride;
header.item(tensor[offset]);
offset += dim.stride;
header.item(tensor[offset]);
return Record.create(1).attr(tag(), header);
}
@Override
public void castTensor(Item item, TensorDims dim, float[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
tensor[offset] = header.getItem(0).floatValue(0.0f);
offset += dim.stride;
tensor[offset] = header.getItem(1).floatValue(0.0f);
offset += dim.stride;
tensor[offset] = header.getItem(2).floatValue(0.0f);
}
}
@Override
public void castTensor(Item item, TensorDims dim, double[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
tensor[offset] = header.getItem(0).doubleValue(0.0);
offset += dim.stride;
tensor[offset] = header.getItem(1).doubleValue(0.0);
offset += dim.stride;
tensor[offset] = header.getItem(2).doubleValue(0.0);
}
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorRN.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Kind;
import swim.structure.Record;
import swim.structure.Value;
import swim.util.Murmur3;
public class VectorRN implements Debug {
final double[] array;
public VectorRN(double... array) {
this.array = array;
}
public final int dimension() {
return this.array.length;
}
public final double get(int i) {
return this.array[i];
}
public final VectorRN plus(VectorRN that) {
final double[] us = this.array;
final double[] vs = that.array;
final int n = us.length;
if (n != vs.length) {
throw new DimensionException();
}
final double[] ws = new double[n];
for (int i = 0; i < n; i += 1) {
ws[i] = us[i] + vs[i];
}
return new VectorRN(ws);
}
public final VectorRN opposite() {
final double[] us = this.array;
final int n = us.length;
final double[] ws = new double[n];
for (int i = 0; i < n; i += 1) {
ws[i] = -us[i];
}
return new VectorRN(ws);
}
public final VectorRN minus(VectorRN that) {
final double[] us = this.array;
final double[] vs = that.array;
final int n = us.length;
if (n != vs.length) {
throw new DimensionException();
}
final double[] ws = new double[n];
for (int i = 0; i < n; i += 1) {
ws[i] = us[i] - vs[i];
}
return new VectorRN(ws);
}
public final VectorRN times(double scalar) {
final double[] us = this.array;
final int n = us.length;
final double[] ws = new double[n];
for (int i = 0; i < n; i += 1) {
ws[i] = us[i] * scalar;
}
return new VectorRN(ws);
}
public Value toValue() {
final double[] us = this.array;
final int n = us.length;
final Record header = Record.create(n);
for (int i = 0; i < n; i += 1) {
header.item(us[i]);
}
return Record.create(1).attr("vector", header);
}
protected boolean canEqual(VectorRN that) {
return true;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof VectorRN) {
final VectorRN that = (VectorRN) other;
if (that.canEqual(this)) {
final double[] us = this.array;
final double[] vs = that.array;
final int n = us.length;
if (n == vs.length) {
for (int i = 0; i < n; i += 1) {
if (us[i] != vs[i]) {
return false;
}
}
return true;
}
}
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(VectorRN.class);
}
int code = hashSeed;
final double[] us = this.array;
final int n = us.length;
for (int i = 0; i < n; i += 1) {
code = Murmur3.mix(code, Murmur3.hash(us[i]));
}
return Murmur3.mash(code);
}
@Override
public void debug(Output<?> output) {
output = output.write("VectorRN").write('.').write("of").write('(');
final double[] us = this.array;
final int n = us.length;
if (n > 0) {
output = output.debug(us[0]);
for (int i = 1; i < n; i += 1) {
output = output.write(", ").debug(us[i]);
}
}
output = output.write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static TensorForm<VectorRN> form;
public static VectorRN of(double... array) {
return new VectorRN(array);
}
@Kind
public static TensorForm<VectorRN> form() {
if (form == null) {
form = new VectorRNForm();
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorRNForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
final class VectorRNForm extends TensorForm<VectorRN> {
@Override
public String tag() {
return "vector";
}
@Override
public Class<?> type() {
return VectorRN.class;
}
@Override
public Item mold(VectorRN vector) {
if (vector != null) {
final double[] us = vector.array;
final int n = us.length;
final Record header = Record.create(n);
for (int i = 0; i < n; i += 1) {
header.item(us[i]);
}
return Record.create(1).attr(tag(), header);
} else {
return Item.extant();
}
}
@Override
public VectorRN cast(Item item) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final int n = header.length();
final double[] us = new double[n];
for (int i = 0; i < n; i += 1) {
us[i] = header.getItem(i).doubleValue(0.0);
}
return new VectorRN(us);
} else {
return null;
}
}
@Override
public VectorRN fromTensor(TensorDims dim, float[] tensor, int offset) {
final int n = dim.size;
final double[] us = new double[n];
for (int i = 0; i < n; i += 1) {
us[i] = (double) tensor[offset];
offset += dim.stride;
}
return new VectorRN(us);
}
@Override
public VectorRN fromTensor(TensorDims dim, double[] tensor, int offset) {
final int n = dim.size;
final double[] us = new double[n];
if (dim.stride == 1) {
System.arraycopy(tensor, offset, us, 0, n);
} else {
for (int i = 0; i < n; i += 1) {
us[i] = tensor[offset];
offset += dim.stride;
}
}
return new VectorRN(us);
}
public void toTensor(VectorRN vector, TensorDims dim, float[] tensor, int offset) {
final double[] us = vector.array;
final int n = us.length;
if (n != dim.size || dim.next != null) {
throw new DimensionException();
}
for (int i = 0; i < n; i += 1) {
tensor[offset] = (float) us[i];
offset += dim.stride;
}
}
public void toTensor(VectorRN vector, TensorDims dim, double[] tensor, int offset) {
final double[] us = vector.array;
final int n = us.length;
if (n != dim.size || dim.next != null) {
throw new DimensionException();
}
for (int i = 0; i < n; i += 1) {
tensor[offset] = us[i];
offset += dim.stride;
}
}
@Override
public Item moldTensor(TensorDims dim, float[] tensor, int offset) {
final int n = dim.size;
final Record header = Record.create(n);
for (int i = 0; i < n; i += 1) {
header.item(tensor[offset]);
offset += dim.stride;
}
return Record.create(1).attr(tag(), header);
}
@Override
public Item moldTensor(TensorDims dim, double[] tensor, int offset) {
final int n = dim.size;
final Record header = Record.create(n);
for (int i = 0; i < n; i += 1) {
header.item(tensor[offset]);
offset += dim.stride;
}
return Record.create(1).attr(tag(), header);
}
@Override
public void castTensor(Item item, TensorDims dim, float[] tensor, int offset) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final int n = header.length();
if (n != dim.size || dim.next != null) {
throw new DimensionException();
}
for (int i = 0; i < n; i += 1) {
tensor[offset] = header.getItem(i).floatValue(0.0f);
offset += dim.stride;
}
}
}
@Override
public void castTensor(Item item, TensorDims dim, double[] tensor, int offset) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final int n = header.length();
if (n != dim.size || dim.next != null) {
throw new DimensionException();
}
for (int i = 0; i < n; i += 1) {
tensor[offset] = header.getItem(i).doubleValue(0.0);
offset += dim.stride;
}
}
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorSpace.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface VectorSpace<V, S> extends VectorModule<V, S>, TensorSpace<V, S> {
@Override
Field<S> scalar();
@Override
V zero();
@Override
V add(V u, V v);
@Override
V opposite(V v);
@Override
V subtract(V u, V v);
@Override
V multiply(V u, S a);
@Override
V combine(S a, V u, S b, V v);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorZ2.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Kind;
import swim.structure.Value;
import swim.util.Murmur3;
public class VectorZ2 implements Debug {
public final long x;
public final long y;
public VectorZ2(long x, long y) {
this.x = x;
this.y = y;
}
public final VectorZ2 plus(VectorZ2 that) {
return new VectorZ2(this.x + that.x, this.y + that.y);
}
public final VectorZ2 opposite() {
return new VectorZ2(-this.x, -this.y);
}
public final VectorZ2 minus(VectorZ2 that) {
return new VectorZ2(this.x - that.x, this.y - that.y);
}
public final VectorZ2 times(long scalar) {
return new VectorZ2(this.x * scalar, this.y * scalar);
}
public VectorR2 transform(Z2ToR2Function f) {
return new VectorR2(f.transformX(this.x, this.y), f.transformY(this.x, this.y));
}
public Value toValue() {
return form().mold(this).toValue();
}
protected boolean canEqual(VectorZ2 that) {
return true;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof VectorZ2) {
final VectorZ2 that = (VectorZ2) other;
return that.canEqual(this) && this.x == that.x && this.y == that.y;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(VectorZ2.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
Murmur3.hash(this.x)), Murmur3.hash(this.y)));
}
@Override
public void debug(Output<?> output) {
output.write("VectorZ2").write('.').write("of").write('(')
.debug(this.x).write(", ").debug(this.y).write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static VectorZ2 zero;
private static TensorForm<VectorZ2> form;
public static VectorZ2 zero() {
if (zero == null) {
zero = new VectorZ2(0L, 0L);
}
return zero;
}
public static VectorZ2 of(long x, long y) {
return new VectorZ2(x, y);
}
@Kind
public static TensorForm<VectorZ2> form() {
if (form == null) {
form = new VectorZ2Form();
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorZ2Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
final class VectorZ2Form extends TensorForm<VectorZ2> {
@Override
public String tag() {
return "vector";
}
@Override
public VectorZ2 unit() {
return VectorZ2.zero();
}
@Override
public Class<?> type() {
return VectorZ2.class;
}
@Override
public Item mold(VectorZ2 vector) {
if (vector != null) {
return Record.create(1).attr(tag(), Record.create(2)
.item(vector.x).item(vector.y));
} else {
return Item.extant();
}
}
@Override
public VectorZ2 cast(Item item) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final long x = header.getItem(0).longValue(0L);
final long y = header.getItem(1).longValue(0L);
return new VectorZ2(x, y);
} else {
return null;
}
}
@Override
public VectorZ2 fromTensor(TensorDims dim, float[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final long x = (long) Math.round(tensor[offset]);
offset += dim.stride;
final long y = (long) Math.round(tensor[offset]);
return new VectorZ2(x, y);
}
@Override
public VectorZ2 fromTensor(TensorDims dim, double[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final long x = Math.round(tensor[offset]);
offset += dim.stride;
final long y = Math.round(tensor[offset]);
return new VectorZ2(x, y);
}
public void toTensor(VectorZ2 vector, TensorDims dim, float[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
tensor[offset] = (float) vector.x;
offset += dim.stride;
tensor[offset] = (float) vector.y;
}
public void toTensor(VectorZ2 vector, TensorDims dim, double[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
tensor[offset] = (double) vector.x;
offset += dim.stride;
tensor[offset] = (double) vector.y;
}
@Override
public Value moldTensor(TensorDims dim, float[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final Record header = Record.create(2);
header.item(Math.round(tensor[offset]));
offset += dim.stride;
header.item(Math.round(tensor[offset]));
return Record.create(1).attr(tag(), header);
}
@Override
public Value moldTensor(TensorDims dim, double[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final Record header = Record.create(2);
header.item(Math.round(tensor[offset]));
offset += dim.stride;
header.item(Math.round(tensor[offset]));
return Record.create(1).attr(tag(), header);
}
@Override
public void castTensor(Item item, TensorDims dim, float[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
tensor[offset] = header.getItem(0).floatValue(0.0f);
offset += dim.stride;
tensor[offset] = header.getItem(1).floatValue(0.0f);
}
}
@Override
public void castTensor(Item item, TensorDims dim, double[] tensor, int offset) {
if (dim.size != 2 || dim.next != null) {
throw new DimensionException();
}
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
tensor[offset] = header.getItem(0).doubleValue(0.0);
offset += dim.stride;
tensor[offset] = header.getItem(1).doubleValue(0.0);
}
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorZ3.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Kind;
import swim.structure.Value;
import swim.util.Murmur3;
public class VectorZ3 implements Debug {
public final long x;
public final long y;
public final long z;
public VectorZ3(long x, long y, long z) {
this.x = x;
this.y = y;
this.z = z;
}
public final VectorZ3 plus(VectorZ3 that) {
return new VectorZ3(this.x + that.x, this.y + that.y, this.z + that.z);
}
public final VectorZ3 opposite() {
return new VectorZ3(-this.x, -this.y, -this.z);
}
public final VectorZ3 minus(VectorZ3 that) {
return new VectorZ3(this.x - that.x, this.y - that.y, this.z - that.z);
}
public final VectorZ3 times(long scalar) {
return new VectorZ3(this.x * scalar, this.y * scalar, this.z * scalar);
}
public VectorR3 transform(Z3ToR3Function f) {
return new VectorR3(f.transformX(this.x, this.y, this.z),
f.transformY(this.x, this.y, this.z),
f.transformZ(this.x, this.y, this.z));
}
public Value toValue() {
return form().mold(this).toValue();
}
protected boolean canEqual(VectorZ3 that) {
return true;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof VectorZ3) {
final VectorZ3 that = (VectorZ3) other;
return that.canEqual(this) && this.x == that.x && this.y == that.y && this.z == that.z;
}
return false;
}
@Override
public int hashCode() {
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(0x42B7FBDF,
Murmur3.hash(x)), Murmur3.hash(y)), Murmur3.hash(z)));
}
@Override
public void debug(Output<?> output) {
output.write("VectorZ3").write('.').write("of").write('(')
.debug(this.x).write(", ").debug(this.y).write(", ").debug(this.z).write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static VectorZ3 zero;
private static TensorForm<VectorZ3> form;
public static VectorZ3 zero() {
if (zero == null) {
zero = new VectorZ3(0L, 0L, 0L);
}
return zero;
}
public static VectorZ3 of(long x, long y, long z) {
return new VectorZ3(x, y, z);
}
@Kind
public static TensorForm<VectorZ3> form() {
if (form == null) {
form = new VectorZ3Form();
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/VectorZ3Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
final class VectorZ3Form extends TensorForm<VectorZ3> {
@Override
public String tag() {
return "vector";
}
@Override
public VectorZ3 unit() {
return VectorZ3.zero();
}
@Override
public Class<?> type() {
return VectorZ3.class;
}
@Override
public Item mold(VectorZ3 vector) {
if (vector != null) {
return Record.create(1).attr(tag(), Record.create(3)
.item(vector.x).item(vector.y).item(vector.z));
} else {
return Item.extant();
}
}
@Override
public VectorZ3 cast(Item item) {
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
final long x = header.getItem(0).longValue(0L);
final long y = header.getItem(1).longValue(0L);
final long z = header.getItem(2).longValue(0L);
return new VectorZ3(x, y, z);
} else {
return null;
}
}
@Override
public VectorZ3 fromTensor(TensorDims dim, float[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final long x = (long) Math.round(tensor[offset]);
offset += dim.stride;
final long y = (long) Math.round(tensor[offset]);
offset += dim.stride;
final long z = (long) Math.round(tensor[offset]);
return new VectorZ3(x, y, z);
}
@Override
public VectorZ3 fromTensor(TensorDims dim, double[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final long x = Math.round(tensor[offset]);
offset += dim.stride;
final long y = Math.round(tensor[offset]);
offset += dim.stride;
final long z = Math.round(tensor[offset]);
return new VectorZ3(x, y, z);
}
public void toTensor(VectorZ3 vector, TensorDims dim, float[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
tensor[offset] = (float) vector.x;
offset += dim.stride;
tensor[offset] = (float) vector.y;
offset += dim.stride;
tensor[offset] = (float) vector.z;
}
public void toTensor(VectorZ3 vector, TensorDims dim, double[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
tensor[offset] = (double) vector.x;
offset += dim.stride;
tensor[offset] = (double) vector.y;
offset += dim.stride;
tensor[offset] = (double) vector.z;
}
@Override
public Item moldTensor(TensorDims dim, float[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final Record header = Record.create(3);
header.item(Math.round(tensor[offset]));
offset += dim.stride;
header.item(Math.round(tensor[offset]));
offset += dim.stride;
header.item(Math.round(tensor[offset]));
return Record.create(1).attr(tag(), header);
}
@Override
public Item moldTensor(TensorDims dim, double[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final Record header = Record.create(3);
header.item(Math.round(tensor[offset]));
offset += dim.stride;
header.item(Math.round(tensor[offset]));
offset += dim.stride;
header.item(Math.round(tensor[offset]));
return Record.create(1).attr(tag(), header);
}
@Override
public void castTensor(Item item, TensorDims dim, float[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
tensor[offset] = header.getItem(0).floatValue(0.0f);
offset += dim.stride;
tensor[offset] = header.getItem(1).floatValue(0.0f);
offset += dim.stride;
tensor[offset] = header.getItem(2).floatValue(0.0f);
}
}
@Override
public void castTensor(Item item, TensorDims dim, double[] tensor, int offset) {
if (dim.size != 3 || dim.next != null) {
throw new DimensionException();
}
final Value header = item.toValue().header(tag());
if (header.isDefined()) {
tensor[offset] = header.getItem(0).doubleValue(0.0);
offset += dim.stride;
tensor[offset] = header.getItem(1).doubleValue(0.0);
offset += dim.stride;
tensor[offset] = header.getItem(2).doubleValue(0.0);
}
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
public class Z implements VectorModule<Long, Long>, OrderedRing<Long>, Debug {
protected Z() {
// stub
}
@Override
public Z scalar() {
return this;
}
@Override
public final Long zero() {
return 0L;
}
@Override
public final Long unit() {
return 1L;
}
@Override
public final Long add(Long a, Long b) {
return a + b;
}
@Override
public final Long opposite(Long a) {
return -a;
}
@Override
public final Long subtract(Long a, Long b) {
return a - b;
}
@Override
public final Long multiply(Long a, Long b) {
return a * b;
}
@Override
public Long combine(Long a, Long u, Long b, Long v) {
return a * u + b * v;
}
@Override
public final Long abs(Long a) {
return Math.abs(a);
}
@Override
public final Long min(Long a, Long b) {
return Math.min(a, b);
}
@Override
public final Long max(Long a, Long b) {
return Math.max(a, b);
}
@Override
public final int compare(Long a, Long b) {
return Long.compare(a, b);
}
@Override
public void debug(Output<?> output) {
output.write('Z').write('.').write("ring").write('(').write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static Z ring;
public static Z ring() {
if (ring == null) {
ring = new Z();
}
return ring;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z2.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
public class Z2 implements F2<VectorZ2, Long>, Debug {
protected Z2() {
// stub
}
@Override
public final Z scalar() {
return Z.ring();
}
@Override
public final VectorZ2 zero() {
return VectorZ2.zero();
}
@Override
public final VectorZ2 of(Long x, Long y) {
return VectorZ2.of(x, y);
}
@Override
public final Long getX(VectorZ2 v) {
return v.x;
}
@Override
public final Long getY(VectorZ2 v) {
return v.y;
}
@Override
public final VectorZ2 add(VectorZ2 u, VectorZ2 v) {
return u.plus(v);
}
@Override
public final VectorZ2 opposite(VectorZ2 v) {
return v.opposite();
}
@Override
public final VectorZ2 subtract(VectorZ2 u, VectorZ2 v) {
return u.minus(v);
}
@Override
public final VectorZ2 multiply(VectorZ2 u, Long a) {
return u.times(a);
}
@Override
public final VectorZ2 combine(Long a, VectorZ2 u, Long b, VectorZ2 v) {
return new VectorZ2(a * u.x + b * v.x, a * u.y + b * v.y);
}
@Override
public void debug(Output<?> output) {
output.write("Z2").write('.').write("module").write('(').write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static Z2 module;
public static Z2 module() {
if (module == null) {
module = new Z2();
}
return module;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z2Boundary.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface Z2Boundary<T> extends Boundary<T> {
long getXMin(T object);
long getYMin(T object);
long getXMax(T object);
long getYMax(T object);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z2Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Form;
public abstract class Z2Form<T> extends Form<T> implements Z2Boundary<T> {
@Override
public abstract long getXMin(T object);
@Override
public abstract long getYMin(T object);
@Override
public abstract long getXMax(T object);
@Override
public abstract long getYMax(T object);
@Override
public abstract boolean contains(T outer, T inner);
@Override
public abstract boolean intersects(T s, T t);
public static <T> R2Form<T> transformed(Z2Form<T> form, Z2ToR2Function function) {
return new Z2ToR2Form<T>(form, function);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z2Shape.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public abstract class Z2Shape implements Shape {
public abstract long xMin();
public abstract long yMin();
public abstract long xMax();
public abstract long yMax();
@Override
public boolean contains(Shape shape) {
if (shape instanceof Z2Shape) {
return contains((Z2Shape) shape);
} else {
return false;
}
}
@Override
public boolean intersects(Shape shape) {
if (shape instanceof Z2Shape) {
return intersects((Z2Shape) shape);
} else {
return false;
}
}
public abstract boolean contains(Z2Shape shape);
public abstract boolean intersects(Z2Shape shape);
public abstract R2Shape transform(Z2ToR2Function f);
private static Z2Form<Z2Shape> shapeForm;
public static Z2Form<Z2Shape> shapeForm() {
if (shapeForm == null) {
shapeForm = new Z2ShapeForm();
}
return shapeForm;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z2ShapeForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Value;
final class Z2ShapeForm extends Z2Form<Z2Shape> {
@Override
public Class<?> type() {
return Z2Shape.class;
}
@Override
public long getXMin(Z2Shape shape) {
return shape.xMin();
}
@Override
public long getYMin(Z2Shape shape) {
return shape.yMin();
}
@Override
public long getXMax(Z2Shape shape) {
return shape.xMax();
}
@Override
public long getYMax(Z2Shape shape) {
return shape.yMax();
}
@Override
public boolean contains(Z2Shape outer, Z2Shape inner) {
return outer.contains(inner);
}
@Override
public boolean intersects(Z2Shape s, Z2Shape t) {
return s.intersects(t);
}
@Override
public Item mold(Z2Shape shape) {
if (shape != null) {
return shape.toValue();
} else {
return Item.extant();
}
}
@Override
public Z2Shape cast(Item item) {
final Value value = item.toValue();
final String tag = value.tag();
if ("point".equals(tag)) {
return PointZ2.form().cast(value);
} else if ("box".equals(tag)) {
return BoxZ2.form().cast(value);
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z2ToR2Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
final class Z2ToR2Form<T> extends R2Form<T> {
final Z2Form<T> form;
final Z2ToR2Function function;
Z2ToR2Form(Z2Form<T> form, Z2ToR2Function function) {
this.form = form;
this.function = function;
}
@Override
public String tag() {
return this.form.tag();
}
@Override
public T unit() {
return this.form.unit();
}
@Override
public Class<?> type() {
return this.form.type();
}
@Override
public double getXMin(T object) {
return this.function.transformX(this.form.getXMin(object), this.form.getYMin(object));
}
@Override
public double getYMin(T object) {
return this.function.transformY(this.form.getXMin(object), this.form.getYMin(object));
}
@Override
public double getXMax(T object) {
return this.function.transformX(this.form.getXMax(object), this.form.getYMax(object));
}
@Override
public double getYMax(T object) {
return this.function.transformY(this.form.getXMax(object), this.form.getYMax(object));
}
@Override
public boolean contains(T outer, T inner) {
return this.form.contains(outer, inner);
}
@Override
public boolean intersects(T s, T t) {
return this.form.intersects(s, t);
}
@Override
public Item mold(T object) {
return this.form.mold(object);
}
@Override
public T cast(Item item) {
return this.form.cast(item);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z2ToR2Function.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface Z2ToR2Function {
double transformX(long x, long y);
double transformY(long x, long y);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z2ToR2Operator.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface Z2ToR2Operator extends Z2ToR2Function {
R2ToZ2Operator inverse();
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z3.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
public class Z3 implements F3<VectorZ3, Long>, Debug {
protected Z3() {
// stub
}
@Override
public final Z scalar() {
return Z.ring();
}
@Override
public final VectorZ3 zero() {
return VectorZ3.zero();
}
@Override
public final VectorZ3 of(Long x, Long y, Long z) {
return VectorZ3.of(x, y, z);
}
@Override
public final Long getX(VectorZ3 v) {
return v.x;
}
@Override
public final Long getY(VectorZ3 v) {
return v.y;
}
@Override
public final Long getZ(VectorZ3 v) {
return v.z;
}
@Override
public final VectorZ3 add(VectorZ3 u, VectorZ3 v) {
return u.plus(v);
}
@Override
public final VectorZ3 opposite(VectorZ3 v) {
return v.opposite();
}
@Override
public final VectorZ3 subtract(VectorZ3 u, VectorZ3 v) {
return u.minus(v);
}
@Override
public final VectorZ3 multiply(VectorZ3 u, Long a) {
return u.times(a);
}
@Override
public final VectorZ3 combine(Long a, VectorZ3 u, Long b, VectorZ3 v) {
return new VectorZ3(a * u.x + b * v.x, a * u.y + b * v.y, a * u.z + b * v.z);
}
@Override
public void debug(Output<?> output) {
output.write("Z3").write('.').write("module").write('(').write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
private static Z3 module;
public static Z3 module() {
if (module == null) {
module = new Z3();
}
return module;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z3Boundary.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface Z3Boundary<T> extends Boundary<T> {
long getXMin(T object);
long getYMin(T object);
long getZMin(T object);
long getXMax(T object);
long getYMax(T object);
long getZMax(T object);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z3Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Form;
public abstract class Z3Form<T> extends Form<T> implements Z3Boundary<T> {
@Override
public abstract long getXMin(T object);
@Override
public abstract long getYMin(T object);
@Override
public abstract long getZMin(T object);
@Override
public abstract long getXMax(T object);
@Override
public abstract long getYMax(T object);
@Override
public abstract long getZMax(T object);
@Override
public abstract boolean contains(T outer, T inner);
@Override
public abstract boolean intersects(T s, T t);
public static <T> R3Form<T> transformed(Z3Form<T> form, Z3ToR3Function function) {
return new Z3ToR3Form<T>(form, function);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z3Shape.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Kind;
public abstract class Z3Shape implements Shape {
public abstract long xMin();
public abstract long yMin();
public abstract long zMin();
public abstract long xMax();
public abstract long yMax();
public abstract long zMax();
@Override
public boolean contains(Shape shape) {
if (shape instanceof Z3Shape) {
return contains((Z3Shape) shape);
} else {
return false;
}
}
@Override
public boolean intersects(Shape shape) {
if (shape instanceof Z3Shape) {
return intersects((Z3Shape) shape);
} else {
return false;
}
}
public abstract boolean contains(Z3Shape shape);
public abstract boolean intersects(Z3Shape shape);
public abstract R3Shape transform(Z3ToR3Function f);
private static Z3Form<Z3Shape> shapeForm;
@Kind
public static Z3Form<Z3Shape> shapeForm() {
if (shapeForm == null) {
shapeForm = new Z3ShapeForm();
}
return shapeForm;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z3ShapeForm.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
import swim.structure.Value;
final class Z3ShapeForm extends Z3Form<Z3Shape> {
@Override
public Class<?> type() {
return Z3Shape.class;
}
@Override
public long getXMin(Z3Shape shape) {
return shape.xMin();
}
@Override
public long getYMin(Z3Shape shape) {
return shape.yMin();
}
@Override
public long getZMin(Z3Shape shape) {
return shape.zMin();
}
@Override
public long getXMax(Z3Shape shape) {
return shape.xMax();
}
@Override
public long getYMax(Z3Shape shape) {
return shape.yMax();
}
@Override
public long getZMax(Z3Shape shape) {
return shape.zMax();
}
@Override
public boolean contains(Z3Shape outer, Z3Shape inner) {
return outer.contains(inner);
}
@Override
public boolean intersects(Z3Shape s, Z3Shape t) {
return s.intersects(t);
}
@Override
public Item mold(Z3Shape shape) {
if (shape != null) {
return shape.toValue();
} else {
return Item.extant();
}
}
@Override
public Z3Shape cast(Item item) {
final Value value = item.toValue();
final String tag = value.tag();
if ("point".equals(tag)) {
return PointZ3.form().cast(value);
} else if ("box".equals(tag)) {
return BoxZ3.form().cast(value);
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z3ToR3Form.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
import swim.structure.Item;
final class Z3ToR3Form<T> extends R3Form<T> {
final Z3Form<T> form;
final Z3ToR3Function function;
Z3ToR3Form(Z3Form<T> form, Z3ToR3Function function) {
this.form = form;
this.function = function;
}
@Override
public String tag() {
return this.form.tag();
}
@Override
public T unit() {
return this.form.unit();
}
@Override
public Class<?> type() {
return this.form.type();
}
@Override
public double getXMin(T object) {
return this.function.transformX(this.form.getXMin(object), this.form.getYMin(object), this.form.getZMin(object));
}
@Override
public double getYMin(T object) {
return this.function.transformY(this.form.getXMin(object), this.form.getYMin(object), this.form.getZMin(object));
}
@Override
public double getZMin(T object) {
return this.function.transformZ(this.form.getXMin(object), this.form.getYMin(object), this.form.getZMin(object));
}
@Override
public double getXMax(T object) {
return this.function.transformX(this.form.getXMax(object), this.form.getYMax(object), this.form.getZMax(object));
}
@Override
public double getYMax(T object) {
return this.function.transformY(this.form.getXMax(object), this.form.getYMax(object), this.form.getZMax(object));
}
@Override
public double getZMax(T object) {
return this.function.transformZ(this.form.getXMax(object), this.form.getYMax(object), this.form.getZMax(object));
}
@Override
public boolean contains(T outer, T inner) {
return this.form.contains(outer, inner);
}
@Override
public boolean intersects(T s, T t) {
return this.form.intersects(s, t);
}
@Override
public Item mold(T object) {
return this.form.mold(object);
}
@Override
public T cast(Item item) {
return this.form.cast(item);
}
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z3ToR3Function.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface Z3ToR3Function {
double transformX(long x, long y, long z);
double transformY(long x, long y, long z);
double transformZ(long x, long y, long z);
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/Z3ToR3Operator.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.math;
public interface Z3ToR3Operator extends Z3ToR3Function {
R3ToZ3Operator inverse();
}
|
0 | java-sources/ai/swim/swim-math/3.10.0/swim | java-sources/ai/swim/swim-math/3.10.0/swim/math/package-info.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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.
/**
* Mathematical and geometric structures.
*/
package swim.math;
|
0 | java-sources/ai/swim/swim-mqtt | java-sources/ai/swim/swim-mqtt/3.10.0/module-info.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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.
/**
* MQTT wire protocol model, decoders, and encoders.
*/
module swim.mqtt {
requires swim.util;
requires transitive swim.codec;
requires transitive swim.collections;
requires transitive swim.structure;
exports swim.mqtt;
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/Mqtt.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
public final class Mqtt {
private Mqtt() {
// stub
}
private static MqttDecoder standardDecoder;
private static MqttEncoder standardEncoder;
public static MqttDecoder standardDecoder() {
if (standardDecoder == null) {
standardDecoder = new MqttDecoder();
}
return standardDecoder;
}
public static MqttEncoder standardEncoder() {
if (standardEncoder == null) {
standardEncoder = new MqttEncoder();
}
return standardEncoder;
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttConnAck.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Debug;
import swim.codec.Encoder;
import swim.codec.Format;
import swim.codec.Output;
import swim.codec.OutputBuffer;
import swim.util.Murmur3;
public final class MqttConnAck extends MqttPacket<Object> implements Debug {
final int packetFlags;
final int connectFlags;
final int connectCode;
MqttConnAck(int packetFlags, int connectFlags, int connectCode) {
this.packetFlags = packetFlags;
this.connectFlags = connectFlags;
this.connectCode = connectCode;
}
@Override
public int packetType() {
return 2;
}
@Override
public int packetFlags() {
return this.packetFlags;
}
public MqttConnAck packetFlags(int packetFlags) {
return new MqttConnAck(packetFlags, this.connectFlags, this.connectCode);
}
public int connectFlags() {
return this.connectFlags;
}
public int connectCode() {
return this.connectCode;
}
public boolean sessionPresent() {
return (this.connectFlags & SESSION_PRESENT_FLAG) != 0;
}
public MqttConnAck sessionPresent(boolean sessionPresent) {
final int connectFlags = sessionPresent
? this.connectFlags | SESSION_PRESENT_FLAG
: this.connectFlags & ~SESSION_PRESENT_FLAG;
return new MqttConnAck(this.packetFlags, connectFlags, this.connectCode);
}
public MqttConnStatus connectStatus() {
return MqttConnStatus.from(connectCode);
}
public MqttConnAck connectStatus(MqttConnStatus connectStatus) {
return new MqttConnAck(this.packetFlags, this.connectFlags, connectStatus.code);
}
@Override
int bodySize(MqttEncoder mqtt) {
return 2;
}
@Override
public Encoder<?, MqttConnAck> mqttEncoder(MqttEncoder mqtt) {
return mqtt.connAckEncoder(this);
}
@Override
public Encoder<?, MqttConnAck> encodeMqtt(OutputBuffer<?> output, MqttEncoder mqtt) {
return mqtt.encodeConnAck(this, output);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof MqttConnAck) {
final MqttConnAck that = (MqttConnAck) other;
return this.packetFlags == that.packetFlags && this.connectFlags == that.connectFlags
&& this.connectCode == that.connectCode;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(MqttConnAck.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed,
this.packetFlags), this.connectFlags), this.connectCode));
}
@Override
public void debug(Output<?> output) {
output = output.write("MqttConnAck").write('.');
if (this.packetFlags == 0 && this.connectFlags == 0 && this.connectCode == 0) {
output = output.write('.').write("accepted").write('(').write(')');
} else {
output = output.write("from").write('(').debug(connectStatus()).write(')');
if (this.packetFlags != 0) {
output = output.write('.').write("packetFlags").write('(').debug(this.packetFlags).write(')');
}
if (sessionPresent()) {
output = output.write('.').write("sessionPresent").write('(').write("true").write(')');
}
}
}
@Override
public String toString() {
return Format.debug(this);
}
static final int SESSION_PRESENT_FLAG = 0x01;
private static int hashSeed;
private static MqttConnAck accepted;
public static MqttConnAck accepted() {
if (accepted == null) {
accepted = new MqttConnAck(0, 0, 0);
}
return accepted;
}
public static MqttConnAck from(int packetFlags, int connectFlags, int connectCode) {
if (packetFlags == 0 && connectFlags == 0 && connectCode == 0) {
return accepted();
} else {
return new MqttConnAck(packetFlags, connectFlags, connectCode);
}
}
public static MqttConnAck from(MqttConnStatus connectStatus) {
if (connectStatus.code == 0) {
return accepted();
} else {
return new MqttConnAck(0, 0, connectStatus.code);
}
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttConnAckDecoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Decoder;
import swim.codec.DecoderException;
import swim.codec.InputBuffer;
final class MqttConnAckDecoder extends Decoder<MqttConnAck> {
final MqttDecoder mqtt;
final int packetFlags;
final int connectFlags;
final int connectCode;
final int remaining;
final int step;
MqttConnAckDecoder(MqttDecoder mqtt, int packetFlags, int connectFlags,
int connectCode, int remaining, int step) {
this.mqtt = mqtt;
this.packetFlags = packetFlags;
this.connectFlags = connectFlags;
this.connectCode = connectCode;
this.remaining = remaining;
this.step = step;
}
MqttConnAckDecoder(MqttDecoder mqtt) {
this(mqtt, 0, 0, 0, 0, 1);
}
@Override
public Decoder<MqttConnAck> feed(InputBuffer input) {
return decode(input, this.mqtt, this.packetFlags, this.connectFlags,
this.connectCode, this.remaining, this.step);
}
static Decoder<MqttConnAck> decode(InputBuffer input, MqttDecoder mqtt,
int packetFlags, int connectFlags,
int connectCode, int remaining, int step) {
if (step == 1 && input.isCont()) {
packetFlags = input.head() & 0x0f;
input = input.step();
step = 2;
}
while (step >= 2 && step <= 5 && input.isCont()) {
final int b = input.head();
input = input.step();
remaining |= (b & 0x7f) << (7 * (step - 2));
if ((b & 0x80) == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long"));
}
}
if (step == 6 && remaining > 0 && input.isCont()) {
connectFlags = input.head();
input = input.step();
remaining -= 1;
step = 7;
}
if (step == 7 && remaining > 0 && input.isCont()) {
connectCode = input.head();
input = input.step();
remaining -= 1;
step = 8;
}
if (step == 8 && remaining == 0) {
return done(mqtt.connAck(packetFlags, connectFlags, connectCode));
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (input.isDone()) {
return error(new DecoderException("incomplete"));
}
return new MqttConnAckDecoder(mqtt, packetFlags, connectFlags, connectCode,
remaining, step);
}
static Decoder<MqttConnAck> decode(InputBuffer input, MqttDecoder mqtt) {
return decode(input, mqtt, 0, 0, 0, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttConnAckEncoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Encoder;
import swim.codec.EncoderException;
import swim.codec.OutputBuffer;
final class MqttConnAckEncoder extends Encoder<Object, MqttConnAck> {
final MqttEncoder mqtt;
final MqttConnAck packet;
final int length;
final int remaining;
final int step;
MqttConnAckEncoder(MqttEncoder mqtt, MqttConnAck packet, int length, int remaining, int step) {
this.mqtt = mqtt;
this.packet = packet;
this.length = length;
this.remaining = remaining;
this.step = step;
}
MqttConnAckEncoder(MqttEncoder mqtt, MqttConnAck packet) {
this(mqtt, packet, 0, 0, 1);
}
@Override
public Encoder<Object, MqttConnAck> pull(OutputBuffer<?> output) {
return encode(output, this.mqtt, this.packet, this.length, this.remaining, this.step);
}
static Encoder<Object, MqttConnAck> encode(OutputBuffer<?> output, MqttEncoder mqtt,
MqttConnAck packet, int length, int remaining, int step) {
if (step == 1 && output.isCont()) {
length = packet.bodySize(mqtt);
remaining = length;
output = output.write(packet.packetType() << 4 | packet.packetFlags & 0x0f);
step = 2;
}
while (step >= 2 && step <= 5 && output.isCont()) {
int b = length & 0x7f;
length = length >>> 7;
if (length > 0) {
b |= 0x80;
}
output = output.write(b);
if (length == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long: " + remaining));
}
}
if (step == 6 && remaining > 0 && output.isCont()) {
output = output.write(packet.connectFlags);
remaining -= 1;
step = 7;
}
if (step == 7 && remaining > 0 && output.isCont()) {
output = output.write(packet.connectCode);
remaining -= 1;
step = 8;
}
if (step == 8 && remaining == 0) {
return done(packet);
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (output.isDone()) {
return error(new EncoderException("truncated"));
} else if (output.isError()) {
return error(output.trap());
}
return new MqttConnAckEncoder(mqtt, packet, length, remaining, step);
}
static Encoder<Object, MqttConnAck> encode(OutputBuffer<?> output, MqttEncoder mqtt,
MqttConnAck packet) {
return encode(output, mqtt, packet, 0, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttConnStatus.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.util.Murmur3;
public final class MqttConnStatus implements Debug {
public final int code;
MqttConnStatus(int code) {
this.code = code;
}
public boolean isAccepted() {
return this.code == 0;
}
public boolean isRefused() {
return this.code != 0;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof MqttConnStatus) {
final MqttConnStatus that = (MqttConnStatus) other;
return this.code == that.code;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(MqttConnStatus.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.code));
}
@Override
public void debug(Output<?> output) {
output = output.write("MqttConnStatus").write('.');
switch (this.code) {
case 0: output.write("ACCEPTED"); break;
case 1: output.write("UNACCEPTABLE_PROTOCOL_VERSION"); break;
case 2: output.write("IDENTIFIER_REJECTED"); break;
case 3: output.write("SERVER_UNAVAILABLE"); break;
case 4: output.write("BAD_USERNAME_OR_PASSWORD"); break;
case 5: output.write("NOT_AUTHORIZED"); break;
default: output.write("from").write('(').debug(this.code).write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
public static final MqttConnStatus ACCEPTED = new MqttConnStatus(0);
public static final MqttConnStatus UNACCEPTABLE_PROTOCOL_VERSION = new MqttConnStatus(1);
public static final MqttConnStatus IDENTIFIER_REJECTED = new MqttConnStatus(2);
public static final MqttConnStatus SERVER_UNAVAILABLE = new MqttConnStatus(3);
public static final MqttConnStatus BAD_USERNAME_OR_PASSWORD = new MqttConnStatus(4);
public static final MqttConnStatus NOT_AUTHORIZED = new MqttConnStatus(5);
public static MqttConnStatus from(int code) {
switch (code) {
case 0: return ACCEPTED;
case 1: return UNACCEPTABLE_PROTOCOL_VERSION;
case 2: return IDENTIFIER_REJECTED;
case 3: return SERVER_UNAVAILABLE;
case 4: return BAD_USERNAME_OR_PASSWORD;
case 5: return NOT_AUTHORIZED;
default: return new MqttConnStatus(code);
}
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttConnect.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Debug;
import swim.codec.Encoder;
import swim.codec.Format;
import swim.codec.Output;
import swim.codec.OutputBuffer;
import swim.structure.Data;
import swim.util.Murmur3;
public final class MqttConnect extends MqttPacket<Object> implements Debug {
final int packetFlags;
final String protocolName;
final int protocolLevel;
final int connectFlags;
final int keepAlive;
final String clientId;
final String willTopic;
final Data willMessage;
final String username;
final Data password;
MqttConnect(int packetFlags, String protocolName, int protocolLevel, int connectFlags,
int keepAlive, String clientId, String willTopic, Data willMessage,
String username, Data password) {
this.packetFlags = packetFlags;
this.protocolName = protocolName;
this.protocolLevel = protocolLevel;
this.connectFlags = connectFlags;
this.keepAlive = keepAlive;
this.clientId = clientId;
this.willTopic = willTopic;
this.willMessage = willMessage != null ? willMessage.commit() : null;
this.username = username;
this.password = password != null ? password.commit() : null;
}
@Override
public int packetType() {
return 1;
}
@Override
public int packetFlags() {
return this.packetFlags;
}
public MqttConnect packetFlags(int packetFlags) {
return new MqttConnect(packetFlags, this.protocolName, this.protocolLevel,
this.connectFlags, this.keepAlive, this.clientId,
this.willTopic, this.willMessage, this.username, this.password);
}
public String protocolName() {
return this.protocolName;
}
public MqttConnect protocolName(String protocolName) {
return new MqttConnect(this.packetFlags, protocolName, this.protocolLevel,
this.connectFlags, this.keepAlive, this.clientId,
this.willTopic, this.willMessage, this.username, this.password);
}
public int protocolLevel() {
return this.protocolLevel;
}
public MqttConnect protocolLevel(int protocolLevel) {
return new MqttConnect(this.packetFlags, this.protocolName, protocolLevel,
this.connectFlags, this.keepAlive, this.clientId,
this.willTopic, this.willMessage, this.username, this.password);
}
public int connectFlags() {
return this.connectFlags;
}
public int keepAlive() {
return this.keepAlive;
}
public MqttConnect keepAlive(int keepAlive) {
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
this.connectFlags, keepAlive, this.clientId, this.willTopic,
this.willMessage, this.username, this.password);
}
public String clientId() {
return this.clientId;
}
public MqttConnect clientId(String clientId) {
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
this.connectFlags, this.keepAlive, clientId, this.willTopic,
this.willMessage, this.username, this.password);
}
public boolean cleanSession() {
return (this.connectFlags & CLEAN_SESSION_FLAG) != 0;
}
public MqttConnect cleanSession(boolean cleanSession) {
final int connectFlags = cleanSession
? this.connectFlags | CLEAN_SESSION_FLAG
: this.connectFlags & ~CLEAN_SESSION_FLAG;
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
connectFlags, this.keepAlive, this.clientId, this.willTopic,
this.willMessage, this.username, this.password);
}
public String willTopic() {
return this.willTopic;
}
public MqttConnect willTopic(String willTopic) {
final int connectFlags = willTopic != null && this.willMessage != null
? this.connectFlags | WILL_FLAG
: this.connectFlags & ~WILL_FLAG;
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
connectFlags, this.keepAlive, this.clientId, willTopic,
this.willMessage, this.username, this.password);
}
public Data willMessage() {
return this.willMessage;
}
public MqttConnect willMessage(Data willMessage) {
final int connectFlags = this.willTopic != null && willMessage != null
? this.connectFlags | WILL_FLAG
: this.connectFlags & ~WILL_FLAG;
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
connectFlags, this.keepAlive, this.clientId, this.willTopic,
willMessage != null ? willMessage.commit() : null,
this.username, this.password);
}
public MqttQoS willQoS() {
return MqttQoS.from((this.connectFlags & WILL_QOS_MASK) >>> WILL_QOS_SHIFT);
}
public MqttConnect willQoS(MqttQoS willQoS) {
final int connectFlags = this.connectFlags & ~WILL_QOS_MASK
| (willQoS.code << WILL_QOS_SHIFT) & WILL_QOS_MASK;
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
connectFlags, this.keepAlive, this.clientId, this.willTopic,
this.willMessage, this.username, this.password);
}
public boolean willRetain() {
return (this.connectFlags & WILL_RETAIN_FLAG) != 0;
}
public MqttConnect willRetain(boolean willRetain) {
final int connectFlags = willRetain
? this.connectFlags | WILL_RETAIN_FLAG
: this.connectFlags & ~WILL_RETAIN_FLAG;
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
connectFlags, this.keepAlive, this.clientId, this.willTopic,
this.willMessage, this.username, this.password);
}
public String username() {
return this.username;
}
public MqttConnect username(String username) {
final int connectFlags = username != null
? this.connectFlags | USERNAME_FLAG
: this.connectFlags & ~USERNAME_FLAG;
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
connectFlags, this.keepAlive, this.clientId, this.willTopic,
this.willMessage, username, this.password);
}
public Data password() {
return this.password;
}
public MqttConnect password(Data password) {
final int connectFlags = password != null
? this.connectFlags | PASSWORD_FLAG
: this.connectFlags & ~PASSWORD_FLAG;
return new MqttConnect(this.packetFlags, this.protocolName, this.protocolLevel,
connectFlags, this.keepAlive, this.clientId, this.willTopic,
this.willMessage, this.username,
password != null ? password.commit() : null);
}
@Override
int bodySize(MqttEncoder mqtt) {
int size = mqtt.sizeOfString(this.protocolName) + 4;
size += mqtt.sizeOfString(this.clientId);
if ((this.connectFlags & WILL_FLAG) != 0) {
size += mqtt.sizeOfString(this.willTopic);
size += mqtt.sizeOfData(this.willMessage);
}
if ((this.connectFlags & USERNAME_FLAG) != 0) {
size += mqtt.sizeOfString(this.username);
}
if ((this.connectFlags & PASSWORD_FLAG) != 0) {
size += mqtt.sizeOfData(this.password);
}
return size;
}
@Override
public Encoder<?, MqttConnect> mqttEncoder(MqttEncoder mqtt) {
return mqtt.connectEncoder(this);
}
@Override
public Encoder<?, MqttConnect> encodeMqtt(OutputBuffer<?> output, MqttEncoder mqtt) {
return mqtt.encodeConnect(this, output);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof MqttConnect) {
final MqttConnect that = (MqttConnect) other;
return this.packetFlags == that.packetFlags && this.protocolName.equals(that.protocolName)
&& this.protocolLevel == that.protocolLevel && this.connectFlags == that.connectFlags
&& this.keepAlive == that.keepAlive && this.clientId.equals(that.clientId)
&& (this.willTopic == null ? that.willTopic == null : this.willTopic.equals(that.willTopic))
&& (this.willMessage == null ? that.willMessage == null : this.willMessage.equals(that.willMessage))
&& (this.username == null ? that.username == null : this.username.equals(that.username))
&& (this.password == null ? that.password == null : this.password.equals(that.password));
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(MqttConnect.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(
Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed,
this.packetFlags), this.protocolName.hashCode()), this.protocolLevel),
this.connectFlags), this.keepAlive), this.clientId.hashCode()),
Murmur3.hash(this.willTopic)), Murmur3.hash(this.willMessage)),
Murmur3.hash(this.username)), Murmur3.hash(this.password)));
}
@Override
public void debug(Output<?> output) {
output = output.write("MqttConnect").write('.').write("from").write('(')
.debug(this.clientId).write(')');
if (this.packetFlags != 0) {
output = output.write('.').write("packetFlags").write('(').debug(this.packetFlags).write(')');
}
if ("MQTT".equals(this.protocolName)) {
output = output.write('.').write("protocolName").write('(').debug(this.protocolName).write(')');
}
if (this.protocolLevel != 4) {
output = output.write('.').write("protocolLevel").write('(').debug(this.protocolLevel).write(')');
}
if (this.keepAlive != 0) {
output = output.write('.').write("keepAlive").write('(').debug(this.keepAlive).write(')');
}
if (cleanSession()) {
output = output.write('.').write("cleanSession").write('(').write("true").write(')');
}
if (this.willTopic != null) {
output = output.write('.').write("willTopic").write('(').debug(this.willTopic).write(')');
}
if (this.willMessage != null) {
output = output.write('.').write("willMessage").write('(').debug(this.willMessage).write(')');
}
if (willQoS().code != 0) {
output = output.write('.').write("willQoS").write('(').debug(willQoS()).write(')');
}
if (willRetain()) {
output = output.write('.').write("willRetain").write('(').write("true").write(')');
}
if (this.username != null) {
output = output.write('.').write("username").write('(').debug(this.username).write(')');
}
if (this.password != null) {
output = output.write('.').write("password").write('(').debug(this.password).write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
static final int CLEAN_SESSION_FLAG = 0x02;
static final int WILL_FLAG = 0x04;
static final int WILL_QOS_MASK = 0x18;
static final int WILL_QOS_SHIFT = 3;
static final int WILL_RETAIN_FLAG = 0x20;
static final int PASSWORD_FLAG = 0x40;
static final int USERNAME_FLAG = 0x80;
public static MqttConnect from(int packetFlags, String protocolName, int protocolLevel,
int connectFlags, int keepAlive, String clientId, String willTopic,
Data willMessage, String username, Data password) {
return new MqttConnect(packetFlags, protocolName, protocolLevel, connectFlags,
keepAlive, clientId, willTopic, willMessage, username, password);
}
public static MqttConnect from(String clientId) {
return new MqttConnect(0, "MQTT", 4, CLEAN_SESSION_FLAG, 0, clientId, null, null, null, null);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttConnectDecoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Decoder;
import swim.codec.DecoderException;
import swim.codec.InputBuffer;
import swim.structure.Data;
final class MqttConnectDecoder extends Decoder<MqttConnect> {
final MqttDecoder mqtt;
final int packetFlags;
final Decoder<String> protocolName;
final int protocolLevel;
final int connectFlags;
final int keepAlive;
final Decoder<String> clientId;
final Decoder<String> willTopic;
final Decoder<Data> willMessage;
final Decoder<String> username;
final Decoder<Data> password;
final int remaining;
final int step;
MqttConnectDecoder(MqttDecoder mqtt, int packetFlags, Decoder<String> protocolName,
int protocolLevel, int connectFlags, int keepAlive, Decoder<String> clientId,
Decoder<String> willTopic, Decoder<Data> willMessage, Decoder<String> username,
Decoder<Data> password, int remaining, int step) {
this.mqtt = mqtt;
this.packetFlags = packetFlags;
this.remaining = remaining;
this.protocolName = protocolName;
this.protocolLevel = protocolLevel;
this.connectFlags = connectFlags;
this.keepAlive = keepAlive;
this.clientId = clientId;
this.willTopic = willTopic;
this.willMessage = willMessage;
this.username = username;
this.password = password;
this.step = step;
}
MqttConnectDecoder(MqttDecoder mqtt) {
this(mqtt, 0, null, 0, 0, 0, null, null, null, null, null, 0, 1);
}
@Override
public Decoder<MqttConnect> feed(InputBuffer input) {
return decode(input, this.mqtt, this.packetFlags, this.protocolName, this.protocolLevel,
this.connectFlags, this.keepAlive, this.clientId, this.willTopic,
this.willMessage, this.username, this.password, this.remaining, this.step);
}
static Decoder<MqttConnect> decode(InputBuffer input, MqttDecoder mqtt, int packetFlags,
Decoder<String> protocolName, int protocolLevel,
int connectFlags, int keepAlive, Decoder<String> clientId,
Decoder<String> willTopic, Decoder<Data> willMessage,
Decoder<String> username, Decoder<Data> password,
int remaining, int step) {
if (step == 1 && input.isCont()) {
packetFlags = input.head() & 0x0f;
input = input.step();
step = 2;
}
while (step >= 2 && step <= 5 && input.isCont()) {
final int b = input.head();
input = input.step();
remaining |= (b & 0x7f) << (7 * (step - 2));
if ((b & 0x80) == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long"));
}
}
if (step == 6) {
final int inputStart = input.index();
final int inputLimit = input.limit();
final int inputRemaining = inputLimit - inputStart;
if (remaining < inputRemaining) {
input = input.limit(inputStart + remaining);
}
final boolean inputPart = input.isPart();
input = input.isPart(remaining > inputRemaining);
if (protocolName == null) {
protocolName = mqtt.decodeString(input);
} else {
protocolName = protocolName.feed(input);
}
input = input.limit(inputLimit).isPart(inputPart);
remaining -= input.index() - inputStart;
if (protocolName.isDone()) {
step = 7;
} else if (protocolName.isError()) {
return protocolName.asError();
}
}
if (step == 7 && remaining > 0 && input.isCont()) {
protocolLevel = input.head();
input = input.step();
remaining -= 1;
step = 8;
}
if (step == 8 && remaining > 0 && input.isCont()) {
connectFlags = input.head();
input = input.step();
remaining -= 1;
step = 9;
}
if (step == 9 && remaining > 0 && input.isCont()) {
keepAlive = input.head() << 8;
input = input.step();
remaining -= 1;
step = 10;
}
if (step == 10 && remaining > 0 && input.isCont()) {
keepAlive |= input.head();
input = input.step();
remaining -= 1;
step = 11;
}
if (step == 11) {
final int inputStart = input.index();
final int inputLimit = input.limit();
final int inputRemaining = inputLimit - inputStart;
if (remaining < inputRemaining) {
input = input.limit(inputStart + remaining);
}
final boolean inputPart = input.isPart();
input = input.isPart(remaining > inputRemaining);
if (clientId == null) {
clientId = mqtt.decodeString(input);
} else {
clientId = clientId.feed(input);
}
input = input.limit(inputLimit).isPart(inputPart);
remaining -= input.index() - inputStart;
if (clientId.isDone()) {
if ((connectFlags & MqttConnect.WILL_FLAG) != 0) {
step = 12;
} else if ((connectFlags & MqttConnect.USERNAME_FLAG) != 0) {
step = 14;
} else if ((connectFlags & MqttConnect.PASSWORD_FLAG) != 0) {
step = 15;
} else {
step = 16;
}
} else if (clientId.isError()) {
return clientId.asError();
}
}
if (step == 12) {
final int inputStart = input.index();
final int inputLimit = input.limit();
final int inputRemaining = inputLimit - inputStart;
if (remaining < inputRemaining) {
input = input.limit(inputStart + remaining);
}
final boolean inputPart = input.isPart();
input = input.isPart(remaining > inputRemaining);
if (willTopic == null) {
willTopic = mqtt.decodeString(input);
} else {
willTopic = willTopic.feed(input);
}
input = input.limit(inputLimit).isPart(inputPart);
remaining -= input.index() - inputStart;
if (willTopic.isDone()) {
step = 13;
} else if (willTopic.isError()) {
return willTopic.asError();
}
}
if (step == 13) {
final int inputStart = input.index();
final int inputLimit = input.limit();
final int inputRemaining = inputLimit - inputStart;
if (remaining < inputRemaining) {
input = input.limit(inputStart + remaining);
}
final boolean inputPart = input.isPart();
input = input.isPart(remaining > inputRemaining);
if (willMessage == null) {
willMessage = mqtt.decodeData(input);
} else {
willMessage = willMessage.feed(input);
}
input = input.limit(inputLimit).isPart(inputPart);
remaining -= input.index() - inputStart;
if (willMessage.isDone()) {
if ((connectFlags & MqttConnect.USERNAME_FLAG) != 0) {
step = 14;
} else if ((connectFlags & MqttConnect.PASSWORD_FLAG) != 0) {
step = 15;
} else {
step = 16;
}
} else if (willMessage.isError()) {
return willMessage.asError();
}
}
if (step == 14) {
final int inputStart = input.index();
final int inputLimit = input.limit();
final int inputRemaining = inputLimit - inputStart;
if (remaining < inputRemaining) {
input = input.limit(inputStart + remaining);
}
final boolean inputPart = input.isPart();
input = input.isPart(remaining > inputRemaining);
if (username == null) {
username = mqtt.decodeString(input);
} else {
username = username.feed(input);
}
input = input.limit(inputLimit).isPart(inputPart);
remaining -= input.index() - inputStart;
if (username.isDone()) {
if ((connectFlags & MqttConnect.PASSWORD_FLAG) != 0) {
step = 15;
} else {
step = 16;
}
} else if (username.isError()) {
return username.asError();
}
}
if (step == 15) {
final int inputStart = input.index();
final int inputLimit = input.limit();
final int inputRemaining = inputLimit - inputStart;
if (remaining < inputRemaining) {
input = input.limit(inputStart + remaining);
}
final boolean inputPart = input.isPart();
input = input.isPart(remaining > inputRemaining);
if (password == null) {
password = mqtt.decodeData(input);
} else {
password = password.feed(input);
}
input = input.limit(inputLimit).isPart(inputPart);
remaining -= input.index() - inputStart;
if (password.isDone()) {
step = 16;
} else if (password.isError()) {
return password.asError();
}
}
if (step == 16 && remaining == 0) {
return done(mqtt.connect(packetFlags, protocolName.bind(),
protocolLevel, connectFlags, keepAlive,
clientId != null ? clientId.bind() : null,
willTopic != null ? willTopic.bind() : null,
willMessage != null ? willMessage.bind() : null,
username != null ? username.bind() : null,
password != null ? password.bind() : null));
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (input.isDone()) {
return error(new DecoderException("incomplete"));
} else if (input.isError()) {
return error(input.trap());
}
return new MqttConnectDecoder(mqtt, packetFlags, protocolName, protocolLevel,
connectFlags, keepAlive, clientId, willTopic,
willMessage, username, password, remaining, step);
}
static Decoder<MqttConnect> decode(InputBuffer input, MqttDecoder mqtt) {
return decode(input, mqtt, 0, null, 0, 0, 0, null, null, null, null, null, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttConnectEncoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Encoder;
import swim.codec.EncoderException;
import swim.codec.OutputBuffer;
final class MqttConnectEncoder extends Encoder<Object, MqttConnect> {
final MqttEncoder mqtt;
final MqttConnect packet;
final Encoder<?, ?> part;
final int length;
final int remaining;
final int step;
MqttConnectEncoder(MqttEncoder mqtt, MqttConnect packet, Encoder<?, ?> part,
int length, int remaining, int step) {
this.mqtt = mqtt;
this.packet = packet;
this.part = part;
this.length = length;
this.remaining = remaining;
this.step = step;
}
MqttConnectEncoder(MqttEncoder mqtt, MqttConnect packet) {
this(mqtt, packet, null, 0, 0, 1);
}
@Override
public Encoder<Object, MqttConnect> pull(OutputBuffer<?> output) {
return encode(output, this.mqtt, this.packet, this.part, this.length,
this.remaining, this.step);
}
static Encoder<Object, MqttConnect> encode(OutputBuffer<?> output, MqttEncoder mqtt,
MqttConnect packet, Encoder<?, ?> part,
int length, int remaining, int step) {
if (step == 1 && output.isCont()) {
length = packet.bodySize(mqtt);
remaining = length;
output = output.write(packet.packetType() << 4 | packet.packetFlags & 0x0f);
step = 2;
}
while (step >= 2 && step <= 5 && output.isCont()) {
int b = length & 0x7f;
length = length >>> 7;
if (length > 0) {
b |= 0x80;
}
output = output.write(b);
if (length == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long: " + remaining));
}
}
if (step == 6) {
final int outputStart = output.index();
final int outputLimit = output.limit();
final int outputRemaining = outputLimit - outputStart;
if (remaining < outputRemaining) {
output = output.limit(outputStart + remaining);
}
final boolean outputPart = output.isPart();
output = output.isPart(remaining > outputRemaining);
if (part == null) {
part = mqtt.encodeString(packet.protocolName, output);
} else {
part = part.pull(output);
}
output = output.limit(outputLimit).isPart(outputPart);
remaining -= output.index() - outputStart;
if (part.isDone()) {
part = null;
step = 7;
} else if (part.isError()) {
return part.asError();
}
}
if (step == 7 && remaining > 0 && output.isCont()) {
output = output.write(packet.protocolLevel);
remaining -= 1;
step = 8;
}
if (step == 8 && remaining > 0 && output.isCont()) {
output = output.write(packet.connectFlags);
remaining -= 1;
step = 9;
}
if (step == 9 && remaining > 0 && output.isCont()) {
output = output.write(packet.keepAlive >>> 8);
remaining -= 1;
step = 10;
}
if (step == 10 && remaining > 0 && output.isCont()) {
output = output.write(packet.keepAlive);
remaining -= 1;
step = 11;
}
if (step == 11) {
final int outputStart = output.index();
final int outputLimit = output.limit();
final int outputRemaining = outputLimit - outputStart;
if (remaining < outputRemaining) {
output = output.limit(outputStart + remaining);
}
final boolean outputPart = output.isPart();
output = output.isPart(remaining > outputRemaining);
if (part == null) {
part = mqtt.encodeString(packet.clientId, output);
} else {
part = part.pull(output);
}
output = output.limit(outputLimit).isPart(outputPart);
remaining -= output.index() - outputStart;
if (part.isDone()) {
part = null;
if ((packet.connectFlags & MqttConnect.WILL_FLAG) != 0) {
step = 12;
} else if ((packet.connectFlags & MqttConnect.USERNAME_FLAG) != 0) {
step = 14;
} else if ((packet.connectFlags & MqttConnect.PASSWORD_FLAG) != 0) {
step = 15;
} else {
step = 16;
}
} else if (part.isError()) {
return part.asError();
}
}
if (step == 12) {
final int outputStart = output.index();
final int outputLimit = output.limit();
final int outputRemaining = outputLimit - outputStart;
if (remaining < outputRemaining) {
output = output.limit(outputStart + remaining);
}
final boolean outputPart = output.isPart();
output = output.isPart(remaining > outputRemaining);
if (part == null) {
part = mqtt.encodeString(packet.willTopic, output);
} else {
part = part.pull(output);
}
output = output.limit(outputLimit).isPart(outputPart);
remaining -= output.index() - outputStart;
if (part.isDone()) {
part = null;
step = 13;
} else if (part.isError()) {
return part.asError();
}
}
if (step == 13) {
final int outputStart = output.index();
final int outputLimit = output.limit();
final int outputRemaining = outputLimit - outputStart;
if (remaining < outputRemaining) {
output = output.limit(outputStart + remaining);
}
final boolean outputPart = output.isPart();
output = output.isPart(remaining > outputRemaining);
if (part == null) {
part = mqtt.encodeData(packet.willMessage, output);
} else {
part = part.pull(output);
}
output = output.limit(outputLimit).isPart(outputPart);
remaining -= output.index() - outputStart;
if (part.isDone()) {
part = null;
if ((packet.connectFlags & MqttConnect.USERNAME_FLAG) != 0) {
step = 14;
} else if ((packet.connectFlags & MqttConnect.PASSWORD_FLAG) != 0) {
step = 15;
} else {
step = 16;
}
} else if (part.isError()) {
return part.asError();
}
}
if (step == 14) {
final int outputStart = output.index();
final int outputLimit = output.limit();
final int outputRemaining = outputLimit - outputStart;
if (remaining < outputRemaining) {
output = output.limit(outputStart + remaining);
}
final boolean outputPart = output.isPart();
output = output.isPart(remaining > outputRemaining);
if (part == null) {
part = mqtt.encodeString(packet.username, output);
} else {
part = part.pull(output);
}
output = output.limit(outputLimit).isPart(outputPart);
remaining -= output.index() - outputStart;
if (part.isDone()) {
part = null;
if ((packet.connectFlags & MqttConnect.PASSWORD_FLAG) != 0) {
step = 15;
} else {
step = 16;
}
} else if (part.isError()) {
return part.asError();
}
}
if (step == 15) {
final int outputStart = output.index();
final int outputLimit = output.limit();
final int outputRemaining = outputLimit - outputStart;
if (remaining < outputRemaining) {
output = output.limit(outputStart + remaining);
}
final boolean outputPart = output.isPart();
output = output.isPart(remaining > outputRemaining);
if (part == null) {
part = mqtt.encodeData(packet.password, output);
} else {
part = part.pull(output);
}
output = output.limit(outputLimit).isPart(outputPart);
remaining -= output.index() - outputStart;
if (part.isDone()) {
part = null;
step = 16;
} else if (part.isError()) {
return part.asError();
}
}
if (step == 16 && remaining == 0) {
return done(packet);
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (output.isDone()) {
return error(new EncoderException("truncated"));
} else if (output.isError()) {
return error(output.trap());
}
return new MqttConnectEncoder(mqtt, packet, part, length, remaining, step);
}
static Encoder<Object, MqttConnect> encode(OutputBuffer<?> output, MqttEncoder mqtt,
MqttConnect packet) {
return encode(output, mqtt, packet, null, 0, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttDataDecoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Binary;
import swim.codec.Decoder;
import swim.codec.DecoderException;
import swim.codec.InputBuffer;
import swim.structure.Data;
final class MqttDataDecoder extends Decoder<Data> {
final Decoder<byte[]> decoder;
final int remaining;
final int step;
MqttDataDecoder(Decoder<byte[]> decoder, int remaining, int step) {
this.decoder = decoder;
this.remaining = remaining;
this.step = step;
}
MqttDataDecoder() {
this(null, 0, 1);
}
@Override
public Decoder<Data> feed(InputBuffer input) {
return decode(input, this.decoder, this.remaining, this.step);
}
static Decoder<Data> decode(InputBuffer input, Decoder<byte[]> decoder, int remaining, int step) {
if (step == 1 && input.isCont()) {
remaining = input.head() << 8;
input = input.step();
step = 2;
}
if (step == 2 && input.isCont()) {
remaining |= input.head();
input = input.step();
step = 3;
}
if (step == 3) {
final int inputStart = input.index();
final int inputLimit = input.limit();
final int inputRemaining = inputLimit - inputStart;
if (remaining < inputRemaining) {
input = input.limit(inputStart + remaining);
}
final boolean inputPart = input.isPart();
input = input.isPart(remaining > inputRemaining);
if (decoder == null) {
decoder = Binary.parseOutput(Binary.byteArrayOutput(remaining), input);
} else {
decoder = decoder.feed(input);
}
input = input.limit(inputLimit).isPart(inputPart);
remaining -= input.index() - inputStart;
if (decoder.isDone()) {
return done(Data.wrap(decoder.bind()));
} else if (decoder.isError()) {
return decoder.asError();
}
}
if (input.isDone()) {
return error(new DecoderException("incomplete"));
} else if (input.isError()) {
return error(input.trap());
}
return new MqttDataDecoder(decoder, remaining, step);
}
static Decoder<Data> decode(InputBuffer input) {
return decode(input, null, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttDataEncoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Encoder;
import swim.codec.EncoderException;
import swim.codec.OutputBuffer;
import swim.structure.Data;
final class MqttDataEncoder extends Encoder<Data, Data> {
final Data data;
final Encoder<?, ?> encoder;
final int step;
MqttDataEncoder(Data data, Encoder<?, ?> encoder, int step) {
this.data = data;
this.encoder = encoder;
this.step = step;
}
MqttDataEncoder(Data data) {
this(data, null, 1);
}
@Override
public Encoder<Data, Data> feed(Data data) {
return new MqttDataEncoder(data, null, 1);
}
@Override
public Encoder<Data, Data> pull(OutputBuffer<?> output) {
return encode(output, this.data, this.encoder, this.step);
}
static int sizeOf(Data data) {
return 2 + data.size();
}
static Encoder<Data, Data> encode(OutputBuffer<?> output, Data data,
Encoder<?, ?> encoder, int step) {
if (step == 1 && output.isCont()) {
if (data.size() > 65535) {
return error(new MqttException("data too long (" + data.size() + " bytes)"));
}
output = output.write(data.size() >>> 8);
step = 2;
}
if (step == 2 && output.isCont()) {
output = output.write(data.size());
step = 3;
}
if (step == 3) {
if (encoder == null) {
encoder = data.write(output);
} else {
encoder = encoder.pull(output);
}
if (encoder.isDone()) {
return done(data);
} else if (encoder.isError()) {
return encoder.asError();
}
}
if (output.isDone()) {
return error(new EncoderException("truncated"));
} else if (output.isError()) {
return error(output.trap());
}
return new MqttDataEncoder(data, encoder, step);
}
static Encoder<Data, Data> encode(OutputBuffer<?> output, Data data) {
return encode(output, data, null, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttDecoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Decoder;
import swim.codec.InputBuffer;
import swim.collections.FingerTrieSeq;
import swim.structure.Data;
public class MqttDecoder {
public MqttConnect connect(int packetFlags, String protocolName, int protocolLevel,
int connectFlags, int keepAlive, String clientId, String willTopic,
Data willMessage, String username, Data password) {
return MqttConnect.from(packetFlags, protocolName, protocolLevel,
connectFlags, keepAlive, clientId, willTopic,
willMessage, username, password);
}
public MqttConnAck connAck(int packetFlags, int connectFlags, int connectCode) {
return MqttConnAck.from(packetFlags, connectFlags, connectCode);
}
public <T> MqttPublish<T> publish(int packetFlags, String topicName,
int packetId, MqttEntity<T> payload) {
return MqttPublish.from(packetFlags, topicName, packetId, payload);
}
public MqttPubAck pubAck(int packetFlags, int packetId) {
return MqttPubAck.from(packetFlags, packetId);
}
public MqttPubRec pubRec(int packetFlags, int packetId) {
return MqttPubRec.from(packetFlags, packetId);
}
public MqttPubRel pubRel(int packetFlags, int packetId) {
return MqttPubRel.from(packetFlags, packetId);
}
public MqttPubComp pubComp(int packetFlags, int packetId) {
return MqttPubComp.from(packetFlags, packetId);
}
public MqttSubscribe subscribe(int packetFlags, int packetId,
FingerTrieSeq<MqttSubscription> subscriptions) {
return MqttSubscribe.from(packetFlags, packetId, subscriptions);
}
public MqttSubAck subAck(int packetFlags, int packetId,
FingerTrieSeq<MqttSubStatus> subscriptions) {
return MqttSubAck.from(packetFlags, packetId, subscriptions);
}
public MqttUnsubscribe unsubscribe(int packetFlags, int packetId,
FingerTrieSeq<String> topicNames) {
return MqttUnsubscribe.from(packetFlags, packetId, topicNames);
}
public MqttUnsubAck unsubAck(int packetFlags, int packetId) {
return MqttUnsubAck.from(packetFlags, packetId);
}
public MqttPingReq pingReq(int packetFlags) {
return MqttPingReq.from(packetFlags);
}
public MqttPingResp pingResp(int packetFlags) {
return MqttPingResp.from(packetFlags);
}
public MqttDisconnect disconnect(int packetFlags) {
return MqttDisconnect.from(packetFlags);
}
public MqttSubscription subscription(String topicName, int flags) {
return MqttSubscription.from(topicName, flags);
}
public MqttSubStatus subStatus(int code) {
return MqttSubStatus.from(code);
}
public <T> Decoder<MqttPacket<T>> packetDecoder(Decoder<T> content) {
return new MqttPacketDecoder<T>(this, content);
}
public <T> Decoder<MqttPacket<T>> decodePacket(Decoder<T> content, InputBuffer input) {
return MqttPacketDecoder.decode(input, this, content);
}
@SuppressWarnings("unchecked")
public <T> Decoder<MqttPacket<T>> decodePacketType(int packetType, Decoder<T> content,
InputBuffer input) {
final Decoder<?> decoder;
switch (packetType) {
case 1: decoder = decodeConnect(input); break;
case 2: decoder = decodeConnAck(input); break;
case 3: decoder = decodePublish(content, input); break;
case 4: decoder = decodePubAck(input); break;
case 5: decoder = decodePubRec(input); break;
case 6: decoder = decodePubRel(input); break;
case 7: decoder = decodePubComp(input); break;
case 8: decoder = decodeSubscribe(input); break;
case 9: decoder = decodeSubAck(input); break;
case 10: decoder = decodeUnsubscribe(input); break;
case 11: decoder = decodeUnsubAck(input); break;
case 12: decoder = decodePingReq(input); break;
case 13: decoder = decodePingResp(input); break;
case 14: decoder = decodeDisconnect(input); break;
default: return Decoder.error(new MqttException("reserved packet type: " + packetType));
}
return (Decoder<MqttPacket<T>>) decoder;
}
public Decoder<MqttConnect> decodeConnect(InputBuffer input) {
return MqttConnectDecoder.decode(input, this);
}
public Decoder<MqttConnAck> decodeConnAck(InputBuffer input) {
return MqttConnAckDecoder.decode(input, this);
}
public <T> Decoder<MqttPublish<T>> decodePublish(Decoder<T> content, InputBuffer input) {
return MqttPublishDecoder.decode(input, this, content);
}
public Decoder<MqttPubAck> decodePubAck(InputBuffer input) {
return MqttPubAckDecoder.decode(input, this);
}
public Decoder<MqttPubRec> decodePubRec(InputBuffer input) {
return MqttPubRecDecoder.decode(input, this);
}
public Decoder<MqttPubRel> decodePubRel(InputBuffer input) {
return MqttPubRelDecoder.decode(input, this);
}
public Decoder<MqttPubComp> decodePubComp(InputBuffer input) {
return MqttPubCompDecoder.decode(input, this);
}
public Decoder<MqttSubscribe> decodeSubscribe(InputBuffer input) {
return MqttSubscribeDecoder.decode(input, this);
}
public Decoder<MqttSubAck> decodeSubAck(InputBuffer input) {
return MqttSubAckDecoder.decode(input, this);
}
public Decoder<MqttUnsubscribe> decodeUnsubscribe(InputBuffer input) {
return MqttUnsubscribeDecoder.decode(input, this);
}
public Decoder<MqttUnsubAck> decodeUnsubAck(InputBuffer input) {
return MqttUnsubAckDecoder.decode(input, this);
}
public Decoder<MqttPingReq> decodePingReq(InputBuffer input) {
return MqttPingReqDecoder.decode(input, this);
}
public Decoder<MqttPingResp> decodePingResp(InputBuffer input) {
return MqttPingRespDecoder.decode(input, this);
}
public Decoder<MqttDisconnect> decodeDisconnect(InputBuffer input) {
return MqttDisconnectDecoder.decode(input, this);
}
public Decoder<MqttSubscription> subscriptionDecoder() {
return new MqttSubscriptionDecoder(this);
}
public Decoder<MqttSubscription> decodeSubscription(InputBuffer input) {
return MqttSubscriptionDecoder.decode(input, this);
}
public Decoder<String> stringDecoder() {
return new MqttStringDecoder();
}
public Decoder<String> decodeString(InputBuffer input) {
return MqttStringDecoder.decode(input);
}
public Decoder<Data> dataDecoder() {
return new MqttDataDecoder();
}
public Decoder<Data> decodeData(InputBuffer input) {
return MqttDataDecoder.decode(input);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttDisconnect.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Debug;
import swim.codec.Encoder;
import swim.codec.Format;
import swim.codec.Output;
import swim.codec.OutputBuffer;
import swim.util.Murmur3;
public final class MqttDisconnect extends MqttPacket<Object> implements Debug {
final int packetFlags;
MqttDisconnect(int packetFlags) {
this.packetFlags = packetFlags;
}
@Override
public int packetType() {
return 14;
}
@Override
public int packetFlags() {
return this.packetFlags;
}
public MqttDisconnect packetFlags(int packetFlags) {
return new MqttDisconnect(packetFlags);
}
@Override
int bodySize(MqttEncoder mqtt) {
return 0;
}
@Override
public Encoder<?, MqttDisconnect> mqttEncoder(MqttEncoder mqtt) {
return mqtt.disconnectEncoder(this);
}
@Override
public Encoder<?, MqttDisconnect> encodeMqtt(OutputBuffer<?> output, MqttEncoder mqtt) {
return mqtt.encodeDisconnect(this, output);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof MqttDisconnect) {
final MqttDisconnect that = (MqttDisconnect) other;
return this.packetFlags == that.packetFlags;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(MqttDisconnect.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.packetFlags));
}
@Override
public void debug(Output<?> output) {
output = output.write("MqttDisconnect").write('.').write("packet").write('(').write(')');
if (this.packetFlags != 0) {
output = output.write('.').write("packetFlags").write('(').debug(this.packetFlags).write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static MqttDisconnect packet;
public static MqttDisconnect packet() {
if (packet == null) {
packet = new MqttDisconnect(0);
}
return packet;
}
public static MqttDisconnect from(int packetFlags) {
if (packetFlags == 0) {
return packet();
} else {
return new MqttDisconnect(packetFlags);
}
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttDisconnectDecoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Decoder;
import swim.codec.DecoderException;
import swim.codec.InputBuffer;
final class MqttDisconnectDecoder extends Decoder<MqttDisconnect> {
final MqttDecoder mqtt;
final int packetFlags;
final int remaining;
final int step;
MqttDisconnectDecoder(MqttDecoder mqtt, int packetFlags, int remaining, int step) {
this.mqtt = mqtt;
this.packetFlags = packetFlags;
this.remaining = remaining;
this.step = step;
}
MqttDisconnectDecoder(MqttDecoder mqtt) {
this(mqtt, 0, 0, 1);
}
@Override
public Decoder<MqttDisconnect> feed(InputBuffer input) {
return decode(input, this.mqtt, this.packetFlags, this.remaining, this.step);
}
static Decoder<MqttDisconnect> decode(InputBuffer input, MqttDecoder mqtt,
int packetFlags, int remaining, int step) {
if (step == 1 && input.isCont()) {
packetFlags = input.head() & 0x0f;
input = input.step();
step = 2;
}
while (step >= 2 && step <= 5 && input.isCont()) {
final int b = input.head();
input = input.step();
remaining |= (b & 0x7f) << (7 * (step - 2));
if ((b & 0x80) == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long"));
}
}
if (step == 6 && remaining == 0) {
return done(mqtt.disconnect(packetFlags));
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (input.isDone()) {
return error(new DecoderException("incomplete"));
} else if (input.isError()) {
return error(input.trap());
}
return new MqttDisconnectDecoder(mqtt, packetFlags, remaining, step);
}
static Decoder<MqttDisconnect> decode(InputBuffer input, MqttDecoder mqtt) {
return decode(input, mqtt, 0, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttDisconnectEncoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Encoder;
import swim.codec.EncoderException;
import swim.codec.OutputBuffer;
final class MqttDisconnectEncoder extends Encoder<Object, MqttDisconnect> {
final MqttEncoder mqtt;
final MqttDisconnect packet;
final int length;
final int remaining;
final int step;
MqttDisconnectEncoder(MqttEncoder mqtt, MqttDisconnect packet, int length, int remaining, int step) {
this.mqtt = mqtt;
this.packet = packet;
this.length = length;
this.remaining = remaining;
this.step = step;
}
MqttDisconnectEncoder(MqttEncoder mqtt, MqttDisconnect packet) {
this(mqtt, packet, 0, 0, 1);
}
@Override
public Encoder<Object, MqttDisconnect> pull(OutputBuffer<?> output) {
return encode(output, this.mqtt, this.packet, this.length, this.remaining, this.step);
}
static Encoder<Object, MqttDisconnect> encode(OutputBuffer<?> output, MqttEncoder mqtt,
MqttDisconnect packet, int length,
int remaining, int step) {
if (step == 1 && output.isCont()) {
length = packet.bodySize(mqtt);
remaining = length;
output = output.write(packet.packetType() << 4 | packet.packetFlags & 0x0f);
step = 2;
}
while (step >= 2 && step <= 5 && output.isCont()) {
int b = length & 0x7f;
length = length >>> 7;
if (length > 0) {
b |= 0x80;
}
output = output.write(b);
if (length == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long: " + remaining));
}
}
if (step == 6 && remaining == 0) {
return done(packet);
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (output.isDone()) {
return error(new EncoderException("truncated"));
} else if (output.isError()) {
return error(output.trap());
}
return new MqttDisconnectEncoder(mqtt, packet, length, remaining, step);
}
static Encoder<Object, MqttDisconnect> encode(OutputBuffer<?> output, MqttEncoder mqtt,
MqttDisconnect packet) {
return encode(output, mqtt, packet, 0, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttEmpty.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Debug;
import swim.codec.Encoder;
import swim.codec.Format;
import swim.codec.Output;
import swim.codec.OutputBuffer;
final class MqttEmpty extends MqttEntity<Object> implements Debug {
@Override
public boolean isDefined() {
return false;
}
@Override
public Object get() {
return null;
}
@Override
public int mqttSize() {
return 0;
}
@Override
public Encoder<?, ?> mqttEncoder(MqttEncoder mqtt) {
return Encoder.done();
}
@Override
public Encoder<?, ?> encodeMqtt(OutputBuffer<?> output, MqttEncoder mqtt) {
return Encoder.done();
}
@Override
public void debug(Output<?> output) {
output = output.write("MqttEntity").write('.').write("empty").write('(').write(')');
}
@Override
public String toString() {
return Format.debug(this);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttEncoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Encoder;
import swim.codec.OutputBuffer;
import swim.structure.Data;
public class MqttEncoder {
public Encoder<?, MqttConnect> connectEncoder(MqttConnect packet) {
return new MqttConnectEncoder(this, packet);
}
public Encoder<?, MqttConnect> encodeConnect(MqttConnect packet, OutputBuffer<?> output) {
return MqttConnectEncoder.encode(output, this, packet);
}
public Encoder<?, MqttConnAck> connAckEncoder(MqttConnAck packet) {
return new MqttConnAckEncoder(this, packet);
}
public Encoder<?, MqttConnAck> encodeConnAck(MqttConnAck packet, OutputBuffer<?> output) {
return MqttConnAckEncoder.encode(output, this, packet);
}
public <T> Encoder<?, MqttPublish<T>> publishEncoder(MqttPublish<T> packet) {
return new MqttPublishEncoder<T>(this, packet);
}
public <T> Encoder<?, MqttPublish<T>> encodePublish(MqttPublish<T> packet, OutputBuffer<?> output) {
return MqttPublishEncoder.encode(output, this, packet);
}
public Encoder<?, MqttPubAck> pubAckEncoder(MqttPubAck packet) {
return new MqttPubAckEncoder(this, packet);
}
public Encoder<?, MqttPubAck> encodePubAck(MqttPubAck packet, OutputBuffer<?> output) {
return MqttPubAckEncoder.encode(output, this, packet);
}
public Encoder<?, MqttPubRec> pubRecEncoder(MqttPubRec packet) {
return new MqttPubRecEncoder(this, packet);
}
public Encoder<?, MqttPubRec> encodePubRec(MqttPubRec packet, OutputBuffer<?> output) {
return MqttPubRecEncoder.encode(output, this, packet);
}
public Encoder<?, MqttPubRel> pubRelEncoder(MqttPubRel packet) {
return new MqttPubRelEncoder(this, packet);
}
public Encoder<?, MqttPubRel> encodePubRel(MqttPubRel packet, OutputBuffer<?> output) {
return MqttPubRelEncoder.encode(output, this, packet);
}
public Encoder<?, MqttPubComp> pubCompEncoder(MqttPubComp packet) {
return new MqttPubCompEncoder(this, packet);
}
public Encoder<?, MqttPubComp> encodePubComp(MqttPubComp packet, OutputBuffer<?> output) {
return MqttPubCompEncoder.encode(output, this, packet);
}
public Encoder<?, MqttSubscribe> subscribeEncoder(MqttSubscribe packet) {
return new MqttSubscribeEncoder(this, packet);
}
public Encoder<?, MqttSubscribe> encodeSubscribe(MqttSubscribe packet, OutputBuffer<?> output) {
return MqttSubscribeEncoder.encode(output, this, packet);
}
public Encoder<?, MqttSubAck> subAckEncoder(MqttSubAck packet) {
return new MqttSubAckEncoder(this, packet);
}
public Encoder<?, MqttSubAck> encodeSubAck(MqttSubAck packet, OutputBuffer<?> output) {
return MqttSubAckEncoder.encode(output, this, packet);
}
public Encoder<?, MqttUnsubscribe> unsubscribeEncoder(MqttUnsubscribe packet) {
return new MqttUnsubscribeEncoder(this, packet);
}
public Encoder<?, MqttUnsubscribe> encodeUnsubscribe(MqttUnsubscribe packet, OutputBuffer<?> output) {
return MqttUnsubscribeEncoder.encode(output, this, packet);
}
public Encoder<?, MqttUnsubAck> unsubAckEncoder(MqttUnsubAck packet) {
return new MqttUnsubAckEncoder(this, packet);
}
public Encoder<?, MqttUnsubAck> encodeUnsubAck(MqttUnsubAck packet, OutputBuffer<?> output) {
return MqttUnsubAckEncoder.encode(output, this, packet);
}
public Encoder<?, MqttPingReq> pingReqEncoder(MqttPingReq packet) {
return new MqttPingReqEncoder(this, packet);
}
public Encoder<?, MqttPingReq> encodePingReq(MqttPingReq packet, OutputBuffer<?> output) {
return MqttPingReqEncoder.encode(output, this, packet);
}
public Encoder<?, MqttPingResp> pingRespEncoder(MqttPingResp packet) {
return new MqttPingRespEncoder(this, packet);
}
public Encoder<?, MqttPingResp> encodePingResp(MqttPingResp packet, OutputBuffer<?> output) {
return MqttPingRespEncoder.encode(output, this, packet);
}
public Encoder<?, MqttDisconnect> disconnectEncoder(MqttDisconnect packet) {
return new MqttDisconnectEncoder(this, packet);
}
public Encoder<?, MqttDisconnect> encodeDisconnect(MqttDisconnect packet, OutputBuffer<?> output) {
return MqttDisconnectEncoder.encode(output, this, packet);
}
public int sizeOfSubscription(MqttSubscription subscription) {
return MqttSubscriptionEncoder.sizeOf(this, subscription);
}
public Encoder<MqttSubscription, MqttSubscription> subscriptionEncoder(MqttSubscription subscription) {
return new MqttSubscriptionEncoder(this, subscription);
}
public Encoder<MqttSubscription, MqttSubscription> encodeSubscription(MqttSubscription subscription,
OutputBuffer<?> output) {
return MqttSubscriptionEncoder.encode(output, this, subscription);
}
public int sizeOfString(String string) {
return MqttStringEncoder.sizeOf(string);
}
public Encoder<String, String> stringEncoder(String string) {
return new MqttStringEncoder(string);
}
public Encoder<String, String> encodeString(String string, OutputBuffer<?> output) {
return MqttStringEncoder.encode(output, string);
}
public int sizeOfData(Data data) {
return MqttDataEncoder.sizeOf(data);
}
public Encoder<Data, Data> dataEncoder(Data data) {
return new MqttDataEncoder(data);
}
public Encoder<Data, Data> encodeData(Data data, OutputBuffer<?> output) {
return MqttDataEncoder.encode(output, data);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttEntity.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
public abstract class MqttEntity<T> extends MqttPart {
public abstract boolean isDefined();
public abstract T get();
public abstract int mqttSize();
private static MqttEntity<Object> empty;
@SuppressWarnings("unchecked")
public static <T> MqttEntity<T> empty() {
if (empty == null) {
empty = new MqttEmpty();
}
return (MqttEntity<T>) empty;
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttException.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
public class MqttException extends RuntimeException {
private static final long serialVersionUID = 1L;
public MqttException(String message, Throwable cause) {
super(message, cause);
}
public MqttException(String message) {
super(message);
}
public MqttException(Throwable cause) {
super(cause);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPacket.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
public abstract class MqttPacket<T> extends MqttPart {
public abstract int packetType();
public abstract int packetFlags();
abstract int bodySize(MqttEncoder mqtt);
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPacketDecoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Decoder;
import swim.codec.InputBuffer;
final class MqttPacketDecoder<T> extends Decoder<MqttPacket<T>> {
final MqttDecoder mqtt;
final Decoder<T> content;
MqttPacketDecoder(MqttDecoder mqtt, Decoder<T> content) {
this.mqtt = mqtt;
this.content = content;
}
@Override
public Decoder<MqttPacket<T>> feed(InputBuffer input) {
return decode(input, this.mqtt, this.content);
}
static <T> Decoder<MqttPacket<T>> decode(InputBuffer input, MqttDecoder mqtt, Decoder<T> content) {
if (input.isCont()) {
final int packetType = (input.head() & 0xf0) >>> 4;
return mqtt.decodePacketType(packetType, content, input);
}
return new MqttPacketDecoder<T>(mqtt, content);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPart.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Encoder;
import swim.codec.OutputBuffer;
public abstract class MqttPart {
public abstract Encoder<?, ?> mqttEncoder(MqttEncoder mqtt);
public Encoder<?, ?> mqttEncoder() {
return mqttEncoder(Mqtt.standardEncoder());
}
public abstract Encoder<?, ?> encodeMqtt(OutputBuffer<?> output, MqttEncoder mqtt);
public Encoder<?, ?> encodeMqtt(OutputBuffer<?> output) {
return encodeMqtt(output, Mqtt.standardEncoder());
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPayload.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import java.nio.ByteBuffer;
import swim.codec.Binary;
import swim.codec.Debug;
import swim.codec.Encoder;
import swim.codec.Format;
import swim.codec.Output;
import swim.codec.OutputBuffer;
import swim.codec.Utf8;
import swim.util.Murmur3;
public final class MqttPayload<T> extends MqttEntity<T> implements Debug {
final T value;
final Encoder<?, ?> content;
final int length;
MqttPayload(T value, Encoder<?, ?> content, int length) {
this.value = value;
this.content = content;
this.length = length;
}
public boolean isDefined() {
return this.value != null;
}
public T get() {
return this.value;
}
public int mqttSize() {
return this.length;
}
@Override
public Encoder<?, ?> mqttEncoder(MqttEncoder mqtt) {
return this.content;
}
@Override
public Encoder<?, ?> encodeMqtt(OutputBuffer<?> output, MqttEncoder mqtt) {
return this.content.pull(output);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof MqttPayload) {
final MqttPayload<?> that = (MqttPayload<?>) other;
return (this.value == null ? that.value == null : this.value.equals(that.value))
&& this.content.equals(that.content) && this.length == that.length;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(MqttPayload.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed,
Murmur3.hash(this.value)), this.content.hashCode()), this.length));
}
@Override
public void debug(Output<?> output) {
output = output.write("MqttPayload").write('.');
if (this.value == null && this.content.isDone() && this.length == 0) {
output = output.write("empty").write('(').write(')');
} else {
output = output.write("from").write('(');
if (this.value != null) {
output.debug(this.value).write(", ");
}
output.debug(this.content).write(", ").debug(this.length).write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static MqttPayload<Object> empty;
@SuppressWarnings("unchecked")
public static <T> MqttPayload<T> empty() {
if (empty == null) {
empty = new MqttPayload<Object>(null, Encoder.done(), 0);
}
return (MqttPayload<T>) empty;
}
public static <T> MqttPayload<T> from(T value, Encoder<?, ?> content, int length) {
return new MqttPayload<T>(value, content, length);
}
public static <T> MqttPayload<T> from(Encoder<?, ?> content, int length) {
return new MqttPayload<T>(null, content, length);
}
public static <T> MqttPayload<T> from(ByteBuffer data) {
return new MqttPayload<T>(null, Binary.byteBufferWriter(data), data.remaining());
}
public static MqttPayload<String> from(String content) {
Output<ByteBuffer> output = Utf8.encodedOutput(Binary.byteBufferOutput(content.length()));
output = output.write(content);
final ByteBuffer data = output.bind();
return new MqttPayload<String>(content, Binary.byteBufferWriter(data), data.remaining());
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPingReq.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Debug;
import swim.codec.Encoder;
import swim.codec.Format;
import swim.codec.Output;
import swim.codec.OutputBuffer;
import swim.util.Murmur3;
public final class MqttPingReq extends MqttPacket<Object> implements Debug {
final int packetFlags;
MqttPingReq(int packetFlags) {
this.packetFlags = packetFlags;
}
@Override
public int packetType() {
return 12;
}
@Override
public int packetFlags() {
return this.packetFlags;
}
public MqttPingReq packetFlags(int packetFlags) {
return new MqttPingReq(packetFlags);
}
@Override
int bodySize(MqttEncoder mqtt) {
return 0;
}
@Override
public Encoder<?, MqttPingReq> mqttEncoder(MqttEncoder mqtt) {
return mqtt.pingReqEncoder(this);
}
@Override
public Encoder<?, MqttPingReq> encodeMqtt(OutputBuffer<?> output, MqttEncoder mqtt) {
return mqtt.encodePingReq(this, output);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof MqttPingReq) {
final MqttPingReq that = (MqttPingReq) other;
return this.packetFlags == that.packetFlags;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(MqttPingReq.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.packetFlags));
}
@Override
public void debug(Output<?> output) {
output = output.write("MqttPingReq").write('.').write("packet").write('(').write(')');
if (this.packetFlags != 0) {
output = output.write('.').write("packetFlags").write('(').debug(this.packetFlags).write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static MqttPingReq packet;
public static MqttPingReq packet() {
if (packet == null) {
packet = new MqttPingReq(0);
}
return packet;
}
public static MqttPingReq from(int packetFlags) {
if (packetFlags == 0) {
return packet();
} else {
return new MqttPingReq(packetFlags);
}
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPingReqDecoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Decoder;
import swim.codec.DecoderException;
import swim.codec.InputBuffer;
final class MqttPingReqDecoder extends Decoder<MqttPingReq> {
final MqttDecoder mqtt;
final int packetFlags;
final int remaining;
final int step;
MqttPingReqDecoder(MqttDecoder mqtt, int packetFlags, int remaining, int step) {
this.mqtt = mqtt;
this.packetFlags = packetFlags;
this.remaining = remaining;
this.step = step;
}
MqttPingReqDecoder(MqttDecoder mqtt) {
this(mqtt, 0, 0, 1);
}
@Override
public Decoder<MqttPingReq> feed(InputBuffer input) {
return decode(input, this.mqtt, this.packetFlags, this.remaining, this.step);
}
static Decoder<MqttPingReq> decode(InputBuffer input, MqttDecoder mqtt,
int packetFlags, int remaining, int step) {
if (step == 1 && input.isCont()) {
packetFlags = input.head() & 0x0f;
input = input.step();
step = 2;
}
while (step >= 2 && step <= 5 && input.isCont()) {
final int b = input.head();
input = input.step();
remaining |= (b & 0x7f) << (7 * (step - 2));
if ((b & 0x80) == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long"));
}
}
if (step == 6 && remaining == 0) {
return done(mqtt.pingReq(packetFlags));
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (input.isDone()) {
return error(new DecoderException("incomplete"));
} else if (input.isError()) {
return error(input.trap());
}
return new MqttPingReqDecoder(mqtt, packetFlags, remaining, step);
}
static Decoder<MqttPingReq> decode(InputBuffer input, MqttDecoder mqtt) {
return decode(input, mqtt, 0, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPingReqEncoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Encoder;
import swim.codec.EncoderException;
import swim.codec.OutputBuffer;
final class MqttPingReqEncoder extends Encoder<Object, MqttPingReq> {
final MqttEncoder mqtt;
final MqttPingReq packet;
final int length;
final int remaining;
final int step;
MqttPingReqEncoder(MqttEncoder mqtt, MqttPingReq packet, int length, int remaining, int step) {
this.mqtt = mqtt;
this.packet = packet;
this.length = length;
this.remaining = remaining;
this.step = step;
}
MqttPingReqEncoder(MqttEncoder mqtt, MqttPingReq packet) {
this(mqtt, packet, 0, 0, 1);
}
@Override
public Encoder<Object, MqttPingReq> pull(OutputBuffer<?> output) {
return encode(output, this.mqtt, this.packet, this.length, this.remaining, this.step);
}
static Encoder<Object, MqttPingReq> encode(OutputBuffer<?> output, MqttEncoder mqtt,
MqttPingReq packet, int length, int remaining, int step) {
if (step == 1 && output.isCont()) {
length = packet.bodySize(mqtt);
remaining = length;
output = output.write(packet.packetType() << 4 | packet.packetFlags & 0x0f);
step = 2;
}
while (step >= 2 && step <= 5 && output.isCont()) {
int b = length & 0x7f;
length = length >>> 7;
if (length > 0) {
b |= 0x80;
}
output = output.write(b);
if (length == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long: " + remaining));
}
}
if (step == 6 && remaining == 0) {
return done(packet);
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (output.isDone()) {
return error(new EncoderException("truncated"));
} else if (output.isError()) {
return error(output.trap());
}
return new MqttPingReqEncoder(mqtt, packet, length, remaining, step);
}
static Encoder<Object, MqttPingReq> encode(OutputBuffer<?> output, MqttEncoder mqtt,
MqttPingReq packet) {
return encode(output, mqtt, packet, 0, 0, 1);
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPingResp.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Debug;
import swim.codec.Encoder;
import swim.codec.Format;
import swim.codec.Output;
import swim.codec.OutputBuffer;
import swim.util.Murmur3;
public final class MqttPingResp extends MqttPacket<Object> implements Debug {
final int packetFlags;
MqttPingResp(int packetFlags) {
this.packetFlags = packetFlags;
}
@Override
public int packetType() {
return 13;
}
@Override
public int packetFlags() {
return this.packetFlags;
}
public MqttPingResp packetFlags(int packetFlags) {
return new MqttPingResp(packetFlags);
}
@Override
int bodySize(MqttEncoder mqtt) {
return 0;
}
@Override
public Encoder<?, MqttPingResp> mqttEncoder(MqttEncoder mqtt) {
return mqtt.pingRespEncoder(this);
}
@Override
public Encoder<?, MqttPingResp> encodeMqtt(OutputBuffer<?> output, MqttEncoder mqtt) {
return mqtt.encodePingResp(this, output);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof MqttPingResp) {
final MqttPingResp that = (MqttPingResp) other;
return this.packetFlags == that.packetFlags;
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(MqttPingResp.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.packetFlags));
}
@Override
public void debug(Output<?> output) {
output = output.write("MqttPingResp").write('.').write("packet").write('(').write(')');
if (this.packetFlags != 0) {
output = output.write('.').write("packetFlags").write('(').debug(this.packetFlags).write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
private static MqttPingResp packet;
public static MqttPingResp packet() {
if (packet == null) {
packet = new MqttPingResp(0);
}
return packet;
}
public static MqttPingResp from(int packetFlags) {
if (packetFlags == 0) {
return packet();
} else {
return new MqttPingResp(packetFlags);
}
}
}
|
0 | java-sources/ai/swim/swim-mqtt/3.10.0/swim | java-sources/ai/swim/swim-mqtt/3.10.0/swim/mqtt/MqttPingRespDecoder.java | // Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.mqtt;
import swim.codec.Decoder;
import swim.codec.DecoderException;
import swim.codec.InputBuffer;
final class MqttPingRespDecoder extends Decoder<MqttPingResp> {
final MqttDecoder mqtt;
final int packetFlags;
final int remaining;
final int step;
MqttPingRespDecoder(MqttDecoder mqtt, int packetFlags, int remaining, int step) {
this.mqtt = mqtt;
this.packetFlags = packetFlags;
this.remaining = remaining;
this.step = step;
}
MqttPingRespDecoder(MqttDecoder mqtt) {
this(mqtt, 0, 0, 1);
}
@Override
public Decoder<MqttPingResp> feed(InputBuffer input) {
return decode(input, this.mqtt, this.packetFlags, this.remaining, this.step);
}
static Decoder<MqttPingResp> decode(InputBuffer input, MqttDecoder mqtt,
int packetFlags, int remaining, int step) {
if (step == 1 && input.isCont()) {
packetFlags = input.head() & 0x0f;
input = input.step();
step = 2;
}
while (step >= 2 && step <= 5 && input.isCont()) {
final int b = input.head();
input = input.step();
remaining |= (b & 0x7f) << (7 * (step - 2));
if ((b & 0x80) == 0) {
step = 6;
break;
} else if (step < 5) {
step += 1;
} else {
return error(new MqttException("packet length too long"));
}
}
if (step == 6 && remaining == 0) {
return done(mqtt.pingResp(packetFlags));
}
if (remaining < 0) {
return error(new MqttException("packet length too short"));
} else if (input.isDone()) {
return error(new DecoderException("incomplete"));
} else if (input.isError()) {
return error(input.trap());
}
return new MqttPingRespDecoder(mqtt, packetFlags, remaining, step);
}
static Decoder<MqttPingResp> decode(InputBuffer input, MqttDecoder mqtt) {
return decode(input, mqtt, 0, 0, 1);
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.