diff --git a/src/com/bdjb/api/Buffer.java b/src/com/bdjb/api/Buffer.java index 6cfb428..f858dd5 100644 --- a/src/com/bdjb/api/Buffer.java +++ b/src/com/bdjb/api/Buffer.java @@ -1,91 +1,91 @@ -/* - * Copyright (C) 2021 Andy Nguyen - * - * This software may be modified and distributed under the terms - * of the MIT license. See the LICENSE file for details. - */ - -package com.bdjb.api; - -public class Buffer { - private static final API api; - - static { - try { - api = API.getInstance(); - } catch (Exception e) { - throw new ExceptionInInitializerError(e); - } - } - - private final long address; - - private final int size; - - public Buffer(int size) { - this.address = api.malloc(size); - this.size = size; - } - - public void finalize() { - api.free(address); - } - - public long address() { - return address; - } - - public int size() { - return size; - } - - public byte getByte(int offset) { - checkOffset(offset); - return api.read8(address + offset); - } - - public short getShort(int offset) { - checkOffset(offset); - return api.read16(address + offset); - } - - public int getInt(int offset) { - checkOffset(offset); - return api.read32(address + offset); - } - - public long getLong(int offset) { - checkOffset(offset); - return api.read64(address + offset); - } - - public void putByte(int offset, byte value) { - checkOffset(offset); - api.write8(address + offset, value); - } - - public void putShort(int offset, short value) { - checkOffset(offset); - api.write16(address + offset, value); - } - - public void putInt(int offset, int value) { - checkOffset(offset); - api.write32(address + offset, value); - } - - public void putLong(int offset, long value) { - checkOffset(offset); - api.write64(address + offset, value); - } - - public void fill(byte value) { - api.memset(address, value, size); - } - - private void checkOffset(int offset) { - if (offset < 0 || offset >= size) { - throw new IndexOutOfBoundsException(); - } - } -} +/* + * Copyright (C) 2021 Andy Nguyen + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +package com.bdjb.api; + +public class Buffer { + private static final API api; + + static { + try { + api = API.getInstance(); + } catch (Exception e) { + throw new ExceptionInInitializerError(e); + } + } + + private final long address; + + private final int size; + + public Buffer(int size) { + this.address = api.malloc(size); + this.size = size; + } + + public void finalize() { + api.free(address); + } + + public long address() { + return address; + } + + public int size() { + return size; + } + + public byte getByte(int offset) { + checkOffset(offset); + return api.read8(address + offset); + } + + public short getShort(int offset) { + checkOffset(offset); + return api.read16(address + offset); + } + + public int getInt(int offset) { + checkOffset(offset); + return api.read32(address + offset); + } + + public long getLong(int offset) { + checkOffset(offset); + return api.read64(address + offset); + } + + public void putByte(int offset, byte value) { + checkOffset(offset); + api.write8(address + offset, value); + } + + public void putShort(int offset, short value) { + checkOffset(offset); + api.write16(address + offset, value); + } + + public void putInt(int offset, int value) { + checkOffset(offset); + api.write32(address + offset, value); + } + + public void putLong(int offset, long value) { + checkOffset(offset); + api.write64(address + offset, value); + } + + public void fill(byte value) { + api.memset(address, value, size); + } + + private void checkOffset(int offset) { + if (offset < 0 || offset >= size) { + throw new IndexOutOfBoundsException(); + } + } +} diff --git a/src/com/bdjb/api/Int16.java b/src/com/bdjb/api/Int16.java index 55d1922..8ac3a5a 100644 --- a/src/com/bdjb/api/Int16.java +++ b/src/com/bdjb/api/Int16.java @@ -1,45 +1,45 @@ -/* - * Copyright (C) 2021 Andy Nguyen - * - * This software may be modified and distributed under the terms - * of the MIT license. See the LICENSE file for details. - */ - -package com.bdjb.api; - -public final class Int16 extends IntBase { - public static final int SIZE = 2; - - public Int16(int[] dimensions) { - super(dimensions); - } - - public Int16() { - super(); - } - - public Int16(short value) { - this(); - this.set(value); - } - - int elementSize() { - return SIZE; - } - - public short get() { - return api.read16(address); - } - - public void set(short value) { - api.write16(address, value); - } - - public short get(int[] indices) { - return api.read16(address + offset(indices)); - } - - public void set(int[] indices, short value) { - api.write16(address + offset(indices), value); - } -} +/* + * Copyright (C) 2021 Andy Nguyen + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +package com.bdjb.api; + +public final class Int16 extends IntBase { + public static final int SIZE = 2; + + public Int16(int[] dimensions) { + super(dimensions); + } + + public Int16() { + super(); + } + + public Int16(short value) { + this(); + this.set(value); + } + + int elementSize() { + return SIZE; + } + + public short get() { + return api.read16(address); + } + + public void set(short value) { + api.write16(address, value); + } + + public short get(int[] indices) { + return api.read16(address + offset(indices)); + } + + public void set(int[] indices, short value) { + api.write16(address + offset(indices), value); + } +} diff --git a/src/com/bdjb/api/Int32.java b/src/com/bdjb/api/Int32.java index a9ce17a..5aec925 100644 --- a/src/com/bdjb/api/Int32.java +++ b/src/com/bdjb/api/Int32.java @@ -1,45 +1,45 @@ -/* - * Copyright (C) 2021 Andy Nguyen - * - * This software may be modified and distributed under the terms - * of the MIT license. See the LICENSE file for details. - */ - -package com.bdjb.api; - -public final class Int32 extends IntBase { - public static final int SIZE = 4; - - public Int32(int[] dimensions) { - super(dimensions); - } - - public Int32() { - super(); - } - - public Int32(int value) { - this(); - this.set(value); - } - - int elementSize() { - return SIZE; - } - - public int get() { - return api.read32(address); - } - - public void set(int value) { - api.write32(address, value); - } - - public int get(int[] indices) { - return api.read32(address + offset(indices)); - } - - public void set(int[] indices, int value) { - api.write32(address + offset(indices), value); - } -} +/* + * Copyright (C) 2021 Andy Nguyen + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +package com.bdjb.api; + +public final class Int32 extends IntBase { + public static final int SIZE = 4; + + public Int32(int[] dimensions) { + super(dimensions); + } + + public Int32() { + super(); + } + + public Int32(int value) { + this(); + this.set(value); + } + + int elementSize() { + return SIZE; + } + + public int get() { + return api.read32(address); + } + + public void set(int value) { + api.write32(address, value); + } + + public int get(int[] indices) { + return api.read32(address + offset(indices)); + } + + public void set(int[] indices, int value) { + api.write32(address + offset(indices), value); + } +} diff --git a/src/com/bdjb/api/Int64.java b/src/com/bdjb/api/Int64.java index 0e6d372..160f0df 100644 --- a/src/com/bdjb/api/Int64.java +++ b/src/com/bdjb/api/Int64.java @@ -1,45 +1,45 @@ -/* - * Copyright (C) 2021 Andy Nguyen - * - * This software may be modified and distributed under the terms - * of the MIT license. See the LICENSE file for details. - */ - -package com.bdjb.api; - -public final class Int64 extends IntBase { - public static final int SIZE = 8; - - public Int64(int[] dimensions) { - super(dimensions); - } - - public Int64() { - super(); - } - - public Int64(long value) { - this(); - this.set(value); - } - - int elementSize() { - return SIZE; - } - - public long get() { - return api.read64(address); - } - - public void set(long value) { - api.write64(address, value); - } - - public long get(int[] indices) { - return api.read64(address + offset(indices)); - } - - public void set(int[] indices, long value) { - api.write64(address + offset(indices), value); - } -} +/* + * Copyright (C) 2021 Andy Nguyen + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +package com.bdjb.api; + +public final class Int64 extends IntBase { + public static final int SIZE = 8; + + public Int64(int[] dimensions) { + super(dimensions); + } + + public Int64() { + super(); + } + + public Int64(long value) { + this(); + this.set(value); + } + + int elementSize() { + return SIZE; + } + + public long get() { + return api.read64(address); + } + + public void set(long value) { + api.write64(address, value); + } + + public long get(int[] indices) { + return api.read64(address + offset(indices)); + } + + public void set(int[] indices, long value) { + api.write64(address + offset(indices), value); + } +} diff --git a/src/com/bdjb/api/Int8.java b/src/com/bdjb/api/Int8.java index 2bb74c3..d504704 100644 --- a/src/com/bdjb/api/Int8.java +++ b/src/com/bdjb/api/Int8.java @@ -1,45 +1,45 @@ -/* - * Copyright (C) 2021 Andy Nguyen - * - * This software may be modified and distributed under the terms - * of the MIT license. See the LICENSE file for details. - */ - -package com.bdjb.api; - -public final class Int8 extends IntBase { - public static final int SIZE = 1; - - public Int8(int[] dimensions) { - super(dimensions); - } - - public Int8() { - super(); - } - - public Int8(byte value) { - this(); - this.set(value); - } - - int elementSize() { - return SIZE; - } - - public byte get() { - return api.read8(address); - } - - public void set(byte value) { - api.write8(address, value); - } - - public byte get(int[] indices) { - return api.read8(address + offset(indices)); - } - - public void set(int[] indices, byte value) { - api.write8(address + offset(indices), value); - } -} +/* + * Copyright (C) 2021 Andy Nguyen + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +package com.bdjb.api; + +public final class Int8 extends IntBase { + public static final int SIZE = 1; + + public Int8(int[] dimensions) { + super(dimensions); + } + + public Int8() { + super(); + } + + public Int8(byte value) { + this(); + this.set(value); + } + + int elementSize() { + return SIZE; + } + + public byte get() { + return api.read8(address); + } + + public void set(byte value) { + api.write8(address, value); + } + + public byte get(int[] indices) { + return api.read8(address + offset(indices)); + } + + public void set(int[] indices, byte value) { + api.write8(address + offset(indices), value); + } +} diff --git a/src/com/bdjb/api/IntBase.java b/src/com/bdjb/api/IntBase.java index 08b1eb3..77442a2 100644 --- a/src/com/bdjb/api/IntBase.java +++ b/src/com/bdjb/api/IntBase.java @@ -1,79 +1,79 @@ -/* - * Copyright (C) 2021 Andy Nguyen - * - * This software may be modified and distributed under the terms - * of the MIT license. See the LICENSE file for details. - */ - -package com.bdjb.api; - -abstract class IntBase { - static final API api; - - static { - try { - api = API.getInstance(); - } catch (Exception e) { - throw new ExceptionInInitializerError(e); - } - } - - final long address; - - final int size; - - final int[] dimensions; - - IntBase(int[] dimensions) { - this.dimensions = dimensions; - this.size = size(dimensions); - this.address = api.malloc(size); - } - - IntBase() { - this(new int[] {1}); - } - - abstract int elementSize(); - - public void finalize() { - api.free(address); - } - - public long address() { - return address; - } - - public int size() { - return size; - } - - public int size(int[] dimensions) { - assert (dimensions.length > 0); - int size = 1; - for (int i = 0; i < dimensions.length; i++) { - size *= dimensions[i]; - } - size *= elementSize(); - return size; - } - - public int offset(int[] indices) { - assert (indices.length == dimensions.length); - int offset = 0; - int stride = 1; - for (int i = indices.length - 1; i >= 0; i--) { - offset += stride * indices[i]; - stride *= dimensions[i]; - } - offset *= elementSize(); - checkOffset(offset); - return offset; - } - - private void checkOffset(int offset) { - if (offset < 0 || offset >= size) { - throw new IndexOutOfBoundsException(); - } - } -} +/* + * Copyright (C) 2021 Andy Nguyen + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +package com.bdjb.api; + +abstract class IntBase { + static final API api; + + static { + try { + api = API.getInstance(); + } catch (Exception e) { + throw new ExceptionInInitializerError(e); + } + } + + final long address; + + final int size; + + final int[] dimensions; + + IntBase(int[] dimensions) { + this.dimensions = dimensions; + this.size = size(dimensions); + this.address = api.malloc(size); + } + + IntBase() { + this(new int[] {1}); + } + + abstract int elementSize(); + + public void finalize() { + api.free(address); + } + + public long address() { + return address; + } + + public int size() { + return size; + } + + public int size(int[] dimensions) { + assert (dimensions.length > 0); + int size = 1; + for (int i = 0; i < dimensions.length; i++) { + size *= dimensions[i]; + } + size *= elementSize(); + return size; + } + + public int offset(int[] indices) { + assert (indices.length == dimensions.length); + int offset = 0; + int stride = 1; + for (int i = indices.length - 1; i >= 0; i--) { + offset += stride * indices[i]; + stride *= dimensions[i]; + } + offset *= elementSize(); + checkOffset(offset); + return offset; + } + + private void checkOffset(int offset) { + if (offset < 0 || offset >= size) { + throw new IndexOutOfBoundsException(); + } + } +}