Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Nama : Bellalita R.S.I - 1006685714
- // SDA WORKSHEET 7
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.IOException;
- public class SDA11107 {
- public static void main(String[]args) {
- BufferedReader br = new BufferedReader
- (new InputStreamReader(System.in));
- BufferedWriter bw = new BufferedWriter
- (new OutputStreamWriter(System.out));
- /** HashTable hb = new HashTable();
- String operasItung;
- String[] sr;
- String[] st;
- try {
- while((operasItung = br.readLine()) != null) {
- if(operasItung.contains("Setor:")) {
- sr=operasItung.split(" ");
- KeyValuePair pp=new KeyValuePair(sr[1],Integer.parseInt(sr[2]));
- hb.put(pp);
- } else if(operasItung.contains("Tanya:")) {
- sr=operasItung.split(" ");
- st = sr[1].split(",");
- }
- }
- */
- }
- catch (Exception theException) {
- theException.printStackTrace();
- }
- }
- public class HashSet<AnyType> extends AbstractCollection<AnyType>{
- // Construct an empty HashSet.
- public HashSet( ) {
- allocateArray( DEFAULT_TABLE_SIZE );
- clear( );
- }
- // Construct a HashSet from any collection.
- public HashSet( Collection<? extends AnyType> other ) {
- allocateArray( nextPrime( other.size( ) * 2 ) );
- clear( );
- for( AnyType val : other )
- add( val );
- }
- public AnyType getMatch( AnyType x )
- {
- int currentPos = findPos( x );
- if( isActive( array, currentPos ) )
- return (AnyType) array[ currentPos ].element;
- return null;
- }
- public boolean contains( Object x )
- {
- return isActive( array, findPos( x ) );
- }
- private static boolean isActive( HashEntry [ ] arr, int pos )
- {
- return arr[ pos ] != null && arr[ pos ].isActive;
- }
- public boolean add( AnyType x )
- {
- int currentPos = findPos( x );
- if( isActive( array, currentPos ) )
- return false;
- if( array[ currentPos ] == null )
- occupied++;
- array[ currentPos ] = new HashEntry( x, true );
- currentSize++;
- modCount++;
- if( occupied > array.length / 2 )
- rehash();
- return true;
- }
- private static class HashEntry
- {
- public Object element; // the element
- public boolean isActive; // false if marked deleted
- public HashEntry( Object e )
- {
- this( e, true );
- }
- public HashEntry( Object e, boolean i )
- {
- element = e;
- isActive = i;
- }
- }
- private void rehash( ) {
- HashEntry [ ] oldArray = array;
- // Create a new, empty table
- allocateArray( nextPrime( 4 * size( ) ) );
- currentSize = 0;
- occupied = 0;
- // Copy table over
- for( int i = 0; i < oldArray.length; i++ )
- if( isActive( oldArray, i ) )
- add( (AnyType) oldArray[ i ].element );
- }
- private void allocateArray( int arraySize ) {
- array = new HashEntry[ nextPrime( arraySize ) ];
- }
- public boolean remove( Object x )
- {
- int currentPos = findPos( x );
- if( !isActive( array, currentPos ) )
- return false;
- array[ currentPos ].isActive = false;
- currentSize--;
- modCount++;
- if( currentSize < array.length / 8 )
- rehash( );
- return true;
- }
- private int findPos( Object x ) {
- int offset = 1;
- int currentPos = ( x == null ) ? 0 :
- Math.abs( x.hashCode( ) % array.length );
- while( array[ currentPos ] != null ) {
- if( x == null ) {
- if( array[ currentPos ].element == null )
- break;
- }
- else if( x.equals( array[ currentPos ].element ) )
- break;
- currentPos += offset; // Compute ith probe
- offset += 2;
- if( currentPos >= array.length ) // Implement the mod
- currentPos -= array.length;
- }
- return currentPos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement