/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.util;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;
import libcore.util.EmptyArray;
/**
* SparseArrays map integers to Objects. Unlike a normal array of Objects,
* there can be gaps in the indices. It is intended to be more memory efficient
* than using a HashMap to map Integers to Objects, both because it avoids
* auto-boxing keys and its data structure doesn't rely on an extra entry object
* for each mapping.
*
* <p>Note that this container keeps its mappings in an array data structure,
* using a binary search to find keys. The implementation is not intended to be appropriate for
* data structures
* that may contain large numbers of items. It is generally slower than a traditional
* HashMap, since lookups require a binary search and adds and removes require inserting
* and deleting entries in the array. For containers holding up to hundreds of items,
* the performance difference is not significant, less than 50%.</p>
*
* <p>To help with performance, the container includes an optimization when removing
* keys: instead of compacting its array immediately, it leaves the removed entry marked
* as deleted. The entry can then be re-used for the same key, or compacted later in
* a single garbage collection step of all removed entries. This garbage collection will
* need to be performed at any time the array needs to be grown or the the map size or
* entry values are retrieved.</p>
*
* <p>It is possible to iterate over the items in this container using
* {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using
* <code>keyAt(int)</code> with ascending values of the index will return the
* keys in ascending order, or the values corresponding to the keys in ascending
* order in the case of <code>valueAt(int)</code>.</p>
*/
public class SparseArray<E> implements Cloneable {
private static final Object DELETED = new Object();
//數(shù)據(jù)結(jié)構(gòu)中是否包含為空的垃圾
private boolean mGarbage = false;
//一個(gè)存key的數(shù)組
private int[] mKeys;
//一個(gè)存value的數(shù)組
private Object[] mValues;
private int mSize;
/**
* Creates a new SparseArray containing no mappings.
* 默認(rèn)創(chuàng)建大小為10
*/
public SparseArray() {
this(10);
}
/**
* Creates a new SparseArray containing no mappings that will not
* require any additional memory allocation to store the specified
* number of mappings. If you supply an initial capacity of 0, the
* sparse array will be initialized with a light-weight representation
* not requiring any additional array allocations.
*/
public SparseArray(int initialCapacity) {
if (initialCapacity == 0) {
mKeys = EmptyArray.INT;
mValues = EmptyArray.OBJECT;
} else {
mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity);
mKeys = new int[mValues.length];
}
mSize = 0;
}
@Override
@SuppressWarnings("unchecked")
public SparseArray<E> clone() {
SparseArray<E> clone = null;
try {
clone = (SparseArray<E>) super.clone();
clone.mKeys = mKeys.clone();
clone.mValues = mValues.clone();
} catch (CloneNotSupportedException cnse) {
/* ignore */
}
return clone;
}
/**
* Gets the Object mapped from the specified key, or <code>null</code>
* if no such mapping has been made.
* 根據(jù)key查找數(shù)據(jù)赚哗,查找不到則返回null
*/
public E get(int key) {
return get(key, null);
}
/**
* Gets the Object mapped from the specified key, or the specified Object
* if no such mapping has been made.
* 根據(jù)key查找數(shù)據(jù)成艘,查找不到則返回為默認(rèn)值valueIfKeyNotFound
*/
@SuppressWarnings("unchecked")
public E get(int key, E valueIfKeyNotFound) {
//二分查找法
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i < 0 || mValues[i] == DELETED) {
//沒找到聚凹,或者找到空對(duì)象
return valueIfKeyNotFound;
} else {
//找到了
return (E) mValues[i];
}
}
/**
* Removes the mapping from the specified key, if there was any.
* 根據(jù)key刪除
*/
public void delete(int key) {
//二分法
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
//找到目標(biāo)
if (i >= 0) {
if (mValues[i] != DELETED) {//不為空對(duì)象尖坤。則置為空對(duì)象
mValues[i] = DELETED;
mGarbage = true;//存在垃圾
}
}
}
/**
* @hide
* Removes the mapping from the specified key, if there was any, returning the old value.
* 根據(jù)key刪除并且返回刪除的內(nèi)容
*/
public E removeReturnOld(int key) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i >= 0) {
if (mValues[i] != DELETED) {
final E old = (E) mValues[i];
mValues[i] = DELETED;
mGarbage = true;
return old;
}
}
return null;
}
/**
* Alias for {@link #delete(int)}.
*/
public void remove(int key) {
delete(key);
}
/**
* Removes the mapping at the specified index.
*
* <p>For indices outside of the range <code>0...size()-1</code>,
* the behavior is undefined.</p>
* 根據(jù)位置移除
*/
public void removeAt(int index) {
if (mValues[index] != DELETED) {
mValues[index] = DELETED;
mGarbage = true;
}
}
/**
* Remove a range of mappings as a batch.
*
* @param index Index to begin at
* @param size Number of mappings to remove
*
* <p>For indices outside of the range <code>0...size()-1</code>,
* the behavior is undefined.</p>
* 批量處理一系列的刪除操作
*/
public void removeAtRange(int index, int size) {
final int end = Math.min(mSize, index + size);
for (int i = index; i < end; i++) {
removeAt(i);
}
}
/**
* 處理垃圾回收
*/
private void gc() {
// Log.e("SparseArray", "gc start with " + mSize);
int n = mSize;
int o = 0;
int[] keys = mKeys;
Object[] values = mValues;
for (int i = 0; i < n; i++) {
Object val = values[i];
if (val != DELETED) {
if (i != o) {
keys[o] = keys[i];
values[o] = val;
values[i] = null;
}
o++;
}
}
mGarbage = false;
mSize = o;
// Log.e("SparseArray", "gc end with " + mSize);
}
/**
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
* 添加鍵值對(duì)
*/
public void put(int key, E value) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i >= 0) {//如果數(shù)組中已經(jīng)有這個(gè)key.則替換這個(gè)key對(duì)應(yīng)的值坯临。
mValues[i] = value;
} else { //如果沒有則i為反
i = ~i;//i為負(fù)數(shù)说榆。所以取反
//鍵在規(guī)定范圍內(nèi)影钉。對(duì)應(yīng)的值也正好為空對(duì)象御蒲。那么正好替換。
if (i < mSize && mValues[i] == DELETED) {
mKeys[i] = key;
mValues[i] = value;
return;
}
//如果存在垃圾官套。需要空間大于實(shí)際空間酒奶。那么先回收蚁孔。再重新計(jì)算i.
if (mGarbage && mSize >= mKeys.length) {
gc();
// Search again because indices may have changed.
i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
}
//向數(shù)組中插入數(shù)據(jù) 源碼在末尾
mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
mSize++;
}
}
/**
* Returns the number of key-value mappings that this SparseArray
* currently stores.
* 返回大小。
*/
public int size() {
if (mGarbage) {
gc();
}
return mSize;
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the key from the <code>index</code>th key-value mapping that this
* SparseArray stores.
*
* <p>The keys corresponding to indices in ascending order are guaranteed to
* be in ascending order, e.g., <code>keyAt(0)</code> will return the
* smallest key and <code>keyAt(size()-1)</code> will return the largest
* key.</p>
*
* <p>For indices outside of the range <code>0...size()-1</code>,
* the behavior is undefined.</p>
* 根據(jù)位置返回當(dāng)前位置的key
*/
public int keyAt(int index) {
if (mGarbage) {
gc();
}
return mKeys[index];
}
/**
* Given an index in the range <code>0...size()-1</code>, returns
* the value from the <code>index</code>th key-value mapping that this
* SparseArray stores.
*
* <p>The values corresponding to indices in ascending order are guaranteed
* to be associated with keys in ascending order, e.g.,
* <code>valueAt(0)</code> will return the value associated with the
* smallest key and <code>valueAt(size()-1)</code> will return the value
* associated with the largest key.</p>
*
* <p>For indices outside of the range <code>0...size()-1</code>,
* the behavior is undefined.</p>
* 根據(jù)位置返回當(dāng)前的value
*/
@SuppressWarnings("unchecked")
public E valueAt(int index) {
if (mGarbage) {
gc();
}
return (E) mValues[index];
}
/**
* Given an index in the range <code>0...size()-1</code>, sets a new
* value for the <code>index</code>th key-value mapping that this
* SparseArray stores.
*
* <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined.</p>
* 根據(jù)位置設(shè)置value
*/
public void setValueAt(int index, E value) {
if (mGarbage) {
gc();
}
mValues[index] = value;
}
/**
* Returns the index for which {@link #keyAt} would return the
* specified key, or a negative number if the specified
* key is not mapped.
* 根據(jù)key返回key的位置
*/
public int indexOfKey(int key) {
if (mGarbage) {
gc();
}
return ContainerHelpers.binarySearch(mKeys, mSize, key);
}
/**
* Returns an index for which {@link #valueAt} would return the
* specified key, or a negative number if no keys map to the
* specified value.
* <p>Beware that this is a linear search, unlike lookups by key,
* and that multiple keys can map to the same value and this will
* find only one of them.
* <p>Note also that unlike most collections' {@code indexOf} methods,
* this method compares values using {@code ==} rather than {@code equals}.
* 根據(jù)value返回value的位置
*/
public int indexOfValue(E value) {
if (mGarbage) {
gc();
}
for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i;
return -1;
}
/**
* Removes all key-value mappings from this SparseArray.
* 清空
*/
public void clear() {
int n = mSize;
Object[] values = mValues;
for (int i = 0; i < n; i++) {
values[i] = null;
}
mSize = 0;
mGarbage = false;
}
/**
* Puts a key/value pair into the array, optimizing for the case where
* the key is greater than all existing keys in the array.
* 添加新的key.value 到最高效的位置
*/
public void append(int key, E value) {
if (mSize != 0 && key <= mKeys[mSize - 1]) {
put(key, value);
return;
}
if (mGarbage && mSize >= mKeys.length) {
gc();
}
mKeys = GrowingArrayUtils.append(mKeys, mSize, key);
mValues = GrowingArrayUtils.append(mValues, mSize, value);
mSize++;
}
/**
* {@inheritDoc}
*
* <p>This implementation composes a string by iterating over its mappings. If
* this map contains itself as a value, the string "(this Map)"
* will appear in its place.
*
*/
@Override
public String toString() {
if (size() <= 0) {
return "{}";
}
StringBuilder buffer = new StringBuilder(mSize * 28);
buffer.append('{');
for (int i=0; i<mSize; i++) {
if (i > 0) {
buffer.append(", ");
}
int key = keyAt(i);
buffer.append(key);
buffer.append('=');
Object value = valueAt(i);
if (value != this) {
buffer.append(value);
} else {
buffer.append("(this Map)");
}
}
buffer.append('}');
return buffer.toString();
}
}
/**
* Inserts an element into the array at the specified index, growing the array if there is no
* more room.
*
* @param array The array to which to append the element. Must NOT be null.
* @param currentSize The number of elements in the array. Must be less than or equal to
* array.length.
* @param element The element to insert.
* @return the array to which the element was appended. This may be different than the given
* array.
* 向數(shù)組中插入數(shù)據(jù)
*/
public static <T> T[] insert(T[] array, int currentSize, int index, T element) {
assert currentSize <= array.length;
if (currentSize + 1 <= array.length) {
System.arraycopy(array, index, array, index + 1, currentSize - index);
array[index] = element;
return array;
}
@SuppressWarnings("unchecked")
T[] newArray = ArrayUtils.newUnpaddedArray((Class<T>)array.getClass().getComponentType(),
growSize(currentSize));
System.arraycopy(array, 0, newArray, 0, index);
newArray[index] = element;
System.arraycopy(array, index, newArray, index + 1, array.length - index);
return newArray;
}
SparseArray根據(jù)源碼簡單分析
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門堕汞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勺爱,“玉大人,你說我怎么就攤上這事讯检∷雎常” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵人灼,是天一觀的道長围段。 經(jīng)常有香客問我,道長投放,這世上最難降的妖魔是什么奈泪? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮灸芳,結(jié)果婚禮上涝桅,老公的妹妹穿的比我還像新娘。我一直安慰自己耗绿,他們只是感情好苹支,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著误阻,像睡著了一般债蜜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上究反,一...
- 文/蒼蘭香墨 我猛地睜開眼恼蓬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了僵芹?” 一聲冷哼從身側(cè)響起处硬,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎拇派,沒想到半個(gè)月后荷辕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡件豌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年疮方,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片茧彤。...
- 正文 年R本政府宣布遭殉,位于F島的核電站,受9級(jí)特大地震影響博助,放射性物質(zhì)發(fā)生泄漏险污。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一富岳、第九天 我趴在偏房一處隱蔽的房頂上張望蛔糯。 院中可真熱鬧,春花似錦窖式、人聲如沸蚁飒。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽淮逻。三九已至,卻和暖如春阁簸,著一層夾襖步出監(jiān)牢的瞬間爬早,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓饶米,卻偏偏與公主長得像桨啃,于是被迫代替她去往敵國和親车胡。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 【蝴蝶效應(yīng)】 蝴蝶效應(yīng):上個(gè)世紀(jì)70年代,美國一個(gè)名叫洛倫茲的氣象學(xué)家在解釋空氣系統(tǒng)理論時(shí)說网杆,亞馬遜雨林一只蝴蝶...
- -01- 在外上學(xué)碳却,已有半年未歸队秩。終于放假,遠(yuǎn)遠(yuǎn)地就看到外婆在樓下守望昼浦。 外婆已近九十高齡馍资,只見她手哆嗦著握著和我...
- 上一節(jié) 連載1 目錄 她們這才敢抬頭使兔,原來是一個(gè)年輕的軍官建钥。見是國軍便不怕了,月亭還有些忿忿虐沥,“你們憑什么殺人熊经?”...
- 法國是個(gè)充滿魅力的國家。不過欲险,我們對(duì)生活在這里的人镐依,又了解多少呢? 今天我們討論的是這樣一群人:她們?cè)谏鐣?huì)上大量存...
- 春天,扒開冬雪的鎧甲 遙望 千里煙瘴的江南 用一雙纖纖柔指 繞上了冬的脖頸 掐出一點(diǎn)瘀痕 冬天的脖頸 黝黑而硬朗 ...