*** empty log message ***
[IRC.git] / Robust / Transactions / dstm2 / src / dstm2 / util / HashMap.java
index df7c54ed0c311998a0b6e2c08dd847da6e19823a..cf3f44c043c50d2db32580b9fd558327c62267f6 100644 (file)
@@ -1,5 +1,6 @@
 package dstm2.util;
 
+import dstm2.AtomicArray;
 import java.io.IOException;
 import java.util.AbstractSet;
 import java.util.ConcurrentModificationException;
@@ -12,52 +13,53 @@ import dstm2.Thread;
 import dstm2.atomic;
 import dstm2.factory.Factory;
 
-public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
-       
+public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V> {
+
     /**
      * The default initial capacity - MUST be a power of two.
      */
-    static final int DEFAULT_INITIAL_CAPACITY = 16;
-
+    static final int DEFAULT_INITIAL_CAPACITY = 4096;
     /**
      * The maximum capacity, used if a higher value is implicitly specified
      * by either of the constructors with arguments.
      * MUST be a power of two <= 1<<30.
      */
     static final int MAXIMUM_CAPACITY = 1 << 30;
-
     /**
      * The load factor used when none specified in constructor.
      **/
     static final float DEFAULT_LOAD_FACTOR = 0.75f;
-
     /**
      * The table, resized as necessary. Length MUST Always be a power of two.
      */
     //transient TEntry<V>[] table;
-    dstm2.AtomicArray<TEntry> table;
-    final private Factory<TEntry> factory;
-       
+    //dstm2.AtomicArray<TEntry> table;
+    //dstm2.AtomicArray<Wrapper> table;
+    tableHolder tableholder;
+    //dstm2.AtomicArray<Wrapper> wrappers;
+    final private Factory<tableHolder> factoryTable;
+    final private Factory<TEntry> factoryTEntry;
+    final private Factory<Wrapper> factorywrapper;
+    final private Factory<transactionalintfield> factoryint;
     //transient Entry<K, V>[] table;
-
     /**
      * The number of key-value mappings contained in this identity hash map.
      */
     //transient int size;
-    
-    java.util.concurrent.atomic.AtomicInteger size;
-    
+    //  java.util.concurrent.atomic.AtomicInteger size;
     /*
      * The capacity of the table
      */
-    transient int capacity;
-  
+    transactionalintfield capacity;
+//    transient int capacity;
     /**
      * The next size value at which to resize (capacity * load factor).
      * @serial
      */
-    int threshold;
-  
+    transactionalintfield threshold;
+    //transactionalintfield size;
+    final int size = 2400;
+    //int threshold;
     /**
      * The load factor for the hash table.
      *
@@ -72,8 +74,7 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * rehash).  This field is used to make iterators on Collection-views of
      * the HashMap fail-fast.  (See ConcurrentModificationException).
      */
-    transient volatile int modCount;
-
+//    transient volatile int modCount;
     /**
      * Constructs an empty <tt>HashMap</tt> with the specified initial
      * capacity and load factor.
@@ -84,33 +85,54 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      *         or the load factor is nonpositive.
      */
     public HashMap(int initialCapacity, float loadFactor) {
-       size = new AtomicInteger(0);
-       factory = Thread.makeFactory(TEntry.class);
-        if (initialCapacity < 0)
+        //  size = new AtomicInteger(0);
+        factoryTEntry = Thread.makeFactory(TEntry.class);
+        factorywrapper = Thread.makeFactory(Wrapper.class);
+        factoryTable = Thread.makeFactory(tableHolder.class);
+        factoryint = Thread.makeFactory(transactionalintfield.class);
+        if (initialCapacity < 0) {
             throw new IllegalArgumentException("Illegal initial capacity: " +
-                                               initialCapacity);
-        if (initialCapacity > MAXIMUM_CAPACITY)
+                    initialCapacity);
+        }
+        if (initialCapacity > MAXIMUM_CAPACITY) {
             initialCapacity = MAXIMUM_CAPACITY;
-        if (loadFactor <= 0 || Float.isNaN(loadFactor))
+        }
+        if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
             throw new IllegalArgumentException("Illegal load factor: " +
-                                               loadFactor);
+                    loadFactor);
 
         // Find a power of 2 >= initialCapacity
-        int capacity = 1;
-        while (capacity < initialCapacity) 
-            capacity <<= 1;
-    
-        this.capacity = capacity;
+        }
+        int tmpcapacity = 1;
+        while (tmpcapacity < initialCapacity) {
+            tmpcapacity <<= 1;
+        }
+        capacity = factoryint.create();
+        threshold = factoryint.create();
+//        size = factoryint.create();
+        // size = 2047;
+     //   size.setValue(0);
+
+        capacity.setValue(tmpcapacity);
         this.loadFactor = loadFactor;
-        threshold = (int)(capacity * loadFactor);
-        table = new dstm2.AtomicArray<TEntry>(TEntry.class, capacity);
+        threshold.setValue((int) (capacity.getValue() * loadFactor));
+        //threshold = (int) (capacity * loadFactor);
+        //table = new dstm2.AtomicArray<TEntry>(TEntry.class, capacity);
+        tableholder = factoryTable.create();
+        AtomicArray<Wrapper> table = new dstm2.AtomicArray<Wrapper>(Wrapper.class, capacity.getValue());
+        tableholder.setTable(table);
+        for (int i = 0; i < capacity.getValue(); i++) {
+            tableholder.getTable().set(i, factorywrapper.create());
+            tableholder.getTable().get(i).setTEntry(factoryTEntry.create());
+        //table.get(i).setTEntry(factoryTEntry.create());
+        }
 //        for(int i = 0; i < capacity; i++) {
 //             table[i] = factory.create();
 //        }
         //table = new Entry[capacity];
         init();
     }
-  
+
     /**
      * Constructs an empty <tt>HashMap</tt> with the specified initial
      * capacity and the default load factor (0.75).
@@ -127,7 +149,7 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * (16) and the default load factor (0.75).
      */
     public HashMap() {
-       this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
+        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
     }
 
     /**
@@ -141,12 +163,11 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      */
     public HashMap(HashMap<? extends V> m) {
         this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
-                      DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
+                DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
         putAllForCreate(m);
     }
 
     // internal utilities
-
     /**
      * Initialization hook for subclasses. This method is called
      * in all constructors and pseudo-constructors (clone, readObject)
@@ -156,8 +177,9 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      */
     void init() {
     }
-    public dstm2.AtomicArray<TEntry> getBuckets() {
-       return table;
+
+    public dstm2.AtomicArray<Wrapper>/*dstm2.AtomicArray<TEntry>*/ getBuckets() {
+        return tableholder.getTable();
     }
     /**
      * Value representing null keys inside tables.
@@ -168,7 +190,7 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * Returns internal representation for key. Use NULL_KEY if key is null.
      */
     static <T> T maskNull(T key) {
-        return key == null ? (T)NULL_KEY : key;
+        return key == null ? (T) NULL_KEY : key;
     }
 
     /**
@@ -177,7 +199,6 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
     static <T> T unmaskNull(T key) {
         return (key == NULL_KEY ? null : key);
     }
-
     /**
      * Whether to prefer the old supplemental hash function, for
      * compatibility with broken applications that rely on the
@@ -187,13 +208,17 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * -XX:+UseNewHashFunction or -XX:+AggressiveOpts
      */
     private static final boolean useNewHash;
-    static { useNewHash = false; }
+    
+
+    static {
+        useNewHash = false;
+    }
 
     private static int oldHash(int h) {
         h += ~(h << 9);
-        h ^=  (h >>> 14);
-        h +=  (h << 4);
-        h ^=  (h >>> 10);
+        h ^= (h >>> 14);
+        h += (h << 4);
+        h ^= (h >>> 10);
         return h;
     }
 
@@ -213,11 +238,11 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * in lower bits.
      */
     public static int hash(int h) {
-       return useNewHash ? newHash(h) : oldHash(h);
+        return useNewHash ? newHash(h) : oldHash(h);
     }
 
     static int hash(Object key) {
-       return hash(key.hashCode());
+        return hash(key.hashCode());
     }
 
     /** 
@@ -231,25 +256,25 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * Returns index for hash code h. 
      */
     public static int indexFor(int h, int length) {
-        return h & (length-1);
+        return h & (length - 1);
     }
+
     /**
      * Returns the number of key-value mappings in this map.
      *
      * @return the number of key-value mappings in this map.
      */
     public int size() {
-        return size.get();
+        return size;//.getValue();
     }
-  
+
     /**
      * Returns <tt>true</tt> if this map contains no key-value mappings.
      *
      * @return <tt>true</tt> if this map contains no key-value mappings.
      */
     public boolean isEmpty() {
-        return size.get() == 0;
+        return size/*.getValue()*/ == 0;
     }
 
     /**
@@ -269,12 +294,14 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
 //             if (key == null)
 //                 return getForNullKey();
         int hash = hash(key);
-        for (TEntry<V> e = table.get(indexFor(hash, capacity));
-             e != null;
-             e = e.getNext()) {
-            Object k;
-            if (e.getHash() == hash)
+        //for (TEntry<V> e = table.get(indexFor(hash, capacity))
+
+        for (TEntry<V> e = tableholder.getTable().get(indexFor(hash, capacity.getValue())).getTEntry();
+                e != null;
+                e = e.getNext()) {
+            if (e.getHash() == hash) {
                 return e.getValue();
+            }
         }
         return null;
     }
@@ -292,7 +319,6 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
 //            e = e.getNext();
 //        }
 //    }
-
     /**
      * Returns <tt>true</tt> if this map contains a mapping for the
      * specified key.
@@ -304,11 +330,13 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
     public boolean containsKey(int key) {
         Object k = maskNull(key);
         int hash = hash(k);
-        int i = indexFor(hash, capacity);
-        TEntry<V> e = table.get(i); 
+        int i = indexFor(hash, capacity.getValue());
+        //TEntry<V> e = table.get(i);
+        TEntry<V> e = tableholder.getTable().get(i).getTEntry();
         while (e != null) {
-            if (e.getHash() == hash) 
+            if (e.getHash() == hash) {
                 return true;
+            }
             e = e.getNext();
         }
         return false;
@@ -321,13 +349,15 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      */
     TEntry<V> getEntry(int key) {
         int hash = hash(key);
-        int i = indexFor(hash, capacity);
-        TEntry<V> e = table.get(i); 
-        while (e != null && !(e.getHash() == hash))
+        int i = indexFor(hash, capacity.getValue());
+        //TEntry<V> e = table.get(i);
+        TEntry<V> e = tableholder.getTable().get(i).getTEntry();
+        while (e != null && !(e.getHash() == hash)) {
             e = e.getNext();
+        }
         return e;
     }
-  
+
     /**
      * Associates the specified value with the specified key in this map.
      * If the map previously contained a mapping for this key, the old
@@ -342,22 +372,25 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      */
     public V put(int key, V value) {
         int hash = hash(key);
-        int i = indexFor(hash, capacity);
-        for (TEntry<V> e = table.get(i); e != null; e = e.getNext()) {
-            if (e.getHash() == hash) {
-                V oldValue = e.getValue();
-                e.setValue(value);
+        int i = indexFor(hash, capacity.getValue());
+        //for (TEntry<V> e = table.get(i); e != null; e = e.getNext()) {
+        try {
+            for (TEntry<V> e = tableholder.getTable().get(i).getTEntry(); e != null; e = e.getNext()) {
+                if (e.getHash() == hash) {
+                    V oldValue = e.getValue();
+                    e.setValue(value);
 //                e.recordAccess(this);
-                return oldValue;
+                    return oldValue;
+                }
             }
+        } catch (NullPointerException e) {
+            e.printStackTrace();
         }
-        modCount++;
+//        modCount++;
         addEntry(hash, value, i);
         return null;
     }
 
-
-
     /**
      * This method is used instead of put by constructors and
      * pseudoconstructors (clone, readObject).  It does not resize the table,
@@ -366,14 +399,15 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      */
     private void putForCreate(int key, V value) {
         int hash = hash(key);
-        int i = indexFor(hash, capacity);
+        int i = indexFor(hash, capacity.getValue());
 
         /**
          * Look for preexisting entry for key.  This will never happen for
          * clone or deserialize.  It will only happen for construction if the
          * input Map is a sorted map whose ordering is inconsistent w/ equals.
          */
-        for (TEntry<V> e = table.get(i); e != null; e = e.getNext()) {
+        //for (TEntry<V> e = table.get(i); e != null; e = e.getNext()) {
+        for (TEntry<V> e = tableholder.getTable().get(i).getTEntry(); e != null; e = e.getNext()) {
             if (e.getHash() == hash) {
                 e.setValue(value);
                 return;
@@ -384,7 +418,7 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
     }
 
     void putAllForCreate(HashMap<? extends V> m) {
-        for (Iterator<? extends HashMap.TEntry<? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
+        for (Iterator<? extends HashMap.TEntry<? extends V>> i = m.entrySet().iterator(); i.hasNext();) {
             HashMap.TEntry<? extends V> e = i.next();
             putForCreate(e.getHash(), e.getValue());
         }
@@ -405,35 +439,58 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      *        is irrelevant).
      */
     void resize(int newCapacity) {
-        dstm2.AtomicArray<TEntry> oldTable = table;
-        int oldCapacity = capacity;
+        //dstm2.AtomicArray<TEntry> oldTable = table;
+        //dstm2.AtomicArray<Wrapper> oldTable = tableholder.getTable();
+        int oldCapacity = capacity.getValue();
         if (oldCapacity == MAXIMUM_CAPACITY) {
-            threshold = Integer.MAX_VALUE;
+            threshold.setValue(Integer.MAX_VALUE);
             return;
         }
 
-        dstm2.AtomicArray<TEntry> newTable = new dstm2.AtomicArray<TEntry>(TEntry.class, newCapacity);
+        //dstm2.AtomicArray<TEntry> newTable = new dstm2.AtomicArray<TEntry>(TEntry.class, newCapacity);
+        dstm2.AtomicArray<Wrapper> newTable = new dstm2.AtomicArray<Wrapper>(Wrapper.class, newCapacity);
+        for (int i = 0; i < newCapacity; i++) {
+            if (i < capacity.getValue()) {
+                newTable.set(i, tableholder.getTable().get(i)/*factorywrapper.create()*/);
+                  newTable.get(i).setTEntry(factoryTEntry.create());
+            } else {
+                newTable.set(i, factorywrapper.create());
+                newTable.get(i).setTEntry(factoryTEntry.create());
+            }
+        }
+        //capacity.setValue(newCapacity);
         transfer(newTable, newCapacity);
-        table = newTable;
-        threshold = (int)(newCapacity * loadFactor);
-        capacity = newCapacity;
+        tableholder.setTable(newTable);
+        threshold.setValue((int) (newCapacity * loadFactor));
+        capacity.setValue(newCapacity);
     }
 
     /** 
      * Transfer all entries from current table to newTable.
      */
-    void transfer(dstm2.AtomicArray<TEntry> newTable, int nc) {
-       dstm2.AtomicArray<TEntry> src = table;
+    //void transfer(dstm2.AtomicArray<TEntry> newTable, int nc) {
+    void transfer(dstm2.AtomicArray<Wrapper> newTable, int nc) {
+        //dstm2.AtomicArray<TEntry> src = table;
+        //dstm2.AtomicArray<Wrapper> src = table;
         int newCapacity = nc;
-        for (int j = 0; j < capacity; j++) {
-            TEntry<V> e = src.get(j);
+        for (int j = 0; j < capacity.getValue(); j++) {
+            TEntry<V> e = tableholder.getTable().get(j).getTEntry();
+            //TEntry<V> e = src.get(j);
             if (e != null) {
-                src.set(j, null);
+                tableholder.getTable().set(j, null);
                 do {
-                    TEntry<V> next = e.getNext();
-                    int i = indexFor(e.getHash(), newCapacity);  
+                    /*TEntry<V> next = e.getNext();
+                    int i = indexFor(e.getHash(), newCapacity);
                     e.setNext(newTable.get(i));
                     newTable.set(i, e);
+                    e = next;*/
+
+                    TEntry<V> next = e.getNext();
+                    int i = indexFor(e.getHash(), newCapacity);
+                    e.setNext(newTable.get(i).getTEntry());
+                    //e.setNext(tableholder.getTable().get(i).getTEntry());
+                    newTable.get(i).setTEntry(e);
+                    //tableholder.getTable().get(i).setTEntry(e);
                     e = next;
                 } while (e != null);
             }
@@ -450,7 +507,7 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      */
     public void putAll(HashMap<? extends V> m) {
         int numKeysToBeAdded = m.size();
-        if (numKeysToBeAdded == 0)
+        if (numKeysToBeAdded == 0) {
             return;
 
         /*
@@ -462,23 +519,27 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
          * By using the conservative calculation, we subject ourself
          * to at most one extra resize.
          */
-        if (numKeysToBeAdded > threshold) {
-            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
-            if (targetCapacity > MAXIMUM_CAPACITY)
+        }
+        if (numKeysToBeAdded > threshold.getValue()) {
+            int targetCapacity = (int) (numKeysToBeAdded / loadFactor + 1);
+            if (targetCapacity > MAXIMUM_CAPACITY) {
                 targetCapacity = MAXIMUM_CAPACITY;
-            int newCapacity = capacity;
-            while (newCapacity < targetCapacity)
+            }
+            int newCapacity = capacity.getValue();
+            while (newCapacity < targetCapacity) {
                 newCapacity <<= 1;
-            if (newCapacity > capacity)
+            }
+            if (newCapacity > capacity.getValue()) {
                 resize(newCapacity);
+            }
         }
 
-        for (Iterator<? extends HashMap.TEntry<? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
+        for (Iterator<? extends HashMap.TEntry<? extends V>> i = m.entrySet().iterator(); i.hasNext();) {
             HashMap.TEntry<? extends V> e = i.next();
             put(e.getHash(), e.getValue());
         }
     }
-  
+
     /**
      * Removes the mapping for this key from this map if present.
      *
@@ -500,26 +561,30 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      */
     TEntry<V> removeEntryForKey(int key) {
         int hash = hash(key);
-        int i = indexFor(hash, capacity);
-        TEntry<V> prev = table.get(i);
+        int i = indexFor(hash, capacity.getValue());
+        //TEntry<V> prev = table.get(i);
+        TEntry<V> prev = tableholder.getTable().get(i).getTEntry();
         TEntry<V> e = prev;
 
         while (e != null) {
             TEntry<V> next = e.getNext();
             if (e.getHash() == hash) {
-                modCount++;
-                size.decrementAndGet();
-                if (prev == e) 
-                    table.set(i, next);
-                else
+//                modCount++;
+               // size.setValue(size.getValue() - 1);
+            //    size--;
+                if (prev == e) {
+                    tableholder.getTable().get(i).setTEntry(next);
+                //table.set(i, next);
+                } else {
                     prev.setNext(next);
 //                e.recordRemoval(this);
+                }
                 return e;
             }
             prev = e;
             e = next;
         }
-   
+
         return e;
     }
 
@@ -530,26 +595,30 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
 
         TEntry<V> entry = o;
         int hash = hash(o.getHash());
-        int i = indexFor(hash, capacity);
-        TEntry<V> prev = table.get(i);
+        int i = indexFor(hash, capacity.getValue());
+        //TEntry<V> prev = table.get(i);
+        TEntry<V> prev = tableholder.getTable().get(i).getTEntry();
         TEntry<V> e = prev;
 
         while (e != null) {
             TEntry<V> next = e.getNext();
             if (e.getHash() == hash) {
-                modCount++;
-                size.decrementAndGet();
-                if (prev == e) 
-                       table.set(i,  next);
-                else
+///                modCount++;
+//                size.setValue(size.getValue() - 1);
+                //size--;
+                if (prev == e) {
+                    //table.set(i, next);
+                    tableholder.getTable().get(i).setTEntry(next);
+                } else {
                     prev.setNext(next);
 //                e.recordRemoval(this);
+                }
                 return e;
             }
             prev = e;
             e = next;
         }
-   
+
         return e;
     }
 
@@ -557,11 +626,14 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * Removes all mappings from this map.
      */
     public void clear() {
-        modCount++;
-        dstm2.AtomicArray<TEntry> tab = table;
-        for (int i = 0; i < capacity; i++) 
-               table.set(i, null);
-        size.set(0);
+//        modCount++;
+        //dstm2.AtomicArray<TEntry> tab = table;
+
+        for (int i = 0; i < capacity.getValue(); i++) {
+            tableholder.getTable().set(i, null);
+        }
+      //  size = 0;
+        //size.setValue(0);
     }
 
     /**
@@ -573,39 +645,78 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      *         specified value.
      */
     public boolean containsValue(Object value) {
-       if (value == null) 
+        if (value == null) {
             return containsNullValue();
-
-       dstm2.AtomicArray<TEntry> tab = table;
-        for (int i = 0; i < capacity; i++)
-            for (TEntry e = tab.get(i); e != null ; e = e.getNext())
-                if (value.equals(e.getValue()))
+        }
+        //dstm2.AtomicArray<TEntry> tab = table;
+        //dstm2.AtomicArray<Wrapper> tab = table;
+        for (int i = 0; i < capacity.getValue(); i++) {
+            //for (TEntry e = tab.get(i); e != null; e = e.getNext()) {
+            for (TEntry e = tableholder.getTable().get(i).getTEntry(); e != null; e = e.getNext()) {
+                if (value.equals(e.getValue())) {
                     return true;
-       return false;
+                }
+            }
+        }
+        return false;
     }
 
     /**
      * Special-case code for containsValue with null argument
      **/
     private boolean containsNullValue() {
-       dstm2.AtomicArray<TEntry> tab = table;
-        for (int i = 0; i < capacity ; i++)
-            for (TEntry e = tab.get(i) ; e != null ; e = e.getNext())
-                if (e.getValue() == null)
+        //dstm2.AtomicArray<TEntry> tab = table;
+        //dstm2.AtomicArray<Wrapper> tab = table;
+        for (int i = 0; i < capacity.getValue(); i++) {
+            for (TEntry e = tableholder.getTable().get(i).getTEntry(); e != null; e = e.getNext()) {
+                //for (TEntry e = tab.get(i); e != null; e = e.getNext()) {
+                if (e.getValue() == null) {
                     return true;
-       return false;
+                }
+            }
+        }
+        return false;
     }
 
-    @atomic public interface TEntry<V> {
-       int getHash();
-               V   getValue();
-       TEntry<V> getNext();
-       void setHash(int h);
-       void setValue(V v);
-       void setNext(TEntry<V> n);
-       
+    @atomic
+    public interface transactionalintfield {
+
+        int getValue();
+
+        void setValue(int val);
+    }
+
+    @atomic
+    public interface tableHolder {
+
+        dstm2.AtomicArray<Wrapper> getTable();
+
+        void setTable(dstm2.AtomicArray<Wrapper> table);
+    }
+
+    @atomic
+    public interface Wrapper {
+
+        TEntry getTEntry();
+
+        void setTEntry(TEntry n);
+    }
+
+    @atomic
+    public interface TEntry<V> {
+
+        int getHash();
+
+        V getValue();
+
+        TEntry<V> getNext();
+
+        void setHash(int h);
+
+        void setValue(V v);
+
+        void setNext(TEntry<V> n);
     }
-    
 
     /**
      * Add a new entry with the specified key, value and hash code to
@@ -615,18 +726,25 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * Subclass overrides this to alter the behavior of put method.
      */
     void addEntry(int hash, V value, int bucketIndex) {
-       TEntry<V> e = table.get(bucketIndex);
-       TEntry<V> n = factory.create();
+        //TEntry<V> e = table.get(bucketIndex);
+        TEntry<V> e = tableholder.getTable().get(bucketIndex).getTEntry();
+        TEntry<V> n = factoryTEntry.create();
         n.setHash(hash);
         n.setValue(value);
         n.setNext(e);
-       table.set(bucketIndex, n);
-        if (size.incrementAndGet() >= threshold) {
-               synchronized(this) {
-                       if(size.get() >= threshold)
-                               resize(2 * capacity);
-               }
-        }
+        
+        //table.set(bucketIndex, n);
+        tableholder.getTable().get(bucketIndex).setTEntry(n);
+        //should be uncommmented
+        
+        //size.setValue(size.getValue() + 1);
+        
+        //synchronized (this) {
+       // if (size >= threshold.getValue()) {
+       //     resize(2 * capacity.getValue());
+       // } //}
+       
+
     }
 
     /**
@@ -638,29 +756,40 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * clone, and readObject.
      */
     void createEntry(int hash, V value, int bucketIndex) {
-       TEntry<V> e = table.get(bucketIndex);
-       TEntry<V> n = factory.create();
+        //TEntry<V> e = table.get(bucketIndex);
+        TEntry<V> e = tableholder.getTable().get(bucketIndex).getTEntry();
+        TEntry<V> n = factoryTEntry.create();
         n.setHash(hash);
         n.setValue(value);
         n.setNext(e);
-       table.set(bucketIndex, n);
-        size.incrementAndGet();
+        //table.set(bucketIndex, n);
+        tableholder.getTable().get(bucketIndex).setTEntry(n);
+        //size++;
+        //size.setValue(size.getValue() + 1);
     }
 
     private abstract class HashIterator<E> implements Iterator<E> {
+
         TEntry<V> next;        // next entry to return
+
         int expectedModCount;  // For fast-fail 
+
         int index;             // current slot 
+
         TEntry<V> current;     // current entry
 
+
         HashIterator() {
-            expectedModCount = modCount;
-            dstm2.AtomicArray<TEntry> t = table;
-            int i = capacity;
+            //        expectedModCount = modCount;
+            //dstm2.AtomicArray<TEntry> t = table;
+            //dstm2.AtomicArray<Wrapper> t = table;
+            int i = capacity.getValue();
             TEntry<V> n = null;
-            if (size.get() != 0) { // advance to first entry
-                while (i > 0 && (n = t.get(--i)) == null)
-                    ;
+            if (size != 0){
+            //if (size.getValue() != 0) { // advance to first entry
+                //while (i > 0 && (n = t.get(--i)) == null);    
+
+                while (i > 0 && (n = tableholder.getTable().get(--i).getTEntry()) == null);
             }
             next = n;
             index = i;
@@ -670,43 +799,50 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
             return next != null;
         }
 
-        TEntry<V> nextEntry() { 
-            if (modCount != expectedModCount)
-                throw new ConcurrentModificationException();
+        TEntry<V> nextEntry() {
+//            if (modCount != expectedModCount) {
+            //              throw new ConcurrentModificationException();
+            //        }
             TEntry<V> e = next;
-            if (e == null) 
+            if (e == null) {
                 throw new NoSuchElementException();
-                
+            }
             TEntry<V> n = e.getNext();
-            dstm2.AtomicArray<TEntry> t = table;
+            //dstm2.AtomicArray<TEntry> t = table;
+            //dstm2.AtomicArray<Wrapper> t = table;
             int i = index;
-            while (n == null && i > 0)
-                n = t.get(--i);
+            while (n == null && i > 0) {
+                //n = t.get(--i);
+                n = tableholder.getTable().get(--i).getTEntry();
+            }
             index = i;
             next = n;
             return current = e;
         }
 
         public void remove() {
-            if (current == null)
+            if (current == null) {
                 throw new IllegalStateException();
-            if (modCount != expectedModCount)
-                throw new ConcurrentModificationException();
+            }
+//            if (modCount != expectedModCount) {
+            //              throw new ConcurrentModificationException();
+            //        }
             int k = current.getHash();
             current = null;
             HashMap.this.removeEntryForKey(k);
-            expectedModCount = modCount;
+        //  expectedModCount = modCount;
         }
-
     }
 
     private class ValueIterator extends HashIterator<V> {
+
         public V next() {
             return nextEntry().getValue();
         }
     }
 
     private class KeyIterator extends HashIterator<Integer> {
+
         public Integer next() {
             return nextEntry().getHash();
         }
@@ -719,43 +855,42 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
 //    }
 
     // Subclass overrides these to alter behavior of views' iterator() method
-    public Iterator<Integer> newKeyIterator()   {
+    public Iterator<Integer> newKeyIterator() {
         return new KeyIterator();
     }
-    public Iterator<V> newValueIterator()   {
+
+    public Iterator<V> newValueIterator() {
         return new ValueIterator();
     }
 //    Iterator<Map.Entry<K,V>> newEntryIterator()   {
 //        return new EntryIterator();
 //    }
-
-
     // Views
-
     private transient Set<HashMap.TEntry<V>> entrySet = null;
 
-
-
     private class KeySet extends AbstractSet<Integer> {
+
         public Iterator<Integer> iterator() {
             return newKeyIterator();
         }
+
         public int size() {
-            return size.get();
+            return size;/*.getValue();*/
         }
+
         public boolean contains(Integer o) {
             return containsKey(o);
         }
+
         public boolean remove(Integer o) {
             return HashMap.this.removeEntryForKey(o) != null;
         }
+
         public void clear() {
             HashMap.this.clear();
         }
     }
 
-
-
     /**
      * Returns a collection view of the mappings contained in this map.  Each
      * element in the returned collection is a <tt>Map.Entry</tt>.  The
@@ -778,17 +913,21 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
 //        public Iterator/*<Map.Entry<K,V>>*/ iterator() {
 //            return newEntryIterator();
 //        }
+
         public boolean contains(HashMap.TEntry<V> o) {
             HashMap.TEntry<V> e = (HashMap.TEntry<V>) o;
             TEntry<V> candidate = getEntry(e.getHash());
             return candidate != null && candidate.equals(e);
         }
+
         public boolean remove(HashMap.TEntry<V> o) {
             return removeMapping(o) != null;
         }
+
         public int size() {
-            return size.get();
+            return size;//.getValue();
         }
+
         public void clear() {
             HashMap.this.clear();
         }
@@ -808,27 +947,25 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * 
      */
     private void writeObject(java.io.ObjectOutputStream s)
-        throws IOException
-    {
-       Iterator<HashMap.TEntry<V>> i = entrySet().iterator();
+            throws IOException {
+        Iterator<HashMap.TEntry<V>> i = entrySet().iterator();
 
-       // Write out the threshold, loadfactor, and any hidden stuff
-       s.defaultWriteObject();
+        // Write out the threshold, loadfactor, and any hidden stuff
+        s.defaultWriteObject();
 
-       // Write out number of buckets
-       s.writeInt(capacity);
+        // Write out number of buckets
+        s.writeInt(capacity.getValue());
 
-       // Write out size (number of Mappings)
-       s.writeInt(size.get());
+        // Write out size (number of Mappings)
+        s.writeInt(size/*.getValue()*/);
 
         // Write out keys and values (alternating)
-       while (i.hasNext()) { 
+        while (i.hasNext()) {
             HashMap.TEntry<V> e = i.next();
             s.writeObject(e.getHash());
             s.writeObject(e.getValue());
         }
     }
-
     private static final long serialVersionUID = 362498820763181265L;
 
     /**
@@ -836,59 +973,67 @@ public class HashMap<V extends dstm2.AtomicSuperClass> implements Iterable<V>{
      * deserialize it).
      */
     private void readObject(java.io.ObjectInputStream s)
-         throws IOException, ClassNotFoundException
-    {
-       // Read in the threshold, loadfactor, and any hidden stuff
-       s.defaultReadObject();
+            throws IOException, ClassNotFoundException {
+        // Read in the threshold, loadfactor, and any hidden stuff
+        s.defaultReadObject();
 
-       // Read in number of buckets and allocate the bucket array;
-       int numBuckets = s.readInt();
-       table = new dstm2.AtomicArray(TEntry.class, numBuckets);
+        // Read in number of buckets and allocate the bucket array;
+        int numBuckets = s.readInt();
+        tableholder.setTable(new dstm2.AtomicArray(TEntry.class, numBuckets));
 
         init();  // Give subclass a chance to do its thing.
 
-       // Read in size (number of Mappings)
-       int size = s.readInt();
+        // Read in size (number of Mappings)
+        int size = s.readInt();
 
-       // Read the keys and values, and put the mappings in the HashMap
-       for (int i=0; i<size; i++) {
-           int key = (Integer) s.readObject();
-           V value = (V) s.readObject();
-           putForCreate(key, value);
-       }
+        // Read the keys and values, and put the mappings in the HashMap
+        for (int i = 0; i < size; i++) {
+            int key = (Integer) s.readObject();
+            V value = (V) s.readObject();
+            putForCreate(key, value);
+        }
     }
 
     // These methods are used when serializing HashSets
-    int   capacity()     { return capacity; }
-    float loadFactor()   { return loadFactor;   }
-
-       public int getCapacity() {
-               return capacity;
-       }
-
-       
-         public Iterator<V> iterator() {
-           return new Iterator<V>() {
-             int tableIndex = 0;
-             public TEntry<V> cursor = table.get(tableIndex);
-             public boolean hasNext() {
-               return cursor != null;
-             }
-             public V next() {
-               TEntry<V> node = cursor;
-               cursor = cursor.getNext();
-               while(cursor==null) {
-                       tableIndex++;
-                       if(tableIndex < capacity) {
-                               cursor = table.get(tableIndex);
-                       }
-               }
-               return node.getValue();
-             }
-             public void remove() {
-               throw new UnsupportedOperationException();
-             }
-             
-           };
-         }
+    int capacity() {
+        return capacity.getValue();
+    }
+
+    float loadFactor() {
+        return loadFactor;
+    }
+
+    public int getCapacity() {
+        return capacity.getValue();
+    }
+
+    public Iterator<V> iterator() {
+        return new Iterator<V>() {
+
+            int tableIndex = 0;
+            //public TEntry<V> cursor = table.get(tableIndex);
+            public TEntry<V> cursor = tableholder.getTable().get(tableIndex).getTEntry();
+
+            public boolean hasNext() {
+                return cursor != null;
+            }
+
+            public V next() {
+                TEntry<V> node = cursor;
+                cursor = cursor.getNext();
+                while (cursor == null) {
+                    tableIndex++;
+                    if (tableIndex < capacity.getValue()) {
+                        //cursor = table.get(tableIndex);
+                        cursor = tableholder.getTable().get(tableIndex).getTEntry();
+                    }
+                }
+                return node.getValue();
+            }
+
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+        };
+    }
 }