forked from Berkeley-CS61B/skeleton-sp21
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9251a4e
commit 9b67b51
Showing
9 changed files
with
820 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package bstmap; | ||
|
||
import java.util.HashMap; | ||
import java.util.TreeMap; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
|
||
import edu.princeton.cs.algs4.Stopwatch; | ||
|
||
/** | ||
* Performs a timing test on three different set implementations. | ||
* For BSTMap purposes assumes that <K,V> are <String, Integer> pairs. | ||
* | ||
* @author Josh Hug | ||
* @author Brendan Hu | ||
*/ | ||
public class InsertInOrderSpeedTest { | ||
/** | ||
* Requests user input and performs tests of three different set | ||
* implementations. ARGS is unused. | ||
*/ | ||
public static void main(String[] args) throws IOException { | ||
Scanner input = new Scanner(System.in); | ||
|
||
// borrow waitForPositiveInt(Scanner input) from InsertRandomSpeedTest | ||
InsertRandomSpeedTest i = new InsertRandomSpeedTest(); | ||
System.out.println("This program inserts lexicographically " | ||
+ "increasing Strings into Maps as <String, Integer> pairs."); | ||
|
||
String repeat = "y"; | ||
do { | ||
System.out.print("\nEnter # strings to insert into the maps: "); | ||
int N = i.waitForPositiveInt(input); | ||
timeInOrderMap61B(new ULLMap<>(), N); | ||
timeInOrderMap61B(new BSTMap<>(), N); | ||
timeInOrderTreeMap(new TreeMap<>(), N); | ||
timeInOrderHashMap(new HashMap<>(), N); | ||
|
||
System.out.print("Would you like to try more timed-tests? (y/n): "); | ||
repeat = input.nextLine(); | ||
} while (!repeat.equalsIgnoreCase("n") && !repeat.equalsIgnoreCase("no")); | ||
input.close(); | ||
} | ||
|
||
/** | ||
* Returns time needed to put N strings into a Map61B in increasing order. | ||
* makes use of StringUtils.nextString(String s) | ||
*/ | ||
public static double insertInOrder(Map61B<String, Integer> map61B, int N) { | ||
Stopwatch sw = new Stopwatch(); | ||
String s = "cat"; | ||
for (int i = 0; i < N; i++) { | ||
s = StringUtils.nextString(s); | ||
map61B.put(s, new Integer(i)); | ||
} | ||
return sw.elapsedTime(); | ||
} | ||
|
||
/** | ||
* Returns time needed to put N strings into TreeMap in increasing order. | ||
*/ | ||
public static double insertInOrder(TreeMap<String, Integer> ts, int N) { | ||
Stopwatch sw = new Stopwatch(); | ||
String s = "cat"; | ||
for (int i = 0; i < N; i++) { | ||
s = StringUtils.nextString(s); | ||
ts.put(s, new Integer(i)); | ||
} | ||
return sw.elapsedTime(); | ||
} | ||
|
||
public static double insertInOrder(HashMap<String, Integer> ts, int N) { | ||
Stopwatch sw = new Stopwatch(); | ||
String s = "cat"; | ||
for (int i = 0; i < N; i++) { | ||
s = StringUtils.nextString(s); | ||
ts.put(s, new Integer(i)); | ||
} | ||
return sw.elapsedTime(); | ||
} | ||
|
||
/** | ||
* Attempts to insert N in-order strings of length L into map, | ||
* Prints time of the N insert calls, otherwise | ||
* Prints a nice message about the error | ||
*/ | ||
public static void timeInOrderMap61B(Map61B<String, Integer> map, int N) { | ||
try { | ||
double mapTime = insertInOrder(map, N); | ||
System.out.printf(map.getClass() + ": %.2f sec\n", mapTime); | ||
} catch (StackOverflowError e) { | ||
printInfoOnStackOverflow(N); | ||
} catch (RuntimeException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Attempts to insert N in-order strings of length L into TreeMap, | ||
* Prints time of the N insert calls, otherwise | ||
* Prints a nice message about the error | ||
*/ | ||
public static void timeInOrderTreeMap(TreeMap<String, Integer> treeMap, int N) { | ||
try { | ||
double javaTime = insertInOrder(treeMap, N); | ||
System.out.printf("Java's Built-in TreeMap: %.2f sec\n", javaTime); | ||
} catch (StackOverflowError e) { | ||
printInfoOnStackOverflow(N); | ||
} catch (RuntimeException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Attempts to insert N in-order strings of length L into HashMap, | ||
* Prints time of the N insert calls, otherwise | ||
* Prints a nice message about the error | ||
*/ | ||
public static void timeInOrderHashMap(HashMap<String, Integer> hashMap, int N) { | ||
try { | ||
double javaTime = insertInOrder(hashMap, N); | ||
System.out.printf("Java's Built-in HashMap: %.2f sec\n", javaTime); | ||
} catch (StackOverflowError e) { | ||
printInfoOnStackOverflow(N); | ||
} catch (RuntimeException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/* ---------------------- Private methods ---------------------- */ | ||
|
||
/** | ||
* To be called after catching a StackOverflowError | ||
* Prints the error with corresponding N and L | ||
*/ | ||
private static void printInfoOnStackOverflow(int N) { | ||
System.out.println("--Stack Overflow -- couldn't add " | ||
+ N + " strings."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package bstmap; | ||
|
||
import java.util.HashMap; | ||
import java.util.TreeMap; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
import edu.princeton.cs.algs4.Stopwatch; | ||
|
||
/** Performs a timing test on three different set implementations. | ||
* @author Josh Hug | ||
* @author Brendan Hu | ||
*/ | ||
public class InsertRandomSpeedTest { | ||
/** | ||
Requests user input and performs tests of three different set | ||
implementations. ARGS is unused. | ||
*/ | ||
public static void main(String[] args) throws IOException { | ||
Scanner input = new Scanner(System.in); | ||
|
||
System.out.println("This program inserts random " | ||
+ "Strings of length L " | ||
+ "into different types of maps " | ||
+ "as <String, Integer> pairs."); | ||
System.out.print("Please enter desired length of each string: "); | ||
int L = waitForPositiveInt(input); | ||
|
||
String repeat = "y"; | ||
do { | ||
System.out.print("\nEnter # strings to insert into the maps: "); | ||
int N = waitForPositiveInt(input); | ||
timeRandomMap61B(new ULLMap<>(), N, L); | ||
timeRandomMap61B(new BSTMap<>(), N, L); | ||
timeRandomTreeMap(new TreeMap<>(), N, L); | ||
timeRandomHashMap(new HashMap<>(), N, L); | ||
|
||
System.out.print("Would you like to try more timed-tests? (y/n)"); | ||
repeat = input.nextLine(); | ||
} while (!repeat.equalsIgnoreCase("n") && !repeat.equalsIgnoreCase("no")); | ||
input.close(); | ||
} | ||
|
||
/** Returns time needed to put N random strings of length L into the | ||
* Map61B 61bMap. */ | ||
public static double insertRandom(Map61B<String, Integer> map61B, int N, int L) { | ||
Stopwatch sw = new Stopwatch(); | ||
String s = "cat"; | ||
for (int i = 0; i < N; i++) { | ||
s = StringUtils.randomString(L); | ||
map61B.put(s, new Integer(i)); | ||
} | ||
return sw.elapsedTime(); | ||
} | ||
|
||
/** Returns time needed to put N random strings of length L into the | ||
* TreeMap treeMap. */ | ||
public static double insertRandom(TreeMap<String, Integer> treeMap, int N, int L) { | ||
Stopwatch sw = new Stopwatch(); | ||
String s = "cat"; | ||
for (int i = 0; i < N; i++) { | ||
s = StringUtils.randomString(L); | ||
treeMap.put(s, new Integer(i)); | ||
} | ||
return sw.elapsedTime(); | ||
} | ||
|
||
/** Returns time needed to put N random strings of length L into the | ||
* HashMap treeMap. */ | ||
public static double insertRandom(HashMap<String, Integer> treeMap, int N, int L) { | ||
Stopwatch sw = new Stopwatch(); | ||
String s = "cat"; | ||
for (int i = 0; i < N; i++) { | ||
s = StringUtils.randomString(L); | ||
treeMap.put(s, new Integer(i)); | ||
} | ||
return sw.elapsedTime(); | ||
} | ||
|
||
/** | ||
Attempts to insert N random strings of length L into map, | ||
Prints time of the N insert calls, otherwise | ||
Prints a nice message about the error | ||
*/ | ||
public static void timeRandomMap61B(Map61B<String, Integer> map, int N, int L) { | ||
try { | ||
double mapTime = insertRandom(map, N, L); | ||
System.out.printf(map.getClass() + ": %.2f sec\n", mapTime); | ||
} catch (StackOverflowError e) { | ||
printInfoOnStackOverflow(N, L); | ||
} catch (RuntimeException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
Attempts to insert N random strings of length L into a TreeMap | ||
Prints time of the N insert calls, otherwise | ||
Prints a nice message about the error | ||
*/ | ||
public static void timeRandomTreeMap(TreeMap<String, Integer> treeMap, int N, int L) { | ||
try { | ||
double javaTime = insertRandom(treeMap, N, L); | ||
System.out.printf("Java's Built-in TreeMap: %.2f sec\n", javaTime); | ||
} catch (StackOverflowError e) { | ||
printInfoOnStackOverflow(N, L); | ||
} catch (RuntimeException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
Attempts to insert N random strings of length L into a HashMap | ||
Prints time of the N insert calls, otherwise | ||
Prints a nice message about the error | ||
*/ | ||
public static void timeRandomHashMap(HashMap<String, Integer> hashMap, int N, int L) { | ||
try { | ||
double javaTime = insertRandom(hashMap, N, L); | ||
System.out.printf("Java's Built-in HashMap: %.2f sec\n", javaTime); | ||
} catch (StackOverflowError e) { | ||
printInfoOnStackOverflow(N, L); | ||
} catch (RuntimeException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
Waits for the user on other side of Scanner | ||
to enter a positive int, | ||
and outputs that int | ||
*/ | ||
public static int waitForPositiveInt(Scanner input) { | ||
int ret = 0; | ||
do { | ||
while (!input.hasNextInt()) { | ||
errorBadIntegerInput(); | ||
input.next(); | ||
} | ||
ret = input.nextInt(); | ||
input.nextLine(); //consume not taken by nextInt() | ||
} while (ret <= 0); | ||
return ret; | ||
} | ||
/* ------------------------------- Private methods ------------------------------- */ | ||
/** | ||
To be called after catching a StackOverflowError | ||
Prints the error with corresponding N and L | ||
*/ | ||
private static void printInfoOnStackOverflow(int N, int L) { | ||
System.out.println("--Stack Overflow -- couldn't add " + N | ||
+ " strings of length " + L + "."); | ||
} | ||
|
||
/** Prints a nice message for the user on bad input */ | ||
private static void errorBadIntegerInput() { | ||
System.out.print("Please enter a positive integer: "); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package bstmap; | ||
|
||
import java.util.Set; | ||
|
||
/* Your implementation BSTMap should implement this interface. To do so, | ||
* append "implements Map61B<K,V>" to the end of your "public class..." | ||
* declaration, though you can use other formal type parameters if you'd like. | ||
*/ | ||
public interface Map61B<K, V> extends Iterable<K> { | ||
|
||
/** Removes all of the mappings from this map. */ | ||
void clear(); | ||
|
||
/* Returns true if this map contains a mapping for the specified key. */ | ||
boolean containsKey(K key); | ||
|
||
/* Returns the value to which the specified key is mapped, or null if this | ||
* map contains no mapping for the key. | ||
*/ | ||
V get(K key); | ||
|
||
/* Returns the number of key-value mappings in this map. */ | ||
int size(); | ||
|
||
/* Associates the specified value with the specified key in this map. */ | ||
void put(K key, V value); | ||
|
||
/* Returns a Set view of the keys contained in this map. Not required for Lab 7. | ||
* If you don't implement this, throw an UnsupportedOperationException. */ | ||
Set<K> keySet(); | ||
|
||
/* Removes the mapping for the specified key from this map if present. | ||
* Not required for Lab 7. If you don't implement this, throw an | ||
* UnsupportedOperationException. */ | ||
V remove(K key); | ||
|
||
/* Removes the entry for the specified key only if it is currently mapped to | ||
* the specified value. Not required for Lab 7. If you don't implement this, | ||
* throw an UnsupportedOperationException.*/ | ||
V remove(K key, V value); | ||
|
||
} |
Oops, something went wrong.