Commit 988d8e2f authored by Michael Lee's avatar Michael Lee
Browse files

Make FixedSizeFIFOWorkList comparable

parent 8c6af25f
......@@ -6,48 +6,52 @@ import java.util.Iterator;
import cse332.interfaces.worklists.FixedSizeFIFOWorkList;
import datastructures.worklists.CircularArrayFIFOQueue;
public abstract class BString<Alphabet> implements Iterable<Alphabet> {
public abstract class BString<Alphabet extends Comparable<Alphabet>> implements Iterable<Alphabet>, Comparable<BString<Alphabet>> {
protected FixedSizeFIFOWorkList<Alphabet> str;
public BString(Alphabet[] str) {
this.str = new CircularArrayFIFOQueue<Alphabet>(str.length);
for (int i = 0; i < str.length; i++) {
this.str.add(str[i]);
}
this.str = new CircularArrayFIFOQueue<Alphabet>(str.length);
for (int i = 0; i < str.length; i++) {
this.str.add(str[i]);
}
}
public BString(FixedSizeFIFOWorkList<Alphabet> q) {
this.str = q;
this.str = q;
}
@Override
public final Iterator<Alphabet> iterator() {
return str.iterator();
return this.str.iterator();
}
@SuppressWarnings("unchecked")
public static <A, X extends BString<A>> Class<A> getLetterType(Class<X> clz) {
public static <A extends Comparable<A>, X extends BString<A>> Class<A> getLetterType(Class<X> clz) {
try {
return (Class<A>) clz.getMethod("getLetterType", (Class<?>[])null).invoke(null, (Object[])null);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
return (Class<A>) clz.getMethod("getLetterType", (Class<?>[]) null)
.invoke(null, (Object[]) null);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e) {
e.printStackTrace();
e.printStackTrace();
System.err.println(clz.getName() + " does not have a getLetterType() method");
System.exit(1);
/* This is not reachable... */
return null;
}
}
public int size() {
return str.size();
return this.str.size();
}
public final boolean isEmpty() {
return str.size() == 0;
return this.str.size() == 0;
}
@Override
public String toString() {
StringBuilder build = new StringBuilder();
for (Alphabet chr : this) {
......@@ -62,7 +66,7 @@ public abstract class BString<Alphabet> implements Iterable<Alphabet> {
out[i] = arr[i];
}
return out;
}
}
protected static Byte[] wrap(byte[] arr) {
Byte[] out = new Byte[arr.length];
......@@ -71,4 +75,23 @@ public abstract class BString<Alphabet> implements Iterable<Alphabet> {
}
return out;
}
@Override
public int compareTo(BString<Alphabet> other) {
return this.str.compareTo(other.str);
}
@Override
public int hashCode() {
return this.str.hashCode();
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object other) {
if (!(other instanceof BString)) {
return false;
}
return this.str.equals(((BString<Alphabet>) other).str);
}
}
......@@ -24,7 +24,7 @@ import datastructures.worklists.ListFIFOQueue;
*
* @author Adam Blank
*/
public abstract class TrieMap<A, K extends BString<A>, V> extends Dictionary<K, V> {
public abstract class TrieMap<A extends Comparable<A>, K extends BString<A>, V> extends Dictionary<K, V> {
protected TrieNode<?, ?> root;
/**
......
......@@ -2,7 +2,7 @@ package cse332.interfaces.trie;
import cse332.interfaces.misc.Set;
public abstract class TrieSet<A, E extends BString<A>> extends Set<E> {
public abstract class TrieSet<A extends Comparable<A>, E extends BString<A>> extends Set<E> {
protected TrieSet(TrieMap<A, E, Boolean> backingMap) {
super(backingMap);
}
......
package cse332.interfaces.worklists;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A subclass of FIFOWorkList that has a capacity. This subclass of WorkList
* can be useful when performance is important; the limit on the number of
* elements in the queue can allow the implementation to be faster.
*
* A subclass of FIFOWorkList that has a capacity. This subclass of WorkList can
* be useful when performance is important; the limit on the number of elements
* in the queue can allow the implementation to be faster.
*
* @author Adam Blank
*
* @param <E> the type of element in the worklist
* @param <E>
* the type of element in the worklist
*/
public abstract class FixedSizeFIFOWorkList<E> extends FIFOWorkList<E> {
private int capacity;
public abstract class FixedSizeFIFOWorkList<E> extends FIFOWorkList<E>
implements Comparable<FixedSizeFIFOWorkList<E>> {
private final int capacity;
public FixedSizeFIFOWorkList(int capacity) {
if (capacity < 0) {
......@@ -20,64 +23,74 @@ public abstract class FixedSizeFIFOWorkList<E> extends FIFOWorkList<E> {
}
this.capacity = capacity;
}
/**
* Adds work to the worklist. This method should conform to any additional contracts that
* the particular type of worklist has.
*
* Adds work to the worklist. This method should conform to any additional
* contracts that the particular type of worklist has.
*
* @precondition isFull() is false
*
* @param work the work to add to the worklist
* @throws IllegalStateException iff isFull()
*
* @param work
* the work to add to the worklist
* @throws IllegalStateException
* iff isFull()
*/
@Override
public abstract void add(E work);
/**
* Returns a view of the ith element of the worklist. Since this worklist is a FIFO
* worklist, it has a well-defined order.
*
* Returns a view of the ith element of the worklist. Since this worklist is
* a FIFO worklist, it has a well-defined order.
*
* @precondition 0 <= i < size()
* @postcondition the structure of this worklist remains unchanged
* @throws NoSuchElementException if hasWork() is false
* (this exception takes precedence over all others)
* @throws IndexOutOfBoundsException if i < 0 or i >= size()
*
* @param i the index of the element to peek at
* @return the ith element in this worklist
* @throws NoSuchElementException
* if hasWork() is false (this exception takes precedence over
* all others)
* @throws IndexOutOfBoundsException
* if i < 0 or i >= size()
*
* @param i
* the index of the element to peek at
* @return the ith element in this worklist
*/
public abstract E peek(int i);
/**
* Replaces the ith element of this worklist with value. Since this worklist
* Replaces the ith element of this worklist with value. Since this worklist
* is a FIFO worklist it has a well-defined order.
*
*
* @precondition 0 <= i < size()
* @postcondition only the ith element of the structure is changed
* @throws NoSuchElementException if hasWork() is false
* (this exception takes precedence over all others)
* @throws IndexOutOfBoundsException if i < 0 or i >= size()
*
* @param i the index of the element to update
* @param value the value to update index i with
* @throws NoSuchElementException
* if hasWork() is false (this exception takes precedence over
* all others)
* @throws IndexOutOfBoundsException
* if i < 0 or i >= size()
*
* @param i
* the index of the element to update
* @param value
* the value to update index i with
*/
public abstract void update(int i, E value);
/**
* This element returns the capacity of the worklist.
*
* @return the capacity of the worklist
*
* @return the capacity of the worklist
*/
public int capacity() {
return this.capacity;
}
/**
* Returns true iff this worklist cannot accommodate another element.
*
* @return true if this worklist is full
* Returns true iff this worklist cannot accommodate another element.
*
* @return true if this worklist is full
*/
public boolean isFull() {
return this.size() >= this.capacity();
return size() >= this.capacity();
}
@Override
......@@ -90,12 +103,12 @@ public abstract class FixedSizeFIFOWorkList<E> extends FIFOWorkList<E> {
@Override
public boolean hasNext() {
return idx < FixedSizeFIFOWorkList.this.size();
return this.idx < size();
}
@Override
public E next() {
return FixedSizeFIFOWorkList.this.peek(idx++);
return FixedSizeFIFOWorkList.this.peek(this.idx++);
}
}
}
......@@ -14,7 +14,7 @@ import cse332.interfaces.trie.TrieMap;
* and cse332/interfaces/misc/Dictionary.java
* for method specifications.
*/
public class HashTrieMap<A, K extends BString<A>, V> extends TrieMap<A, K, V> {
public class HashTrieMap<A extends Comparable<A>, K extends BString<A>, V> extends TrieMap<A, K, V> {
public class HashTrieNode extends TrieNode<Map<A, HashTrieNode>, HashTrieNode> {
public HashTrieNode() {
this(null);
......
......@@ -4,7 +4,7 @@ import cse332.exceptions.NotYetImplementedException;
import cse332.interfaces.trie.BString;
import cse332.interfaces.trie.TrieSet;
public class HashTrieSet<A, E extends BString<A>> extends TrieSet<A, E> {
public class HashTrieSet<A extends Comparable<A>, E extends BString<A>> extends TrieSet<A, E> {
/* Note: You should not be adding any methods to this class...you only need to implement
* the constructor! */
......
......@@ -47,4 +47,22 @@ public class CircularArrayFIFOQueue<E> extends FixedSizeFIFOWorkList<E> {
public void clear() {
throw new NotYetImplementedException();
}
@Override
public int hashCode() {
// You will implement this method in p2. Leave this method unchanged for p1.
throw new NotYetImplementedException();
}
@Override
public boolean equals(Object obj) {
// You will implement this method in p2. Leave this method unchanged for p1.
throw new NotYetImplementedException();
}
@Override
public int compareTo(FixedSizeFIFOWorkList<E> o) {
// You will implement this method in p2. Leave this method unchanged for p1.
throw new NotYetImplementedException();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment