commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/map AbstractHashedMap.java
Date Fri, 04 Jun 2004 23:27:30 GMT
scolebourne    2004/06/04 16:27:30

  Modified:    collections/src/java/org/apache/commons/collections/map
                        AbstractHashedMap.java
  Log:
  Improve capacity handling and checks for large maps
  
  Revision  Changes    Path
  1.19      +30 -21    jakarta-commons/collections/src/java/org/apache/commons/collections/map/AbstractHashedMap.java
  
  Index: AbstractHashedMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/map/AbstractHashedMap.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- AbstractHashedMap.java	26 May 2004 21:56:05 -0000	1.18
  +++ AbstractHashedMap.java	4 Jun 2004 23:27:30 -0000	1.19
  @@ -292,7 +292,8 @@
           if (mapSize == 0) {
               return;
           }
  -        ensureCapacity(calculateNewCapacity(size + mapSize));
  +        int newSize = (int) ((size + mapSize) / loadFactor + 1);
  +        ensureCapacity(calculateNewCapacity(newSize));
           for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
               Map.Entry entry = (Map.Entry) it.next();
               put(entry.getKey(), entry.getValue());
  @@ -578,39 +579,47 @@
        */
       protected void checkCapacity() {
           if (size >= threshold) {
  -            ensureCapacity(data.length * 2);
  +            int newCapacity = data.length * 2;
  +            if (newCapacity <= MAXIMUM_CAPACITY) {
  +                ensureCapacity(newCapacity);
  +            }
           }
       }
       
       /**
        * Changes the size of the data structure to the capacity proposed.
        * 
  -     * @param newCapacity  the new capacity of the array
  +     * @param newCapacity  the new capacity of the array (a power of two, less or equal
to max)
        */
       protected void ensureCapacity(int newCapacity) {
           int oldCapacity = data.length;
           if (newCapacity <= oldCapacity) {
               return;
           }
  -        HashEntry oldEntries[] = data;
  -        HashEntry newEntries[] = new HashEntry[newCapacity];
  +        if (size == 0) {
  +            threshold = calculateThreshold(newCapacity, loadFactor);
  +            data = new HashEntry[newCapacity];
  +        } else {
  +            HashEntry oldEntries[] = data;
  +            HashEntry newEntries[] = new HashEntry[newCapacity];
   
  -        modCount++;
  -        for (int i = oldCapacity - 1; i >= 0; i--) {
  -            HashEntry entry = oldEntries[i];
  -            if (entry != null) {
  -                oldEntries[i] = null;  // gc
  -                do {
  -                    HashEntry next = entry.next;
  -                    int index = hashIndex(entry.hashCode, newCapacity);  
  -                    entry.next = newEntries[index];
  -                    newEntries[index] = entry;
  -                    entry = next;
  -                } while (entry != null);
  +            modCount++;
  +            for (int i = oldCapacity - 1; i >= 0; i--) {
  +                HashEntry entry = oldEntries[i];
  +                if (entry != null) {
  +                    oldEntries[i] = null;  // gc
  +                    do {
  +                        HashEntry next = entry.next;
  +                        int index = hashIndex(entry.hashCode, newCapacity);  
  +                        entry.next = newEntries[index];
  +                        newEntries[index] = entry;
  +                        entry = next;
  +                    } while (entry != null);
  +                }
               }
  +            threshold = calculateThreshold(newCapacity, loadFactor);
  +            data = newEntries;
           }
  -        threshold = calculateThreshold(newCapacity, loadFactor);
  -        data = newEntries;
       }
   
       /**
  @@ -628,7 +637,7 @@
               while (newCapacity < proposedCapacity) {
                   newCapacity <<= 1;  // multiply by two
               }
  -            if (proposedCapacity > MAXIMUM_CAPACITY) {
  +            if (newCapacity > MAXIMUM_CAPACITY) {
                   newCapacity = MAXIMUM_CAPACITY;
               }
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message