mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Refactor FloatBuffer
This commit is contained in:
parent
89b65fef8a
commit
999e02d316
|
@ -206,4 +206,6 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
|
|||
public abstract TIntBuffer asIntBuffer();
|
||||
|
||||
public abstract TLongBuffer asLongBuffer();
|
||||
|
||||
public abstract TFloatBuffer asFloatBuffer();
|
||||
}
|
||||
|
|
|
@ -138,4 +138,13 @@ class TByteBufferImpl extends TByteBuffer {
|
|||
result.byteOrder = order;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TFloatBuffer asFloatBuffer() {
|
||||
int sz = remaining() / 4;
|
||||
TFloatBufferOverByteBuffer result = new TFloatBufferOverByteBuffer(start + position, sz, this, 0, sz,
|
||||
isReadOnly());
|
||||
result.byteOrder = order;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,13 +20,8 @@ package org.teavm.classlib.java.nio;
|
|||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||
*/
|
||||
public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatBuffer> {
|
||||
int start;
|
||||
float[] array;
|
||||
|
||||
TFloatBuffer(int start, int capacity, float[] array, int position, int limit) {
|
||||
TFloatBuffer(int capacity, int position, int limit) {
|
||||
super(capacity);
|
||||
this.start = start;
|
||||
this.array = array;
|
||||
this.position = position;
|
||||
this.limit = limit;
|
||||
}
|
||||
|
@ -35,11 +30,11 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
if (capacity < 0) {
|
||||
throw new IllegalArgumentException("Capacity is negative: " + capacity);
|
||||
}
|
||||
return new TFloatBufferImpl(capacity);
|
||||
return new TFloatBufferOverArray(capacity);
|
||||
}
|
||||
|
||||
public static TFloatBuffer wrap(float[] array, int offset, int length) {
|
||||
return new TFloatBufferImpl(0, array.length, array, offset, offset + length, false);
|
||||
return new TFloatBufferOverArray(0, array.length, array, offset, offset + length, false);
|
||||
}
|
||||
|
||||
public static TFloatBuffer wrap(float[] array) {
|
||||
|
@ -60,6 +55,10 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
|
||||
public abstract TFloatBuffer put(int index, float b);
|
||||
|
||||
abstract float getElement(int index);
|
||||
|
||||
abstract void putElement(int index, float value);
|
||||
|
||||
public TFloatBuffer get(float[] dst, int offset, int length) {
|
||||
if (offset < 0 || offset >= dst.length) {
|
||||
throw new IndexOutOfBoundsException("Offset " + offset + " is outside of range [0;" + dst.length + ")");
|
||||
|
@ -74,9 +73,9 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
if (length < 0) {
|
||||
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
||||
}
|
||||
int pos = position + start;
|
||||
int pos = position;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
dst[offset++] = array[pos++];
|
||||
dst[offset++] = getElement(pos++);
|
||||
}
|
||||
position += length;
|
||||
return this;
|
||||
|
@ -87,7 +86,20 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
}
|
||||
|
||||
public TFloatBuffer put(TFloatBuffer src) {
|
||||
return put(src.array, src.start + src.position, src.remaining());
|
||||
if (isReadOnly()) {
|
||||
throw new TReadOnlyBufferException();
|
||||
}
|
||||
if (remaining() < src.remaining()) {
|
||||
throw new TBufferOverflowException();
|
||||
}
|
||||
int length = src.remaining();
|
||||
int pos = position;
|
||||
int offset = src.position;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
putElement(pos++, src.getElement(offset++));
|
||||
}
|
||||
position += length;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TFloatBuffer put(float[] src, int offset, int length) {
|
||||
|
@ -107,9 +119,9 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
if (length < 0) {
|
||||
throw new IndexOutOfBoundsException("Length " + length + " must be non-negative");
|
||||
}
|
||||
int pos = position + start;
|
||||
int pos = position;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
array[pos++] = src[offset++];
|
||||
putElement(pos++, src[offset++]);
|
||||
}
|
||||
position += length;
|
||||
return this;
|
||||
|
@ -120,20 +132,26 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean hasArray() {
|
||||
return true;
|
||||
public final boolean hasArray() {
|
||||
return isArrayPresent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final float[] array() {
|
||||
return array;
|
||||
return getArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int arrayOffset() {
|
||||
return start;
|
||||
public final int arrayOffset() {
|
||||
return getArrayOffset();
|
||||
}
|
||||
|
||||
abstract boolean isArrayPresent();
|
||||
|
||||
abstract float[] getArray();
|
||||
|
||||
abstract int getArrayOffset();
|
||||
|
||||
public abstract TFloatBuffer compact();
|
||||
|
||||
@Override
|
||||
|
@ -148,9 +166,9 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
@Override
|
||||
public int hashCode() {
|
||||
int hashCode = 0;
|
||||
int pos = position + start;
|
||||
int pos = position;
|
||||
for (int i = position; i < limit; ++i) {
|
||||
hashCode = 31 * hashCode + Float.floatToIntBits(array[pos++]);
|
||||
hashCode = 31 * hashCode + Float.floatToIntBits(getElement(pos++));
|
||||
}
|
||||
return hashCode;
|
||||
}
|
||||
|
@ -168,10 +186,10 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
if (sz != other.remaining()) {
|
||||
return false;
|
||||
}
|
||||
int a = position + start;
|
||||
int b = other.position + other.start;
|
||||
int a = position;
|
||||
int b = other.position;
|
||||
for (int i = 0; i < sz; ++i) {
|
||||
if (array[a++] != other.array[b++]) {
|
||||
if (getElement(a++) != other.getElement(b++)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -184,14 +202,16 @@ public abstract class TFloatBuffer extends TBuffer implements Comparable<TFloatB
|
|||
return 0;
|
||||
}
|
||||
int sz = Math.min(remaining(), other.remaining());
|
||||
int a = position + start;
|
||||
int b = other.position + other.start;
|
||||
int a = position;
|
||||
int b = other.position;
|
||||
for (int i = 0; i < sz; ++i) {
|
||||
int r = Float.compare(array[a++], other.array[b++]);
|
||||
int r = Float.compare(getElement(a++), other.getElement(b++));
|
||||
if (r != 0) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
return Integer.compare(remaining(), other.remaining());
|
||||
}
|
||||
|
||||
public abstract TByteOrder order();
|
||||
}
|
||||
|
|
|
@ -19,50 +19,45 @@ package org.teavm.classlib.java.nio;
|
|||
*
|
||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||
*/
|
||||
class TFloatBufferImpl extends TFloatBuffer {
|
||||
private boolean readOnly;
|
||||
|
||||
public TFloatBufferImpl(int capacity) {
|
||||
this(0, capacity, new float[capacity], 0, capacity, false);
|
||||
}
|
||||
|
||||
public TFloatBufferImpl(int start, int capacity, float[] array, int position, int limit, boolean readOnly) {
|
||||
super(start, capacity, array, position, limit);
|
||||
this.readOnly = readOnly;
|
||||
abstract class TFloatBufferImpl extends TFloatBuffer {
|
||||
public TFloatBufferImpl(int capacity, int position, int limit) {
|
||||
super(capacity, position, limit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TFloatBuffer slice() {
|
||||
return new TFloatBufferImpl(position, limit - position, array, 0, limit - position, readOnly);
|
||||
return duplicate(position, limit - position, 0, limit - position, isReadOnly());
|
||||
}
|
||||
|
||||
@Override
|
||||
public TFloatBuffer duplicate() {
|
||||
return new TFloatBufferImpl(start, capacity, array, position, limit, readOnly);
|
||||
return duplicate(0, capacity, position, limit, isReadOnly());
|
||||
}
|
||||
|
||||
@Override
|
||||
public TFloatBuffer asReadOnlyBuffer() {
|
||||
return new TFloatBufferImpl(start, capacity, array, position, limit, true);
|
||||
return duplicate(0, capacity, position, limit, true);
|
||||
}
|
||||
|
||||
abstract TFloatBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly);
|
||||
|
||||
@Override
|
||||
public float get() {
|
||||
if (position >= limit) {
|
||||
throw new TBufferUnderflowException();
|
||||
}
|
||||
return array[start + position++];
|
||||
return getElement(position++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TFloatBuffer put(float b) {
|
||||
if (readOnly) {
|
||||
if (isReadOnly()) {
|
||||
throw new TReadOnlyBufferException();
|
||||
}
|
||||
if (position >= limit) {
|
||||
throw new TBufferOverflowException();
|
||||
}
|
||||
array[start + position++] = b;
|
||||
putElement(position++, b);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -71,32 +66,31 @@ class TFloatBufferImpl extends TFloatBuffer {
|
|||
if (index < 0 || index >= limit) {
|
||||
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
||||
}
|
||||
return array[start + index];
|
||||
return getElement(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TFloatBuffer put(int index, float b) {
|
||||
if (readOnly) {
|
||||
if (isReadOnly()) {
|
||||
throw new TReadOnlyBufferException();
|
||||
}
|
||||
if (index < 0 || index >= limit) {
|
||||
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + limit + ")");
|
||||
}
|
||||
array[start + index] = b;
|
||||
putElement(index, b);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TFloatBuffer compact() {
|
||||
if (readOnly) {
|
||||
if (isReadOnly()) {
|
||||
throw new TReadOnlyBufferException();
|
||||
}
|
||||
if (position > 0) {
|
||||
int sz = remaining();
|
||||
int dst = start;
|
||||
int src = start + position;
|
||||
int src = position;
|
||||
for (int i = 0; i < sz; ++i) {
|
||||
array[dst++] = array[src++];
|
||||
putElement(i, getElement(src++));
|
||||
}
|
||||
position = sz;
|
||||
}
|
||||
|
@ -112,6 +106,8 @@ class TFloatBufferImpl extends TFloatBuffer {
|
|||
|
||||
@Override
|
||||
public boolean isReadOnly() {
|
||||
return readOnly;
|
||||
return readOnly();
|
||||
}
|
||||
|
||||
abstract boolean readOnly();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright 2014 Alexey Andreev.
|
||||
*
|
||||
* 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 org.teavm.classlib.java.nio;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||
*/
|
||||
class TFloatBufferOverArray extends TFloatBufferImpl {
|
||||
boolean readOnly;
|
||||
int start;
|
||||
float[] array;
|
||||
|
||||
public TFloatBufferOverArray(int capacity) {
|
||||
this(0, capacity, new float[capacity], 0, capacity, false);
|
||||
}
|
||||
|
||||
public TFloatBufferOverArray(int start, int capacity, float[] array, int position, int limit, boolean readOnly) {
|
||||
super(capacity, position, limit);
|
||||
this.start = start;
|
||||
this.readOnly = readOnly;
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
@Override
|
||||
TFloatBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) {
|
||||
return new TFloatBufferOverArray(this.start + start, capacity, array, position, limit, readOnly);
|
||||
}
|
||||
|
||||
@Override
|
||||
float getElement(int index) {
|
||||
return array[index + start];
|
||||
}
|
||||
|
||||
@Override
|
||||
void putElement(int index, float value) {
|
||||
array[index + start] = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isArrayPresent() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
float[] getArray() {
|
||||
return array;
|
||||
}
|
||||
|
||||
@Override
|
||||
int getArrayOffset() {
|
||||
return start;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean readOnly() {
|
||||
return readOnly;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteOrder order() {
|
||||
return TByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Copyright 2014 Alexey Andreev.
|
||||
*
|
||||
* 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 org.teavm.classlib.java.nio;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||
*/
|
||||
class TFloatBufferOverByteBuffer extends TFloatBufferImpl {
|
||||
private TByteBufferImpl byteByffer;
|
||||
TByteOrder byteOrder = TByteOrder.BIG_ENDIAN;
|
||||
boolean readOnly;
|
||||
private int start;
|
||||
|
||||
public TFloatBufferOverByteBuffer(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit,
|
||||
boolean readOnly) {
|
||||
super(capacity, position, limit);
|
||||
this.start = start;
|
||||
this.byteByffer = byteBuffer;
|
||||
this.readOnly = readOnly;
|
||||
}
|
||||
|
||||
@Override
|
||||
TFloatBuffer duplicate(int start, int capacity, int position, int limit, boolean readOnly) {
|
||||
TFloatBufferOverByteBuffer result = new TFloatBufferOverByteBuffer(this.start + start * 2, capacity,
|
||||
byteByffer, position, limit, readOnly);
|
||||
result.byteOrder = byteOrder;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
float getElement(int index) {
|
||||
int value;
|
||||
if (byteOrder == TByteOrder.BIG_ENDIAN) {
|
||||
value = (byteByffer.array[start + index * 4] << 24) |
|
||||
(byteByffer.array[start + index * 4 + 1] << 16) |
|
||||
(byteByffer.array[start + index * 4 + 2] << 8) |
|
||||
(byteByffer.array[start + index * 4 + 3]);
|
||||
} else {
|
||||
value = (byteByffer.array[start + index * 4]) |
|
||||
(byteByffer.array[start + index * 4 + 1] << 8) |
|
||||
(byteByffer.array[start + index * 4 + 2] << 16) |
|
||||
(byteByffer.array[start + index * 4 + 3] << 24);
|
||||
}
|
||||
return Float.intBitsToFloat(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
void putElement(int index, float f) {
|
||||
int value = Float.floatToIntBits(f);
|
||||
if (byteOrder == TByteOrder.BIG_ENDIAN) {
|
||||
byteByffer.array[start + index * 4] = (byte)(value >> 24);
|
||||
byteByffer.array[start + index * 4 + 1] = (byte)(value >> 16);
|
||||
byteByffer.array[start + index * 4 + 2] = (byte)(value >> 8);
|
||||
byteByffer.array[start + index * 4 + 3] = (byte)value;
|
||||
} else {
|
||||
byteByffer.array[start + index * 4] = (byte)value;
|
||||
byteByffer.array[start + index * 4 + 1] = (byte)(value >> 8);
|
||||
byteByffer.array[start + index * 4 + 2] = (byte)(value >> 16);
|
||||
byteByffer.array[start + index * 4 + 3] = (byte)(value >> 24);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isArrayPresent() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
float[] getArray() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
int getArrayOffset() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean readOnly() {
|
||||
return readOnly;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteOrder order() {
|
||||
return byteOrder;
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@
|
|||
package org.teavm.classlib.java.nio;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
import java.nio.*;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -103,6 +103,31 @@ public class ByteBufferWrapperTest {
|
|||
assertThat(buffer.get(7), is((byte)0x32));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapsIntoFloatBuffer() {
|
||||
byte[] array = new byte[100];
|
||||
ByteBuffer buffer = ByteBuffer.wrap(array);
|
||||
buffer.limit(70);
|
||||
buffer.get(new byte[10]);
|
||||
buffer = buffer.slice();
|
||||
buffer.put(0, (byte)0x40);
|
||||
buffer.put(1, (byte)0x49);
|
||||
buffer.put(2, (byte)0x0F);
|
||||
buffer.put(3, (byte)0xD0);
|
||||
|
||||
FloatBuffer wrapper = buffer.asFloatBuffer();
|
||||
assertThat(wrapper.capacity(), is(15));
|
||||
assertThat(wrapper.position(), is(0));
|
||||
assertThat(wrapper.limit(), is(15));
|
||||
assertEquals(3.14159, wrapper.get(0), 0.00001);
|
||||
|
||||
wrapper.put(0, 2.71828F);
|
||||
assertThat(buffer.get(0), is((byte)0x40));
|
||||
assertThat(buffer.get(1), is((byte)0x2D));
|
||||
assertThat(buffer.get(2), is((byte)0xF8));
|
||||
assertThat(buffer.get(3) & 0xF0, is(0x40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shortEndiannesWorks() {
|
||||
byte[] array = new byte[100];
|
||||
|
|
Loading…
Reference in New Issue
Block a user