int indexOfNull() { int k = this.mSize; if (k == 0) { i = -1; } int j; do { do { return i; j = ContainerHelpers.binarySearch(this.mHashes, k, 0); i = j; } while (j < 0); i = j; } while (this.mArray[(j << 1)] == null); int i = j + 1; while ((i < k) && (this.mHashes[i] == 0)) { if (this.mArray[(i << 1)] == null) { return i; } i += 1; } j -= 1; while ((j >= 0) && (this.mHashes[j] == 0)) { if (this.mArray[(j << 1)] == null) { return j; } j -= 1; } return i ^ 0xFFFFFFFF; }
/** * Returns the index for which {@link #keyAt} would return the specified key, or a negative number * if the specified key is not mapped. */ public int indexOfKey(int key) { if (mGarbage) { gc(); } return ContainerHelpers.binarySearch(mKeys, mSize, key); }
int indexOf(Object paramObject, int paramInt) { int k = this.mSize; if (k == 0) { i = -1; } int j; do { do { return i; j = ContainerHelpers.binarySearch(this.mHashes, k, paramInt); i = j; } while (j < 0); i = j; } while (paramObject.equals(this.mArray[(j << 1)])); int i = j + 1; while ((i < k) && (this.mHashes[i] == paramInt)) { if (paramObject.equals(this.mArray[(i << 1)])) { return i; } i += 1; } j -= 1; while ((j >= 0) && (this.mHashes[j] == paramInt)) { if (paramObject.equals(this.mArray[(j << 1)])) { return j; } j -= 1; } return i ^ 0xFFFFFFFF; }
int indexOfNull() { int N = this.mSize; if (N == 0) { return -1; } int index = ContainerHelpers.binarySearch(this.mHashes, N, 0); if (index < 0 || this.mArray[index << 1] == null) { return index; } int end = index + 1; while (end < N && this.mHashes[end] == 0) { if (this.mArray[end << 1] == null) { return end; } end++; } int i = index - 1; while (i >= 0 && this.mHashes[i] == 0) { if (this.mArray[i << 1] == null) { return i; } i--; } return end ^ -1; }
int indexOf(Object key, int hash) { int N = this.mSize; if (N == 0) { return -1; } int index = ContainerHelpers.binarySearch(this.mHashes, N, hash); if (index < 0 || key.equals(this.mArray[index << 1])) { return index; } int end = index + 1; while (end < N && this.mHashes[end] == hash) { if (key.equals(this.mArray[end << 1])) { return end; } end++; } int i = index - 1; while (i >= 0 && this.mHashes[i] == hash) { if (key.equals(this.mArray[i << 1])) { return i; } i--; } return end ^ -1; }
/** * Adds a mapping from the specified key to the specified value, replacing the previous mapping * from the specified key if there was one. */ public void put(int key, E value) { int i = ContainerHelpers.binarySearch(mKeys, mSize, key); if (i >= 0) { mValues[i] = value; } else { i = ~i; if (i < mSize && mValues[i] == DELETED) { mKeys[i] = key; mValues[i] = value; return; } if (mGarbage && mSize >= mKeys.length) { gc(); // Search again because indices may have changed. i = ~ContainerHelpers.binarySearch(mKeys, mSize, key); } if (mSize >= mKeys.length) { int n = idealIntArraySize(mSize + 1); int[] nkeys = new int[n]; Object[] nvalues = new Object[n]; // Log.e("SparseArray", "grow " + mKeys.length + " to " + n); System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); System.arraycopy(mValues, 0, nvalues, 0, mValues.length); mKeys = nkeys; mValues = nvalues; } if (mSize - i != 0) { // Log.e("SparseArray", "move " + (mSize - i)); System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); System.arraycopy(mValues, i, mValues, i + 1, mSize - i); } mKeys[i] = key; mValues[i] = value; mSize++; } }
/** Removes the mapping from the specified key, if there was any. */ public void delete(int key) { int i = ContainerHelpers.binarySearch(mKeys, mSize, key); if (i >= 0) { if (mValues[i] != DELETED) { mValues[i] = DELETED; mGarbage = true; } } }
/** * Gets the Object mapped from the specified key, or the specified Object if no such mapping has * been made. */ @SuppressWarnings("unchecked") public E get(int key, E valueIfKeyNotFound) { int i = ContainerHelpers.binarySearch(mKeys, mSize, key); if (i < 0 || mValues[i] == DELETED) { return valueIfKeyNotFound; } else { return (E) mValues[i]; } }
public SparseArrayCompat(int i) { this.mGarbage = false; if (i == 0) { this.mKeys = ContainerHelpers.EMPTY_INTS; this.mValues = ContainerHelpers.EMPTY_OBJECTS; } else { int idealIntArraySize = ContainerHelpers.idealIntArraySize(i); this.mKeys = new int[idealIntArraySize]; this.mValues = new Object[idealIntArraySize]; } this.mSize = 0; }
int indexOf(Object key, int hash) { final int N = mSize; // Important fast case: if nothing is in here, nothing to look for. if (N == 0) { return ~0; } int index = ContainerHelpers.binarySearch(mHashes, N, hash); // If the hash code wasn't found, then we have no entry for this key. if (index < 0) { return index; } // If the key at the returned index matches, that's what we want. if (key.equals(mArray[index << 1])) { return index; } // Search for a matching key after the index. int end; for (end = index + 1; end < N && mHashes[end] == hash; end++) { if (key.equals(mArray[end << 1])) return end; } // Search for a matching key before the index. for (int i = index - 1; i >= 0 && mHashes[i] == hash; i--) { if (key.equals(mArray[i << 1])) return i; } // Key not found -- return negative value indicating where a // new entry for this key should go. We use the end of the // hash chain to reduce the number of array entries that will // need to be copied when inserting. return ~end; }