CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays,...

43
Array 8 February 2019 OSU CSE 1

Transcript of CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays,...

Page 1: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Array

8 February 2019 OSU CSE 1

Page 2: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Array

• The Array component family allows you to manipulate arrays in a way that overcomes surprising limitations of built-in Java arrays, but retains the time/space performance of built-in arrays– Another generic type like Sequence and Set– A best practice alternative from the OSU

CSE components to the built-in Java array (more like built-in arrays than Sequence is)

8 February 2019 OSU CSE 2

Page 3: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

• Suppose you want to declare an array of type T, where T is a generic parameter, e.g.:T[] table = new T[100];

• Oops; this won’t compile!– The reason is complicated, having to do with

backward compatibility when Java generics were introduced and the inability to call a constructor for type T, among other oddities

8 February 2019 OSU CSE 3

Problems With Built-in Arrays

Page 4: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

• Or, suppose you want to declare an array of type Queue<T>, e.g.:Queue<T>[] table = new Queue<T>[100];

• Oops; this won’t compile either!– Yet this is exactly the kind of thing you want to

do when creating a hashtable: the buckets are collections of a parameterized type

8 February 2019 OSU CSE 4

Problems With Built-in Arrays

Page 5: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Workarounds

• There are various “workarounds” that can be used, but they are ugly and generate warnings that you should generally heed rather than ignore

• These problems do not arise with the Array component family, so it can (and should) be used in many cases where you might be inclined to try built-in Java arrays

8 February 2019 OSU CSE 5

Page 6: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

The Intuition

• The rather simple idea is to provide a generic interface Array<T> that provides the same functionality as built-in arrays, but where the entries may be of any type T

• The data representation used in Array1L<T> is a built-in array– But this data representation is hidden from the

client, along with one of the workarounds mentioned earlier

8 February 2019 OSU CSE 6

Page 7: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Interfaces and Classes

ArrayKernel

extends

Standard

extends

8 February 2019 OSU CSE 7

Array

implements

Array1L

Iterable

extends

Page 8: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Interfaces and Classes

8 February 2019 OSU CSE 8

ArrayKernel

extends

Standard

extends

Array

implements

Array1L

Iterable

extends

ArrayKernel has contracts for four

methods:setEntryentry

mayBeExaminedlength

Page 9: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Interfaces and Classes

8 February 2019 OSU CSE 9

ArrayKernel

extends

Standard

extends

Array

implements

Array1L

Iterable

extendsArray has contracts for

two methods:replaceEntry

exchangeEntries

Page 10: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Interfaces and Classes

8 February 2019 OSU CSE 10

ArrayKernel

extends

Standard

extends

Array

implements

Array1L

Iterable

extends

There is really an abstract class as usual in the chain

here, but it is not shown because these slides

describe the client view, and a client needs only interface Array and class Array1L.

Page 11: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Mathematical ModelARRAY_MODEL is (

entries: string of T,

examinableIndices: finite set of integer)exemplar a

constraintfor all i: integer

where (i is in a.examinableIndices)(0 <= i and i < |a.entries|)

type ArrayKernel is modeled by ARRAY_MODEL

8 February 2019 OSU CSE 11

Page 12: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Mathematical ModelARRAY_MODEL is (

entries: string of T,

examinableIndices: finite set of integer)exemplar a

constraintfor all i: integer

where (i is in a.examinableIndices)(0 <= i and i < |a.entries|)

type ArrayKernel is modeled by ARRAY_MODEL

8 February 2019 OSU CSE 12

The set examinableIndiceskeeps track of the indices in the Array that have been “initialized” by a call to setEntry, because

only these indices may be examined by a call to entry.

Page 13: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Constructor from int

• There is no no-argument constructor, but there is a constructor with one parameter int n

• Requires:n >= 0

• Ensures:|this.entries| = n andthis.examinableIndices = {}

8 February 2019 OSU CSE 13

Page 14: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 14

Code State

Array<Integer> ai =new Array1L<>(3);

Page 15: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 15

Code State

Array<Integer> ai =new Array1L<>(3);

ai = (<?, ?, ?>,{})

Page 16: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 16

Code State

Array<Integer> ai =new Array1L<>(3);

ai = (<?, ?, ?>,{})

All we know from the constructor contract is that the length of thisstring of integeris 3; we do not know its

actual value.

Page 17: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

setEntryvoid setEntry(int i, T x)• Sets the entry at index i of this to x.• Aliases: reference x• Updates: this• Requires:

0 <= i and i < |this.entries|

• Ensures:this.entries = #this.entries[0, i) * <x> *

#this.entries[i+1, |#this.entries|) andthis.examinableIndices = #this.examinableIndices

union {i}

8 February 2019 OSU CSE 17

Page 18: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 18

Code Stateai = (<?, ?, ?>,

{})z = 70

ai.setEntry(1, z);

Page 19: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 19

Code Stateai = (<?, ?, ?>,

{})z = 70

ai.setEntry(1, z);

ai = (<?, 70, ?>,{1})

z = 70

Page 20: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 20

Code Stateai = (<?, ?, ?>,

{})z = 70

ai.setEntry(1, z);

ai = (<?, 70, ?>,{1})

z = 70

Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.

Page 21: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Another Example

8 February 2019 OSU CSE 21

Code Stateai = (<?, -8, ?>,

{1})z = 70

ai.setEntry(1, z);

Page 22: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Another Example

8 February 2019 OSU CSE 22

Code Stateai = (<?, -8, ?>,

{1})z = 70

ai.setEntry(1, z);

ai = (<?, 70, ?>,{1})

z = 70

Page 23: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

entry

T entry (int i)

• Reports the entry at index i of this.• Aliases: reference returned by entry• Requires:i is in this.examinableIndices

• Ensures:<entry> = this.entries[i, i+1)

8 February 2019 OSU CSE 23

Page 24: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

entry

T entry(int i)

• Reports the entry at position i of this.• Aliases: reference returned by entryAt• Requires:i is in this.examinableIndices

• Ensures:<entry> = this.entries[i, i+1)

8 February 2019 OSU CSE 24

Note that this implies the index i is “within bounds”, i.e.:

0 <= i andi < |this.entries|

Page 25: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 25

Code Stateai = (<?, 70, -3>,

{1, 2})z = –584

z = ai.entry(1);

Page 26: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 26

Code Stateai = (<?, 70, -3>,

{1, 2})z = –584

z = ai.entry(1);

ai = (<?, 70, -3>,{1, 2})

z = 70

Page 27: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 27

Code Stateai = (<?, 70, -3>,

{1, 2})z = –584

z = ai.entry(1);

ai = (<?, 70, -3>,{1, 2})

z = 70

Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.

Page 28: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

mayBeExamined

boolean mayBeExamined(int i)

• Reports whether the entry at index i of this may be examined using entry.

• Ensures:mayBeExamined =

i is in this.examinableIndices

8 February 2019 OSU CSE 28

Page 29: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 29

Code State

ai = (<?, 70, -3>,{1, 2})

boolean ok =ai.mayBeExamined(1);

Page 30: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 30

Code State

ai = (<?, 70, -3>,{1, 2})

boolean ok =ai.mayBeExamined(1);

ai = (<?, 70, -3>,{1, 2})

ok = true

Page 31: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

length

int length()

• Reports the length of this.• Ensures:length = |this.entries|

8 February 2019 OSU CSE 31

Page 32: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

replaceEntryT replaceEntry(int i, T x)• Replaces the entry at index i of this with x, and

returns the old entry at that index.• Aliases: reference x• Updates: this.entries• Requires:

i is in this.examinableIndices

• Ensures:this.entries = #this.entries[0, i) * <x> *

#this.entries[i+1, |#this|) and<replaceEntry> = #this.entries[i, i+1)

8 February 2019 OSU CSE 32

Page 33: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 33

Code Stateai = (<?, 70, -3>,

{1, 2})z = 58w = 94

w = ai.replaceEntry(2, z);

Page 34: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 34

Code Stateai = (<?, 70, -3>,

{1, 2})z = 58w = 94

w = ai.replaceEntry(2, z);

ai = (<?, 70, 58>,{1, 2})

z = 58w = -3

Page 35: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 35

Code Stateai = (<?, 70, -3>,

{1, 2})z = 58w = 94

w = ai.replaceEntry(2, z);

ai = (<?, 70, 58>,{1, 2})

z = 58w = -3

Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.

Page 36: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Another Example

8 February 2019 OSU CSE 36

Code Stateai = (<?, 70, -3>,

{1, 2})z = 58

z = ai.replaceEntry(2, z);

Page 37: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Another Example

8 February 2019 OSU CSE 37

Code Stateai = (<?, 70, -3>,

{1, 2})z = 58

z = ai.replaceEntry(2, z);

ai = (<?, 70, 58>,{1, 2})

z = -3

Page 38: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Another Example

8 February 2019 OSU CSE 38

Code Stateai = (<?, 70, -3>,

{1, 2})z = 58

z = ai.replaceEntry(2, z);

ai = (<?, 70, 58>,{1, 2})

z = -3

This use of the method avoids creating an alias: it

swaps z with the entry previously at index 2.

Page 39: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

exchangeEntriesvoid exchangeEntries(int i, int j)• Exchanges entries at indices i and j of this.• Updates: this.entries• Requires:

{i, j} is subset of this.examinableIndices

• Ensures:this.entries = [#this.entries with entries

at indices i and j exchanged]

8 February 2019 OSU CSE 39

Page 40: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 40

Code State

ai = (<?, 70, -3>,{1, 2})

ai.exchangeEntries(1, 2);

Page 41: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Example

8 February 2019 OSU CSE 41

Code State

ai = (<?, 70, -3>,{1, 2})

ai.exchangeEntries(1, 2);

ai = (<?, -3, 70>,{1, 2})

Page 42: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

iterator

Iterator<T> iterator()

• Returns an iterator over a set of elements of type T.

• Ensures:~this.seen * ~this.unseen =

[string of entries of this.entries,in order by increasing index, where

index is in this.examinableIndices]

8 February 2019 OSU CSE 42

Page 43: CSE 2231 - Arrayweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/08.Array.pdfJava arrays, but retains the time/space performance of built-in arrays – Another generic type

Resources

• OSU CSE Components API: Array– http://cse.osu.edu/software/common/doc/

8 February 2019 OSU CSE 43