Skip to content

Instantly share code, notes, and snippets.

@quoll
Created August 21, 2019 04:52
Show Gist options
  • Save quoll/192efb8060fcb47bcdca2912f1ca66bc to your computer and use it in GitHub Desktop.
Save quoll/192efb8060fcb47bcdca2912f1ca66bc to your computer and use it in GitHub Desktop.
package buffer;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
public class BufferList {
public static int ELEMENT_SIZE = 2 * Integer.BYTES;
public static int NULL = -1;
private static int NEXT_OFFSET = 0;
private static int VALUE_OFFSET = 1;
private ByteBuffer buffer;
private int nextAvailable;
public BufferList() {
this(10);
}
public BufferList(int length) {
buffer = ByteBuffer.allocate(ELEMENT_SIZE * length);
nextAvailable = 0;
}
public BufferList(ByteBuffer buffer) {
this.buffer = buffer;
nextAvailable = buffer.capacity() / ELEMENT_SIZE;
}
public Element addToHead(int value) {
return addToHead(null, value);
}
public Element addToHead(Element nextElt, int value) {
if (nextAvailable * ELEMENT_SIZE >= buffer.capacity()) {
throw new RuntimeException("Out of memory");
}
return new Element(nextAvailable++, nextElt, value);
}
public void write(String filename) throws IOException {
RandomAccessFile file = new RandomAccessFile(filename, "rw");
buffer.position(0).limit(nextAvailable * ELEMENT_SIZE);
file.getChannel().write(buffer);
file.close();
}
public static BufferList read(String filename) throws IOException {
RandomAccessFile file = new RandomAccessFile(filename, "r");
ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
file.getChannel().read(byteBuffer);
file.close();
return new BufferList(byteBuffer);
}
public static String listString(Element element) {
Element next = element.getNext();
String valueString = Integer.toString(element.getValue());
if (next == null) return valueString;
else return valueString + ", " + listString(next);
}
public class Element {
private final IntBuffer intBuffer;
private final int index;
Element(int index) {
this.index = index;
int offset = index * ELEMENT_SIZE;
if (offset > buffer.limit()) {
intBuffer = buffer.limit(offset + ELEMENT_SIZE).position(offset).slice().asIntBuffer();
} else {
intBuffer = buffer.position(offset).limit(offset + ELEMENT_SIZE).slice().asIntBuffer();
}
}
Element(int index, Element next, int value) {
this(index);
setNext(next);
setValue(value);
}
Element(int index, int value) {
this(index, null, value);
}
public int getIndex() {
return index;
}
public int getValue() {
return intBuffer.get(VALUE_OFFSET);
}
public Element getNext() {
int nextId = intBuffer.get(NEXT_OFFSET);
return nextId == NULL ? null : new Element(nextId);
}
public Element setValue(int value) {
intBuffer.put(VALUE_OFFSET, value);
return this;
}
public Element setNext(Element next) {
intBuffer.put(NEXT_OFFSET, next == null ? NULL : next.getIndex());
return this;
}
public String toString() {
return listString(this);
}
}
}
package buffer;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.nio.ByteBuffer;
public class MultiExample {
public static void main(String[] args) throws IOException {
BufferList bufferList = new BufferList(20);
BufferList.Element list1, list2, list3;
list1 = bufferList.addToHead(34);
list1 = bufferList.addToHead(list1, 21);
list1 = bufferList.addToHead(list1, 13);
list2 = bufferList.addToHead(list1, 8); // note that this is list2
list1 = bufferList.addToHead(list2, 5);
list1 = bufferList.addToHead(list1, 3);
list1 = bufferList.addToHead(list1, 2);
list1 = bufferList.addToHead(list1, 1);
list1 = bufferList.addToHead(list1, 1);
list2 = bufferList.addToHead(list2, 7);
list2 = bufferList.addToHead(list2, 6);
list2 = bufferList.addToHead(list2, 5);
list3 = bufferList.addToHead(6);
list3 = bufferList.addToHead(list3, 2);
list3 = bufferList.addToHead(list3, 9);
list3 = bufferList.addToHead(list3, 5);
list3 = bufferList.addToHead(list3, 1);
list3 = bufferList.addToHead(list3, 4);
list3 = bufferList.addToHead(list3, 1);
list3 = bufferList.addToHead(list3, 3);
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
bufferList.write("buffer.bin");
writeLists(list1, list2, list3);
}
static void writeLists(BufferList.Element... lists) throws IOException {
RandomAccessFile file = new RandomAccessFile("meta.bin", "rw");
for (BufferList.Element element: lists) {
file.writeInt(element.getIndex());
}
file.close();
}
}
package buffer;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.nio.ByteBuffer;
public class MultiExample2 {
public static void main(String[] args) throws IOException {
BufferList bufferList = BufferList.read("buffer.bin");
for (BufferList.Element list: readLists(bufferList)) {
System.out.println(list);
}
}
static BufferList.Element[] readLists(BufferList bufferList) throws IOException {
RandomAccessFile file = new RandomAccessFile("meta.bin", "r");
int length = (int)(file.length() / Integer.BYTES);
BufferList.Element[] lists = new BufferList.Element[length];
for (int number = 0; number < length; number++) {
lists[number] = bufferList.new Element(file.readInt());
}
file.close();
return lists;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment