Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package adt.hashtable.closed;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.ListIterator;
  5.  
  6. import adt.hashtable.hashfunction.HashFunction;
  7. import adt.hashtable.hashfunction.HashFunctionClosedAddress;
  8. import adt.hashtable.hashfunction.HashFunctionClosedAddressMethod;
  9. import adt.hashtable.hashfunction.HashFunctionFactory;
  10. import util.Util;
  11.  
  12. public class HashtableClosedAddressImpl<T> extends AbstractHashtableClosedAddress<T> {
  13.  
  14.     /**
  15.      * A hash table with closed address works with a hash function with closed
  16.      * address. Such a function can follow one of these methods: DIVISION or
  17.      * MULTIPLICATION. In the DIVISION method, it is useful to change the size
  18.      * of the table to an integer that is prime. This can be achieved by
  19.      * producing such a prime number that is bigger and close to the desired
  20.      * size.
  21.      *
  22.      * For doing that, you have auxiliary methods: Util.isPrime and
  23.      * getPrimeAbove as documented bellow.
  24.      *
  25.      * The length of the internal table must be the immediate prime number
  26.      * greater than the given size. For example, if size=10 then the length must
  27.      * be 11. If size=20, the length must be 23. You must implement this idea in
  28.      * the auxiliary method getPrimeAbove(int size) and use it.
  29.      *
  30.      * @param desiredSize
  31.      * @param method
  32.      */
  33.  
  34.     @SuppressWarnings({ "rawtypes", "unchecked" })
  35.     public HashtableClosedAddressImpl(int desiredSize, HashFunctionClosedAddressMethod method) {
  36.         int realSize = desiredSize;
  37.  
  38.         if (method == HashFunctionClosedAddressMethod.DIVISION) {
  39.             realSize = this.getPrimeAbove(desiredSize); // real size must the
  40.             // the immediate prime
  41.             // above
  42.         }
  43.         initiateInternalTable(realSize);
  44.         HashFunction function = HashFunctionFactory.createHashFunction(method, realSize);
  45.         this.hashFunction = function;
  46.     }
  47.  
  48.     // AUXILIARY
  49.     /**
  50.      * It returns the prime number that is closest (and greater) to the given
  51.      * number. You can use the method Util.isPrime to check if a number is
  52.      * prime.
  53.      */
  54.     int getPrimeAbove(int number) {
  55.         while (!Util.isPrime(number)) {
  56.             number++;
  57.         }
  58.         return number;
  59.     }
  60.  
  61.     @Override
  62.     public void insert(T element) {
  63.         if (element != null) {
  64.  
  65.             HashFunctionClosedAddress<T> function = (HashFunctionClosedAddress<T>) this.getHashFunction();
  66.  
  67.             int posicaoElemento = function.hash(element);
  68.  
  69.             if (table[posicaoElemento] == null) {
  70.                 table[posicaoElemento] = new LinkedList<T>();
  71.             }
  72.             ((LinkedList<T>) table[posicaoElemento]).add(element);
  73.             elements++;
  74.            
  75.         }
  76.     }
  77.  
  78.     @Override
  79.     public void remove(T element) {
  80.         if (element != null) {
  81.             HashFunctionClosedAddress<T> function = (HashFunctionClosedAddress<T>) this.getHashFunction();
  82.  
  83.             int posicaoElemento = function.hash(element);
  84.  
  85.             if (table[posicaoElemento] != null) {
  86.                 ((LinkedList<T>) table[posicaoElemento]).remove(element);
  87.                 elements--;
  88.             }
  89.         }
  90.     }
  91.  
  92.     @Override
  93.     public T search(T element) {
  94.         T auxiliar = null;
  95.         if (element != null) {
  96.             HashFunctionClosedAddress<T> function = (HashFunctionClosedAddress<T>) this.getHashFunction();
  97.  
  98.             int posicaoElemento = function.hash(element);
  99.  
  100.             LinkedList<T> lista = ((LinkedList<T>) table[posicaoElemento]);
  101.  
  102.             ListIterator<T> listIterator = lista.listIterator();
  103.             while (listIterator.hasNext()) {
  104.                 if (listIterator.equals(element)) {
  105.                     auxiliar = (T) listIterator;
  106.                     break;
  107.                 }
  108.                 listIterator.next();
  109.             }
  110.         }
  111.         return auxiliar;
  112.     }
  113.  
  114.     @Override
  115.     public int indexOf(T element) {
  116.         // TODO Auto-generated method stub
  117.         throw new UnsupportedOperationException("Not implemented yet!");
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement