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

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Linked Lists Linear collections.
Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
– Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces.
CS-I Final Review Hao Jiang Computer Science Department Boston College.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
Arrays An array is a collection of variables, all of the same type. An array is created with the new operator. The size of an array must be specified at.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Object-Oriented Programming (Java) Review Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
OOPSLA Lab1 Chapter 7 Java Binding Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Sections 3.4 Formal Specification
Efficiency of in Binary Trees
Data Structures Lakshmish Ramaswamy.
Implementing ArrayList Part 1
null, true, and false are also reserved.
Programming in Java Lecture 11: ArrayList
Lecture 26: Advanced List Implementation
Linked Lists [AJ 15].
JavaScript Reserved Words
Stacks Abstract Data Types (ADTs) Stacks
ArrayLists 22-Feb-19.
Collections Framework
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
ArrayLists 27-Apr-19.
Presentation transcript:

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. – 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

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); }

Persistent List public abstract class AbstractPersistentList implements PersistentList public PersistentList addAll(PersistentList other) { PersistentList result = this; while(!other.isEmpty()) { result.add(other.first()); other = other.rest(); } return result; 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 public PersistentList addAll(PersistentList other) { PersistentList result = this; while(!other.isEmpty()) { result.add(other.first()); other = other.rest(); } return result; 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 }

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); } }

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); } }

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? }