Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.

Similar presentations


Presentation on theme: "Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never."— Presentation transcript:

1 Java Programming Persistent Data Types

2 Persistent Data Structure A persistent data structure is a data structure having an internal state that never changes. – No operation affects the public state of the data structure. – Any operation that would produce a state change in a non-persistent structure, will instead produce a new data structure that reflects the updated state. – In this discussion, the term ‘persistent’ does not mean ‘saved’ We will describe a list as having two parts – first: the first element in the list – rest: a list of the remaining elements in the list

3 Persistent List public interface PersistentList { // READ ONLY public boolean isEmpty(); public int size(); public E get(int index); public E first(); public boolean contains(Object obj); public boolean containsAll(PersistentList other); // STATE CHANGING OPERATIONS public PersistentList rest(); public PersistentList append(PersistentList suffix); public PersistentList reverse(); public PersistentList add(E elem); public PersistentList addAll(PersistentList other); } public interface PersistentList { // READ ONLY public boolean isEmpty(); public int size(); public E get(int index); public E first(); public boolean contains(Object obj); public boolean containsAll(PersistentList other); // STATE CHANGING OPERATIONS public PersistentList rest(); public PersistentList append(PersistentList suffix); public PersistentList reverse(); public PersistentList add(E elem); public PersistentList addAll(PersistentList other); }

4 Persistent List public abstract class AbstractPersistentList implements PersistentList { @Override public PersistentList addAll(PersistentList other) { PersistentList result = this; while(!other.isEmpty()) { result.add(other.first()); other = other.rest(); } return result; } @Override public boolean containsAll(PersistentList other) { while(!other.isEmpty()) { if(!this.contains(other.first())) return false; } return true; } // we can also do “add” but will write this method later } public abstract class AbstractPersistentList implements PersistentList { @Override public PersistentList addAll(PersistentList other) { PersistentList result = this; while(!other.isEmpty()) { result.add(other.first()); other = other.rest(); } return result; } @Override public boolean containsAll(PersistentList other) { while(!other.isEmpty()) { if(!this.contains(other.first())) return false; } return true; } // we can also do “add” but will write this method later }

5 EmptyPersistentList public class EmptyPersistentList extends AbstractPersistentList { public EmptyPersistentList() { } public boolean isEmpty() { return true; } public E first() { throw new EmptyListException(); } public PersistentList rest() { throw new EmptyListException(); } public E get(int index) { throw new IndexOutOfBoundsException(); } public int size() { return 0; } public boolean contains(Object obj) { return false; } public PersistentList append(PersistentList suffix) { return suffix; } public PersistentList reverse() { return this; } public PersistentList add(E elem) { return new NonEmptyPersistentList(elem, this); } } public class EmptyPersistentList extends AbstractPersistentList { public EmptyPersistentList() { } public boolean isEmpty() { return true; } public E first() { throw new EmptyListException(); } public PersistentList rest() { throw new EmptyListException(); } public E get(int index) { throw new IndexOutOfBoundsException(); } public int size() { return 0; } public boolean contains(Object obj) { return false; } public PersistentList append(PersistentList suffix) { return suffix; } public PersistentList reverse() { return this; } public PersistentList add(E elem) { return new NonEmptyPersistentList(elem, this); } }

6 EmptyPersistentList public class NonEmptyPersistentList extends AbstractPersistentList { private final E first; private final PersistentList rest; public NonEmptyPersistentList(E first, PersistentList rest) { this.first = first; this.rest = rest; } public E get(int index) { if(index == 0) { return first; } else { return rest.get(index-1); } public int size() { return 1 + rest.size(); } public E first() { return first; } public PersistentList rest() { return rest; } public PersistentList append(PersistentList suffix) { return new NonEmptyPersistentList(first, rest.append(suffix)); } public PersistentList reverse() { return rest.reverse().append(new NonEmptyPersistentList(first, new EmptyPersistentList())); } public boolean contains(Object obj) { return first.equals(obj) || rest.contains(obj); } public boolean isEmpty() { return false; } public PersistentList add(E elem) { return new NonEmptyPersistentList(elem, this); } } public class NonEmptyPersistentList extends AbstractPersistentList { private final E first; private final PersistentList rest; public NonEmptyPersistentList(E first, PersistentList rest) { this.first = first; this.rest = rest; } public E get(int index) { if(index == 0) { return first; } else { return rest.get(index-1); } public int size() { return 1 + rest.size(); } public E first() { return first; } public PersistentList rest() { return rest; } public PersistentList append(PersistentList suffix) { return new NonEmptyPersistentList(first, rest.append(suffix)); } public PersistentList reverse() { return rest.reverse().append(new NonEmptyPersistentList(first, new EmptyPersistentList())); } public boolean contains(Object obj) { return first.equals(obj) || rest.contains(obj); } public boolean isEmpty() { return false; } public PersistentList add(E elem) { return new NonEmptyPersistentList(elem, this); } }

7 How to use a persistent list Can we write a function to create a list of N random values in [0-1)? Can we write a function to convert an array of elements into a persistent list? Can we write a function to modify the state of a PersistentList ? public static PersistentList toList(E[] data) { PersistentList result = new EmptyPersistentList<>(); for(int i=data.length-1; i>=0; i--) { result = new NonEmptyPersistentList<>(data[i], result); } return result; } public static PersistentList toList(E[] data) { PersistentList result = new EmptyPersistentList<>(); for(int i=data.length-1; i>=0; i--) { result = new NonEmptyPersistentList<>(data[i], result); } return result; } public static PersistentList getRandomList(int n) { PersistentList randoms = new EmptyPersistentList<>(); for(int i = 0; i < n; i++){ randoms = new NonEmptyPersistentList(Math.random(), randoms); } return randoms; } public static PersistentList getRandomList(int n) { PersistentList randoms = new EmptyPersistentList<>(); for(int i = 0; i < n; i++){ randoms = new NonEmptyPersistentList(Math.random(), randoms); } return randoms; } public static void modifyState(PersistentList list) { /// what could I write to modify the state of list? } public static void modifyState(PersistentList list) { /// what could I write to modify the state of list? }


Download ppt "Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never."

Similar presentations


Ads by Google