Sequence 7 January 2019 OSU CSE 1 Sequence The Sequence - - PowerPoint PPT Presentation

sequence
SMART_READER_LITE
LIVE PREVIEW

Sequence 7 January 2019 OSU CSE 1 Sequence The Sequence - - PowerPoint PPT Presentation

Sequence 7 January 2019 OSU CSE 1 Sequence The Sequence component family allows you to manipulate strings of entries of any (arbitrary) type through direct access by position, similar to an array Another generic type like Queue and Set


slide-1
SLIDE 1

Sequence

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Sequence

  • The Sequence component family allows

you to manipulate strings of entries of any (arbitrary) type through direct access by position, similar to an array

– Another generic type like Queue and Set – One possible best practice alternative to the built-in Java array, from the OSU CSE components

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

Interfaces and Classes

Sequence- Kernel extends Standard extends

7 January 2019 OSU CSE 3

Sequence Sequence1L implements implements Sequence2L Sequence3

slide-4
SLIDE 4

Interfaces and Classes

Sequence- Kernel extends Standard extends

7 January 2019 OSU CSE 4

Sequence Sequence1L implements implements Sequence2L Sequence3 Standard has contracts for three methods: clear newInstance transferFrom

slide-5
SLIDE 5

Interfaces and Classes

Sequence- Kernel extends Standard extends

7 January 2019 OSU CSE 5

Sequence Sequence1L implements implements Sequence2L Sequence3 SequenceKernel has contracts for three methods: add remove length

slide-6
SLIDE 6

Interfaces and Classes

Sequence- Kernel extends Standard extends

7 January 2019 OSU CSE 6

Sequence Sequence1L implements implements Sequence2L Sequence3 Sequence has contracts for six other methods: entry replaceEntry append flip insert extract

slide-7
SLIDE 7

Mathematical Model

  • The value of a Sequence variable is

modeled as a string of entries of type T

  • Formally:

type Sequence is modeled by string of T

7 January 2019 OSU CSE 7

slide-8
SLIDE 8

No-argument Constructor

  • Ensures:

this = < >

7 January 2019 OSU CSE 8

slide-9
SLIDE 9

Example

7 January 2019 OSU CSE 9

Code State

Sequence<Integer> si = new Sequence1L<>();

slide-10
SLIDE 10

Example

7 January 2019 OSU CSE 10

Code State

Sequence<Integer> si = new Sequence1L<>(); si = < >

slide-11
SLIDE 11

add

void add(int pos, T x)

  • Adds x at position pos of this.
  • Aliases: reference x
  • Updates: this
  • Requires:

0 <= pos and pos <= |this|

  • Ensures:

this = #this[0, pos) * <x> * #this[pos, |#this|)

7 January 2019 OSU CSE 11

slide-12
SLIDE 12

Example

7 January 2019 OSU CSE 12

Code State

si = < 49, 3 > z = 70 si.add(1, z);

slide-13
SLIDE 13

Example

7 January 2019 OSU CSE 13

Code State

si = < 49, 3 > z = 70 si.add(1, z); si = < 49, 70, 3 > z = 70

slide-14
SLIDE 14

Example

7 January 2019 OSU CSE 14

Code State

si = < 49, 3 > z = 70 si.add(1, z); si = < 49, 70, 3 > 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.

slide-15
SLIDE 15

remove

T remove(int pos)

  • Removes and returns the entry at position pos
  • f this.
  • Updates: this
  • Requires:

0 <= pos and pos < |this|

  • Ensures:

this = #this[0, pos) * #this[pos+1, |#this|) and <remove> = #this[pos, pos+1)

7 January 2019 OSU CSE 15

slide-16
SLIDE 16

Example

7 January 2019 OSU CSE 16

Code State

si = < 49, 3, 70 > z = –584 z = si.remove(1);

slide-17
SLIDE 17

Example

7 January 2019 OSU CSE 17

Code State

si = < 49, 3, 70 > z = –584 z = si.remove(1); si = < 49, 70 > z = 3

slide-18
SLIDE 18

length

int length()

  • Reports the length of this.
  • Ensures:

length = |this|

7 January 2019 OSU CSE 18

slide-19
SLIDE 19

entry

T entry(int pos)

  • Reports the entry at position pos of this.
  • Aliases: reference returned by entry
  • Requires:

0 <= pos and pos < |this|

  • Ensures:

<entry> = this[pos, pos+1)

7 January 2019 OSU CSE 19

slide-20
SLIDE 20

Example

7 January 2019 OSU CSE 20

Code State

si = < 49, 3, 70 > z = –584 z = si.entry(1);

slide-21
SLIDE 21

Example

7 January 2019 OSU CSE 21

Code State

si = < 49, 3, 70 > z = –584 z = si.entry(1); si = < 49, 3, 70 > z = 3

slide-22
SLIDE 22

Example

7 January 2019 OSU CSE 22

Code State

si = < 49, 3, 70 > z = –584 z = si.entryAt(1); si = < 49, 3, 70 > z = 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.

slide-23
SLIDE 23

replaceEntry

T replaceEntry(int pos, T x)

  • Replaces the entry at position pos of this with x, and

returns the old entry at that position.

  • Aliases: reference x
  • Updates: this
  • Requires:

0 <= pos and pos < |this|

  • Ensures:

this = #this[0, pos) * <x> * #this[pos+1, |#this|) and <replaceEntry> = #this[pos, pos+1)

7 January 2019 OSU CSE 23

slide-24
SLIDE 24

Example

7 January 2019 OSU CSE 24

Code State

si = < 49, 70 > z = –8 w = -584 w = si.replaceEntry(1, z);

slide-25
SLIDE 25

Example

7 January 2019 OSU CSE 25

Code State

si = < 49, 70 > z = –8 w = -584 w = si.replaceEntry(1, z); si = < 49, -8 > z = –8 w = 70

slide-26
SLIDE 26

Example

7 January 2019 OSU CSE 26

Code State

si = < 49, 70 > z = –8 w = -584 w = si.replaceEntryAt(1, z); si = < 49, -8 > z = –8 w = 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.

slide-27
SLIDE 27

Another Example

7 January 2019 OSU CSE 27

Code State

si = < 49, 70 > z = –8 z = si.replaceEntry(1, z);

slide-28
SLIDE 28

Another Example

7 January 2019 OSU CSE 28

Code State

si = < 49, 70 > z = –8 z = si.replaceEntry(1, z); si = < 49, -8 > z = 70

slide-29
SLIDE 29

Another Example

7 January 2019 OSU CSE 29

Code State

si = < 49, 70 > z = –8 z = si.replaceEntry(1, z); si = < 49, -8 > z = 70

This use of the method avoids creating an alias: it swaps z with the entry previously at position 1.

slide-30
SLIDE 30

append

void append(Sequence<T> s)

  • Concatenates (“appends”) s to the end of

this.

  • Updates: this
  • Clears: s
  • Ensures:

this = #this * #s

7 January 2019 OSU CSE 30

slide-31
SLIDE 31

flip

void flip()

  • Reverses (“flips”) this.
  • Updates: this
  • Ensures:

this = rev(#this)

7 January 2019 OSU CSE 31

slide-32
SLIDE 32

insert

void insert(int pos, Sequence<T> s)

  • Inserts s at position pos of this, and clears s.
  • Updates: this
  • Clears: s
  • Requires:

0 <= pos and pos <= |this|

  • Ensures:

this = #this[0, pos) * #s * #this[pos, |#this|)

7 January 2019 OSU CSE 32

slide-33
SLIDE 33

Example

7 January 2019 OSU CSE 33

Code State

si1 = < 8, 6, 92 > si2 = < 1, -7 > si1.insert(2, si2);

slide-34
SLIDE 34

Example

7 January 2019 OSU CSE 34

Code State

si1 = < 8, 6, 92 > si2 = < 1, -7 > si1.insert(2, si2); si1 = < 8, 6, 1, -7, 92 > si2 = < >

slide-35
SLIDE 35

extract

void extract(int pos1, int pos2, Sequence<T> s)

  • Removes the substring of this starting at position pos1 and

ending at position pos2-1, and puts it in s.

  • Updates: this
  • Replaces: s
  • Requires:

0 <= pos1 and pos1 <= pos2 and pos2 <= |this|

  • Ensures:

this = #this[0, pos1) * #this[pos2, |#this|) and s = #this[pos1, pos2)

7 January 2019 OSU CSE 35

slide-36
SLIDE 36

Example

7 January 2019 OSU CSE 36

Code State

si1 = < 8, 6, 92, 27, 0 > si2 = < 1, -7, 562 > si1.extract(1, 3, si2);

slide-37
SLIDE 37

Example

7 January 2019 OSU CSE 37

Code State

si1 = < 8, 6, 92, 27, 0 > si2 = < 1, -7, 562 > si1.extract(1, 3, si2); si1 = < 8, 27, 0 > si2 = < 6, 92 >

slide-38
SLIDE 38

Resources

  • OSU CSE Components API: Sequence

– http://cse.osu.edu/software/common/doc/

7 January 2019 OSU CSE 38