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 (24)
Showing
with 161 additions and 70 deletions
...@@ -25,6 +25,7 @@ dependencies { ...@@ -25,6 +25,7 @@ dependencies {
test { test {
useJUnitPlatform() useJUnitPlatform()
maxHeapSize = "4096m"
} }
java { java {
......
package chat; package chat;
import javax.swing.*;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
...@@ -72,12 +73,12 @@ public class UMessageServerConnection extends Thread { ...@@ -72,12 +73,12 @@ public class UMessageServerConnection extends Thread {
try { try {
cmd = cmd.trim(); cmd = cmd.trim();
switch (cmd) { switch (cmd) {
case "MAIN": case "MAIN":
m_channel(UMessageServerConnection.CHAT_CHANNEL, text); m_channel(UMessageServerConnection.CHAT_CHANNEL, text);
break; break;
default: default:
String[] parts = text.split(" ", 2); String[] parts = text.split(" ", 2);
cmd = parts[0].trim(); cmd = parts[0].trim();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -112,34 +113,52 @@ public class UMessageServerConnection extends Thread { ...@@ -112,34 +113,52 @@ public class UMessageServerConnection extends Thread {
while ((line = this.in.readLine()) != null) { while ((line = this.in.readLine()) != null) {
if (line.startsWith("PING")) { if (line.startsWith("PING")) {
write("PONG", line.substring(5)); write("PONG", line.substring(5));
} } else if (line.split(" ")[1].trim().equals("353")) {
else if (line.startsWith(":umessage")) {
int code = Integer.parseInt(line.split(" ")[1]); int code = Integer.parseInt(line.split(" ")[1]);
switch (code) { switch (code) {
// List of users... // List of users...
case IRCCodes.RplNamReply: case IRCCodes.RplNamReply:
String[] names = line.split(":")[2].split(" "); String[] names = line.split(":")[2].split(" ");
this.main.window.addUsers(names); SwingUtilities.invokeLater(new Runnable() {
break; @Override
public void run() {
main.window.addUsers(names);
}
});
break;
} }
} } else {
else {
String cmd = line.split(" ")[1]; String cmd = line.split(" ")[1];
final String[] lineParts = line.split(":");
switch (cmd) { switch (cmd) {
case "PART": case "QUIT":
this.main.window.removeUser(line.split(":")[1].split("!")[0]); SwingUtilities.invokeLater(new Runnable() {
break; @Override
case "JOIN": public void run() {
this.main.window.addUser(line.split(":")[1].split("!")[0]); main.window.removeUser(lineParts[1].split("!")[0]);
break; }
case "PRIVMSG": });
if (!line.split(" ")[2].equals(this.username)) { break;
return; case "JOIN":
} SwingUtilities.invokeLater(new Runnable() {
String[] lineParts = line.split(":"); @Override
this.main.window.gotMessage(lineParts[1].split("!")[0], public void run() {
lineParts[2]); main.window.addUser(lineParts[1].split("!")[0]);
break; }
});
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;
} }
} }
} }
......
...@@ -19,10 +19,16 @@ import java.util.function.Supplier; ...@@ -19,10 +19,16 @@ import java.util.function.Supplier;
public class uMessage { public class uMessage {
private static final int N = 3; private static final int N = 3;
private static final String CORPUS = "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 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 private static final Supplier<Dictionary<AlphabeticString, Integer>> NEW_INNER = NGramTester
.trieConstructor(AlphabeticString.class); .binarySearchTreeConstructor();
/* /*
* *
......
...@@ -19,9 +19,9 @@ import cse332.datastructures.trees.BinarySearchTree; ...@@ -19,9 +19,9 @@ import cse332.datastructures.trees.BinarySearchTree;
* lead to highly perplexing and erroneous behavior. Instead, * lead to highly perplexing and erroneous behavior. Instead,
* continue using the existing BSTNode children array. * continue using the existing BSTNode children array.
* 4. Ensure that the class does not have redundant methods * 4. Ensure that the class does not have redundant methods
* 5. Cast children array to AVLNode whenever necessary in your * 5. Cast a BSTNode to an AVLNode whenever necessary in your AVLTree.
* AVLTree. This will result a lot of casts, so we recommend you make * This will result a lot of casts, so we recommend you make private methods
* private methods that encapsulate those casts. * that encapsulate those casts.
* 6. Do NOT override the toString method. It is used for grading. * 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 * 7. The internal structure of your AVLTree (from this.root to the leaves) must be correct
*/ */
......
...@@ -9,23 +9,24 @@ import java.util.Iterator; ...@@ -9,23 +9,24 @@ import java.util.Iterator;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
* 1. You must implement a generic chaining hashtable. You may not * - You must implement a generic chaining hashtable. You may not
* restrict the size of the input domain (i.e., it must accept * 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). * 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!). * - ChainingHashTable should rehash as appropriate (use load factor as shown in lecture!).
* 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 * - ChainingHashTable must resize its capacity into prime numbers via given PRIME_SIZES list.
* continue to resize using some other mechanism. * Past this, it should continue to resize using some other mechanism (primes not necessary).
* 6. We suggest you hard code some prime numbers. You can use this *
* list: http://primes.utm.edu/lists/small/100000.txt * - When implementing your iterator, you should NOT copy every item to another
* NOTE: Do NOT copy the whole list! * dictionary/list and return that dictionary/list's iterator.
* 7. 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> { public class ChainingHashTable<K, V> extends DeletelessDictionary<K, V> {
private Supplier<Dictionary<K, V>> newChain; 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) { public ChainingHashTable(Supplier<Dictionary<K, V>> newChain) {
this.newChain = newChain; this.newChain = newChain;
} }
...@@ -44,4 +45,13 @@ public class ChainingHashTable<K, V> extends DeletelessDictionary<K, V> { ...@@ -44,4 +45,13 @@ public class ChainingHashTable<K, V> extends DeletelessDictionary<K, V> {
public Iterator<Item<K, V>> iterator() { public Iterator<Item<K, V>> iterator() {
throw new NotYetImplementedException(); 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; ...@@ -9,9 +9,9 @@ import java.util.Iterator;
/** /**
* 1. The list is typically not sorted. * 1. The list is typically not sorted.
* 2. Add new items to the front of the list. * 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 * 3. Whenever find or insert is called on an existing key, move it
* list. This means you remove the node from its current position * to the front of the list. This means you remove the node from its
* and make it the first node in the list. * current position and make it the first node in the list.
* 4. You need to implement an iterator. The iterator SHOULD NOT move * 4. You need to implement an iterator. The iterator SHOULD NOT move
* elements to the front. The iterator should return elements in * elements to the front. The iterator should return elements in
* the order they are stored in the list, starting with the first * the order they are stored in the list, starting with the first
......
...@@ -36,7 +36,7 @@ public class NGramTester { ...@@ -36,7 +36,7 @@ public class NGramTester {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
WordSuggestor suggestions = new WordSuggestor("eggs.txt", 2, -1, WordSuggestor suggestions = new WordSuggestor("corpus/eggs.txt", 2, -1,
NGramTester.trieConstructor(NGram.class), NGramTester.trieConstructor(NGram.class),
NGramTester.trieConstructor(AlphabeticString.class)); NGramTester.trieConstructor(AlphabeticString.class));
System.out.println(suggestions); System.out.println(suggestions);
......
...@@ -9,9 +9,7 @@ public class TopKSort { ...@@ -9,9 +9,7 @@ public class TopKSort {
sort(array, k, (x, y) -> x.compareTo(y)); sort(array, k, (x, y) -> x.compareTo(y));
} }
/**
* Behaviour is undefined when k > array.length
*/
public static <E> void sort(E[] array, int k, Comparator<E> comparator) { public static <E> void sort(E[] array, int k, Comparator<E> comparator) {
throw new NotYetImplementedException(); throw new NotYetImplementedException();
} }
......
...@@ -17,7 +17,7 @@ public class SpellingCorrector { ...@@ -17,7 +17,7 @@ public class SpellingCorrector {
this.dictionary = new AutocompleteTrie(); this.dictionary = new AutocompleteTrie();
Scanner dict; Scanner dict;
try { try {
dict = new Scanner(new File("dictionary2.txt")); dict = new Scanner(new File("./corpus/dictionary2.txt"));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new RuntimeException( throw new RuntimeException(
"Couldn't read dictionary file for spelling correction and autocompletion."); "Couldn't read dictionary file for spelling correction and autocompletion.");
......
...@@ -7,6 +7,8 @@ import cse332.misc.LargeValueFirstItemComparator; ...@@ -7,6 +7,8 @@ import cse332.misc.LargeValueFirstItemComparator;
import cse332.sorts.InsertionSort; import cse332.sorts.InsertionSort;
import cse332.types.AlphabeticString; import cse332.types.AlphabeticString;
import cse332.types.NGram; import cse332.types.NGram;
import p2.sorts.QuickSort;
import p2.sorts.TopKSort;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
...@@ -73,10 +75,16 @@ public class NGramToNextChoicesMap { ...@@ -73,10 +75,16 @@ public class NGramToNextChoicesMap {
Comparator<Item<String, Integer>> comp = new LargeValueFirstItemComparator<String, Integer>(); Comparator<Item<String, Integer>> comp = new LargeValueFirstItemComparator<String, Integer>();
if (k < 0) { if (k < 0) {
InsertionSort.sort(afterNGrams, comp); QuickSort.sort(afterNGrams, comp);
} else { } else {
// You must fix this line toward the end of the project Comparator<Item<String, Integer>> revComp = (o1, o2) -> comp.compare(o2, o1);
throw new NotYetImplementedException(); 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]; String[] nextWords = new String[k < 0 ? afterNGrams.length : k];
......
package ckpt2; package provided;
import cse332.datastructures.containers.Item; import cse332.datastructures.containers.Item;
import cse332.datastructures.trees.BinarySearchTree.BSTNode; import cse332.datastructures.trees.BinarySearchTree.BSTNode;
......
package ckpt2; package provided;
import cse332.datastructures.containers.Item; import cse332.datastructures.containers.Item;
import cse332.datastructures.trees.BinarySearchTree;
import cse332.interfaces.misc.Dictionary; import cse332.interfaces.misc.Dictionary;
import datastructures.dictionaries.ChainingHashTable; import datastructures.dictionaries.ChainingHashTable;
import datastructures.dictionaries.MoveToFrontList; import datastructures.dictionaries.MoveToFrontList;
...@@ -25,8 +26,11 @@ public class ChainingHashTableTests { ...@@ -25,8 +26,11 @@ public class ChainingHashTableTests {
@Test() @Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS) @Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_insertFind_manyElements_correctStructure() { public void test_insertFind_manyElements_correctStructure() {
ChainingHashTable<String, Integer> list = new ChainingHashTable<>(MoveToFrontList::new); /*
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; int n = 1000;
// Add them // Add them
......
package ckpt1; package provided;
import datastructures.worklists.CircularArrayFIFOQueue; import datastructures.worklists.CircularArrayFIFOQueue;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
......
package ckpt2; package provided;
import datastructures.worklists.CircularArrayFIFOQueue; import datastructures.worklists.CircularArrayFIFOQueue;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
......
package ckpt2; package provided;
import cse332.types.AlphabeticString; import cse332.types.AlphabeticString;
import datastructures.dictionaries.HashTrieMap; import datastructures.dictionaries.HashTrieMap;
......
package provided;
import cse332.types.AlphabeticString;
import datastructures.dictionaries.HashTrieSet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
public class HashTrieSetTests {
/**
* Test to check if HashTrieSet has been copied over from P1
* */
@Test()
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void test_isHashTrieSetAvailable() {
HashTrieSet<Character, AlphabeticString> STUDENT = new HashTrieSet<>(AlphabeticString.class);
String[] words = {"dog", "doggy", "doge", "dragon", "cat", "draggin"};
String[] invalid = {"d", "cataract", "", "do"};
for (String word: words) {
STUDENT.add(a(word));
}
assertEquals(6, STUDENT.size());
for (String word: words) {
assertTrue(STUDENT.contains(a(word)));
}
for (String invalid_word: invalid) {
assertFalse(STUDENT.contains(a(invalid_word)));
}
}
private static AlphabeticString a(String s) {
return new AlphabeticString(s);
}
}
package ckpt2; package provided;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.Timeout;
......
package ckpt1; package provided;
import cse332.interfaces.worklists.PriorityWorkList; import cse332.interfaces.worklists.PriorityWorkList;
import cse332.interfaces.worklists.WorkList; import cse332.interfaces.worklists.WorkList;
......
package ckpt2; package provided;
import cse332.interfaces.worklists.PriorityWorkList; import cse332.interfaces.worklists.PriorityWorkList;
import cse332.interfaces.worklists.WorkList; import cse332.interfaces.worklists.WorkList;
......
package ckpt1; package provided;
import cse332.datastructures.containers.Item; import cse332.datastructures.containers.Item;
import datastructures.dictionaries.MoveToFrontList; import datastructures.dictionaries.MoveToFrontList;
......