queue
play

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


  1. Queue 7 January 2019 OSU CSE 1

  2. 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 entries are put into the string and taken out 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

  3. Interfaces and Classes Standard extends QueueKernel extends Queue implements implements Queue1L Queue2 7 January 2019 OSU CSE 3

  4. Interfaces and Classes Standard extends Standard has contracts QueueKernel for three methods: clear newInstance extends transferFrom Queue implements implements Queue1L Queue2 7 January 2019 OSU CSE 4

  5. Interfaces and Classes Standard extends QueueKernel extends QueueKernel has Queue contracts for three methods: enqueue implements implements dequeue length Queue1L Queue2 7 January 2019 OSU CSE 5

  6. Interfaces and Classes Queue has contracts for six other Standard methods: append flip extends front replaceFront QueueKernel sort rotate extends Queue implements implements Queue1L Queue2 7 January 2019 OSU CSE 6

  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

  8. Generics • Note that Queue is a generic type (also called a parameterized type ) • The actual type of the entries is selected only 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

  9. Generics • Note that Queue is a generic type (also The formal type parameter was called T ; called a parameterized type ) here, the actual type or • The actual type of the entries is selected argument type is Integer . only 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

  10. Generics • Note that Queue is a generic type (also As of Java 7, generic called a parameterized type ) arguments in a constructor call are inferred from the • The actual type of the entries is selected declared type... only 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

  11. Generics • Note that Queue is a generic type (also ... so this diamond operator means the same called a parameterized type ) thing as the constructor • The actual type of the entries is selected with explicit generic arguments. only 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

  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

  13. Wrapper Types primitive type wrapper type boolean Boolean char Character int Integer double Double 7 January 2019 OSU CSE 13

  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

  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

  16. No-argument Constructor • Ensures: this = < > 7 January 2019 OSU CSE 16

  17. Example Code State Queue<Integer> qi = new Queue1L<>(); 7 January 2019 OSU CSE 17

  18. Example Code State Queue<Integer> qi = new Queue1L<>(); qi = < > 7 January 2019 OSU CSE 18

  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 of type Queue<T> for some T 7 January 2019 OSU CSE 19

  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

  21. enqueue void enqueue(T x) • Adds x at the back (right end) of this . • Aliases: reference x • Updates: this The list of references that might be aliases upon return from the • Ensures: method is advertised here, this = # this * <x> because aliasing is important and otherwise is not specified. 7 January 2019 OSU CSE 21

  22. Example Code State qi = < 49, 3 > k = 70 qi.enqueue(k); 7 January 2019 OSU CSE 22

  23. Example Code State qi = < 49, 3 > k = 70 qi.enqueue(k); qi = < 49, 3, 70 > k = 70 7 January 2019 OSU CSE 23

  24. Meaning of “Aliases: ...” Before... 49 3 70 7 January 2019 OSU CSE 24

  25. Meaning of “Aliases: ...” After... 49 3 70 7 January 2019 OSU CSE 25

  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

  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

  28. Example Code State qi = < 49, 3, 70 > k = –584 k = qi.dequeue(); 7 January 2019 OSU CSE 28

  29. Example Code State qi = < 49, 3, 70 > k = –584 k = qi.dequeue(); qi = < 3, 70 > k = 49 7 January 2019 OSU CSE 29

  30. length int length() • Reports the length of this . • Ensures: length = | this | 7 January 2019 OSU CSE 30

  31. Example Code State qi = < 49, 3, 70 > n = –45843 n = qi.length(); 7 January 2019 OSU CSE 31

  32. Example Code State qi = < 49, 3, 70 > n = –45843 n = qi.length(); qi = < 49, 3, 70 > n = 3 7 January 2019 OSU CSE 32

  33. front T front() • Returns the entry at the the front (left end) of this . • Aliases: reference returned by front • Requires: this /= < > • Ensures: <front> is prefix of this 7 January 2019 OSU CSE 33

  34. Example Code State qi = < 49, 3, 70 > k = –58 k = qi.front(); 7 January 2019 OSU CSE 34

  35. Example Code State qi = < 49, 3, 70 > k = –58 k = qi.front(); qi = < 49, 3, 70 > k = 49 7 January 2019 OSU CSE 35

  36. Meaning of “Aliases: ...” Before... -58 49 3 70 7 January 2019 OSU CSE 36

  37. Meaning of “Aliases: ...” After... -58 49 3 70 7 January 2019 OSU CSE 37

  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

  39. Example Code State qi = < 49, 70 > k = –58 j = 16 k = qi.replaceFront(j); 7 January 2019 OSU CSE 39

  40. Example Code State qi = < 49, 70 > k = –58 j = 16 k = qi.replaceFront(j); qi = < 16, 70 > k = 49 j = 16 7 January 2019 OSU CSE 40

  41. Meaning of “Aliases: ...” Before... -58 16 49 70 7 January 2019 OSU CSE 41

  42. Meaning of “Aliases: ...” After... -58 16 49 70 7 January 2019 OSU CSE 42

  43. Another Example Code State qi = < 49, 70 > j = 16 j = qi.replaceFront(j); 7 January 2019 OSU CSE 43

  44. Another Example Code State qi = < 49, 70 > j = 16 j = qi.replaceFront(j); qi = < 16, 70 > j = 49 7 January 2019 OSU CSE 44

  45. Another Example This use of the method Code State avoids creating an alias: it swaps j with the entry qi = < 49, 70 > previously at the front. j = 16 j = qi.replaceFront(j); qi = < 16, 70 > j = 49 7 January 2019 OSU CSE 45

  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

  47. Example Code State q1 = < 4, 3, 2 > q2 = < 1, 0 > q1.append(q2); 7 January 2019 OSU CSE 47

  48. Example Code State q1 = < 4, 3, 2 > q2 = < 1, 0 > q1.append(q2); q1 = < 4, 3, 2, 1, 0 > q2 = < > 7 January 2019 OSU CSE 48

  49. flip void flip() • Reverses (“flips”) this . • Updates: this • Ensures: this = rev (# this ) 7 January 2019 OSU CSE 49

  50. Example Code State qi = < 18, 6, 74 > qi.flip(); 7 January 2019 OSU CSE 50

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend