Ejemplo n.º 1
0
 /** Create a new ArrayMap with a given initial capacity. */
 public ArrayMap(int capacity) {
   if (capacity == 0) {
     mHashes = ContainerHelpers.EMPTY_INTS;
     mArray = ContainerHelpers.EMPTY_OBJECTS;
   } else {
     allocArrays(capacity);
   }
   mSize = 0;
 }
Ejemplo n.º 2
0
 /** Ensure the array map can hold at least <var>minimumCapacity</var> items. */
 public void ensureCapacity(int minimumCapacity) {
   if (mHashes.length < minimumCapacity) {
     final int[] ohashes = mHashes;
     final Object[] oarray = mArray;
     allocArrays(minimumCapacity);
     if (mSize > 0) {
       System.arraycopy(ohashes, 0, mHashes, 0, mSize);
       System.arraycopy(oarray, 0, mArray, 0, mSize << 1);
     }
     freeArrays(ohashes, oarray, mSize);
   }
 }
Ejemplo n.º 3
0
  /**
   * Add a new value to the array map.
   *
   * @param key The key under which to store the value. <b>Must not be null.</b> If this key already
   *     exists in the array, its value will be replaced.
   * @param value The value to store for the given key.
   * @return Returns the old value that was stored for the given key, or null if there was no such
   *     key.
   */
  @Override
  public V put(K key, V value) {
    final int hash;
    int index;
    if (key == null) {
      hash = 0;
      index = indexOfNull();
    } else {
      hash = key.hashCode();
      index = indexOf(key, hash);
    }
    if (index >= 0) {
      index = (index << 1) + 1;
      final V old = (V) mArray[index];
      mArray[index] = value;
      return old;
    }

    index = ~index;
    if (mSize >= mHashes.length) {
      final int n =
          mSize >= (BASE_SIZE * 2)
              ? (mSize + (mSize >> 1))
              : (mSize >= BASE_SIZE ? (BASE_SIZE * 2) : BASE_SIZE);

      if (DEBUG) Log.d(TAG, "put: grow from " + mHashes.length + " to " + n);

      final int[] ohashes = mHashes;
      final Object[] oarray = mArray;
      allocArrays(n);

      if (mHashes.length > 0) {
        if (DEBUG) Log.d(TAG, "put: copy 0-" + mSize + " to 0");
        System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length);
        System.arraycopy(oarray, 0, mArray, 0, oarray.length);
      }

      freeArrays(ohashes, oarray, mSize);
    }

    if (index < mSize) {
      if (DEBUG) Log.d(TAG, "put: move " + index + "-" + (mSize - index) + " to " + (index + 1));
      System.arraycopy(mHashes, index, mHashes, index + 1, mSize - index);
      System.arraycopy(mArray, index << 1, mArray, (index + 1) << 1, (mSize - index) << 1);
    }

    mHashes[index] = hash;
    mArray[index << 1] = key;
    mArray[(index << 1) + 1] = value;
    mSize++;
    return null;
  }
Ejemplo n.º 4
0
  /**
   * Remove the key/value mapping at the given index.
   *
   * @param index The desired index, must be between 0 and {@link #size()}-1.
   * @return Returns the value that was stored at this index.
   */
  public V removeAt(int index) {
    final Object old = mArray[(index << 1) + 1];
    if (mSize <= 1) {
      // Now empty.
      if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to 0");
      freeArrays(mHashes, mArray, mSize);
      mHashes = ContainerHelpers.EMPTY_INTS;
      mArray = ContainerHelpers.EMPTY_OBJECTS;
      mSize = 0;
    } else {
      if (mHashes.length > (BASE_SIZE * 2) && mSize < mHashes.length / 3) {
        // Shrunk enough to reduce size of arrays.  We don't allow it to
        // shrink smaller than (BASE_SIZE*2) to avoid flapping between
        // that and BASE_SIZE.
        final int n = mSize > (BASE_SIZE * 2) ? (mSize + (mSize >> 1)) : (BASE_SIZE * 2);

        if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to " + n);

        final int[] ohashes = mHashes;
        final Object[] oarray = mArray;
        allocArrays(n);

        mSize--;
        if (index > 0) {
          if (DEBUG) Log.d(TAG, "remove: copy from 0-" + index + " to 0");
          System.arraycopy(ohashes, 0, mHashes, 0, index);
          System.arraycopy(oarray, 0, mArray, 0, index << 1);
        }
        if (index < mSize) {
          if (DEBUG) Log.d(TAG, "remove: copy from " + (index + 1) + "-" + mSize + " to " + index);
          System.arraycopy(ohashes, index + 1, mHashes, index, mSize - index);
          System.arraycopy(oarray, (index + 1) << 1, mArray, index << 1, (mSize - index) << 1);
        }
      } else {
        mSize--;
        if (index < mSize) {
          if (DEBUG) Log.d(TAG, "remove: move " + (index + 1) + "-" + mSize + " to " + index);
          System.arraycopy(mHashes, index + 1, mHashes, index, mSize - index);
          System.arraycopy(mArray, (index + 1) << 1, mArray, index << 1, (mSize - index) << 1);
        }
        mArray[mSize << 1] = null;
        mArray[(mSize << 1) + 1] = null;
      }
    }
    return (V) old;
  }