Created
August 19, 2019 04:30
-
-
Save quoll/98526b593f6f0150060acb9f1e983857 to your computer and use it in GitHub Desktop.
List Example with Buffer in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package buffer; | |
import java.nio.ByteBuffer; | |
import java.nio.IntBuffer; | |
public class BufferList { | |
// size of the data that represents an element in the buffer | |
public static int ELEMENT_SIZE = 2 * Integer.BYTES; | |
public static int NULL = -1; | |
// offsets of the parts of Element within the buffer | |
private static int NEXT_OFFSET = 0; | |
private static int VALUE_OFFSET = 1; | |
// the buffer for all the Elements | |
private ByteBuffer buffer; | |
// the next free section of buffer that can be used, counted in Elements | |
private int nextAvailable; | |
// by default, the buffer can hold 10 Elements | |
public BufferList() { | |
this(10); | |
} | |
// Initializes the buffer in this list manager | |
public BufferList(int length) { | |
buffer = ByteBuffer.allocate(ELEMENT_SIZE * length); | |
nextAvailable = 0; | |
} | |
// creates a 1 element list | |
public Element addToHead(int value) { | |
return addToHead(null, value); | |
} | |
// prepends an element to an existing list | |
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); | |
} | |
// creates a string representing the list | |
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); | |
} | |
// The internal Element class | |
public class Element { | |
// The slice of buffer referenced by this element | |
private final IntBuffer intBuffer; | |
// the position of the slice within the overall buffer | |
private final int index; | |
// capture a slice of the buffer for this element | |
Element(int index) { | |
this.index = index; | |
int offset = index * ELEMENT_SIZE; | |
intBuffer = buffer.position(offset).limit(offset + ELEMENT_SIZE).slice().asIntBuffer(); | |
} | |
// Associate this element with a slice of the buffer, and then populate it | |
Element(int index, Element next, int value) { | |
this(index); | |
setNext(next); | |
setValue(value); | |
} | |
// Convenience to create an element with no successors | |
Element(int index, int value) { | |
this(index, null, value); | |
} | |
// gets the read-only index of the element | |
public int getIndex() { | |
return index; | |
} | |
// reads the value from the buffer | |
public int getValue() { | |
return intBuffer.get(VALUE_OFFSET); | |
} | |
// reads the location of the next element from the buffer and loads it | |
public Element getNext() { | |
int nextId = intBuffer.get(NEXT_OFFSET); | |
return nextId == NULL ? null : new Element(nextId); | |
} | |
// sets the value for this element in the buffer | |
public Element setValue(int value) { | |
intBuffer.put(VALUE_OFFSET, value); | |
return this; | |
} | |
// sets this element to refer to the given element as the successor | |
public Element setNext(Element next) { | |
intBuffer.put(NEXT_OFFSET, next == null ? NULL : next.getIndex()); | |
return this; | |
} | |
// convenience function to print an entire list from this element | |
public String toString() { | |
return listString(this); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package buffer; | |
public class ListExample { | |
public static void main(String[] args) { | |
BufferList bufferList = new BufferList(10); | |
BufferList.Element list = bufferList.addToHead(34); | |
list = bufferList.addToHead(list, 21); | |
list = bufferList.addToHead(list, 13); | |
list = bufferList.addToHead(list, 8); | |
list = bufferList.addToHead(list, 5); | |
list = bufferList.addToHead(list, 3); | |
list = bufferList.addToHead(list, 2); | |
list = bufferList.addToHead(list, 1); | |
list = bufferList.addToHead(list, 1); | |
System.out.println(list); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment