Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. // Nama : Bellalita R.S.I - 1006685714
  2. // SDA WORKSHEET 7
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.io.IOException;
  9.  
  10. public class SDA11107 {
  11. public static void main(String[]args) {
  12. BufferedReader br = new BufferedReader
  13. (new InputStreamReader(System.in));
  14. BufferedWriter bw = new BufferedWriter
  15. (new OutputStreamWriter(System.out));
  16.  
  17. /** HashTable hb = new HashTable();
  18. String operasItung;
  19. String[] sr;
  20. String[] st;
  21.  
  22. try {
  23.  
  24. while((operasItung = br.readLine()) != null) {
  25. if(operasItung.contains("Setor:")) {
  26. sr=operasItung.split(" ");
  27. KeyValuePair pp=new KeyValuePair(sr[1],Integer.parseInt(sr[2]));
  28. hb.put(pp);
  29. } else if(operasItung.contains("Tanya:")) {
  30. sr=operasItung.split(" ");
  31. st = sr[1].split(",");
  32. }
  33. }
  34. */
  35.  
  36. }
  37. catch (Exception theException) {
  38. theException.printStackTrace();
  39. }
  40. }
  41.  
  42.  
  43.  
  44.  
  45. public class HashSet<AnyType> extends AbstractCollection<AnyType>{
  46.  
  47. // Construct an empty HashSet.
  48.  
  49. public HashSet( ) {
  50. allocateArray( DEFAULT_TABLE_SIZE );
  51. clear( );
  52. }
  53.  
  54. // Construct a HashSet from any collection.
  55.  
  56. public HashSet( Collection<? extends AnyType> other ) {
  57. allocateArray( nextPrime( other.size( ) * 2 ) );
  58. clear( );
  59. for( AnyType val : other )
  60. add( val );
  61. }
  62.  
  63. public AnyType getMatch( AnyType x )
  64. {
  65. int currentPos = findPos( x );
  66. if( isActive( array, currentPos ) )
  67. return (AnyType) array[ currentPos ].element;
  68. return null;
  69. }
  70.  
  71. public boolean contains( Object x )
  72. {
  73. return isActive( array, findPos( x ) );
  74. }
  75.  
  76. private static boolean isActive( HashEntry [ ] arr, int pos )
  77. {
  78. return arr[ pos ] != null && arr[ pos ].isActive;
  79. }
  80.  
  81. public boolean add( AnyType x )
  82. {
  83. int currentPos = findPos( x );
  84. if( isActive( array, currentPos ) )
  85. return false;
  86.  
  87. if( array[ currentPos ] == null )
  88. occupied++;
  89. array[ currentPos ] = new HashEntry( x, true );
  90. currentSize++;
  91. modCount++;
  92.  
  93. if( occupied > array.length / 2 )
  94. rehash();
  95. return true;
  96. }
  97.  
  98. private static class HashEntry
  99. {
  100. public Object element; // the element
  101. public boolean isActive; // false if marked deleted
  102. public HashEntry( Object e )
  103. {
  104. this( e, true );
  105. }
  106. public HashEntry( Object e, boolean i )
  107. {
  108. element = e;
  109. isActive = i;
  110. }
  111. }
  112.  
  113. private void rehash( ) {
  114. HashEntry [ ] oldArray = array;
  115. // Create a new, empty table
  116.  
  117. allocateArray( nextPrime( 4 * size( ) ) );
  118. currentSize = 0;
  119. occupied = 0;
  120.  
  121. // Copy table over
  122. for( int i = 0; i < oldArray.length; i++ )
  123. if( isActive( oldArray, i ) )
  124. add( (AnyType) oldArray[ i ].element );
  125. }
  126.  
  127. private void allocateArray( int arraySize ) {
  128. array = new HashEntry[ nextPrime( arraySize ) ];
  129. }
  130.  
  131. public boolean remove( Object x )
  132. {
  133. int currentPos = findPos( x );
  134. if( !isActive( array, currentPos ) )
  135. return false;
  136. array[ currentPos ].isActive = false;
  137. currentSize--;
  138. modCount++;
  139.  
  140. if( currentSize < array.length / 8 )
  141. rehash( );
  142. return true;
  143. }
  144.  
  145.  
  146. private int findPos( Object x ) {
  147. int offset = 1;
  148. int currentPos = ( x == null ) ? 0 :
  149. Math.abs( x.hashCode( ) % array.length );
  150. while( array[ currentPos ] != null ) {
  151. if( x == null ) {
  152. if( array[ currentPos ].element == null )
  153. break;
  154. }
  155. else if( x.equals( array[ currentPos ].element ) )
  156. break;
  157. currentPos += offset; // Compute ith probe
  158. offset += 2;
  159. if( currentPos >= array.length ) // Implement the mod
  160. currentPos -= array.length;
  161. }
  162. return currentPos;
  163. }
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement