Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package adt.hashtable.closed;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import adt.hashtable.hashfunction.HashFunction;
- import adt.hashtable.hashfunction.HashFunctionClosedAddress;
- import adt.hashtable.hashfunction.HashFunctionClosedAddressMethod;
- import adt.hashtable.hashfunction.HashFunctionFactory;
- import util.Util;
- public class HashtableClosedAddressImpl<T> extends AbstractHashtableClosedAddress<T> {
- /**
- * A hash table with closed address works with a hash function with closed
- * address. Such a function can follow one of these methods: DIVISION or
- * MULTIPLICATION. In the DIVISION method, it is useful to change the size
- * of the table to an integer that is prime. This can be achieved by
- * producing such a prime number that is bigger and close to the desired
- * size.
- *
- * For doing that, you have auxiliary methods: Util.isPrime and
- * getPrimeAbove as documented bellow.
- *
- * The length of the internal table must be the immediate prime number
- * greater than the given size. For example, if size=10 then the length must
- * be 11. If size=20, the length must be 23. You must implement this idea in
- * the auxiliary method getPrimeAbove(int size) and use it.
- *
- * @param desiredSize
- * @param method
- */
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public HashtableClosedAddressImpl(int desiredSize, HashFunctionClosedAddressMethod method) {
- int realSize = desiredSize;
- if (method == HashFunctionClosedAddressMethod.DIVISION) {
- realSize = this.getPrimeAbove(desiredSize); // real size must the
- // the immediate prime
- // above
- }
- initiateInternalTable(realSize);
- HashFunction function = HashFunctionFactory.createHashFunction(method, realSize);
- this.hashFunction = function;
- }
- // AUXILIARY
- /**
- * It returns the prime number that is closest (and greater) to the given
- * number. You can use the method Util.isPrime to check if a number is
- * prime.
- */
- int getPrimeAbove(int number) {
- while (!Util.isPrime(number)) {
- number++;
- }
- return number;
- }
- @Override
- public void insert(T element) {
- if (element != null) {
- HashFunctionClosedAddress<T> function = (HashFunctionClosedAddress<T>) this.getHashFunction();
- int posicaoElemento = function.hash(element);
- if (table[posicaoElemento] == null) {
- table[posicaoElemento] = new LinkedList<T>();
- }
- ((LinkedList<T>) table[posicaoElemento]).add(element);
- elements++;
- }
- }
- @Override
- public void remove(T element) {
- if (element != null) {
- HashFunctionClosedAddress<T> function = (HashFunctionClosedAddress<T>) this.getHashFunction();
- int posicaoElemento = function.hash(element);
- if (table[posicaoElemento] != null) {
- ((LinkedList<T>) table[posicaoElemento]).remove(element);
- elements--;
- }
- }
- }
- @Override
- public T search(T element) {
- T auxiliar = null;
- if (element != null) {
- HashFunctionClosedAddress<T> function = (HashFunctionClosedAddress<T>) this.getHashFunction();
- int posicaoElemento = function.hash(element);
- LinkedList<T> lista = ((LinkedList<T>) table[posicaoElemento]);
- ListIterator<T> listIterator = lista.listIterator();
- while (listIterator.hasNext()) {
- if (listIterator.equals(element)) {
- auxiliar = (T) listIterator;
- break;
- }
- listIterator.next();
- }
- }
- return auxiliar;
- }
- @Override
- public int indexOf(T element) {
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException("Not implemented yet!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement