Advertisement
Guest User

Hash table (97/100)

a guest
Jan 27th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 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.     int hash = hash(key);
  81.    
  82.     // Start at entry hash(key) and probe until..
  83.     for(int i = 0; i < this.table.length; i++) {
  84.       int idx = (hash + i) % this.table.length;
  85.      
  86.       // empty cell is found
  87.       if (isEmpty(idx))
  88.         return null;
  89.        
  90.       // entry with key is found
  91.       if (this.table[idx].getKey() == key) {
  92.         return this.table[idx].getValue();
  93.       }
  94.     }
  95.  
  96.     return null;
  97.   }
  98.  
  99.   /**
  100.    * Remove the entry associated with this key from the hash table.
  101.    *
  102.    * Returns false, if the key is null.
  103.    *
  104.    * @param key
  105.    *     String representing the key of the entry to remove.
  106.    * @return true iff the entry has been successfully removed, else false.
  107.    */
  108.   public boolean remove(String key) {
  109.     if (null == key)
  110.       return false;
  111.    
  112.     int hash = hash(key);
  113.    
  114.     // Search for entry with hash(key) and replace with DEFUNCT
  115.     for(int i = 0; i < this.table.length; i++) {
  116.       int idx = (hash + i) % this.table.length;
  117.      
  118.       // Break if we find empty position
  119.       if(isEmpty(idx))
  120.         return false;
  121.      
  122.       // entry with key is found
  123.       if (this.table[idx].getKey() == key) {
  124.         setDefunct(idx);
  125.         capacity++;
  126.         return true;
  127.       }
  128.     }
  129.    
  130.     return false;
  131.   }
  132.  
  133.   /**
  134.    * Takes as input an index and sets the entry in that location as defunct.
  135.    *
  136.    * @param index
  137.    *     The index of the spot that is defunct.
  138.    */
  139.   public void setDefunct(int index) {
  140.     this.table[index] = new Entry(null, null);
  141.   }
  142.  
  143.     /**
  144.    * Takes as input an index and checks if that location is either null or set as defunct.
  145.    *
  146.    * @param index
  147.    *     The index of the spot that is defunct.
  148.    * @return true iff there is no entry at the given location.
  149.    */
  150.   public boolean isEmpty(int index) {
  151.     return (null == this.table[index] || this.table[index].getKey() == null);
  152.   }
  153.  
  154.   /**
  155.    * Hashes a string representing a key.
  156.    *
  157.    * @param key
  158.    *     String that needs to be hashed.
  159.    * @return the hashcode of the string, modulo the capacity of the HashTable.
  160.    */
  161.   public int hash(String key) {
  162.     return Math.abs(key.hashCode()) % this.table.length;
  163.   }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement