1 / 7

Java Programming

Java Programming. Persistent Data Types. 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.

hei
Download Presentation

Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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<E> { // READ ONLY public booleanisEmpty(); public int size(); public E get(int index); public E first(); public boolean contains(Object obj); public booleancontainsAll(PersistentList<E> other); // STATE CHANGING OPERATIONS public PersistentList<E> rest(); public PersistentList<E> append(PersistentList<E> suffix); public PersistentList<E> reverse(); public PersistentList<E> add(E elem); public PersistentList<E> addAll(PersistentList<E> other); }

  4. Persistent List public abstract class AbstractPersistentList<E> implements PersistentList<E> { @Override public PersistentList<E> addAll(PersistentList<E> other) { PersistentList<E> result = this; while(!other.isEmpty()) { result.add(other.first()); other = other.rest(); } return result; } @Override public booleancontainsAll(PersistentList<E> 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<E> public class EmptyPersistentList<E> extends AbstractPersistentList<E> { public EmptyPersistentList() { } public booleanisEmpty() { return true; } public E first() { throw new EmptyListException(); } public PersistentList<E> 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<E> append(PersistentList<E> suffix) { return suffix; } public PersistentList<E> reverse() { return this; } public PersistentList<E> add(E elem) { return new NonEmptyPersistentList(elem, this); } }

  6. EmptyPersistentList<E> public class NonEmptyPersistentList<E> extends AbstractPersistentList<E> { private final E first; private final PersistentList<E> rest; public NonEmptyPersistentList(E first, PersistentList<E> 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<E> rest() { return rest; } public PersistentList<E> append(PersistentList<E> suffix) { return new NonEmptyPersistentList(first, rest.append(suffix)); } public PersistentList<E> reverse() { return rest.reverse().append(new NonEmptyPersistentList(first, new EmptyPersistentList())); } public boolean contains(Object obj) { return first.equals(obj) || rest.contains(obj); } public booleanisEmpty() { return false; } public PersistentList<E> 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<E>? public static PersistentList<Double> getRandomList(int n) { PersistentList<Double> randoms = new EmptyPersistentList<>(); for(inti = 0; i < n; i++){ randoms= new NonEmptyPersistentList(Math.random(), randoms); } return randoms; } public static <E> PersistentList<E> toList(E[] data) { PersistentList<E> result = new EmptyPersistentList<>(); for(inti=data.length-1; i>=0; i--) { result = new NonEmptyPersistentList<>(data[i], result); } return result; } public static <E> void modifyState(PersistentList<E> list) { /// what could I write to modify the state of list? }

More Related