Set4 15 September 2020 OSU CSE 1 Documenting Set4 (and Map4 ) By - - PowerPoint PPT Presentation

set4
SMART_READER_LITE
LIVE PREVIEW

Set4 15 September 2020 OSU CSE 1 Documenting Set4 (and Map4 ) By - - PowerPoint PPT Presentation

Set4 15 September 2020 OSU CSE 1 Documenting Set4 (and Map4 ) By now you understand hashing and its benefits. But the algorithms we are using are not trivial, and make a lot of assumptions about the representation. How can we


slide-1
SLIDE 1

Set4

15 September 2020 OSU CSE 1

slide-2
SLIDE 2

Documenting Set4 (and Map4)

  • By now you understand hashing and its

benefits.

  • But the algorithms we are using are not

trivial, and make a lot of assumptions about the representation.

  • How can we document those assumptions

in our code?

15 September 2020 OSU CSE 2

slide-3
SLIDE 3

The Representation

15 September 2020 OSU CSE 3

/** * Buckets for hashing. */ private Set<T>[] hashTable; /** * Total size of abstract this. */ private int size;

slide-4
SLIDE 4

The Correspondence of Set4

@correspondence this = union i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (s)

15 September 2020 OSU CSE 4

slide-5
SLIDE 5

The Correspondence of Set4

@correspondence this = union i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (s)

15 September 2020 OSU CSE 5

The prefixed union introduces a universal

  • quantifier. This is the union
  • ver all possible values for

i and s

slide-6
SLIDE 6

The Correspondence of Set4

@correspondence this = union i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (s)

15 September 2020 OSU CSE 6

What does the where clause tell us about the values of i and s that are of interest to us?

slide-7
SLIDE 7

The Correspondence of Set4

@correspondence this = union i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (s)

15 September 2020 OSU CSE 7

From the first two parts of the where clause we can see that i has to be a valid index of our hash table

slide-8
SLIDE 8

The Correspondence of Set4

@correspondence this = union i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (s)

15 September 2020 OSU CSE 8

The third part of the where clause tells is that s has to be the set at position i of

  • ur hash table
slide-9
SLIDE 9

The Correspondence of Set4

@correspondence this = union i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (s)

15 September 2020 OSU CSE 9

And the abstract value of this is the union of all such sets s

slide-10
SLIDE 10

The Correspondence of Set4

@correspondence this = union i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (s)

15 September 2020 OSU CSE 10

Thus the value of this is the union of all the sets in

  • ur hash table
slide-11
SLIDE 11

The Convention of Set4

@convention |$this.hashTable| > 0 and for all i: integer, s: finite set of T, x: T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1) and x is in s) ([computed result of x.hashCode()] mod |$this.hashTable| = i)) and for all i: integer where (0 <= i and i < |$this.hashTable|) ([entry at position i in $this.hashTable is not null]) and $this.size = sum i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (|s|)

15 September 2020 OSU CSE 11

slide-12
SLIDE 12

The Convention of Set4

@convention |$this.hashTable| > 0 and for all i: integer, s: finite set of T, x: T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1) and x is in s) ([computed result of x.hashCode()] mod |$this.hashTable| = i)) and for all i: integer where (0 <= i and i < |$this.hashTable|) ([entry at position i in $this.hashTable is not null]) and $this.size = sum i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (|s|)

15 September 2020 OSU CSE 12

Perhaps it is better if we break this into parts…

slide-13
SLIDE 13

Set4’s Convention (1 of 4)

|$this.hashTable| > 0 and …

15 September 2020 OSU CSE 13

slide-14
SLIDE 14

Set4’s Convention (1 of 4)

|$this.hashTable| > 0 and …

15 September 2020 OSU CSE 14

Surprisingly, Java arrays can have zero elements, but we want to have buckets in our hash table…

slide-15
SLIDE 15

Set4’s Convention (2 of 4)

… and for all i: integer, s: finite set of T, x: T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1) and x is in s) ([computed result of x.hashCode()] mod |$this.hashTable| = i)) and …

15 September 2020 OSU CSE 15

slide-16
SLIDE 16

Set4’s Convention (2 of 4)

… and for all i: integer, s: finite set of T, x: T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1) and x is in s) ([computed result of x.hashCode()] mod |$this.hashTable| = i)) and …

15 September 2020 OSU CSE 16

The first three parts of the where clause look familiar… What values of i and s are we interested in?

slide-17
SLIDE 17

Set4’s Convention (2 of 4)

… and for all i: integer, s: finite set of T, x: T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1) and x is in s) ([computed result of x.hashCode()] mod |$this.hashTable| = i)) and …

15 September 2020 OSU CSE 17

What about the values of x?

slide-18
SLIDE 18

Set4’s Convention (2 of 4)

… and for all i: integer, s: finite set of T, x: T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1) and x is in s) ([computed result of x.hashCode()] mod |$this.hashTable| = i)) and …

15 September 2020 OSU CSE 18

This tells us that if x is in a set in our hash table, then that set is at a specific position in our hash table...

slide-19
SLIDE 19

Set4’s Convention (2 of 4)

… and for all i: integer, s: finite set of T, x: T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1) and x is in s) ([computed result of x.hashCode()] mod |$this.hashTable| = i)) and …

15 September 2020 OSU CSE 19

In other words, this tells us that we are hashing the entries in our set!

slide-20
SLIDE 20

Set4’s Convention (3 of 4)

… and for all i: integer where (0 <= i and i < |$this.hashTable|) ([entry at position i in $this.hashTable is not null]) and …

15 September 2020 OSU CSE 20

slide-21
SLIDE 21

Set4’s Convention (3 of 4)

… and for all i: integer where (0 <= i and i < |$this.hashTable|) ([entry at position i in $this.hashTable is not null]) and …

15 September 2020 OSU CSE 21

What values of i are we interested in?

slide-22
SLIDE 22

Set4’s Convention (3 of 4)

… and for all i: integer where (0 <= i and i < |$this.hashTable|) ([entry at position i in $this.hashTable is not null]) and …

15 September 2020 OSU CSE 22

And for those values of i what must be true?

slide-23
SLIDE 23

Set4’s Convention (3 of 4)

… and for all i: integer where (0 <= i and i < |$this.hashTable|) ([entry at position i in $this.hashTable is not null]) and …

15 September 2020 OSU CSE 23

In other words, this tells us that all entries in the array must have been initialized

slide-24
SLIDE 24

Set4’s Convention (4 of 4)

… and $this.size = sum i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (|s|)

15 September 2020 OSU CSE 24

slide-25
SLIDE 25

Set4’s Convention (4 of 4)

… and $this.size = sum i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (|s|)

15 September 2020 OSU CSE 25

The prefixed sum introduces a universal

  • quantifier. This is the sum
  • ver all possible values for

i and s

slide-26
SLIDE 26

Set4’s Convention (4 of 4)

… and $this.size = sum i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (|s|)

15 September 2020 OSU CSE 26

The where clause looks familiar… What values of i and s are we interested in?

slide-27
SLIDE 27

Set4’s Convention (4 of 4)

… and $this.size = sum i: integer, s: finite set of T where (0 <= i and i < |$this.hashTable| and <s> = $this.hashTable[i, i+1)) (|s|)

15 September 2020 OSU CSE 27

What does this tell us about the value of $this.size with respect to the value of this?