Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package adt.hashtable.open;
- import adt.hashtable.hashfunction.HashFunctionClosedAddressMethod;
- import adt.hashtable.hashfunction.HashFunctionLinearProbing;
- public class HashtableOpenAddressLinearProbingImpl<T extends Storable> extends
- AbstractHashtableOpenAddress<T> {
- public HashtableOpenAddressLinearProbingImpl(int size,
- HashFunctionClosedAddressMethod method) {
- super(size);
- hashFunction = new HashFunctionLinearProbing<T>(size, method);
- this.initiateInternalTable(size);
- }
- @Override
- public void insert(T element) {
- if(this.isFull()) {
- throw new HashtableOverflowException();
- }
- if(element != null) {
- if(search(element) == null) {
- int i = 0;
- int hash = hashFunction(element, i);
- while(i < super.capacity()) {
- if(table[hash] == null || deletedElement.equals(element)) {
- table[hash] = element;
- super.elements++;
- return;
- } else {
- i++;
- hash = hashFunction(element, i);
- super.COLLISIONS++;
- }
- }
- }
- }
- }
- @Override
- public void remove(T element) {
- if(element != null) {
- if(indexOf(element) != -1) {
- int hash = indexOf(element);
- this.table[hash] = super.deletedElement;
- super.elements--;
- }
- }
- }
- @Override
- public T search(T element) {
- if(element != null) {
- int i = 0;
- int hash = hashFunction(element, i);
- if(!this.isEmpty()) {
- while(i < super.capacity() && this.table[hash] != null && !deletedElement.equals(element)) {
- if(table[hash].equals(element)) {
- return element;
- } else {
- i++;
- hash = hashFunction(element, i);
- }
- }
- }
- }
- return null;
- }
- @Override
- public int indexOf(T element) {
- if(element != null) {
- if(!this.isEmpty()) {
- int i = 0;
- int hash = hashFunction(element, i);
- while(i < super.capacity() && this.table[hash] != null && !deletedElement.equals(element)) {
- if(table[hash].equals(element)) {
- return hash;
- } else {
- i++;
- hash = hashFunction(element, i);
- }
- }
- }
- }
- return -1;
- }
- private int hashFunction(T element, int probe) {
- int hashIndex = ((HashFunctionLinearProbing<T>) super.hashFunction).hash(element, probe);
- return hashIndex;
- }
- }
Add Comment
Please, Sign In to add comment