Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. package dataStructures;  
  2.  
  3. public class ChainedHashTable<K extends Comparable<K>, V>
  4.     extends HashTable<K,V>
  5. {
  6.  
  7.     static final long serialVersionUID = 0L;
  8.  
  9.  
  10.     // The array of dictionaries.
  11.     protected Dictionary<K,V>[] table;
  12.  
  13.  
  14.     public ChainedHashTable( int capacity )
  15.     {
  16.         int arraySize = HashTable.nextPrime((int) (1.1 * capacity));
  17.         // Compiler gives a warning.
  18.         table = (Dictionary<K,V>[]) new Dictionary[arraySize];
  19.         for ( int i = 0; i < arraySize; i++ )
  20.             table[i] = new OrderedDoubleList<K,V>();
  21.         maxSize = capacity;
  22.         currentSize = 0;
  23.     }                                      
  24.  
  25.  
  26.     public ChainedHashTable( )
  27.     {
  28.         this(DEFAULT_CAPACITY);
  29.     }                                                                
  30.  
  31.  
  32.     // Returns the hash value of the specified key.
  33.     protected int hash( K key )
  34.     {
  35.         return Math.abs( key.hashCode() ) % table.length;
  36.     }
  37.  
  38.  
  39.     // If there is an entry in the dictionary whose key is the specified key,
  40.     // returns its value; otherwise, returns null.
  41.     public V find( K key )
  42.     {
  43.         return table[ this.hash(key) ].find(key);
  44.     }
  45.  
  46.  
  47.     // If there is an entry in the dictionary whose key is the specified key,
  48.     // replaces its value by the specified value and returns the old value;
  49.     // otherwise, inserts the entry (key, value) and returns null.
  50.     public V insert( K key, V value )
  51.     {
  52.         if ( this.isFull() )
  53.             //TODO: Original comentado para nao dar erro de compilacao.
  54.             //this.rehash();
  55.             return null;
  56.  
  57.         //TODO: Left as an exercise.
  58.        else { currentSize++;
  59.            
  60.            return table[this.hash(key)].insert(key, value);
  61.        }
  62.        
  63.     }
  64.  
  65.  
  66.     // If there is an entry in the dictionary whose key is the specified key,
  67.     // removes it from the dictionary and returns its value;
  68.     // otherwise, returns null.
  69.     public V remove( K key )
  70.     {
  71.         //TODO: Left as an exercise.
  72.         currentSize--;
  73.         return table[this.hash(key)].remove(key) ;
  74.        
  75.     }
  76.  
  77.  
  78.     // Returns an iterator of the entries in the dictionary.
  79.     public Iterator<Entry<K,V>> iterator( ){
  80.     // implementar iterador usndo table e currentsize
  81.         //TODO: Left as an exercise.
  82.        
  83.         return new HashTableIterator<K,V>(table,currentSize);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement