Description
You will modify your implementation of the FMap<K,V> ADT that was specified in assignment 8 to make it efficient even in the worst case.
Collaboration between students is forbidden on this assignment. You are responsible for keeping your code hidden from all other students. Turn in your work on this assignment before 11:59 pm on the due date by following instructions on the course’s main assignments web page, http://www.ccs.neu.edu/course/cs3500sp13/Assignments.html.
Your file of Java code should begin with a block comment that lists
1. Your name, as you want the instructor to write it.
2. Your email address.
3. Any remarks that you wish to make to the instructor.
Part of your grade will depend on the quality, correctness, and efficiency of your code, part will depend on your adherence to object-oriented programming style and idioms as taught in this course, part will depend on the readability of your code (comments and indentation), and part will depend on how well you follow the procedure above for submitting your work. Assignments submitted between 12:00 am and 11:59 pm the day after the due date will receive a 20 percentage point penalty on the assignment.
—————————————————————————————————————————
Your assignment is to write the code for a single file, FMap.java, that implements the specification below as well as the specification of assignments 4, 5, 7, and 8.
You will be given two files in /course/cs3500sp13/Assignments/A9:
Visitor.java
TestFMap.java
The Visitor.java file defines the Visitor<K,V> interface. The TestFMap.java file contains a simple test program.
—————————————————————————————————————————
Specification of the FMap<K,V> ADT.
The FMap<K,V> ADT remains as specified in assignment 8, but several of its operations are now required to be efficient even in the worst case.
Performance requirements:
Suppose c is a comparator that runs in O(1) time, m is an FMap<K,V> that has been created by adding random key-value pairs to FMap.empty(c), iter is an iterator obtained by evaluating m.iterator(), n is m.size(), and v is a Visitor<K,V> such that v.visit(K,V) runs in constant time. Then
m.put(k,v) should run in O(lg n) time
m.isEmpty() should run in O(1) time
m.size() should run in O(1) time
m.containsKey(k) should run in O(lg n) time
m.containsValue(k) should run in O(n) time
m.get(k) should run in O(lg n) time
m.iterator() should run in O(n) time
iter.hasNext() should run in O(1) time
iter.next() should run in O(1) time
m.accept(v) should run in O(n*lg n) time
where all of those times are for the worst case.
—————————————————————————————————————————
Hint: The most reasonable way to achieve that worst-case efficiency is to implement (functional) red-black trees. (The assigned reading by Okasaki is a great resource for this implementation.)
Hint: Correct programs that achieve the above efficiency for the average case but not for the worst case are likely to earn substantially more partial credit than programs that try to achieve the above efficiency for the worst case but don’t actually work.
Hint: Correct programs that don’t even try to be efficient are likely to earn substantially more partial credit than programs that try to be efficient but don’t actually work.
—————————————————————————————————————————
The specification of hashCode() from assignment 4 is strengthened as follows.
If m1 and m2 are values of the FMap ADT, and
m1.equals(m2)
then m1.hashCode() == m2.hashCode().
If m1 and m2 are values of the FMap ADT, and
! (m1.equals(m2))
then m1.hashCode() is unlikely to be equal to m2.hashCode().
Note: The word “unlikely” will be interpreted as follows. For every type K and V, if both m1 and m2 are selected at random from a set of FMap<K,V> values such that for every non-negative integer n and int value h the probability of a randomly selected FMap m having
n == m.size() is
P(n) = 1/(2^(n+1))
and for each key k such that m.containsKey(k) the probability that
h == k.hashCode() is at most 1/5
and for each value v such that v.equals(m.get(k)) the probability that
h == v.hashCode() is at most 1/5
and the three probabilities above are independent
then the probability of m1.hashCode() == m2.hashCode() when m1 and m2 are not equal is less than 2%.