Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.Scanner;
  5.  
  6. class HashTableOpen {
  7.    
  8.     private static Scanner sc;
  9.    
  10.     public static void main(String[] args) {
  11.         sc = new Scanner(System.in);
  12.         int size = sc.nextInt();
  13.         HashTableAberta hashtable = new HashTableAberta(size);
  14.         String op;
  15.         int key;
  16.         do {
  17.             op = sc.nextLine();
  18.             switch (op.split(" ")[0]) {
  19.             case "keys":
  20.                 System.out.println(Arrays.toString(hashtable.keys()));
  21.                 break;
  22.             case "values":
  23.                 System.out.println(Arrays.toString(hashtable.values()));
  24.                 break;
  25.             case "put":
  26.                 key = Integer.parseInt(op.split(" ")[1]);
  27.                 String value = op.split(" ")[2];
  28.                 hashtable.put(key, value);
  29.                 System.out.println(hashtable.toString());
  30.                 break;
  31.             case "remove":
  32.                 key = Integer.parseInt(op.split(" ")[1]);
  33.                 hashtable.remove(key);
  34.                 System.out.println(hashtable.toString());
  35.             default:
  36.                 break;
  37.             }
  38.         } while (!op.equals("end"));
  39.     }
  40.  
  41. }
  42.  
  43. class Pair {
  44.    
  45.     private Integer key;
  46.     private String value;
  47.    
  48.     public Pair(Integer key, String value) {
  49.         this.key = key;
  50.         this.value = value;
  51.     }
  52.    
  53.     public Integer getKey() {
  54.         return key;
  55.     }
  56.    
  57.     public String getValue() {
  58.         return value;
  59.     }
  60.    
  61.     public void setValue(String value) {
  62.         this.value = value;
  63.     }
  64.    
  65.     @Override
  66.     public int hashCode() {
  67.         final int prime = 31;
  68.         int result = 1;
  69.         result = prime * result + ((key == null) ? 0 : key.hashCode());
  70.         return result;
  71.     }
  72.  
  73.     @Override
  74.     public boolean equals(Object obj) {
  75.         if (this == obj)
  76.             return true;
  77.         if (obj == null)
  78.             return false;
  79.         if (getClass() != obj.getClass())
  80.             return false;
  81.         Pair other = (Pair) obj;
  82.         if (key == null) {
  83.             if (other.key != null)
  84.                 return false;
  85.         } else if (!key.equals(other.key))
  86.             return false;
  87.         return true;
  88.     }
  89.  
  90.     public String toString() {
  91.         return "<" + this.getKey() + ", " + this.getValue() + ">";
  92.     }
  93.    
  94. }
  95.  
  96. class HashTableAberta {
  97.    
  98.     private static final String DELETED = "DELETED";
  99.    
  100.     private Pair[] table;
  101.     private int capacity;
  102.     private int elements;
  103.     private ArrayList<Integer> keys;
  104.    
  105.     public HashTableAberta(int size) {
  106.         this.table = new Pair[size];
  107.         this.capacity = size;
  108.         this.elements = 0;
  109.         keys = new ArrayList<Integer>();
  110.     }
  111.    
  112.     private int hashFunction(Integer key) {
  113.         return key % capacity;
  114.     }
  115.    
  116.     private int probeLinear(int key, int probe) {
  117.         return this.hashFunction(key + probe);
  118.     }
  119.    
  120.     public void put(Integer key, String value) {
  121.         int probe = 0;
  122.         int hash = probeLinear(key, probe);
  123.         while (probe < capacity && table[hash] != null
  124.                 && !table[hash].getValue().equals(DELETED) && !table[hash].getKey().equals(key)) {
  125.             hash = probeLinear(key, ++probe);
  126.         }
  127.        
  128.         if (table[hash] != null && table[hash].getKey().equals(key)) {
  129.             table[hash].setValue(value);
  130.         } else if (table[hash] == null || table[hash].getValue().equals(DELETED)) {
  131.             table[hash] = new Pair(key, value);
  132.             elements += 1;
  133.             keys.add(key);
  134.         }
  135.     }
  136.    
  137.     public void remove(Integer key) {
  138.         if (keys.remove(key)) {
  139.             int probe = 0;
  140.             int hash = probeLinear(key, probe);
  141.             while (probe < capacity && table[hash] != null && !table[hash].getKey().equals(key)) {
  142.                 hash = probeLinear(key, ++probe);
  143.             }
  144.            
  145.             if (table[hash] != null && table[hash].getKey().equals(key)) {
  146.                 table[hash].setValue(DELETED);
  147.                 elements -= 1;
  148.             }
  149.         }
  150.     }
  151.    
  152.     public Object[] keys() {
  153.         Collections.sort(keys);
  154.         return keys.toArray();
  155.     }
  156.    
  157.     public String[] values() {
  158.         String[] values = new String[elements];
  159.         int index = 0;
  160.         for (Pair element : table) {
  161.             if (element != null) {
  162.                 values[index++] = element.getValue();
  163.             }
  164.         }
  165.         Arrays.sort(values);
  166.         return values;
  167.     }
  168.    
  169.     public String toString() {
  170.         return Arrays.toString(table);
  171.     }
  172.    
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement