io.vproxy.pni.array.IntArray Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pni-api-jdk22 Show documentation
Show all versions of pni-api-jdk22 Show documentation
panama native interface pni-api-jdk22
The newest version!
package io.vproxy.pni.array;
import io.vproxy.pni.Allocator;
import io.vproxy.pni.NativeObject;
import io.vproxy.pni.NativeObjectTuple;
import io.vproxy.pni.PNIBuf;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.HashSet;
import java.util.Set;
public class IntArray implements NativeObject {
public final MemorySegment MEMORY;
@Override
public MemorySegment MEMORY() {
return MEMORY;
}
public IntArray(MemorySegment buf) {
MEMORY = buf;
}
public IntArray(PNIBuf buf) {
MEMORY = buf.get();
}
public IntArray(Allocator allocator, long len) {
this(allocator.allocate(len * 4));
}
public long length() {
return MEMORY.byteSize() / 4;
}
public int get(long index) {
return MEMORY.get(ValueLayout.JAVA_INT, index * 4);
}
public void set(long index, int v) {
MEMORY.set(ValueLayout.JAVA_INT, index * 4, v);
}
public PNIBuf toPNIBuf(Allocator allocator) {
var pnibuf = new PNIBuf(allocator);
pnibuf.set(MEMORY);
return pnibuf;
}
public String toString() {
var sb = new StringBuilder();
toString(sb, 0, new HashSet<>(), false);
return sb.toString();
}
@SuppressWarnings({"DuplicatedCode"})
@Override
public void toString(StringBuilder sb, int indent, Set visited, boolean corrupted) {
sb.append("IntArray[");
for (long i = 0, len = length(); i < len; ++i) {
if (i != 0) {
sb.append(", ");
}
sb.append(get(i));
}
sb.append("]");
sb.append("@").append(Long.toString(MEMORY.address(), 16));
}
}