Advertisement
gowtham900

chaining

Oct 22nd, 2021
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class ProbeHashMap<K,V>extends AbstractHashMap<K,V> {
  4.     private MapEntry<K,V>[]TABLE;
  5.     private MapEntry<K,V>DEFUNCT=new MapEntry<>(null,null);
  6.     public ProbeHashMap()
  7.     {
  8.         super();
  9.  
  10.     }
  11.     public ProbeHashMap(int cap){
  12.         super(cap);
  13.     }
  14.     public ProbeHashMap(int cap,int p){
  15.         super(cap,p);
  16.     }
  17.     protected void createTable(){
  18.         table=(MapEntry<K,V>[])new MapEntry[capacity];
  19.  
  20.     }
  21.     private boolean isAvailable(int j){
  22.         return (table[j]==null||table[j]==DEFUNCT);
  23.  
  24.     }
  25.     private int findSlot(int h,K k) {
  26.         int avail = -1;
  27.         int j = h;
  28.         do {
  29.             if (isAvailable(j)) {
  30.                 if (avail == -1) avail = j;
  31.                 if (table[j] == null) break;
  32.  
  33.             } else if (table[j].getKey().equals(k))
  34.                 return j;
  35.             j = (j + 1) % capacity;
  36.  
  37.         } while (j != h);
  38.         return -(avail + 1);
  39.     }
  40.     protected V bucketGet(int h,K k){
  41.         int j=findSlot(h,k);
  42.         if(j<0)return null;
  43.         return table[j].getValue();
  44.     }
  45.     protected V bucketPut(int h,K k,V v){
  46.         int j=findSlot(h,k);
  47.         if(j<0)return null;
  48.         V answer=table[j].getValue();
  49.         table[j]=DEFUNCT;
  50.         n--;
  51.         return answer;
  52.     }
  53.     public Iterable<Entry<K,V>>entrySet(){
  54.         ArrayList<Entry<K,V>>buffer=new ArrayList<>();
  55.         for(int h=0;h<capacity;h++)
  56.             if(!isAvailable((h))buffer.add(table[h]));
  57.         return buffer;
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement