Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package adt.hashtable.closed;
- import java.util.ArrayList;
- import adt.hashtable.hashfunction.HashFunction;
- import adt.hashtable.hashfunction.HashFunctionClosedAddressMethod;
- import adt.hashtable.hashfunction.HashFunctionDivisionMethod;
- import adt.hashtable.hashfunction.HashFunctionFactory;
- import adt.hashtable.hashfunction.HashFunctionMultiplicationMethod;
- 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) {
- int result = number;
- while (!Util.isPrime(result)){
- result++;
- }
- return result;
- }
- @Override
- public void insert(T element) {
- if (element != null){
- int valor = 0;
- if (hashFunction instanceof HashFunctionDivisionMethod){
- valor = ((HashFunctionDivisionMethod)this.hashFunction).hash(element);
- } else if (hashFunction instanceof HashFunctionMultiplicationMethod){
- valor = ((HashFunctionMultiplicationMethod)this.hashFunction).hash(element);
- }
- insert(valor, element);
- }
- }
- private void insert(int valor, T element){
- if (table[valor] == null){
- table[valor] = new ArrayList();
- } else {
- super.COLLISIONS++;
- }
- ((ArrayList)table[valor]).add(element);
- }
- @Override
- public void remove(T element) {
- if (element != null) {
- int valor = 0;
- if (hashFunction instanceof HashFunctionDivisionMethod){
- valor = ((HashFunctionDivisionMethod)this.hashFunction).hash(element);
- } else if (hashFunction instanceof HashFunctionMultiplicationMethod){
- valor = ((HashFunctionMultiplicationMethod)this.hashFunction).hash(element);
- }
- remove(valor, element);
- }
- }
- private void remove(int valor, T element){
- if (search(element) != null){
- ((ArrayList)table[valor]).remove(element);
- }
- }
- @Override
- public T search(T element) {
- T result = null;
- if (hashFunction instanceof HashFunctionDivisionMethod){
- int valor = ((HashFunctionDivisionMethod)this.hashFunction).hash(element);
- result = search(valor, element);
- } else if (hashFunction instanceof HashFunctionMultiplicationMethod){
- int valor = ((HashFunctionMultiplicationMethod)this.hashFunction).hash(element);
- result = search(valor, element);
- }
- return result;
- }
- private T search(int valor, T element){
- T result = null;
- if (table[valor] != null){
- ArrayList array = (ArrayList) table[valor];
- for (int i = 0; i < array.size(); i++){
- if (element.equals(array.get(i))){
- result = (T) array.get(i);
- }
- }
- }
- return result;
- }
- @Override
- public int indexOf(T element) {
- int valor = -1;
- if (hashFunction instanceof HashFunctionDivisionMethod){
- if (search(element) != null){
- valor = ((HashFunctionDivisionMethod)this.hashFunction).hash(element);
- }
- } else if (hashFunction instanceof HashFunctionMultiplicationMethod){
- if (search(element) != null){
- valor = ((HashFunctionMultiplicationMethod)this.hashFunction).hash(element);
- }
- }
- return valor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement