Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cse332-22au/p2-public
  • cse332-22sp/p2-public
  • cse332-22su/p2-public
  • cse332-23wi/p2-public
  • cse332-23sp/p2-public
  • cse332-23su/p2-public
  • cse332-23au/p2-public
  • cse332-24wi/p2-public
  • yc888/p-2-public-yc-888
  • ajk1004/p2-public
  • gpletosu/p2-public
  • cse332-24sp/p2-public
  • samganyx/p2-public
13 results
Show changes
Commits on Source (25)
Showing
with 157 additions and 119 deletions
......@@ -25,6 +25,7 @@ dependencies {
test {
useJUnitPlatform()
maxHeapSize = "4096m"
}
java {
......
File moved
File moved
File moved
File moved
File moved
File moved
File moved
package chat;
import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
......@@ -72,12 +73,12 @@ public class UMessageServerConnection extends Thread {
try {
cmd = cmd.trim();
switch (cmd) {
case "MAIN":
m_channel(UMessageServerConnection.CHAT_CHANNEL, text);
break;
default:
String[] parts = text.split(" ", 2);
cmd = parts[0].trim();
case "MAIN":
m_channel(UMessageServerConnection.CHAT_CHANNEL, text);
break;
default:
String[] parts = text.split(" ", 2);
cmd = parts[0].trim();
}
} catch (Exception e) {
e.printStackTrace();
......@@ -112,34 +113,52 @@ public class UMessageServerConnection extends Thread {
while ((line = this.in.readLine()) != null) {
if (line.startsWith("PING")) {
write("PONG", line.substring(5));
}
else if (line.startsWith(":umessage")) {
} else if (line.split(" ")[1].trim().equals("353")) {
int code = Integer.parseInt(line.split(" ")[1]);
switch (code) {
// List of users...
case IRCCodes.RplNamReply:
String[] names = line.split(":")[2].split(" ");
this.main.window.addUsers(names);
break;
// List of users...
case IRCCodes.RplNamReply:
String[] names = line.split(":")[2].split(" ");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
main.window.addUsers(names);
}
});
break;
}
}
else {
} else {
String cmd = line.split(" ")[1];
final String[] lineParts = line.split(":");
switch (cmd) {
case "PART":
this.main.window.removeUser(line.split(":")[1].split("!")[0]);
break;
case "JOIN":
this.main.window.addUser(line.split(":")[1].split("!")[0]);
break;
case "PRIVMSG":
if (!line.split(" ")[2].equals(this.username)) {
return;
}
String[] lineParts = line.split(":");
this.main.window.gotMessage(lineParts[1].split("!")[0],
lineParts[2]);
break;
case "QUIT":
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
main.window.removeUser(lineParts[1].split("!")[0]);
}
});
break;
case "JOIN":
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
main.window.addUser(lineParts[1].split("!")[0]);
}
});
break;
case "PRIVMSG":
if (!line.split(" ")[2].equals(this.username)) {
return;
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
main.window.gotMessage(lineParts[1].split("!")[0],
lineParts[2]);
}
});
break;
}
}
}
......
......@@ -18,11 +18,17 @@ import java.util.function.Supplier;
public class uMessage {
private static final int N = 3;
private static final String CORPUS = "eggs.txt";
private static final String CORPUS = "corpus/eggs.txt";
// Use .binarySearchTreeConstructor(); if you want to test things since it is an implementation we provide and is guaranteed to work
// Other examples:
// .trieConstructor(NGram.class);
// .trieConstructor(AlphabeticString.class);
// .hashtableConstructor(NGramTester.avlTreeConstructor());
// .hashtableConstructor(NGramTester.binarySearchTreeConstructor());
private static final Supplier<Dictionary<NGram, Dictionary<AlphabeticString, Integer>>> NEW_OUTER = NGramTester
.trieConstructor(NGram.class);
.binarySearchTreeConstructor();
private static final Supplier<Dictionary<AlphabeticString, Integer>> NEW_INNER = NGramTester
.trieConstructor(AlphabeticString.class);
.binarySearchTreeConstructor();
/*
*
......
......@@ -18,12 +18,12 @@ import cse332.datastructures.trees.BinarySearchTree;
* the references used to access the instance). Such masking will
* lead to highly perplexing and erroneous behavior. Instead,
* continue using the existing BSTNode children array.
* 4. If this class has redundant methods, your score will be heavily
* penalized.
* 5. Cast children array to AVLNode whenever necessary in your
* AVLTree. This will result a lot of casts, so we recommend you make
* private methods that encapsulate those casts.
* 4. Ensure that the class does not have redundant methods
* 5. Cast a BSTNode to an AVLNode whenever necessary in your AVLTree.
* This will result a lot of casts, so we recommend you make private methods
* that encapsulate those casts.
* 6. Do NOT override the toString method. It is used for grading.
* 7. The internal structure of your AVLTree (from this.root to the leaves) must be correct
*/
public class AVLTree<K extends Comparable<? super K>, V> extends BinarySearchTree<K, V> {
......
......@@ -9,23 +9,24 @@ import java.util.Iterator;
import java.util.function.Supplier;
/**
* 1. You must implement a generic chaining hashtable. You may not
* restrict the size of the input domain (i.e., it must accept
* any key) or the number of inputs (i.e., it must grow as necessary).
* 3. Your HashTable should rehash as appropriate (use load factor as
* shown in class!).
* 5. HashTable should be able to resize its capacity to prime numbers for more
* than 200,000 elements. After more than 200,000 elements, it should
* continue to resize using some other mechanism.
* 6. We suggest you hard code some prime numbers. You can use this
* list: http://primes.utm.edu/lists/small/100000.txt
* NOTE: Do NOT copy the whole list!
* 7. When implementing your iterator, you should NOT copy every item to another
* dictionary/list and return that dictionary/list's iterator.
* - You must implement a generic chaining hashtable. You may not
* restrict the size of the input domain (i.e., it must accept
* any key) or the number of inputs (i.e., it must grow as necessary).
*
* - ChainingHashTable should rehash as appropriate (use load factor as shown in lecture!).
*
* - ChainingHashTable must resize its capacity into prime numbers via given PRIME_SIZES list.
* Past this, it should continue to resize using some other mechanism (primes not necessary).
*
* - When implementing your iterator, you should NOT copy every item to another
* dictionary/list and return that dictionary/list's iterator.
*/
public class ChainingHashTable<K, V> extends DeletelessDictionary<K, V> {
private Supplier<Dictionary<K, V>> newChain;
static final int[] PRIME_SIZES =
{11, 23, 47, 97, 193, 389, 773, 1549, 3089, 6173, 12347, 24697, 49393, 98779, 197573, 395147};
public ChainingHashTable(Supplier<Dictionary<K, V>> newChain) {
this.newChain = newChain;
}
......@@ -44,4 +45,13 @@ public class ChainingHashTable<K, V> extends DeletelessDictionary<K, V> {
public Iterator<Item<K, V>> iterator() {
throw new NotYetImplementedException();
}
/**
* Temporary fix so that you can debug on IntelliJ properly despite a broken iterator
* Remove to see proper String representation (inherited from Dictionary)
*/
@Override
public String toString() {
return "ChainingHashTable String representation goes here.";
}
}
......@@ -9,9 +9,9 @@ import java.util.Iterator;
/**
* 1. The list is typically not sorted.
* 2. Add new items to the front of the list.
* 3. Whenever find is called on an item, move it to the front of the
* list. This means you remove the node from its current position
* and make it the first node in the list.
* 3. Whenever find or insert is called on an existing key, move it
* to the front of the list. This means you remove the node from its
* current position and make it the first node in the list.
* 4. You need to implement an iterator. The iterator SHOULD NOT move
* elements to the front. The iterator should return elements in
* the order they are stored in the list, starting with the first
......
......@@ -36,7 +36,7 @@ public class NGramTester {
public static void main(String[] args) {
try {
WordSuggestor suggestions = new WordSuggestor("eggs.txt", 2, -1,
WordSuggestor suggestions = new WordSuggestor("corpus/eggs.txt", 2, -1,
NGramTester.trieConstructor(NGram.class),
NGramTester.trieConstructor(AlphabeticString.class));
System.out.println(suggestions);
......
......@@ -9,6 +9,7 @@ public class TopKSort {
sort(array, k, (x, y) -> x.compareTo(y));
}
public static <E> void sort(E[] array, int k, Comparator<E> comparator) {
throw new NotYetImplementedException();
}
......
......@@ -21,21 +21,19 @@ public class AutocompleteTrie extends HashTrieMap<Character, AlphabeticString, I
}
}
String result = key;
StringBuilder result = new StringBuilder(key);
while (current.pointers.size() == 1) {
if (current.value != null) {
return null;
}
result += current.pointers.keySet().iterator().next();
result.append(current.pointers.keySet().iterator().next());
current = current.pointers.values().iterator().next();
}
// Switch this to return null to only complete if we're at the end of
// the word
if (current.pointers.size() != 0) {
return result;
return result.toString();
}
return result;
return result.toString();
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ public class SpellingCorrector {
this.dictionary = new AutocompleteTrie();
Scanner dict;
try {
dict = new Scanner(new File("dictionary2.txt"));
dict = new Scanner(new File("./corpus/dictionary2.txt"));
} catch (FileNotFoundException e) {
throw new RuntimeException(
"Couldn't read dictionary file for spelling correction and autocompletion.");
......
......@@ -7,6 +7,8 @@ import cse332.misc.LargeValueFirstItemComparator;
import cse332.sorts.InsertionSort;
import cse332.types.AlphabeticString;
import cse332.types.NGram;
import p2.sorts.QuickSort;
import p2.sorts.TopKSort;
import java.util.Comparator;
import java.util.Iterator;
......@@ -73,10 +75,16 @@ public class NGramToNextChoicesMap {
Comparator<Item<String, Integer>> comp = new LargeValueFirstItemComparator<String, Integer>();
if (k < 0) {
InsertionSort.sort(afterNGrams, comp);
QuickSort.sort(afterNGrams, comp);
} else {
// You must fix this line toward the end of the project
throw new NotYetImplementedException();
Comparator<Item<String, Integer>> revComp = (o1, o2) -> comp.compare(o2, o1);
TopKSort.sort(afterNGrams, k, revComp);
int cutoff = Math.min(k, afterNGrams.length);
for (int i = 0; i < cutoff / 2; i++) {
Item<String, Integer> temp = afterNGrams[i];
afterNGrams[i] = afterNGrams[cutoff - i - 1];
afterNGrams[cutoff - i - 1] = temp;
}
}
String[] nextWords = new String[k < 0 ? afterNGrams.length : k];
......
package ckpt2;
package provided;
import cse332.datastructures.containers.Item;
import cse332.datastructures.trees.BinarySearchTree.BSTNode;
......@@ -15,11 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
public class AVLTreeTests {
private AVLTree<String, Integer> init() {
return new AVLTree<>();
}
private <E extends Comparable<E>> void incCount(Dictionary<E, Integer> tree, E key) {
private <E extends Comparable<E>> void incrementValueWithKey(Dictionary<E, Integer> tree, E key) {
Integer value = tree.find(key);
if (value == null) {
tree.insert(key, 1);
......@@ -31,57 +27,52 @@ public class AVLTreeTests {
@SuppressWarnings("rawtypes")
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void checkStructure() {
public void test_insertFind_severalElements_correctStructure() {
AVLTree<Integer, Integer> tree = new AVLTree<>();
incCount(tree, 10);
incCount(tree, 14);
incCount(tree, 10);
incCount(tree, 31);
incCount(tree, 10);
incCount(tree, 13);
incCount(tree, 10);
incCount(tree, 10);
incCount(tree, 12);
incCount(tree, 10);
incCount(tree, 13);
incCount(tree, 10);
incCount(tree, 10);
incCount(tree, 11);
incCount(tree, 10);
incCount(tree, 14);
incCount(tree, 9);
incCount(tree, 8);
incCount(tree, 7);
incCount(tree, 6);
incCount(tree, 5);
incCount(tree, 4);
incCount(tree, 3);
incCount(tree, 2);
incCount(tree, 1);
incCount(tree, 0);
// {10, 14, 10, 31, 10, 13, 10, 10, 12, 10, 13, 10, 10, 11, 10, 14, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
// {10, 14, 31, 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
// {10, 14, 10, 31, 10, 13, 10, 10, 12, 10, 13, 10, 10, 11, 10, 14, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 14);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 31);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 13);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 12);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 13);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 11);
// {10, 14, 31, 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
incrementValueWithKey(tree, 10);
incrementValueWithKey(tree, 14);
incrementValueWithKey(tree, 9);
incrementValueWithKey(tree, 8);
incrementValueWithKey(tree, 7);
incrementValueWithKey(tree, 6);
incrementValueWithKey(tree, 5);
incrementValueWithKey(tree, 4);
incrementValueWithKey(tree, 3);
incrementValueWithKey(tree, 2);
incrementValueWithKey(tree, 1);
incrementValueWithKey(tree, 0);
BSTNode root = getField(tree, "root");
String trueData = " [8 [4 [2 [1 [0..].] [3..]] [6 [5..] [7..]]] [12 [10 [9..] [11..]] [14 [13..] [31..]]]]";
String trueCounts = " [1 [1 [1 [1 [1..].] [1..]] [1 [1..] [1..]]] [1 [9 [1..] [1..]] [2 [2..] [1..]]]]";
// String trueData = " [10 [6 [2 [1 [0..].] [4 [3..] [5..]]] [8 [7..] [9..]]] [13 [12 [11..].] [14. [31..]]]]";
// String trueCounts = " [9 [1 [1 [1 [1..].] [1 [1..] [1..]]] [1 [1..] [1..]]] [2 [1 [1..].] [2. [1..]]]]";
// System.err.println(nestd(root));
// System.err.println(trueData);
assertEquals(trueData, nestd(root));
assertEquals(trueCounts, nestc(root));
}
@SuppressWarnings("rawtypes")
public String nestd(BSTNode root) {
if(root == null)
return ".";
return " [" + root.key + nestd(root.children[0]) + nestd(root.children[1]) + "]";
}
@SuppressWarnings("rawtypes")
public String nestc(BSTNode root) {
if(root == null)
return ".";
......@@ -90,13 +81,13 @@ public class AVLTreeTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testTreeWith5Items() {
AVLTree<String, Integer> tree = init();
public void test_insertFind_fewElements_correctStructure() {
AVLTree<String, Integer> tree = new AVLTree<>();
String[] tests_struct = { "a", "b", "c", "d", "e" };
String[] tests = { "b", "d", "e", "c", "a" };
for (int i = 0; i < 5; i++) {
String str = tests[i] + "a";
incCount(tree, str);
incrementValueWithKey(tree, str);
}
int i = 0;
......@@ -110,8 +101,8 @@ public class AVLTreeTests {
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHugeTree() {
AVLTree<String, Integer> tree = init();
public void test_insertFind_manyElements_correctStructure() {
AVLTree<String, Integer> tree = new AVLTree<>();
int n = 1000;
// Add them
......@@ -119,7 +110,7 @@ public class AVLTreeTests {
int k = (i % n) * 37 % n;
String str = String.format("%05d", k);
for (int j = 0; j < k + 1; j ++)
incCount(tree, str);
incrementValueWithKey(tree, str);
}
// Calculate count of all values in tree
......
package ckpt2;
package provided;
import cse332.datastructures.containers.Item;
import cse332.datastructures.trees.BinarySearchTree;
import cse332.interfaces.misc.Dictionary;
import datastructures.dictionaries.ChainingHashTable;
import datastructures.dictionaries.MoveToFrontList;
......@@ -12,21 +13,24 @@ import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class HashTableTests {
public class ChainingHashTableTests {
private void incCount(Dictionary<String, Integer> list, String key) {
private void incrementValueWithKey(Dictionary<String, Integer> list, String key) {
Integer find = list.find(key);
if (find == null)
list.insert(key, 1);
else
list.insert(key, 1 + find);
list.insert(key, find + 1);
}
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHugeHashTable() {
ChainingHashTable<String, Integer> list = new ChainingHashTable<>(MoveToFrontList::new);
public void test_insertFind_manyElements_correctStructure() {
/*
Replace BinarySearchTree with your own Dictionary implementations like MoveToFrontList or AVLTree
to test them as chains for the ChainingHashTable (highly recommended to find potential bugs)
* */
ChainingHashTable<String, Integer> list = new ChainingHashTable<>(BinarySearchTree::new);
int n = 1000;
// Add them
......@@ -34,7 +38,7 @@ public class HashTableTests {
int k = (i % n) * 37 % n;
String str = String.format("%05d", k);
for (int j = 0; j < k + 1; j ++)
incCount(list, str);
incrementValueWithKey(list, str);
}
// Delete them all
......