Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting...
Transcript of Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting...
Set4
15 September 2016 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 document those assumptions in our code?
15 September 2016 OSU CSE 2
The correspondence of Set4@correspondencethis =
union i: integer, s: finite set of T
where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))
(s)
15 September 2016 OSU CSE 3
The correspondence of Set4@correspondencethis =
union i: integer, s: finite set of T
where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))
(s)
15 September 2016 OSU CSE 4
The prefixed union introduces a universal
quantifier. This is the union over all possible values for
i and s
The correspondence of Set4@correspondencethis =
union i: integer, s: finite set of T
where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))
(s)
15 September 2016 OSU CSE 5
What does the whereclause tells us about the
values of i and sthat are of interest to us?
The correspondence of Set4@correspondencethis =
union i: integer, s: finite set of T
where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))
(s)
15 September 2016 OSU CSE 6
From the first two parts of the where clause we can
see that i has to be a valid index of our hash table
The correspondence of Set4@correspondencethis =
union i: integer, s: finite set of T
where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))
(s)
15 September 2016 OSU CSE 7
The third part of the whereclause tells is that s has to be the set at position i of
our hash table.
The correspondence of Set4@correspondencethis =
union i: integer, s: finite set of T
where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))
(s)
15 September 2016 OSU CSE 8
And the abstract value of this is the union of all
such sets s.
The correspondence of Set4@correspondencethis =
union i: integer, s: finite set of T
where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))
(s)
15 September 2016 OSU CSE 9
Thus the value of this is the union of all the sets in
our hash table.
The convention of Set4@convention|$this.hashTable.entries| > 0 andfor all i: integer, s: finite set of T, x: T
where (0 <= i and i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)] andx is in s)
([computed result of x.hashCode()] mod|$this.hashTable.entries| = i)) and
|$this.hashTable.examinableIndices| =|$this.hashTable.entries| and
$this.size = sum i: integer, s: finite set of Twhere (0 <= i and i < |$this.hashTable.entries| and
<s> = $this.hashTable.entries[i, i+1))(|s|)
15 September 2016 OSU CSE 10
The convention of Set4@convention|$this.hashTable.entries| > 0 andfor all i: integer, s: finite set of T, x: T
where (0 <= i and i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)] andx is in s)
([computed result of x.hashCode()] mod|$this.hashTable.entries| = i)) and
|$this.hashTable.examinableIndices| =|$this.hashTable.entries| and
$this.size = sum i: integer, s: finite set of Twhere (0 <= i and i < |$this.hashTable.entries| and
<s> = $this.hashTable.entries[i, i+1))(|s|)
15 September 2016 OSU CSE 11
Perhaps it will be better if we break this into parts…
The convention of Set4@convention|$this.hashTable.entries| > 0 andfor all i: integer, s: finite set of T, x: T
where (0 <= i and i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)] andx is in s)
([computed result of x.hashCode()] mod|$this.hashTable.entries| = i)) and
|$this.hashTable.examinableIndices| =|$this.hashTable.entries| and
$this.size = sum i: integer, s: finite set of Twhere (0 <= i and i < |$this.hashTable.entries| and
<s> = $this.hashTable.entries[i, i+1))(|s|)
15 September 2016 OSU CSE 12
Set4’s convention (1 of 4)|$this.hashTable.entries| > 0 and …
15 September 2016 OSU CSE 13
Surprisingly Arrays can have zero elements in
Java, but we want to have buckets in our hash
table…
Set4’s convention (2 of 4)… andfor all i: integer,
s: finite set of T,
x: T
where (0 <= i andi < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)]and x is in s)([computed result of x.hashCode()] mod
|$this.hashTable.entries| = i)) and …
15 September 2016 OSU CSE 14
The first three parts of the where clause look
familiar… What values of i and s are we interested in?
Set4’s convention (2 of 4)… andfor all i: integer,
s: finite set of T,
x: T
where (0 <= i andi < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)]and x is in s)([computed result of x.hashCode()] mod
|$this.hashTable.entries| = i)) and …
15 September 2016 OSU CSE 15
What about the values of x?
Set4’s convention (2 of 4)… andfor all i: integer,
s: finite set of T,
x: T
where (0 <= i andi < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)]and x is in s)([computed result of x.hashCode()] mod
|$this.hashTable.entries| = i)) and …
15 September 2016 OSU CSE 16
This tells us that if xis in a set in our
hash table, then that set is at a specific
position in our hash table...
Set4’s convention (2 of 4)… andfor all i: integer,
s: finite set of T,
x: T
where (0 <= i andi < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)]and x is in s)([computed result of x.hashCode()] mod
|$this.hashTable.entries| = i)) and …
15 September 2016 OSU CSE 17
In other words, this tells us that we are hashing the entries in our set!
Set4’s convention (3 of 4)… and|$this.hashTable.examinableIndices| =
|$this.hashTable.entries| and …
15 September 2016 OSU CSE 18
Set4’s convention (3 of 4)… and|$this.hashTable.examinableIndices| =
|$this.hashTable.entries| and …
15 September 2016 OSU CSE 19
If this is true, can there be a non-examinable
index in our hash table?
Set4’s convention (4 of 4)… and$this.size =
sum i: integer, s: finite set of Twhere (0 <= i and
i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1))
(|s|)
15 September 2016 OSU CSE 20
Set4’s convention (4 of 4)… and$this.size =
sum i: integer, s: finite set of Twhere (0 <= i and
i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1))
(|s|)
15 September 2016 OSU CSE 21
The prefixed sum introduces a universal
quantifier. This is the sum over all possible values for
i and s
Set4’s convention (4 of 4)… and$this.size =
sum i: integer, s: finite set of Twhere (0 <= i and
i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1))
(|s|)
15 September 2016 OSU CSE 22
The where clause looks familiar… What values of i and s are we interested
in?
Set4’s convention (4 of 4)… and$this.size =
sum i: integer, s: finite set of Twhere (0 <= i and
i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1))
(|s|)
15 September 2016 OSU CSE 23
What does this tell us about the value of sizewith respect to the value
of this?