PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/android/support/v4/util/SimpleArrayMap.java

https://gitlab.com/Soundar028/mahendra_result_app
Java | 416 lines | 388 code | 28 blank | 0 comment | 112 complexity | 0eea5351f8747d57a1a07351c3699c30 MD5 | raw file
  1. package android.support.v4.util;
  2. import java.util.Map;
  3. public class SimpleArrayMap<K, V> {
  4. private static final int BASE_SIZE = 4;
  5. private static final int CACHE_SIZE = 10;
  6. private static final boolean DEBUG = false;
  7. private static final String TAG = "ArrayMap";
  8. static Object[] mBaseCache;
  9. static int mBaseCacheSize;
  10. static Object[] mTwiceBaseCache;
  11. static int mTwiceBaseCacheSize;
  12. Object[] mArray;
  13. int[] mHashes;
  14. int mSize;
  15. int indexOf(Object key, int hash) {
  16. int N = this.mSize;
  17. if (N == 0) {
  18. return -1;
  19. }
  20. int index = ContainerHelpers.binarySearch(this.mHashes, N, hash);
  21. if (index < 0 || key.equals(this.mArray[index << 1])) {
  22. return index;
  23. }
  24. int end = index + 1;
  25. while (end < N && this.mHashes[end] == hash) {
  26. if (key.equals(this.mArray[end << 1])) {
  27. return end;
  28. }
  29. end++;
  30. }
  31. int i = index - 1;
  32. while (i >= 0 && this.mHashes[i] == hash) {
  33. if (key.equals(this.mArray[i << 1])) {
  34. return i;
  35. }
  36. i--;
  37. }
  38. return end ^ -1;
  39. }
  40. int indexOfNull() {
  41. int N = this.mSize;
  42. if (N == 0) {
  43. return -1;
  44. }
  45. int index = ContainerHelpers.binarySearch(this.mHashes, N, 0);
  46. if (index < 0 || this.mArray[index << 1] == null) {
  47. return index;
  48. }
  49. int end = index + 1;
  50. while (end < N && this.mHashes[end] == 0) {
  51. if (this.mArray[end << 1] == null) {
  52. return end;
  53. }
  54. end++;
  55. }
  56. int i = index - 1;
  57. while (i >= 0 && this.mHashes[i] == 0) {
  58. if (this.mArray[i << 1] == null) {
  59. return i;
  60. }
  61. i--;
  62. }
  63. return end ^ -1;
  64. }
  65. private void allocArrays(int size) {
  66. Object[] array;
  67. if (size == 8) {
  68. synchronized (ArrayMap.class) {
  69. if (mTwiceBaseCache != null) {
  70. array = mTwiceBaseCache;
  71. this.mArray = array;
  72. mTwiceBaseCache = (Object[]) array[0];
  73. this.mHashes = (int[]) array[1];
  74. array[1] = null;
  75. array[0] = null;
  76. mTwiceBaseCacheSize--;
  77. return;
  78. }
  79. }
  80. } else if (size == BASE_SIZE) {
  81. synchronized (ArrayMap.class) {
  82. if (mBaseCache != null) {
  83. array = mBaseCache;
  84. this.mArray = array;
  85. mBaseCache = (Object[]) array[0];
  86. this.mHashes = (int[]) array[1];
  87. array[1] = null;
  88. array[0] = null;
  89. mBaseCacheSize--;
  90. return;
  91. }
  92. }
  93. }
  94. this.mHashes = new int[size];
  95. this.mArray = new Object[(size << 1)];
  96. }
  97. private static void freeArrays(int[] hashes, Object[] array, int size) {
  98. int i;
  99. if (hashes.length == 8) {
  100. synchronized (ArrayMap.class) {
  101. if (mTwiceBaseCacheSize < CACHE_SIZE) {
  102. array[0] = mTwiceBaseCache;
  103. array[1] = hashes;
  104. for (i = (size << 1) - 1; i >= 2; i--) {
  105. array[i] = null;
  106. }
  107. mTwiceBaseCache = array;
  108. mTwiceBaseCacheSize++;
  109. }
  110. }
  111. } else if (hashes.length == BASE_SIZE) {
  112. synchronized (ArrayMap.class) {
  113. if (mBaseCacheSize < CACHE_SIZE) {
  114. array[0] = mBaseCache;
  115. array[1] = hashes;
  116. for (i = (size << 1) - 1; i >= 2; i--) {
  117. array[i] = null;
  118. }
  119. mBaseCache = array;
  120. mBaseCacheSize++;
  121. }
  122. }
  123. }
  124. }
  125. public SimpleArrayMap() {
  126. this.mHashes = ContainerHelpers.EMPTY_INTS;
  127. this.mArray = ContainerHelpers.EMPTY_OBJECTS;
  128. this.mSize = 0;
  129. }
  130. public SimpleArrayMap(int capacity) {
  131. if (capacity == 0) {
  132. this.mHashes = ContainerHelpers.EMPTY_INTS;
  133. this.mArray = ContainerHelpers.EMPTY_OBJECTS;
  134. } else {
  135. allocArrays(capacity);
  136. }
  137. this.mSize = 0;
  138. }
  139. public SimpleArrayMap(SimpleArrayMap map) {
  140. this();
  141. if (map != null) {
  142. putAll(map);
  143. }
  144. }
  145. public void clear() {
  146. if (this.mSize != 0) {
  147. freeArrays(this.mHashes, this.mArray, this.mSize);
  148. this.mHashes = ContainerHelpers.EMPTY_INTS;
  149. this.mArray = ContainerHelpers.EMPTY_OBJECTS;
  150. this.mSize = 0;
  151. }
  152. }
  153. public void ensureCapacity(int minimumCapacity) {
  154. if (this.mHashes.length < minimumCapacity) {
  155. int[] ohashes = this.mHashes;
  156. Object[] oarray = this.mArray;
  157. allocArrays(minimumCapacity);
  158. if (this.mSize > 0) {
  159. System.arraycopy(ohashes, 0, this.mHashes, 0, this.mSize);
  160. System.arraycopy(oarray, 0, this.mArray, 0, this.mSize << 1);
  161. }
  162. freeArrays(ohashes, oarray, this.mSize);
  163. }
  164. }
  165. public boolean containsKey(Object key) {
  166. return indexOfKey(key) >= 0 ? true : DEBUG;
  167. }
  168. public int indexOfKey(Object key) {
  169. return key == null ? indexOfNull() : indexOf(key, key.hashCode());
  170. }
  171. int indexOfValue(Object value) {
  172. int N = this.mSize * 2;
  173. Object[] array = this.mArray;
  174. int i;
  175. if (value == null) {
  176. for (i = 1; i < N; i += 2) {
  177. if (array[i] == null) {
  178. return i >> 1;
  179. }
  180. }
  181. } else {
  182. for (i = 1; i < N; i += 2) {
  183. if (value.equals(array[i])) {
  184. return i >> 1;
  185. }
  186. }
  187. }
  188. return -1;
  189. }
  190. public boolean containsValue(Object value) {
  191. return indexOfValue(value) >= 0 ? true : DEBUG;
  192. }
  193. public V get(Object key) {
  194. int index = indexOfKey(key);
  195. return index >= 0 ? this.mArray[(index << 1) + 1] : null;
  196. }
  197. public K keyAt(int index) {
  198. return this.mArray[index << 1];
  199. }
  200. public V valueAt(int index) {
  201. return this.mArray[(index << 1) + 1];
  202. }
  203. public V setValueAt(int index, V value) {
  204. index = (index << 1) + 1;
  205. V old = this.mArray[index];
  206. this.mArray[index] = value;
  207. return old;
  208. }
  209. public boolean isEmpty() {
  210. return this.mSize <= 0 ? true : DEBUG;
  211. }
  212. public V put(K key, V value) {
  213. int hash;
  214. int index;
  215. int n = 8;
  216. if (key == null) {
  217. hash = 0;
  218. index = indexOfNull();
  219. } else {
  220. hash = key.hashCode();
  221. index = indexOf(key, hash);
  222. }
  223. if (index >= 0) {
  224. index = (index << 1) + 1;
  225. V old = this.mArray[index];
  226. this.mArray[index] = value;
  227. return old;
  228. }
  229. index ^= -1;
  230. if (this.mSize >= this.mHashes.length) {
  231. if (this.mSize >= 8) {
  232. n = this.mSize + (this.mSize >> 1);
  233. } else if (this.mSize < BASE_SIZE) {
  234. n = BASE_SIZE;
  235. }
  236. int[] ohashes = this.mHashes;
  237. Object[] oarray = this.mArray;
  238. allocArrays(n);
  239. if (this.mHashes.length > 0) {
  240. System.arraycopy(ohashes, 0, this.mHashes, 0, ohashes.length);
  241. System.arraycopy(oarray, 0, this.mArray, 0, oarray.length);
  242. }
  243. freeArrays(ohashes, oarray, this.mSize);
  244. }
  245. if (index < this.mSize) {
  246. System.arraycopy(this.mHashes, index, this.mHashes, index + 1, this.mSize - index);
  247. System.arraycopy(this.mArray, index << 1, this.mArray, (index + 1) << 1, (this.mSize - index) << 1);
  248. }
  249. this.mHashes[index] = hash;
  250. this.mArray[index << 1] = key;
  251. this.mArray[(index << 1) + 1] = value;
  252. this.mSize++;
  253. return null;
  254. }
  255. public void putAll(SimpleArrayMap<? extends K, ? extends V> array) {
  256. int N = array.mSize;
  257. ensureCapacity(this.mSize + N);
  258. if (this.mSize != 0) {
  259. for (int i = 0; i < N; i++) {
  260. put(array.keyAt(i), array.valueAt(i));
  261. }
  262. } else if (N > 0) {
  263. System.arraycopy(array.mHashes, 0, this.mHashes, 0, N);
  264. System.arraycopy(array.mArray, 0, this.mArray, 0, N << 1);
  265. this.mSize = N;
  266. }
  267. }
  268. public V remove(Object key) {
  269. int index = indexOfKey(key);
  270. if (index >= 0) {
  271. return removeAt(index);
  272. }
  273. return null;
  274. }
  275. public V removeAt(int index) {
  276. int n = 8;
  277. Object old = this.mArray[(index << 1) + 1];
  278. if (this.mSize <= 1) {
  279. freeArrays(this.mHashes, this.mArray, this.mSize);
  280. this.mHashes = ContainerHelpers.EMPTY_INTS;
  281. this.mArray = ContainerHelpers.EMPTY_OBJECTS;
  282. this.mSize = 0;
  283. } else if (this.mHashes.length <= 8 || this.mSize >= this.mHashes.length / 3) {
  284. this.mSize--;
  285. if (index < this.mSize) {
  286. System.arraycopy(this.mHashes, index + 1, this.mHashes, index, this.mSize - index);
  287. System.arraycopy(this.mArray, (index + 1) << 1, this.mArray, index << 1, (this.mSize - index) << 1);
  288. }
  289. this.mArray[this.mSize << 1] = null;
  290. this.mArray[(this.mSize << 1) + 1] = null;
  291. } else {
  292. if (this.mSize > 8) {
  293. n = this.mSize + (this.mSize >> 1);
  294. }
  295. int[] ohashes = this.mHashes;
  296. Object[] oarray = this.mArray;
  297. allocArrays(n);
  298. this.mSize--;
  299. if (index > 0) {
  300. System.arraycopy(ohashes, 0, this.mHashes, 0, index);
  301. System.arraycopy(oarray, 0, this.mArray, 0, index << 1);
  302. }
  303. if (index < this.mSize) {
  304. System.arraycopy(ohashes, index + 1, this.mHashes, index, this.mSize - index);
  305. System.arraycopy(oarray, (index + 1) << 1, this.mArray, index << 1, (this.mSize - index) << 1);
  306. }
  307. }
  308. return old;
  309. }
  310. public int size() {
  311. return this.mSize;
  312. }
  313. public boolean equals(Object object) {
  314. if (this == object) {
  315. return true;
  316. }
  317. if (!(object instanceof Map)) {
  318. return DEBUG;
  319. }
  320. Map<?, ?> map = (Map) object;
  321. if (size() != map.size()) {
  322. return DEBUG;
  323. }
  324. int i = 0;
  325. while (i < this.mSize) {
  326. try {
  327. K key = keyAt(i);
  328. V mine = valueAt(i);
  329. Object theirs = map.get(key);
  330. if (mine == null) {
  331. if (theirs != null || !map.containsKey(key)) {
  332. return DEBUG;
  333. }
  334. } else if (!mine.equals(theirs)) {
  335. return DEBUG;
  336. }
  337. i++;
  338. } catch (NullPointerException e) {
  339. return DEBUG;
  340. } catch (ClassCastException e2) {
  341. return DEBUG;
  342. }
  343. }
  344. return true;
  345. }
  346. public int hashCode() {
  347. int[] hashes = this.mHashes;
  348. Object[] array = this.mArray;
  349. int result = 0;
  350. int i = 0;
  351. int v = 1;
  352. int s = this.mSize;
  353. while (i < s) {
  354. Object value = array[v];
  355. result += (value == null ? 0 : value.hashCode()) ^ hashes[i];
  356. i++;
  357. v += 2;
  358. }
  359. return result;
  360. }
  361. public String toString() {
  362. if (isEmpty()) {
  363. return "{}";
  364. }
  365. StringBuilder buffer = new StringBuilder(this.mSize * 28);
  366. buffer.append('{');
  367. for (int i = 0; i < this.mSize; i++) {
  368. if (i > 0) {
  369. buffer.append(", ");
  370. }
  371. SimpleArrayMap key = keyAt(i);
  372. if (key != this) {
  373. buffer.append(key);
  374. } else {
  375. buffer.append("(this Map)");
  376. }
  377. buffer.append('=');
  378. SimpleArrayMap value = valueAt(i);
  379. if (value != this) {
  380. buffer.append(value);
  381. } else {
  382. buffer.append("(this Map)");
  383. }
  384. }
  385. buffer.append('}');
  386. return buffer.toString();
  387. }
  388. }