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.
- */
- package javaapplication10;
- /**
- *
- * @author RamonGaming
- */
- public class DelegateHash implements IDelegateDB {
- private Delegate[] table;
- private static int tableSize = 10;
- private int numEntries;
- private double loadFactor;
- private int pSize;
- private boolean changed = false;
- public DelegateHash() {
- System.out.println("Simple sequence");
- table = new Delegate[tableSize];
- clearDB();
- }
- public void clearDB() {
- numEntries = 0;
- }
- public int loadFactor(){
- double ent = numEntries;
- double tsize = tableSize;
- changed = true;
- loadFactor = (ent / tsize) * 10;
- if (loadFactor > 50){
- loadFactorChange();
- loadFactor = (ent / tsize) * 10;
- }
- return(int)loadFactor;
- }
- public void log(){
- System.out.println(tableSize);
- System.out.println(numEntries);
- System.out.println(loadFactor);
- System.out.println();
- System.out.println(tableSize);
- System.out.println(tableSize);
- if (changed = true){
- System.out.println("Previous array size:" + pSize);
- }
- }
- @Override
- public boolean containsName(String name) {
- int pos = hashFunction(name);
- return table[pos] != null && table[pos].getName().equals(name) && table[pos].getName() != null;
- }
- @Override
- public Delegate get(String name) {
- for(Delegate d: table){
- if(containsName(name)){
- return d;
- }
- }
- return null;
- }
- public void loadFactorChange(){
- Delegate[] temp = new Delegate[tableSize];
- int c = 0;
- pSize = tableSize;
- changed = true;
- for (Delegate d: table){
- if (d != null && d.getName() != null){
- temp[c] = d;
- c++;
- }
- }
- tableSize = tableSize * 2;
- table = new Delegate[tableSize];
- for(Delegate d: temp){
- if (d != null){
- put(d);
- }
- }
- }
- @Override
- public int size() {
- int c = 0;
- for(Delegate d: table){
- if(d != null && d.getName() != null){
- c++;
- }
- }
- System.out.println(c);
- return c;
- }
- @Override
- public boolean isEmpty() {
- return size() == 0;
- }
- /*private int findPos(String name) {
- // returns position where name is, or would go
- assert name != null && !name.equals("");
- int left = 0, right = numEntries;
- while (left != right) {
- int mid = (left + right)/2;
- if (name.compareTo(table[mid].getName()) > 0) left = mid + 1;
- else right = mid;
- }
- return left; // or right, since left == right
- */
- @Override
- public Delegate put(Delegate delegate) {
- assert delegate != null;
- assert delegate.getName() != null && !delegate.getName().equals("");
- String name = delegate.getName();
- int pos = hashFunction(name);
- int i = 0;
- while (pos < tableSize && table[pos] != delegate){
- if (table[pos] == null){
- table[pos] = delegate;
- numEntries++;
- }
- else if (numEntries > 0 && containsName(name)){
- Delegate previous = table[pos];
- table[pos] = delegate;
- return previous;
- }
- else{
- pos = quadprobing(pos, i);
- i++;
- }
- }
- return null;
- }
- /*assert numEntries != tableSize; // very simple
- assert delegate != null;
- String name = delegate.getName();
- assert name != null && !name.equals("");
- // assertions as before
- Delegate previous;
- int pos = hashFunction(name);
- if (pos == numEntries || !name.equals(table[pos].getName())) {// new
- int i = numEntries;
- while (i != pos) { // ‘budging up’ to keep the table ordered
- table[i] = table[i-1]; i--;
- }
- numEntries++;
- previous = null;
- } else {
- previous = table[pos];
- }
- table[pos] = delegate;
- return previous;*/
- /**
- * 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 temporary = new Delegate();
- int pos = hashFunction(name);
- int i = 0;
- while (pos < tableSize ){
- if (table[pos].getName().equals(name)){
- Delegate remove = new Delegate();
- remove = table[pos];
- table[pos] = temporary;
- numEntries--;
- return remove;
- }
- else{
- pos = quadprobing(pos, i);
- i++;
- }
- }
- return null;
- }
- /*Delegate empty = new Delegate();
- int pos = hashFunction(name);
- for(Delegate d: table){
- if(containsName(name)){
- table[pos] = empty;
- numEntries--;
- return d;
- }
- }
- return null;
- }*/
- /*
- System.out.println("delete " + name);
- int pos = hash(name);
- System.out.println(name + " hashes to " + pos);
- pos = probe(pos, name);
- System.out.println("linear probing stops at "+ pos);
- if (table[pos] == null) {
- System.out.println(name + " not found");
- } else {// table[pos].equals(name)
- System.out.println(name + " found at " + pos);
- System.out.println("setting table at " + pos + " to null");
- table[pos] = null; // WRONG!
- size--;
- }
- */ /*delete */
- public int hashFunction(String name){
- return ((int)(name.charAt(0)) + 1) % tableSize;
- }
- public int quadprobing(int pos, int i){
- return (pos + (i * i)) % tableSize;
- }
- /*public int quadraticProbing(int newIndex, int i) {
- return (newIndex + (i*i)); // taken away / SIZE
- }*/
- /**
- * Prints the names and IDs of all the delegates in the database in
- * alphabetic order.
- * @pre true
- */
- @Override
- public void displayDB() {
- int c = 0;
- for(Delegate d: table){
- if(d != null && d.getName() != null){
- System.out.println(d.getName());
- System.out.println(d.getAffiliation());
- c++;
- }
- }
- if(c == 0){
- System.out.println("Table is empty!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement