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 (27)
Showing
with 124 additions and 73 deletions
...@@ -25,6 +25,7 @@ dependencies { ...@@ -25,6 +25,7 @@ dependencies {
test { test {
useJUnitPlatform() useJUnitPlatform()
maxHeapSize = "4096m"
} }
java { java {
......
File moved
File moved
File moved
File moved
File moved
File moved
File moved
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;
} }
} }
} }
......
...@@ -18,11 +18,17 @@ import java.util.function.Supplier; ...@@ -18,11 +18,17 @@ 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 = "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();
/* /*
* *
......
...@@ -16,6 +16,7 @@ import java.util.Iterator; ...@@ -16,6 +16,7 @@ import java.util.Iterator;
public class BinarySearchTree<K extends Comparable<? super K>, V> public class BinarySearchTree<K extends Comparable<? super K>, V>
extends ComparableDictionary<K, V> { extends ComparableDictionary<K, V> {
// The root of the BST. Root is null if and only if the tree is empty. // The root of the BST. Root is null if and only if the tree is empty.
// This MUST be used as your root for any class that extends this
protected BSTNode root; protected BSTNode root;
/** /**
......
...@@ -18,12 +18,12 @@ import cse332.datastructures.trees.BinarySearchTree; ...@@ -18,12 +18,12 @@ import cse332.datastructures.trees.BinarySearchTree;
* the references used to access the instance). Such masking will * the references used to access the instance). Such masking will
* 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. If this class has redundant methods, your score will be heavily * 4. Ensure that the class does not have redundant methods
* penalized. * 5. Cast a BSTNode to an AVLNode whenever necessary in your AVLTree.
* 5. Cast children array to AVLNode whenever necessary in your * This will result a lot of casts, so we recommend you make private methods
* AVLTree. This will result a lot of casts, so we recommend you make * that encapsulate those casts.
* private methods 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
*/ */
public class AVLTree<K extends Comparable<? super K>, V> extends BinarySearchTree<K, V> { public class AVLTree<K extends Comparable<? super K>, V> extends BinarySearchTree<K, V> {
......
...@@ -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
......
...@@ -3,18 +3,11 @@ package datastructures.worklists; ...@@ -3,18 +3,11 @@ package datastructures.worklists;
import cse332.exceptions.NotYetImplementedException; import cse332.exceptions.NotYetImplementedException;
import cse332.interfaces.worklists.FixedSizeFIFOWorkList; import cse332.interfaces.worklists.FixedSizeFIFOWorkList;
import java.util.NoSuchElementException;
/** /**
* See cse332/interfaces/worklists/FixedSizeFIFOWorkList.java * See cse332/interfaces/worklists/FixedSizeFIFOWorkList.java
* for method specifications. * for method specifications.
*/ */
public class CircularArrayFIFOQueue<E extends Comparable<E>> extends FixedSizeFIFOWorkList<E> { public class CircularArrayFIFOQueue<E> extends FixedSizeFIFOWorkList<E> {
private E[] queue;
private int front;
private int size;
public CircularArrayFIFOQueue(int capacity) { public CircularArrayFIFOQueue(int capacity) {
super(capacity); super(capacity);
throw new NotYetImplementedException(); throw new NotYetImplementedException();
...@@ -57,17 +50,31 @@ public class CircularArrayFIFOQueue<E extends Comparable<E>> extends FixedSizeFI ...@@ -57,17 +50,31 @@ public class CircularArrayFIFOQueue<E extends Comparable<E>> extends FixedSizeFI
@Override @Override
public int compareTo(FixedSizeFIFOWorkList<E> other) { public int compareTo(FixedSizeFIFOWorkList<E> other) {
// You will implement this method in project 2. Leave this method unchanged for project 1.
throw new NotYetImplementedException(); throw new NotYetImplementedException();
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public boolean equals(Object obj) { public boolean equals(Object obj) {
throw new NotYetImplementedException(); // You will finish implementing this method in project 2. Leave this method unchanged for project 1.
if (this == obj) {
return true;
} else if (!(obj instanceof FixedSizeFIFOWorkList<?>)) {
return false;
} else {
// Uncomment the line below for p2 when you implement equals
// FixedSizeFIFOWorkList<E> other = (FixedSizeFIFOWorkList<E>) obj;
// Your code goes here
throw new NotYetImplementedException();
}
} }
@Override @Override
public int hashCode() { public int hashCode() {
// You will implement this method in project 2. Leave this method unchanged for project 1.
throw new NotYetImplementedException(); throw new NotYetImplementedException();
} }
} }
...@@ -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,6 +9,7 @@ public class TopKSort { ...@@ -9,6 +9,7 @@ public class TopKSort {
sort(array, k, (x, y) -> x.compareTo(y)); sort(array, k, (x, y) -> x.compareTo(y));
} }
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();
} }
......
...@@ -21,21 +21,19 @@ public class AutocompleteTrie extends HashTrieMap<Character, AlphabeticString, I ...@@ -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) { while (current.pointers.size() == 1) {
if (current.value != null) { if (current.value != null) {
return null; return null;
} }
result += current.pointers.keySet().iterator().next(); result.append(current.pointers.keySet().iterator().next());
current = current.pointers.values().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) { 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 { ...@@ -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];
......