diff --git a/src/cl/cromer/estructuras/Grafico.java b/src/cl/cromer/estructuras/Grafico.java index 9506926..5f8304d 100644 --- a/src/cl/cromer/estructuras/Grafico.java +++ b/src/cl/cromer/estructuras/Grafico.java @@ -25,428 +25,439 @@ import java.util.List; * @author Chris Cromer */ public class Grafico { - /** - * Duración de la animación. - */ - static final public int DURACION = 150; - /** - * Tipo de dibujo rectuangular. - */ - static final public int RECTANGULO = 0; - /** - * Tipo de dibujo circular. - */ - static final public int CIRCULO = 1; - /** - * Tipo de dibjuo texto - */ - static final public int TEXTO = 2; - /** - * La escena donde está cosas graficas. - */ - final private Scene scene; - /** - * Los elementos destacados. - */ - final private List destacados; + /** + * Duración de la animación. + */ + static final public int DURACION = 150; - /** - * Graficar una escena. - * - * @param scene La scene a destacar. - */ - public Grafico(Scene scene) { - this.scene = scene; - destacados = new ArrayList<>(); - } + /** + * Tipo de dibujo rectuangular. + */ + static final public int RECTANGULO = 0; - /** - * Crear una flecha que apunta por abajo. - * - * @return StackPane: Devolver el stackpane que contiene la flecha. - */ - public static StackPane crearFlechaAbajo() { - Polygon polygon = new Polygon(); - polygon.getPoints().addAll( - 0.0, 0.0, - 10.0, 0.0, - 5.0, 10.0 - ); + /** + * Tipo de dibujo circular. + */ + static final public int CIRCULO = 1; - StackPane stackPane = new StackPane(); - stackPane.getChildren().addAll(polygon); - return stackPane; - } + /** + * Tipo de dibjuo texto + */ + static final public int TEXTO = 2; - /** - * Crear una flecha que apunta por arriba. - * - * @return StackPane: Devolver el stackpane que contiene la flecha. - */ - public static StackPane crearFlechaArriba() { - Polygon polygon = new Polygon(); - polygon.getPoints().addAll( - 5.0, 0.0, - 0.0, 10.0, - 10.0, 10.0 - ); + /** + * La escena donde está cosas graficas. + */ + final private Scene scene; - StackPane stackPane = new StackPane(); - stackPane.getChildren().addAll(polygon); - return stackPane; - } + /** + * Los elementos destacados. + */ + final private List destacados; - /** - * Crear una linea vertical. - * - * @return StackPane: Devolver el stackpane que contiene la linea vertical. - */ - public static StackPane crearLineaVertical() { - Line line = new Line(); - line.setStartX(20); - line.setEndX(20); - line.setStartY(0); - line.setEndY(20); + /** + * Graficar una escena. + * + * @param scene La scene a destacar. + */ + public Grafico(Scene scene) { + this.scene = scene; + destacados = new ArrayList<>(); + } - StackPane stackPane = new StackPane(); - stackPane.getChildren().addAll(line); - return stackPane; - } + /** + * Crear una flecha que apunta por abajo. + * + * @return StackPane: Devolver el stackpane que contiene la flecha. + */ + public static StackPane crearFlechaAbajo() { + Polygon polygon = new Polygon(); + polygon.getPoints().addAll( + 0.0, 0.0, + 10.0, 0.0, + 5.0, 10.0 + ); - /** - * Crear la linea circular con flecha. - * - * @param cajas int: La cantidad de cajas que están. - * @return StackPane: Devolver el stackpane que contiene la linea horizontal. - */ - public static Pane crearLineaCircular(int cajas) { - int height = (cajas - 1) * (40 + 20 + 10); - height = height + 20 + 10; + StackPane stackPane = new StackPane(); + stackPane.getChildren().addAll(polygon); + return stackPane; + } - Line top = new Line(); - top.setStartY(20); - top.setEndY(20); - top.setStartX(0); - top.setEndX(20); + /** + * Crear una flecha que apunta por arriba. + * + * @return StackPane: Devolver el stackpane que contiene la flecha. + */ + public static StackPane crearFlechaArriba() { + Polygon polygon = new Polygon(); + polygon.getPoints().addAll( + 5.0, 0.0, + 0.0, 10.0, + 10.0, 10.0 + ); - Polygon flechaDerecha = new Polygon(); - flechaDerecha.getPoints().addAll( - 10.0, 15.0, - 20.0, 20.0, - 10.0, 25.0 - ); + StackPane stackPane = new StackPane(); + stackPane.getChildren().addAll(polygon); + return stackPane; + } - Line vertical = new Line(); - vertical.setStartY(20); - vertical.setEndY(height); + /** + * Crear una linea vertical. + * + * @return StackPane: Devolver el stackpane que contiene la linea vertical. + */ + public static StackPane crearLineaVertical() { + Line line = new Line(); + line.setStartX(20); + line.setEndX(20); + line.setStartY(0); + line.setEndY(20); - Line bottom = new Line(); - bottom.setStartY(height); - bottom.setEndY(height); - bottom.setStartX(0); - bottom.setEndX(20); + StackPane stackPane = new StackPane(); + stackPane.getChildren().addAll(line); + return stackPane; + } + + /** + * Crear la linea circular con flecha. + * + * @param cajas int: La cantidad de cajas que están. + * + * @return StackPane: Devolver el stackpane que contiene la linea horizontal. + */ + public static Pane crearLineaCircular(int cajas) { + int height = (cajas - 1) * (40 + 20 + 10); + height = height + 20 + 10; + + Line top = new Line(); + top.setStartY(20); + top.setEndY(20); + top.setStartX(0); + top.setEndX(20); + + Polygon flechaDerecha = new Polygon(); + flechaDerecha.getPoints().addAll( + 10.0, 15.0, + 20.0, 20.0, + 10.0, 25.0 + ); + + Line vertical = new Line(); + vertical.setStartY(20); + vertical.setEndY(height); + + Line bottom = new Line(); + bottom.setStartY(height); + bottom.setEndY(height); + bottom.setStartX(0); + bottom.setEndX(20); - Pane stackPane = new Pane(); - stackPane.getChildren().addAll(top, flechaDerecha, vertical, bottom); + Pane stackPane = new Pane(); + stackPane.getChildren().addAll(top, flechaDerecha, vertical, bottom); - return stackPane; - } + return stackPane; + } - /** - * Crear un rectangulo. - * - * @param colores Colores: Los colores para dar color al rectangulo. - * @param label String: El texto por el ID de fxml. - * @return StackPane: Devolver el stackpane que contiene el rectangulo y texto. - */ - public static StackPane crearCaja(Colores colores, String label) { - Rectangle rectangle = new Rectangle(); - rectangle.setHeight(40); - rectangle.setWidth(40); - rectangle.setFill(colores.getFondo()); - rectangle.setStroke(Color.BLACK); - rectangle.setId("caja_" + label); - Text text = new Text(); - text.setId("texto_" + label); - text.setStroke(colores.getTexto()); + /** + * Crear un rectangulo. + * + * @param colores Colores: Los colores para dar color al rectangulo. + * @param label String: El texto por el ID de fxml. + * + * @return StackPane: Devolver el stackpane que contiene el rectangulo y texto. + */ + public static StackPane crearCaja(Colores colores, String label) { + Rectangle rectangle = new Rectangle(); + rectangle.setHeight(40); + rectangle.setWidth(40); + rectangle.setFill(colores.getFondo()); + rectangle.setStroke(Color.BLACK); + rectangle.setId("caja_" + label); + Text text = new Text(); + text.setId("texto_" + label); + text.setStroke(colores.getTexto()); - StackPane stackPane = new StackPane(); - stackPane.getChildren().addAll(rectangle, text); - return stackPane; - } + StackPane stackPane = new StackPane(); + stackPane.getChildren().addAll(rectangle, text); + return stackPane; + } - /** - * Crear un rectangulo con texto adentro. - * - * @param colores Colores: Los colores para dar color al rectangulo. - * @param label String: El texto por el ID de fxml. - * @param texto String: El texto a colocar dentro el rectangulo. - * @return StackPane: Devolver el stackpane que contiene el rectangulo y texto. - */ - public static StackPane crearCaja(Colores colores, String label, String texto) { - Rectangle rectangle = new Rectangle(); - rectangle.setHeight(40); - rectangle.setWidth(40); - rectangle.setFill(colores.getFondo()); - rectangle.setStroke(Color.BLACK); - rectangle.setId("caja_" + label); - Text text = new Text(); - text.setId("texto_" + label); - text.setStroke(colores.getTexto()); - text.setText(texto); + /** + * Crear un rectangulo con texto adentro. + * + * @param colores Colores: Los colores para dar color al rectangulo. + * @param label String: El texto por el ID de fxml. + * @param texto String: El texto a colocar dentro el rectangulo. + * + * @return StackPane: Devolver el stackpane que contiene el rectangulo y texto. + */ + public static StackPane crearCaja(Colores colores, String label, String texto) { + Rectangle rectangle = new Rectangle(); + rectangle.setHeight(40); + rectangle.setWidth(40); + rectangle.setFill(colores.getFondo()); + rectangle.setStroke(Color.BLACK); + rectangle.setId("caja_" + label); + Text text = new Text(); + text.setId("texto_" + label); + text.setStroke(colores.getTexto()); + text.setText(texto); - StackPane stackPane = new StackPane(); - stackPane.getChildren().addAll(rectangle, text); - return stackPane; - } + StackPane stackPane = new StackPane(); + stackPane.getChildren().addAll(rectangle, text); + return stackPane; + } - /** - * Crear 3 rectangulos. - * - * @param colores Colores: Los colores para dar color a los rectangulos. - * @param label String: El texto por el ID de fxml. - * @return StackPane: Devolver el stackpane que contiene los rectangulos y textos. - */ - public static StackPane crearHashCajas(Colores colores, String label) { - Rectangle rectangle = new Rectangle(); - rectangle.setHeight(40); - rectangle.setWidth(40); - rectangle.setFill(colores.getFondo()); - rectangle.setStroke(Color.BLACK); - rectangle.setId("indice_caja_" + label); - Text text = new Text(); - text.setId("indice_texto_" + label); - text.setStroke(colores.getTexto()); + /** + * Crear 3 rectangulos. + * + * @param colores Colores: Los colores para dar color a los rectangulos. + * @param label String: El texto por el ID de fxml. + * + * @return StackPane: Devolver el stackpane que contiene los rectangulos y textos. + */ + public static StackPane crearHashCajas(Colores colores, String label) { + Rectangle rectangle = new Rectangle(); + rectangle.setHeight(40); + rectangle.setWidth(40); + rectangle.setFill(colores.getFondo()); + rectangle.setStroke(Color.BLACK); + rectangle.setId("indice_caja_" + label); + Text text = new Text(); + text.setId("indice_texto_" + label); + text.setStroke(colores.getTexto()); - StackPane stackPane1 = new StackPane(); - stackPane1.getChildren().addAll(rectangle, text); + StackPane stackPane1 = new StackPane(); + stackPane1.getChildren().addAll(rectangle, text); - rectangle = new Rectangle(); - rectangle.setHeight(40); - rectangle.setWidth(120); - rectangle.setFill(colores.getFondo()); - rectangle.setStroke(Color.BLACK); - rectangle.setId("llave_caja_" + label); - text = new Text(); - text.setId("llave_texto_" + label); - text.setStroke(colores.getTexto()); + rectangle = new Rectangle(); + rectangle.setHeight(40); + rectangle.setWidth(120); + rectangle.setFill(colores.getFondo()); + rectangle.setStroke(Color.BLACK); + rectangle.setId("llave_caja_" + label); + text = new Text(); + text.setId("llave_texto_" + label); + text.setStroke(colores.getTexto()); - StackPane stackPane2 = new StackPane(); - stackPane2.getChildren().addAll(rectangle, text); + StackPane stackPane2 = new StackPane(); + stackPane2.getChildren().addAll(rectangle, text); - rectangle = new Rectangle(); - rectangle.setHeight(40); - rectangle.setWidth(40); - rectangle.setFill(colores.getFondo()); - rectangle.setStroke(Color.BLACK); - rectangle.setId("valor_caja_" + label); - text = new Text(); - text.setId("valor_texto_" + label); - text.setStroke(colores.getTexto()); + rectangle = new Rectangle(); + rectangle.setHeight(40); + rectangle.setWidth(40); + rectangle.setFill(colores.getFondo()); + rectangle.setStroke(Color.BLACK); + rectangle.setId("valor_caja_" + label); + text = new Text(); + text.setId("valor_texto_" + label); + text.setStroke(colores.getTexto()); - StackPane stackPane3 = new StackPane(); - stackPane3.getChildren().addAll(rectangle, text); + StackPane stackPane3 = new StackPane(); + stackPane3.getChildren().addAll(rectangle, text); - HBox hBox = new HBox(); - hBox.getChildren().addAll(stackPane1, stackPane2, stackPane3); - hBox.setAlignment(Pos.TOP_CENTER); + HBox hBox = new HBox(); + hBox.getChildren().addAll(stackPane1, stackPane2, stackPane3); + hBox.setAlignment(Pos.TOP_CENTER); - StackPane stackPane = new StackPane(); - stackPane.getChildren().addAll(hBox); - return stackPane; - } + StackPane stackPane = new StackPane(); + stackPane.getChildren().addAll(hBox); + return stackPane; + } - /** - * Destacar un elemento - * - * @param id int: El indice a destacar. - * @param tipo int: El tipo de objeto a destacar, {@value #RECTANGULO}, {@value #CIRCULO} o {@value #TEXTO} - */ - public void destacar(String id, int tipo) { - if (tipo != RECTANGULO && tipo != CIRCULO && tipo != TEXTO) { - return; - } + /** + * Destacar un elemento + * + * @param id int: El indice a destacar. + * @param tipo int: El tipo de objeto a destacar, {@value #RECTANGULO}, {@value #CIRCULO} o {@value #TEXTO} + */ + public void destacar(String id, int tipo) { + if (tipo != RECTANGULO && tipo != CIRCULO && tipo != TEXTO) { + return; + } - Colores colores = new Colores(); - Rectangle rectangle = null; - Circle circle = null; - Text text = null; - if (tipo == RECTANGULO) { - rectangle = (Rectangle) scene.lookup(id); - } - else if (tipo == CIRCULO) { - circle = (Circle) scene.lookup(id); - } - else { - text = (Text) scene.lookup(id); - } - PauseTransition changeColor[] = new PauseTransition[Colores.MAX_COLORS]; - for (int i = 0; i < Colores.MAX_COLORS; i++) { - if (tipo == RECTANGULO) { - changeColor[i] = createPauseTransition(rectangle, colores.getFondo()); - } - else if (tipo == CIRCULO) { - changeColor[i] = createPauseTransition(circle, colores.getFondo()); - } - else { - changeColor[i] = createPauseTransition(text, colores.getTexto()); - } - colores.siguinteColor(); - } + Colores colores = new Colores(); + Rectangle rectangle = null; + Circle circle = null; + Text text = null; + if (tipo == RECTANGULO) { + rectangle = (Rectangle) scene.lookup(id); + } + else if (tipo == CIRCULO) { + circle = (Circle) scene.lookup(id); + } + else { + text = (Text) scene.lookup(id); + } + PauseTransition changeColor[] = new PauseTransition[Colores.MAX_COLORS]; + for (int i = 0; i < Colores.MAX_COLORS; i++) { + if (tipo == RECTANGULO) { + changeColor[i] = createPauseTransition(rectangle, colores.getFondo()); + } + else if (tipo == CIRCULO) { + changeColor[i] = createPauseTransition(circle, colores.getFondo()); + } + else { + changeColor[i] = createPauseTransition(text, colores.getTexto()); + } + colores.siguinteColor(); + } - if (tipo == RECTANGULO) { - destacados.add(new Destacados(tipo, id, (Color) rectangle.getFill(), new SequentialTransition(changeColor))); - } - else if (tipo == CIRCULO) { - destacados.add(new Destacados(tipo, id, (Color) circle.getFill(), new SequentialTransition(changeColor))); - } - else { - destacados.add(new Destacados(tipo, id, (Color) text.getStroke(), new SequentialTransition(changeColor))); - } + if (tipo == RECTANGULO) { + destacados.add(new Destacados(tipo, id, (Color) rectangle.getFill(), new SequentialTransition(changeColor))); + } + else if (tipo == CIRCULO) { + destacados.add(new Destacados(tipo, id, (Color) circle.getFill(), new SequentialTransition(changeColor))); + } + else { + destacados.add(new Destacados(tipo, id, (Color) text.getStroke(), new SequentialTransition(changeColor))); + } - destacados.get(destacados.size() - 1).getSequentialTransition().setCycleCount(Animation.INDEFINITE); - destacados.get(destacados.size() - 1).getSequentialTransition().play(); - } + destacados.get(destacados.size() - 1).getSequentialTransition().setCycleCount(Animation.INDEFINITE); + destacados.get(destacados.size() - 1).getSequentialTransition().play(); + } - /** - * Crear un animacion de transicion usando colores que cambian. - * - * @param rectangle Rectangle: El objeto que desea animar. - * @param colorBackground Color: Color del fondo de destacar. - * - * @return PauseTransition: La transition creado con los elementos y colores. - */ - private static PauseTransition createPauseTransition(Rectangle rectangle, Color colorBackground) { - PauseTransition changeColor = new PauseTransition(new Duration(DURACION)); - changeColor.setOnFinished(actionEvent -> rectangle.setFill(colorBackground)); - return changeColor; - } + /** + * Crear un animacion de transicion usando colores que cambian. + * + * @param rectangle Rectangle: El objeto que desea animar. + * @param colorBackground Color: Color del fondo de destacar. + * + * @return PauseTransition: La transition creado con los elementos y colores. + */ + private static PauseTransition createPauseTransition(Rectangle rectangle, Color colorBackground) { + PauseTransition changeColor = new PauseTransition(new Duration(DURACION)); + changeColor.setOnFinished(actionEvent -> rectangle.setFill(colorBackground)); + return changeColor; + } - /** - * Crear un animacion de transicion usando colores que cambian. - * - * @param circle Circle: El objeto que desea animar. - * @param colorBackground Color: Color del fondo de destacar. - * - * @return PauseTransition: La transition creado con los elementos y colores. - */ - private static PauseTransition createPauseTransition(Circle circle, Color colorBackground) { - PauseTransition changeColor = new PauseTransition(new Duration(DURACION)); - changeColor.setOnFinished(actionEvent -> circle.setFill(colorBackground)); - return changeColor; - } + /** + * Crear un animacion de transicion usando colores que cambian. + * + * @param circle Circle: El objeto que desea animar. + * @param colorBackground Color: Color del fondo de destacar. + * + * @return PauseTransition: La transition creado con los elementos y colores. + */ + private static PauseTransition createPauseTransition(Circle circle, Color colorBackground) { + PauseTransition changeColor = new PauseTransition(new Duration(DURACION)); + changeColor.setOnFinished(actionEvent -> circle.setFill(colorBackground)); + return changeColor; + } - /** - * Crear un animacion de transicion usando colores que cambian. - * - * @param text Text: El texto que desea animar. - * @param colorText Color: Color del texto. - * - * @return PauseTransition: La transition creado con los elementos y colores. - */ - private static PauseTransition createPauseTransition(Text text, Color colorText) { - PauseTransition changeColor = new PauseTransition(new Duration(DURACION)); - changeColor.setOnFinished(actionEvent -> text.setStroke(colorText)); - return changeColor; - } + /** + * Crear un animacion de transicion usando colores que cambian. + * + * @param text Text: El texto que desea animar. + * @param colorText Color: Color del texto. + * + * @return PauseTransition: La transition creado con los elementos y colores. + */ + private static PauseTransition createPauseTransition(Text text, Color colorText) { + PauseTransition changeColor = new PauseTransition(new Duration(DURACION)); + changeColor.setOnFinished(actionEvent -> text.setStroke(colorText)); + return changeColor; + } - /** - * Remover todos los elementos destacados. - */ - public void removerDestacar() { - if (destacados.size() != 0) { - for (int i = 0; i < destacados.size(); i++) { - destacados.get(i).getSequentialTransition().stop(); + /** + * Remover todos los elementos destacados. + */ + public void removerDestacar() { + if (destacados.size() != 0) { + int size = destacados.size(); + for (int i = 0; i < size; i++) { + destacados.get(0).getSequentialTransition().stop(); - if (destacados.get(i).getTipo() == RECTANGULO) { - Rectangle rectangle = (Rectangle) scene.lookup(destacados.get(i).getId()); - rectangle.setFill(destacados.get(i).getColor()); - } - else if (destacados.get(i).getTipo() == CIRCULO) { - Circle circle = (Circle) scene.lookup(destacados.get(i).getId()); - circle.setFill(destacados.get(i).getColor()); - } - else { - Text text = (Text) scene.lookup(destacados.get(i).getId()); - text.setStroke(destacados.get(i).getColor()); - } + if (destacados.get(0).getTipo() == RECTANGULO) { + Rectangle rectangle = (Rectangle) scene.lookup(destacados.get(0).getId()); + rectangle.setFill(destacados.get(0).getColor()); + } + else if (destacados.get(0).getTipo() == CIRCULO) { + Circle circle = (Circle) scene.lookup(destacados.get(0).getId()); + circle.setFill(destacados.get(0).getColor()); + } + else { + Text text = (Text) scene.lookup(destacados.get(0).getId()); + text.setStroke(destacados.get(0).getColor()); + } - destacados.remove(i); - } - } - } + destacados.remove(0); + } + } + } - /** - * Clase de elemento destacado. - * - * @author Chris Cromer - */ - final static private class Destacados { - /** - * El tipo de objeto que está destacado. - */ - final private int tipo; + /** + * Clase de elemento destacado. + * + * @author Chris Cromer + */ + final static private class Destacados { + /** + * El tipo de objeto que está destacado. + */ + final private int tipo; - /** - * El id del elemento destacado. - */ - final private String id; + /** + * El id del elemento destacado. + */ + final private String id; - /** - * El color anterior del elemento destacado. - */ - final private Color color; + /** + * El color anterior del elemento destacado. + */ + final private Color color; - /** - * La animación del elemento destacado. - */ - final private SequentialTransition sequentialTransition; + /** + * La animación del elemento destacado. + */ + final private SequentialTransition sequentialTransition; - /** - * Inicilizar. - * @param tipo int: El tipo de elemento destacado, {@value #RECTANGULO}, {@value #CIRCULO} o {@value #TEXTO}. - * @param id String: El id para identificar el elemento. - * @param color Color: El color anterior para cambiarlo cuando {@link #removerDestacar()} es usado. - * @param sequentialTransition SequentialTransition: La animación a usar. - */ - public Destacados(int tipo, String id, Color color, SequentialTransition sequentialTransition) { - this.tipo = tipo; - this.id = id; - this.color = color; - this.sequentialTransition = sequentialTransition; - } + /** + * Inicilizar. + * @param tipo int: El tipo de elemento destacado, {@value #RECTANGULO}, {@value #CIRCULO} o {@value #TEXTO}. + * @param id String: El id para identificar el elemento. + * @param color Color: El color anterior para cambiarlo cuando {@link #removerDestacar()} es usado. + * @param sequentialTransition SequentialTransition: La animación a usar. + */ + public Destacados(int tipo, String id, Color color, SequentialTransition sequentialTransition) { + this.tipo = tipo; + this.id = id; + this.color = color; + this.sequentialTransition = sequentialTransition; + } - /** - * Devolver el tipo de elemento destacado. - * @return int: El tipo destacado. - */ - public int getTipo() { - return tipo; - } + /** + * Devolver el tipo de elemento destacado. + * @return int: El tipo destacado. + */ + public int getTipo() { + return tipo; + } - /** - * Devolver el ID de elemento destacado. - * @return String: El ID destacado. - */ - public String getId() { - return id; - } + /** + * Devolver el ID de elemento destacado. + * @return String: El ID destacado. + */ + public String getId() { + return id; + } - /** - * Devolver el color anterior antes que fue destacado. - * @return Color: El color anterior. - */ - public Color getColor() { - return color; - } + /** + * Devolver el color anterior antes que fue destacado. + * + * @return Color: El color anterior. + */ + public Color getColor() { + return color; + } - /** - * Devolver la animación que es en uso con el elemento. - * @return SequentialTransition: La animación. - */ - public SequentialTransition getSequentialTransition() { - return sequentialTransition; - } - } + /** + * Devolver la animación que es en uso con el elemento. + * @return SequentialTransition: La animación. + */ + public SequentialTransition getSequentialTransition() { + return sequentialTransition; + } + } } diff --git a/src/cl/cromer/estructuras/HashItem.java b/src/cl/cromer/estructuras/HashItem.java index a7f4ed9..f98a12d 100644 --- a/src/cl/cromer/estructuras/HashItem.java +++ b/src/cl/cromer/estructuras/HashItem.java @@ -1,30 +1,68 @@ package cl.cromer.estructuras; +/** + * La estructura de un elemento de una Tabla Hash. + */ public class HashItem { - final private String llave; + /** + * La llave. + */ + final private String llave; - final private int valor; + /** + * El valor. + */ + final private int valor; - private int indice; + /** + * Donde el elemento está en el array. + */ + private int indice; - public HashItem(String llave, int valor) { - this.llave = llave; - this.valor = valor; - } + /** + * Inicilizar. + * + * @param llave String: La llave del elemento. + * @param valor int: El valor del elemento. + */ + public HashItem(String llave, int valor) { + this.llave = llave; + this.valor = valor; + } - public String getLlave() { - return llave; - } + /** + * Devolver la llave del elemento. + * + * @return String: La llave. + */ + public String getLlave() { + return llave; + } - public int getValor() { - return valor; - } + /** + * Devolver el valor del elemento. + * + * @return int: El valor. + */ + public int getValor() { + return valor; + } - public int getIndice() { - return indice; - } + /** + * Devolver el indice del elemento. + * + * @return int: El indice. + */ + public int getIndice() { + return indice; + } - public void setIndice(int indice) { - this.indice = indice; - } + /** + * Cambiar el indice. + * + * @param indice int: El indice. + */ + public void setIndice(int indice) { + this.indice = indice; + } } \ No newline at end of file diff --git a/src/cl/cromer/estructuras/HashTable.java b/src/cl/cromer/estructuras/HashTable.java index 37a72e9..23a6362 100644 --- a/src/cl/cromer/estructuras/HashTable.java +++ b/src/cl/cromer/estructuras/HashTable.java @@ -1,23 +1,50 @@ package cl.cromer.estructuras; +/** + * Esta clase es tipo de estructura de dato Tabla Hash. + * + * @author Chris Cromer + */ public class HashTable { - final private HashItem hashArray[]; + /** + * El array donde están todos los elementos de la tabla hash. + */ + final private HashItem hashArray[]; - final private int tamano; - private int size; + /** + * El tamaño maximo de la tabla hash. + */ + final private int tamano; - public HashTable(int tamano) { + /** + * Cantidad de elementos que están en la tabla hash. + */ + private int size; + + /** + * Inicilizar. + * + * @param tamano int: El tamaño maximo de la tabla hash. + */ + public HashTable(int tamano) { this.tamano = tamano; hashArray = new HashItem[tamano]; } - public int size() { + /** + * Devolver la cantidad de elementos que están en la tabla. + * + * @return int: La cantidad. + */ + public int size() { return size; } /** * Este metodo crea un hash muy único. + * * @param string String: El string a hashear. + * * @return int: El hash a devolver. */ @SuppressWarnings("unused") @@ -43,7 +70,15 @@ public class HashTable { return (Math.abs(sum) % tamano); } - public boolean insertar(String llave, int valor) { + /** + * Insertar una llave y valor en la tabla hash. + * + * @param llave String: La llave para identificar un elemento. + * @param valor int: El valor a insertar. + * + * @return boolean: Verdad si fue insertado, sino está llena la tabla hash. + */ + public boolean insertar(String llave, int valor) { HashItem hashItem = new HashItem(llave, valor); int hashIndice = hash(hashItem.getLlave()); int i = 0; @@ -84,14 +119,22 @@ public class HashTable { return hash % tamano; } - public boolean eliminar(String llave) { + /** + * Eliminar un elemento de la tabla hash. + * + * @param llave String: La llave a elminar. + * + * @return boolean: Verdad si fue borrado, sino no existiá. + */ + public boolean eliminar(String llave) { HashItem hashItem = new HashItem(llave, 0); int hashIndice = hash(hashItem.getLlave()); int i = 0; while (hashArray[hashIndice] != null && hashArray[hashIndice].getLlave() != null && i < tamano) { if (hashArray[hashIndice].getLlave().equals(llave)) { hashArray[hashIndice] = null; - return true; + size--; + return true; } hashIndice++; hashIndice = hashIndice % tamano; @@ -100,7 +143,14 @@ public class HashTable { return false; } - public HashItem buscar(String llave) { + /** + * Buscar una llave en la tabla hash. + * + * @param llave String: La llave a buscar. + * + * @return HashItem: Devolver el elemento que contine la llave. + */ + public HashItem buscar(String llave) { for (int i = 0; i < tamano; i++) { if (hashArray[i] != null && hashArray[i].getLlave().equals(llave)) { return hashArray[i]; @@ -113,6 +163,7 @@ public class HashTable { * Devolver el valor que está guardado en cada indice. Se usa para construir la grafica. * * @param indice int: El indice que desea ver. + * * @return String: El valor que está en dicho indice. */ public HashItem getIndice(int indice) {