Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author moham
- */
- public class DelegateHash implements IDelegateDB{
- private Delegate[] table;
- private static int tableSize = 10;
- private int numEntries;
- private double loadFactor;
- public DelegateHash(){
- table = new Delegate[tableSize];
- }
- /**
- * Empties the database.
- * @pre true
- */
- @Override
- public void clearDB() {
- table = new Delegate[tableSize];
- numEntries = 0;
- }
- /**
- * Determines whether a delegates name exists as a key inside the database
- * @pre name not null or empty string
- * @param name the Delegate name (key) to locate
- * @return true iff the name exists as a key in the database
- */
- @Override
- public boolean containsName(String name){
- assert !name.equals("");
- for(Delegate d: table){
- if((d != null) && d.getName().equals(name)){
- return true;
- }
- }
- return false;
- }
- /**
- * Returns a Delegate object mapped to the supplied name.
- * @pre name not null or empty string
- * @param name The Delegate name (key) to locate
- * @return the Delegate object mapped to the key name if the name
- * exists as key in the database, otherwise null
- */
- @Override
- public Delegate get(String name){
- int pos = hashFunction(name); // get position hashing
- int i = 1;
- pos = quadraticProbing(pos, i);
- while (pos < tableSize) {
- if (table[pos] != null) {
- if(table[pos].getName().equals(name)){
- return table[pos];
- }
- }
- else {
- pos = quadraticProbing(pos, i);
- i++;
- }
- }
- System.out.println(name + " is not in the table");
- return null;
- }
- /**
- * Returns the number of delegates in the database
- * @pre true
- * @return number of delegates in the database. 0 if empty
- */
- @Override
- public int size(){
- return numEntries;
- }
- /**
- * Determines if the database is empty or not.
- * @pre true
- * @return true iff the database is empty
- */
- @Override
- public boolean isEmpty() {
- return size() == 0;
- }
- /**
- * Inserts a delegate object into the database, with the key of the supplied
- * delegates name.
- * Note: If the name already exists as a key, then then the original entry
- * is overwritten.
- * This method must return the previous associated value
- * if one exists, otherwise null
- *
- * @pre delegate not null
- */
- @Override
- public Delegate put(Delegate delegate) {
- assert delegate != null;
- assert delegate.getName() != null && !delegate.getName().equals("");
- int pos = hashFunction(delegate.getName()); // get position hashing
- Delegate previous = null;
- int i = 1;
- pos = quadraticProbing(pos, i);
- while (pos < tableSize && table[pos] != delegate) {
- System.out.println(delegate.getName() + " inital array location (buckets visited) is " + pos);
- if (table[pos] == null) {
- table[pos] = delegate;
- numEntries++;
- }
- else if (table[pos].getName().equals(delegate.getName())) {
- previous = table[pos]; // overwrite it and put prev as one there originally
- table[pos] = delegate;
- }
- else {
- pos = quadraticProbing(pos, i);
- i++;
- }
- }
- System.out.println(delegate.getName() + " newer array location (buckets visited) is " + pos);
- System.out.println("The load factor after putting " + delegate.getName() + " is " + updateLoadFactor());
- return null;
- }
- /**
- * Prints the names and IDs of all the delegates in the database in
- * alphabetic order.
- * @pre true
- */
- @Override
- public void displayDB(){
- String[] tArray = new String[tableSize];
- int counter = 0;
- String temptwo;
- for (Delegate d:table){
- if(d != null && d.getName() != null){
- tArray[counter] = d.getName();
- System.out.println(d.getName());
- System.out.println(d.getAffiliation());
- counter++;
- }
- for (int i = 0; i < tArray.length; i++){
- for (int j = i + 1; j < tArray.length; j++){
- assert tArray[i] != null;
- assert tArray[j] != null;
- if (tArray[j] != null && tArray[i] != null && tArray[i].compareTo(tArray[j])>0){
- temptwo = tArray[i];
- tArray[i] = tArray[j];
- tArray[j] = temptwo;
- }
- }
- }
- for (String names : tArray){
- if (names != null){
- Delegate temp = get(names);
- System.out.println("Name: " + temp.getName() + " Affiliation: " + temp.getAffiliation());
- }
- }
- }
- if(counter == 0){
- System.out.println("The Table is empty.");
- }
- }
- /**
- * Removes and returns a delegate from the database, with the key
- * the supplied name.
- * @param name The name (key) to remove.
- * @pre name not null or empty string
- * @return the removed delegate object mapped to the name, or null if
- * the name does not exist.
- */
- @Override
- public Delegate remove(String name){
- Delegate removeIt = new Delegate();
- int pos = hashFunction(name);
- int i = 1;
- boolean done = false;
- pos = quadraticProbing(pos, i);
- while(!done){
- if(table[pos] == null){
- done = true;
- }
- else if (table[pos].getName().equals(name)){
- Delegate removedDel = new Delegate();
- removedDel = table[pos];
- table[pos] = removeIt;
- numEntries--;
- System.out.println(removedDel + " is removed, the load factor now is: " + updateLoadFactor());
- done = true;
- return removedDel;
- }
- else{
- pos = quadraticProbing(pos, i);
- i++;
- }
- }
- return null;
- }
- public int hashFunction(String name){
- int index = 0;
- int newIndex = 0;
- while(index != name.length()){
- //System.out.println((int)name.charAt(index)); // UNCOMMENT THIS
- newIndex += name.charAt(index);
- index++;
- }
- return newIndex;
- }
- public int quadraticProbing(int newIndex, int i) {
- return (newIndex + (i*i)) % tableSize; // SAY THIS IS OVERFLOW USING MOD
- }
- public double updateLoadFactor(){
- double entry = numEntries;
- double theSize = tableSize;
- loadFactor = entry / theSize;
- if (loadFactor >= 0.5){
- resizeArray();
- }
- loadFactor = entry / theSize;
- return loadFactor;
- }
- public void resizeArray(){
- Delegate[] temp = new Delegate[tableSize];
- int count = 0;
- int previous;
- previous = tableSize;
- System.out.println("The table before resizing is " + tableSize);
- for (Delegate d:table){
- if(d != null && d.getName() != null){
- temp[count] = d;
- count++;
- }
- }
- tableSize = tableSize *2;
- clearDB();
- System.out.println("The table size after resizing is " + tableSize);
- for(Delegate d: temp){
- if(d != null){
- put(d);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement