Advertisement
Guest User

Hash table (94/100)

a guest
Jan 27th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. class SolutionHashTable {
  2.   public Entry[] table;
  3.   public int capacity;
  4.  
  5.   /**
  6.    * Constructs a new HashTable.
  7.    *
  8.    * Capacity of the hash table can not be 0 or negative.
  9.    *
  10.    * @param capacity
  11.    *     to be used as capacity of the table.
  12.    * @throws IllegalArgumentException
  13.    *     if the input capacity is invalid.
  14.    */
  15.   public SolutionHashTable(int capacity) {
  16.     if (capacity < 1)
  17.       throw new IllegalArgumentException();
  18.    
  19.     this.table = new Entry[capacity];
  20.     this.capacity = capacity;
  21.   }
  22.  
  23.   /**
  24.    * Add a new Entry to the hash table,
  25.    * uses linear probing to deal with collisions.
  26.    *
  27.    * Returns false, if the key is null or the table is full.
  28.    *
  29.    * @param key
  30.    *     String representing the key of the entry.
  31.    * @param value
  32.    *     String representing the value of the entry.
  33.    * @return true iff entry has been added successfully, else false.
  34.    */
  35.   public boolean put(String key, String value) {
  36.     // No null keys allowed
  37.     if (null == key)
  38.       return false;
  39.      
  40.     int hash = hash(key);
  41.  
  42.     // If key exists, replace value
  43.     if (!isEmpty(hash) && table[hash].getKey() == key) {
  44.       this.table[hash] = new Entry(key, value);
  45.       return true;
  46.     }
  47.      
  48.     // Otherwise, return false if table is full
  49.     if (this.capacity < 1)
  50.       return false;
  51.    
  52.     // Otherwise start at entry hash(key)
  53.     // and probe until empty spot is found
  54.     for(int i = 0; i < this.table.length; i++) {
  55.       int idx = (hash + i) % this.table.length;
  56.      
  57.       if (isEmpty(idx)) {
  58.         this.table[idx] = new Entry(key, value);
  59.         capacity--; // Reduce capacity
  60.         return true;
  61.       }
  62.     }
  63.    
  64.     return false;
  65.   }
  66.  
  67.   /**
  68.    * Retrieve the value of the entry associated with this key.
  69.    *
  70.    * Returns null, if the key is null.
  71.    *
  72.    * @param key
  73.    *     String representing the key of the entry to look for.
  74.    * @return value of the entry as String iff the entry with this key is found in the hash table, else null.
  75.    */
  76.   public String get(String key) {
  77.     if (null == key)
  78.       return null;
  79.    
  80.     String result = null;
  81.     int hash = hash(key);
  82.    
  83.     // Start at entry hash(key) and probe until..
  84.     for(int i = 0; i < this.table.length; i++) {
  85.       int idx = (hash + i) % this.table.length;
  86.      
  87.       // empty cell is found
  88.       if (isEmpty(idx))
  89.         break;
  90.        
  91.       // entry with key is found
  92.       if (this.table[idx].getKey() == key) {
  93.         result = this.table[idx].getValue();
  94.         break;
  95.       }
  96.     }
  97.  
  98.     return result;
  99.   }
  100.  
  101.   /**
  102.    * Remove the entry associated with this key from the hash table.
  103.    *
  104.    * Returns false, if the key is null.
  105.    *
  106.    * @param key
  107.    *     String representing the key of the entry to remove.
  108.    * @return true iff the entry has been successfully removed, else false.
  109.    */
  110.   public boolean remove(String key) {
  111.     if (null == key)
  112.       return false;
  113.    
  114.     int hash = hash(key);
  115.    
  116.     // Search for entry with hash(key) and replace with DEFUNCT
  117.     for(int i = 0; i < this.table.length; i++) {
  118.       int idx = (hash + i) % this.table.length;
  119.      
  120.       // entry with key is found
  121.       if (!isEmpty(idx) && this.table[idx].getKey() == key) {
  122.         setDefunct(idx);
  123.         capacity++;
  124.         return true;
  125.       }
  126.     }
  127.    
  128.     return false;
  129.   }
  130.  
  131.   /**
  132.    * Takes as input an index and sets the entry in that location as defunct.
  133.    *
  134.    * @param index
  135.    *     The index of the spot that is defunct.
  136.    */
  137.   public void setDefunct(int index) {
  138.     this.table[index] = new Entry(null, null);
  139.   }
  140.  
  141.     /**
  142.    * Takes as input an index and checks if that location is either null or set as defunct.
  143.    *
  144.    * @param index
  145.    *     The index of the spot that is defunct.
  146.    * @return true iff there is no entry at the given location.
  147.    */
  148.   public boolean isEmpty(int index) {
  149.     return (null == this.table[index] || this.table[index].getKey() == null);
  150.   }
  151.  
  152.   /**
  153.    * Hashes a string representing a key.
  154.    *
  155.    * @param key
  156.    *     String that needs to be hashed.
  157.    * @return the hashcode of the string, modulo the capacity of the HashTable.
  158.    */
  159.   public int hash(String key) {
  160.     return Math.abs(key.hashCode()) % this.table.length;
  161.   }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement