ListNode<Key,Value> createNode(Page<ListNode<Key,Value>> page) throws IOException { ListNode<Key,Value> node = new ListNode<Key,Value>(); node.setPage(page); page.set(node); node.setContainingList(this); return node; }
private void split(Transaction tx, boolean isAddFirst) throws IOException { ListNode<Key, Value> extension = getContainingList().createNode(tx); if (isAddFirst) { // head keeps the first entry, insert extension with the rest extension.setEntries(entries.getHead().splitAfter()); extension.setNext(this.getNext()); extension.store(tx, isAddFirst); this.setNext(extension.getPageId()); } else { extension.setEntries(entries.getTail().getPrevious().splitAfter()); extension.setNext(this.getNext()); extension.store(tx, isAddFirst); getContainingList().setTailPageId(extension.getPageId()); this.setNext(extension.getPageId()); } store(tx, true); }
synchronized public Value addFirst(Transaction tx, Key key, Value value) throws IOException { assertLoaded(); getHead(tx).addFirst(tx, key, value); size.incrementAndGet(); flushCache(); return null; }
private void store(Transaction tx, boolean addFirst) throws IOException { try { // keeping splitting till we get down to a single entry // then we need to overflow the value getContainingList().storeNode(tx, this, entries.size() == 1); if (this.next == -1) { getContainingList().setTailPageId(getPageId()); } } catch (Transaction.PageOverflowIOException e) { // If we get an overflow split(tx, addFirst); } }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public ListNode<Key, Value> readPayload(DataInput is) throws IOException { ListNode<Key, Value> node = new ListNode<Key, Value>(); node.setNext(is.readLong()); final short size = is.readShort(); for (short i = 0; i < size; i++) { node.entries.addLast(new KeyValueEntry(keyMarshaller.readPayload(is), valueMarshaller.readPayload(is))); } return node; } }
public void addToBack(String name, String telephoneNum, String email, String address, String dob) { //separate responsibilities, by constructing the node first addToBack(new ListNode(name,telephoneNum,email,address,dob)); } public void addToBack(ListNode newNode) { //do not use a continuation state in a class, fetch the head, inspect the head and if not null pass to the recursion method if(this.head == null) { this.head = newNode; } else { this.addToBack(this.head,newNode); } } public void addToBack(ListNode current, ListNode newNode) { //generic method that adds the node at the end if(current.getNext() == null) {//base case: current is the last node current.setNext(newNode); } else {//recursive case, current is not the next node addToBack(current.getNext(),newNode); } }
//when instantiated, MagazineList's list variable is set to null public void add (Magazine mag) { ListNode node = new ListNode (mag, null); ListNode current; if (list == null) list = node; else { current = list; // you list head while (node.compareTo(current) < 0) current = current.getNext(); ListNode next = current.getNext(); current.setNext(node); node.setNext(next); } }
if (head.getNext() == null) { head.setNext(new ListNode(myTrain)); count++; } else if (head.getNext().getTrain().getTrainNumber() < myTrain.getTrainNumber()) { newNode.setNext(head.getNext()); head.setNext(newNode); count++; } else { add(myTrain, head.getNext()); if (currentNode.getNext() == null) { } else if (currentNode.getNext().getTrain().getTrainNumber() == trainNumber) { ListNode tmp = currentNode.getNext(); currentNode.setNext(currentNode.getNext().getNext()); return tmp.getTrain(); } else { return delete(trainNumber, currentNode.getNext());
public void yourMainMethod() { ListNode head = new ListNode("Overflow!", null); head = new ListNode("Stack", head); head = new ListNode("in", head); head = new ListNode("is", head); head = new ListNode("This", head); printList(head); System.out.println(); printList(head); } //note different name, to clarify this is a separate reference public static void printList(ListNode node) { while(node != null) { System.out.print(node.getValue()+" "); node = node.getNext(); //node.setValue(new Object());//note that this would change the state inside the ListNode passed in } }
public boolean isTail() { return getPageId() == containingList.getTailPageId(); }
ListNode temp = first; while (temp != null){ temp.getEmployee().incrementYears(1); temp = temp.getNext(); }
@Override public String toString(){ StringBuilder builder = new StringBuilder(); builder.append("ReferenceBasedList["); ListNode currentElement = head; while (currentElement != null){ builder.append(currentElement.toString()); currentElement = currentElement.getNext(); //seperator if required: if (currentElement != null){ builder.append(", "); } } builder.append("]"); return builder.toString(); }
@Override synchronized public void clear(Transaction tx) throws IOException { for (Iterator<ListNode<Key,Value>> iterator = listNodeIterator(tx); iterator.hasNext(); ) { ListNode<Key,Value>candidate = iterator.next(); candidate.clear(tx); // break up the transaction tx.commit(); } flushCache(); size.set(0); }
public void storeNode(Transaction tx, ListNode<Key,Value> node, boolean overflow) throws IOException { tx.store(node.getPage(), marshaller, overflow); flushCache(); }
synchronized public Map.Entry<Key,Value> getFirst(Transaction tx) throws IOException { return getHead(tx).getFirst(tx); }
synchronized public Map.Entry<Key,Value> getLast(Transaction tx) throws IOException { return getTail(tx).getLast(tx); }