package cop3530; import java.util.Comparator; public class BogusSet implements Set { @SuppressWarnings("unchecked") public BogusSet( Comparator c ) { currentSize = 0; items = (AnyType[]) new Object[ 5 ]; cmp = c; } public BogusSet( ) { this( null ); } public int size( ) { return currentSize; } public boolean isEmpty( ) { return size( ) == 0; } @SuppressWarnings("unchecked") public boolean add( AnyType x ) { if( currentSize == items.length ) { AnyType [ ] old = items; items = (AnyType[]) new Object[ old.length * 2 + 1 ]; for( int i = 0; i < old.length; i++ ) items[ i ] = old[ i ]; } /* CODE IS REDACTED */ // 1. Find first item >= x // 2. Check for duplicates // 3. Shift right // 4. Drop in x currentSize++; return true; } @SuppressWarnings("unchecked") private int myCompare( AnyType lhs, AnyType rhs ) { if( cmp != null ) return cmp.compare( lhs, rhs ); else return ((Comparable)lhs).compareTo( rhs ); } public boolean remove( AnyType x ) { throw new UnsupportedOperationException( ); } public boolean contains( AnyType x ) { throw new UnsupportedOperationException( ); } public String toString( ) { StringBuilder result = new StringBuilder( "[" ); for( int i = 0; i < currentSize; i++ ) result.append( " " + items[ i ] ); result.append( " ]" ); return new String( result ); } private AnyType[ ] items; private int currentSize; private Comparator cmp; }