Queue 7 January 2019 OSU CSE 1 Queue The Queue component family - - PowerPoint PPT Presentation

queue
SMART_READER_LITE
LIVE PREVIEW

Queue 7 January 2019 OSU CSE 1 Queue The Queue component family - - PowerPoint PPT Presentation

Queue 7 January 2019 OSU CSE 1 Queue The Queue component family allows you to manipulate strings of entries of any (arbitrary) type in FIFO (first-in-first-out) order "First" here refers to the temporal order in which


slide-1
SLIDE 1

Queue

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Queue

  • The Queue component family allows you

to manipulate strings of entries of any (arbitrary) type in FIFO (first-in-first-out)

  • rder

– "First" here refers to the temporal order in which entries are put into the string and taken

  • ut of it, not about the left-to-right or right-to-

left order in the string when it is written down

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

Interfaces and Classes

7 January 2019 OSU CSE 3

Queue Queue1L Queue2 implements implements QueueKernel extends Standard extends

slide-4
SLIDE 4

Interfaces and Classes

7 January 2019 OSU CSE 4

Queue Queue1L Queue2 implements implements QueueKernel extends Standard extends Standard has contracts for three methods: clear newInstance transferFrom

slide-5
SLIDE 5

Interfaces and Classes

7 January 2019 OSU CSE 5

Queue Queue1L Queue2 implements implements QueueKernel extends Standard extends QueueKernel has contracts for three methods: enqueue dequeue length

slide-6
SLIDE 6

Interfaces and Classes

7 January 2019 OSU CSE 6

Queue Queue1L Queue2 implements implements QueueKernel extends Standard extends Queue has contracts for six other methods: append flip front replaceFront sort rotate

slide-7
SLIDE 7

Mathematical Model

  • The value of a Queue variable is modeled

as a string of entries of type T

  • Formally:

type Queue is modeled by string of T

7 January 2019 OSU CSE 7

slide-8
SLIDE 8

Generics

  • Note that Queue is a generic type (also

called a parameterized type)

  • The actual type of the entries is selected
  • nly later by the client when the type

Queue is used to declare or instantiate a variable, e.g.:

Queue<Integer> qi = new Queue1L<Integer>();

7 January 2019 OSU CSE 8

slide-9
SLIDE 9

Generics

  • Note that Queue is a generic type (also

called a parameterized type)

  • The actual type of the entries is selected
  • nly later by the client when the type

Queue is used to declare or instantiate a variable, e.g.:

Queue<Integer> qi = new Queue1L<Integer>();

7 January 2019 OSU CSE 9

The formal type parameter was called T; here, the actual type or argument type is Integer.

slide-10
SLIDE 10

Generics

  • Note that Queue is a generic type (also

called a parameterized type)

  • The actual type of the entries is selected
  • nly later by the client when the type

Queue is used to declare or instantiate a variable, e.g.:

Queue<Integer> qi = new Queue1L<Integer>();

7 January 2019 OSU CSE 10

As of Java 7, generic arguments in a constructor call are inferred from the declared type...

slide-11
SLIDE 11

Generics

  • Note that Queue is a generic type (also

called a parameterized type)

  • The actual type of the entries is selected
  • nly later by the client when the type

Queue is used to declare or instantiate a variable, e.g.:

Queue<Integer> qi = new Queue1L<>();

7 January 2019 OSU CSE 11

... so this diamond

  • perator means the same

thing as the constructor with explicit generic arguments.

slide-12
SLIDE 12

Wrapper Types

  • Note the use of Integer here, not int
  • Java demands that generic arguments

must be reference types

  • Each of the primitive types has a

corresponding wrapper type that is a reference type (in part to satisfy this requirement)

7 January 2019 OSU CSE 12

slide-13
SLIDE 13

Wrapper Types

7 January 2019 OSU CSE 13

primitive type wrapper type

boolean Boolean char Character int Integer double Double

slide-14
SLIDE 14

Wrapper Types

  • Each wrapper type is an immutable type
  • There is a constructor from the

corresponding primitive type

  • Java includes features called auto-boxing

and auto-unboxing so wrapper types can be used with primitive-type syntax almost as if they were primitive types

– Details later (for now, look it up if it seems to matter to your code)

7 January 2019 OSU CSE 14

slide-15
SLIDE 15

Constructors

  • There is one constructor for each

implementation class for Queue

  • As always:

– The name of the constructor is the name of the implementation class – The constructor has its own contract (which is in the kernel interface QueueKernel)

7 January 2019 OSU CSE 15

slide-16
SLIDE 16

No-argument Constructor

  • Ensures:

this = < >

7 January 2019 OSU CSE 16

slide-17
SLIDE 17

Example

7 January 2019 OSU CSE 17

Code State

Queue<Integer> qi = new Queue1L<>();

slide-18
SLIDE 18

Example

7 January 2019 OSU CSE 18

Code State

Queue<Integer> qi = new Queue1L<>(); qi = < >

slide-19
SLIDE 19

Methods for Queue

  • All the methods for Queue are instance

methods, i.e., you call them as follows: q.methodName(arguments) where q is an initialized non-null variable

  • f type Queue<T> for some T

7 January 2019 OSU CSE 19

slide-20
SLIDE 20

enqueue

void enqueue(T x)

  • Adds x at the back (right end) of this.
  • Aliases: reference x
  • Updates: this
  • Ensures:

this = #this * <x>

7 January 2019 OSU CSE 20

slide-21
SLIDE 21

enqueue

void enqueue(T x)

  • Adds x at the back (right end) of this.
  • Aliases: reference x
  • Updates: this
  • Ensures:

this = #this * <x>

7 January 2019 OSU CSE 21

The list of references that might be aliases upon return from the method is advertised here, because aliasing is important and

  • therwise is not specified.
slide-22
SLIDE 22

Example

7 January 2019 OSU CSE 22

Code State

qi = < 49, 3 > k = 70 qi.enqueue(k);

slide-23
SLIDE 23

Example

7 January 2019 OSU CSE 23

Code State

qi = < 49, 3 > k = 70 qi.enqueue(k); qi = < 49, 3, 70 > k = 70

slide-24
SLIDE 24

Meaning of “Aliases: ...”

7 January 2019 OSU CSE 24

49 3 70 Before...

slide-25
SLIDE 25

Meaning of “Aliases: ...”

7 January 2019 OSU CSE 25

49 3 70 After...

slide-26
SLIDE 26

Meaning of “Aliases: ...”

  • The tracing table notation with ➞ gives us

no easy way to describe this situation

– The picture is, however, a handy way to see what’s going on, so draw pictures!

  • Since Integer is immutable, there is no

consequence to this case of aliasing

– But consider: Queue<NaturalNumber> qn = ...

7 January 2019 OSU CSE 26

slide-27
SLIDE 27

dequeue

T dequeue()

  • Removes and returns the entry at the front

(left end) of this.

  • Updates: this
  • Requires:

this /= < >

  • Ensures:

#this = <dequeue> * this

7 January 2019 OSU CSE 27

slide-28
SLIDE 28

Example

7 January 2019 OSU CSE 28

Code State

qi = < 49, 3, 70 > k = –584 k = qi.dequeue();

slide-29
SLIDE 29

Example

7 January 2019 OSU CSE 29

Code State

qi = < 49, 3, 70 > k = –584 k = qi.dequeue(); qi = < 3, 70 > k = 49

slide-30
SLIDE 30

length

int length()

  • Reports the length of this.
  • Ensures:

length = |this|

7 January 2019 OSU CSE 30

slide-31
SLIDE 31

Example

7 January 2019 OSU CSE 31

Code State

qi = < 49, 3, 70 > n = –45843 n = qi.length();

slide-32
SLIDE 32

Example

7 January 2019 OSU CSE 32

Code State

qi = < 49, 3, 70 > n = –45843 n = qi.length(); qi = < 49, 3, 70 > n = 3

slide-33
SLIDE 33

front

T front()

  • Returns the entry at the the front (left end)
  • f this.
  • Aliases: reference returned by front
  • Requires:

this /= < >

  • Ensures:

<front> is prefix of this

7 January 2019 OSU CSE 33

slide-34
SLIDE 34

Example

7 January 2019 OSU CSE 34

Code State

qi = < 49, 3, 70 > k = –58 k = qi.front();

slide-35
SLIDE 35

Example

7 January 2019 OSU CSE 35

Code State

qi = < 49, 3, 70 > k = –58 k = qi.front(); qi = < 49, 3, 70 > k = 49

slide-36
SLIDE 36

Meaning of “Aliases: ...”

7 January 2019 OSU CSE 36

49 3 70

  • 58

Before...

slide-37
SLIDE 37

Meaning of “Aliases: ...”

7 January 2019 OSU CSE 37

49 3 70

  • 58

After...

slide-38
SLIDE 38

replaceFront

T replaceFront(T x)

  • Replaces the front of this with x, and returns the old

front.

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

this /= < >

  • Ensures:

<replaceFront> is prefix of #this and this = <x> * #this[1, |#this|)

7 January 2019 OSU CSE 38

slide-39
SLIDE 39

Example

7 January 2019 OSU CSE 39

Code State

qi = < 49, 70 > k = –58 j = 16

k = qi.replaceFront(j);

slide-40
SLIDE 40

Example

7 January 2019 OSU CSE 40

Code State

qi = < 49, 70 > k = –58 j = 16

k = qi.replaceFront(j);

qi = < 16, 70 > k = 49 j = 16

slide-41
SLIDE 41

Meaning of “Aliases: ...”

7 January 2019 OSU CSE 41

49 70

  • 58

Before... 16

slide-42
SLIDE 42

Meaning of “Aliases: ...”

7 January 2019 OSU CSE 42

49 70

  • 58

After... 16

slide-43
SLIDE 43

Another Example

7 January 2019 OSU CSE 43

Code State

qi = < 49, 70 > j = 16

j = qi.replaceFront(j);

slide-44
SLIDE 44

Another Example

7 January 2019 OSU CSE 44

Code State

qi = < 49, 70 > j = 16

j = qi.replaceFront(j);

qi = < 16, 70 > j = 49

slide-45
SLIDE 45

Another Example

7 January 2019 OSU CSE 45

Code State

qi = < 49, 70 > j = 16

j = qi.replaceFront(j);

qi = < 16, 70 > j = 49 This use of the method avoids creating an alias: it swaps j with the entry previously at the front.

slide-46
SLIDE 46

append

void append(Queue<T> q)

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

this.

  • Updates: this
  • Clears: q
  • Ensures:

this = #this * #q

7 January 2019 OSU CSE 46

slide-47
SLIDE 47

Example

7 January 2019 OSU CSE 47

Code State

q1 = < 4, 3, 2 > q2 = < 1, 0 > q1.append(q2);

slide-48
SLIDE 48

Example

7 January 2019 OSU CSE 48

Code State

q1 = < 4, 3, 2 > q2 = < 1, 0 > q1.append(q2); q1 = < 4, 3, 2, 1, 0 > q2 = < >

slide-49
SLIDE 49

flip

void flip()

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

this = rev(#this)

7 January 2019 OSU CSE 49

slide-50
SLIDE 50

Example

7 January 2019 OSU CSE 50

Code State

qi = < 18, 6, 74 > qi.flip();

slide-51
SLIDE 51

Example

7 January 2019 OSU CSE 51

Code State

qi = < 18, 6, 74 > qi.flip(); qi = < 74, 6, 18 >

slide-52
SLIDE 52

sort

void sort(Comparator<T> order)

  • Sorts this according to the ordering provided

by the compare method from order.

  • Updates: this
  • Requires:

[the relation computed by order.compare is a total preorder]

  • Ensures:

this = [#this ordered by the relation computed by order.compare]

7 January 2019 OSU CSE 52

slide-53
SLIDE 53

Comparators

  • The Java interface Comparator<T> is:

public interface Comparator<T> { /** * Returns a negative integer, zero, or * a positive integer as the first * argument is less than, equal to, or * greater than the second. */ int compare(T o1, T o2);

}

7 January 2019 OSU CSE 53

slide-54
SLIDE 54

Comparators

  • The notion of “less than” and “greater

than” is quite flexible

  • The notion of “equal to” is not flexible

– It is based on mathematical equality, not on a flexible notion of being “equivalent to”

  • There are important technicalities...

7 January 2019 OSU CSE 54

slide-55
SLIDE 55

sort

void sort(Comparator<T> order)

  • Sorts this according to the ordering provided

by the compare method from order.

  • Updates: this
  • Requires:

[the relation computed by order.compare is a total preorder]

  • Ensures:

this = [#this ordered by the relation computed by order.compare]

7 January 2019 OSU CSE 55

A total preorder means that any two values of type T are comparable, and that there are no “cycles”, e.g., nothing like a < b < c < a.

slide-56
SLIDE 56

Creating a Comparator

private static class IntegerLT implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { if (o1 < o2) { return -1; } else if (o1 > o2) { return 1; } else { return 0; } } }

7 January 2019 OSU CSE 56

slide-57
SLIDE 57

Creating a Comparator

private static class IntegerLT implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { if (o1 < o2) { return -1; } else if (o1 > o2) { return 1; } else { return 0; } } }

7 January 2019 OSU CSE 57

A class that implements Comparator is usually a nested class (i.e., declared for local use inside another class), and if so should be declared private static.

slide-58
SLIDE 58

An Easy Comparator

private static class IntegerLT implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }

7 January 2019 OSU CSE 58

Since a generic parameter must be a reference type, and each wrapper type T (here, Integer) implements the interface Comparable<T>, each has a compareTo method that can be called like this to simplify the code for compare in a Comparator<T> implementation.

slide-59
SLIDE 59

Example

7 January 2019 OSU CSE 59

Code State

qi = < 8, 6, 92, 1 > Comparator<Integer> ci = new IntegerLT (); qi.sort(ci);

slide-60
SLIDE 60

Example

7 January 2019 OSU CSE 60

Code State

qi = < 8, 6, 92, 1 > Comparator<Integer> ci = new IntegerLT (); qi.sort(ci); qi = < 1, 6, 8, 92 >

slide-61
SLIDE 61

rotate

void rotate(int distance)

  • Rotates this.
  • Updates: this
  • Ensures:

if #this = <> then this = #this else this = #this[distance mod |#this|, |#this|) * #this[0, distance mod |#this|)

7 January 2019 OSU CSE 61

slide-62
SLIDE 62

Example

7 January 2019 OSU CSE 62

Code State

qi = < 8, 6, 92, 3 > qi.rotate(1);

slide-63
SLIDE 63

Example

7 January 2019 OSU CSE 63

Code State

qi = < 8, 6, 92, 3 > qi.rotate(1); qi = < 6, 92, 3, 8 >

slide-64
SLIDE 64

Example

7 January 2019 OSU CSE 64

Code State

qi = < 8, 6, 92, 3 > qi.rotate(3);

slide-65
SLIDE 65

Example

7 January 2019 OSU CSE 65

Code State

qi = < 8, 6, 92, 3 > qi.rotate(3); qi = < 3, 8, 6, 92 >

slide-66
SLIDE 66

Example

7 January 2019 OSU CSE 66

Code State

qi = < 8, 6, 92, 3 > qi.rotate(-1);

slide-67
SLIDE 67

Example

7 January 2019 OSU CSE 67

Code State

qi = < 8, 6, 92, 3 > qi.rotate(-1); qi = < 3, 8, 6, 92 >

slide-68
SLIDE 68

Resources

  • OSU CSE Components API: Queue

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

  • Java Libraries API: Comparator,

Comparable

– http://docs.oracle.com/javase/8/docs/api/

7 January 2019 OSU CSE 68