18
public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash private int[] table; //dimensione della tabella (meglio se numero primo) private int dim; //METODI // Costruttore. Inizializza le variabili di istanza public SimpleHash(int dim) {} // Inizializza ogni elemento della tabella a -1 (nessun valore inserito) public void initialize() {} //Cerca l'elemento v all'interno della tabella restituisce la posizione in cui si trova l’elemento, -1 se questo non e’ presente. public int search(int v){} //Inserisce l'elemento v nella tabella se la tabella non e' piena e se l'elemento non e' gia' presente public void insert(int v){} //Cancella l'elemento v dalla tabella se presente. Resituisce false se l'elemento non viene cancellato, true altrimenti public boolean cancel(int v){} //Stampa la tabella come una stringa public String toString(){} }

public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash

Embed Size (px)

DESCRIPTION

public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash private int[] table; //dimensione della tabella (meglio se numero primo) private int dim; //METODI // Costruttore. Inizializza le variabili di istanza public SimpleHash(int dim) {} - PowerPoint PPT Presentation

Citation preview

Page 1: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

public class SimpleHash{//DICHIARAZIONE DELLE VARIABILI DI ISTANZA

//tabella hash private int[] table;

//dimensione della tabella (meglio se numero primo) private int dim;

//METODI

// Costruttore. Inizializza le variabili di istanza public SimpleHash(int dim) {}

// Inizializza ogni elemento della tabella a -1 (nessun valore inserito) public void initialize() {}

//Cerca l'elemento v all'interno della tabella restituisce la posizione in cui si trova l’elemento, -1 se questo non e’ presente. public int search(int v){}

//Inserisce l'elemento v nella tabella se la tabella non e' piena e se l'elemento non e' gia' presentepublic void insert(int v){}

//Cancella l'elemento v dalla tabella se presente. Resituisce false se l'elemento non viene cancellato, true altrimentipublic boolean cancel(int v){}

//Stampa la tabella come una stringapublic String toString(){}

}

Page 2: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

/**************************************************************************************************************************

*costruttore. Istanzia l'array con un numero di elementi pari a dim (l'array va da 0 a n-1) *

* Inizializza la dimensione della tabella (non sarebbe necessario avere una variabile esplicita *

* per la dimensione - metodo length) *

**************************************************************************************************************************/

public SimpleHash(int dim)

{

table = new int[dim];

this.dim = dim;

}

/*******************************************************************************************************************

*Inizializza ogni elemento della tabella a -1 (nessun valore inserito) - potrei voler inserire il valore 0, *

*quindi l'inizializzazione di default (0) non va bene *

*******************************************************************************************************************/

public void initialize()

{

for (int i = 0; i < dim; i++) table[i] = -1;

}

Page 3: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

/*******************************************************************************************************************

* Cerca l'elemento v all'interno della tabella. Restituisce l'indice della posizione in cui e' inserito *

* l'elemento se presente in tabella, -1 altrimenti *

******************************************************************************************************************/

public int search(int v)

{ int key = v % dim;

int j = key;

while((table[j] != v) && (table[j] != -1) && ((j+1)%dim != key)) j = (j+1)%dim;

if(table[j] == v) return j;

else return -1;

}

/*******************************************************************************************************************

*Inserisce l'elemento v nella tabella se la tabella non e' piena e se l'elemento non e' gia' presente *

*******************************************************************************************************************/

public void insert(int v)

{ int key = v % dim;

int j = key;

while((table[j] != v) && (table[j] != -1) && ((j+1) % dim != key)) j = (j+1) % dim;

if(table[j] == -1)

{ table[j] = v;

System.out.println("Elemento "+v+" inserito in posizione "+j);

}

else if(table[j] == v)

System.out.println("Elemeno già inserito");

else System.out.println("Impossibile inserire l'elemento: tabella piena");

}

Page 4: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

/**************************************************************************************************************************

*Cancella l'elemento v dalla tabella se presente. Resituisce false se l'elemento non viene cancellato, true *

*altrimenti *

***************************************************************************************************************************/

public boolean cancel(int v)

{

int pos = search(v);

if(pos == -1) return false;

table[pos] = -1;

return true;

}

/******************************************************************************************************************

*Stampa la tabella come una stringa

******************************************************************************************************************/

public String toString()

{

String out = new String("[");

int i;

for(i = 0; i < dim-1; i++) out = out + table[i] +", ";

out = out + table[i]+"]";

return out;

}

Page 5: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

public class SimpleHashTest

{

public static void main(String[] argv)

{

SimpleHash hash = new SimpleHash(5);

System.out.println(hash.toString());

hash.initialize();

System.out.println(hash.toString());

hash.insert(61);

System.out.println(hash.toString());

hash.insert(15);

System.out.println(hash.toString());

hash.insert(5);

System.out.println(hash.toString());

hash.insert(4);

System.out.println(hash.toString());

hash.insert(24);

System.out.println(hash.toString());

hash.insert(0);

System.out.println("Elemento 1 in posizione: "+hash.search(1));

System.out.println("Elemento 33 cancellato :" +hash.cancel(33));

System.out.println("Elemento 61 cancellato: "+ hash.cancel(61));

System.out.println(hash.toString());

System.out.println("Elemento 24 in posizione" + hash.search(24));

hash.insert(24);

System.out.println(hash.toString());

}

}

Page 6: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

SimpleHash hash = new SimpleHash(5)

public SimpleHash(int dim){ table = new int[dim]; this.dim = dim;

}

5dim

table

0 1 2 3 4

0 0 0 0 0

hash.initialize();

public void initialize(){ for (int i = 0; i < dim; i++) table[i] = -1;}

-1 0 0 0 0table

0 1 2 3 4

i 0

-1 -1 0 0 0table

0 1 2 3 4

i 1

-1 -1 -1 0 0table

0 1 2 3 4

i 2

-1 -1 -1 -1 -1table

0 1 2 3 4

i 5

Page 7: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

hash.insert(5)

public void insert(int v)

{

int key = v % dim;

int j = key;

while((table[j] != v) && (table[j] != -1) && ((j+1) % dim != key))

j = (j+1) % dim;

if(table[j] == -1)

{

table[j] = v;

System.out.println("Elemento "+v+" inserito in posizione "+j);

return;

}

if(table[j] == v)

{

System.out.println("Elemeno già inserito");

return;

}

System.out.println("Impossibile inserire l'elemento: tabella piena");

}

15 61 -1 -1 -1table

0 1 2 3 4

key 0 j 0v 5dim 5

15 61 -1 -1 -1table

0 1 2 3 4

key 0 j 1v 5

15 61 -1 -1 -1table

0 1 2 3 4

key 0 j 2v 5

table[j]

15 61 5 -1 -1table

0 1 2 3 4

key 0 j 2v 5

table[j]

table[j]

Page 8: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

hash.search(1)

public int search(int v)

{

int key = v % dim;

int j = key;

while((table[j] != v) && (table[j] != -1) && ((j+1)%dim != key))

j = (j+1)%key;

if(table[j] == v) return j;

return -1;

}

15 61 5 24 4table

0 1 2 3 4

key 1 j 1v 1

table[j]

15 61 5 24 4table

0 1 2 3 4

key 1 j 2v 1

table[j]

15 61 5 24 4table

0 1 2 3 4

key 1 j 3v 1

table[j]

15 61 5 24 4table

0 1 2 3 4

key 1 j 4v 1

table[j]

15 61 5 24 4table

0 1 2 3 4

key 1 j 0v 1

table[j]

Page 9: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

hash.toString()

public String toString()

{

String out = new String("[");

int i;

for(i = 0; i < dim-1; i++)

out = out + table[i] +", ";

out = out + table[i]+"]";

return out;

}

15 61 5 24 4table

0 1 2 3 4

iout [

15 61 5 24 4table

0 1 2 3 4

iout [15, 0

15 61 5 24 4table

0 1 2 3 4

iout [15,61, 1

15 61 5 24 4table

0 1 2 3 4

iout [15,61,5, 2

15 61 5 24 4table

0 1 2 3 4

iout [15,61,5,24, 3

15 61 5 24 4table

0 1 2 3 4

iout [15,61,5,24, 4

out [15,61,5,24,4]

Page 10: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash
Page 11: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

hash.search(24)

public int search(int v)

{ int key = v % dim;

int j = key;

while((table[j] != v) && (table[j] != -1) && ((j+1)%dim != key)) j = (j+1)%key;

if(table[j] == v) return j;

return -1;

}

15 -1 5 24 4table

0 1 2 3 4

key 4 j 4v 24

table[j]

15 -1 5 24 4table

0 1 2 3 4

key 4 j 0v 24

table[j]

15 -1 5 24 4table

0 1 2 3 4

key 4 j 1v 24

table[j]

Page 12: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

Esempio di Soluzione

Page 13: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

public class Elem

{

private int value;

private int link;

public Elem()

{

value = -1;

link = -1;

}

public int getValue()

{

return value;

}

public void setValue(int value)

{

this.value = value;

}

public int getLink()

{

return link;

}

public void setLink(int link)

{

this.link = link;

}

}

Nuova classe che specifica il tipo degli elementi della tabella hash

Ogni elemento è composto da due interi: il valore dell’elemento ed il link – posizione

dell’elemento in cui è memorizzato il successivo elemento associato alla stessa chiave.

Costruttore: inizializza entrambe le variabili d’istanza a -1. All’inizio ogni elemento avrà il valore -1 e non ci saranno associazioni posizioni - chiavi

Metodo getValue(): Restituisce il valore della variabile value

Metodo setValue(int value): Assegna alla variabile value (di questo Elem) il valore del parametro formale value

Metodo getLink(): Restituisce il valore della variabile link

Metodo setLink(int link): Assegna alla variabile link (di questo Elem) il valore del parametro formale link

Page 14: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

public SimpleHash(int dim)

{

table = new Elem[dim];

this.dim = dim;

}

public void initialize()

{

for (int i = 0; i < dim; i++) table[i] = new Elem();

}

public int search(int v)

{

int j = v % dim;

while ((table[j].getValue() != v) && (table[j].getLink() != -1))

j = table[j].getLink();

if(table[j].getValue() == v) return j;

return -1;

}

public void insert(int v)

{

int j = v%dim;

while((table[j].getValue() != v) && (table[j].getLink() != -1))

j = table[j].getLink();

if(table[j].getValue() == v)

{

System.out.println("Elemento "+v+" gia' inserito");

return;

}

int firstFree = getFirstFree(v%dim);

if(firstFree == -1)

{

System.out.println("Impossibile inserire: tabella piena");

return;

}

if(firstFree != v%dim) table[j].setLink(firstFree);

table[firstFree].setValue(v);

System.out.println("Elemento "+v+" inserito in

posizione"+firstFree);

}

Page 15: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

public String toString()

{

String out = new String("[");

int i;

for(i = 0; i < dim-1; i++)

out = out + "(" +table[i].getValue()+","+table[i].getLink()+")" +", ";

out = out + "(" +table[i].getValue()+","+table[i].getLink()+")"+"]";

return out;

}

public int getFirstFree(int from)

{

int j = from;

while(((j+1) % dim != from) && (table[j].getValue() != -1))

j = (j+1)%dim;

if (table[j].getValue() == -1) return j;

return -1;

}

public boolean cancel(int v)

{

int j = v % dim;

int prev = -1;

while ((table[j].getValue() != v) && (table[j].getLink() != -1))

{

prev = j;

j = table[j].getLink();

}

if(table[j].getValue() == v) {

if(prev != -1)

{

table[prev].setLink(table[j].getLink());

table[j].setValue(-1);

table[j].setLink(-1);

return true;

}

if(table[j].getLink() == -1)

{

table[j].setValue(-1);

return true;

}

int value = table[table[j].getLink()].getValue();

int link = table[table[j].getLink()].getLink();

table[j].setValue(value);

table[table[j].getLink()].setValue(-1);

table[table[j].getLink()].setLink(-1);

table[j].setLink(link);

return true;

}

return false;

}

Page 16: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

public class SimpleHashTest1{

public static void main(String[] argv) { SimpleHash3 hash = new SimpleHash3(5); hash.initialize(); System.out.println(hash.toString()); hash.insert(61); System.out.println(hash.toString()); hash.insert(15); System.out.println(hash.toString()); hash.insert(5); System.out.println(hash.toString()); hash.insert(4); System.out.println(hash.toString()); hash.insert(24); System.out.println(hash.toString()); hash.insert(0); System.out.println("Elemento 1 in posizione: "+hash.search(1)); System.out.println("Elemento 4 cancellato :" +hash.cancel(4)); System.out.println("Elemento 61 cancellato: "+ hash.cancel(61)); System.out.println(hash.toString()); System.out.println("Elemento 24 in posizione" + hash.search(24)); hash.insert(24); System.out.println(hash.toString()); }}

Page 17: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

hash.initialize(); table

0 1 2 3 4

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

hash.insert(61); table

0 1 2 3 4

-1 -1 61 -1 -1 -1 -1 -1 -1 -1

hash.insert(15); table

0 1 2 3 4

15 -1 61 -1 -1 -1 -1 -1 -1 -1

hash.insert(5);table

0 1 2 3 4

15 2 61 -1 5 -1 -1 -1 -1 -1

Page 18: public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA      //tabella hash

hash.insert(4);table

0 1 2 3 4

15 2 61 -1 5 -1 -1 -1 4 -1

hash.insert(24);table

0 1 2 3 4

15 2 61 -1 5 -1 24 -1 4 3

43

hash.cancel(4);table

0 1 2 3 4

15 2 61 -1 5 -1 -1 -1 24 -1

hash.cancel(61);table

0 1 2 3 4

15 2 -1 -1 5 -1 -1 -1 24 -1