FieldArray.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. package com.google.protobuf.nano;
  31. /**
  32. * A custom version of {@code android.util.SparseArray} with the minimal API
  33. * for storing {@link FieldData} objects.
  34. *
  35. * <p>This class is an internal implementation detail of nano and should not
  36. * be called directly by clients.
  37. *
  38. * Based on {@code android.support.v4.util.SpareArrayCompat}.
  39. */
  40. public final class FieldArray implements Cloneable {
  41. private static final FieldData DELETED = new FieldData();
  42. private boolean mGarbage = false;
  43. private int[] mFieldNumbers;
  44. private FieldData[] mData;
  45. private int mSize;
  46. /**
  47. * Creates a new FieldArray containing no fields.
  48. */
  49. FieldArray() {
  50. this(10);
  51. }
  52. /**
  53. * Creates a new FieldArray containing no mappings that will not
  54. * require any additional memory allocation to store the specified
  55. * number of mappings.
  56. */
  57. FieldArray(int initialCapacity) {
  58. initialCapacity = idealIntArraySize(initialCapacity);
  59. mFieldNumbers = new int[initialCapacity];
  60. mData = new FieldData[initialCapacity];
  61. mSize = 0;
  62. }
  63. /**
  64. * Gets the FieldData mapped from the specified fieldNumber, or <code>null</code>
  65. * if no such mapping has been made.
  66. */
  67. FieldData get(int fieldNumber) {
  68. int i = binarySearch(fieldNumber);
  69. if (i < 0 || mData[i] == DELETED) {
  70. return null;
  71. } else {
  72. return mData[i];
  73. }
  74. }
  75. /**
  76. * Removes the data from the specified fieldNumber, if there was any.
  77. */
  78. void remove(int fieldNumber) {
  79. int i = binarySearch(fieldNumber);
  80. if (i >= 0 && mData[i] != DELETED) {
  81. mData[i] = DELETED;
  82. mGarbage = true;
  83. }
  84. }
  85. private void gc() {
  86. int n = mSize;
  87. int o = 0;
  88. int[] keys = mFieldNumbers;
  89. FieldData[] values = mData;
  90. for (int i = 0; i < n; i++) {
  91. FieldData val = values[i];
  92. if (val != DELETED) {
  93. if (i != o) {
  94. keys[o] = keys[i];
  95. values[o] = val;
  96. values[i] = null;
  97. }
  98. o++;
  99. }
  100. }
  101. mGarbage = false;
  102. mSize = o;
  103. }
  104. /**
  105. * Adds a mapping from the specified fieldNumber to the specified data,
  106. * replacing the previous mapping if there was one.
  107. */
  108. void put(int fieldNumber, FieldData data) {
  109. int i = binarySearch(fieldNumber);
  110. if (i >= 0) {
  111. mData[i] = data;
  112. } else {
  113. i = ~i;
  114. if (i < mSize && mData[i] == DELETED) {
  115. mFieldNumbers[i] = fieldNumber;
  116. mData[i] = data;
  117. return;
  118. }
  119. if (mGarbage && mSize >= mFieldNumbers.length) {
  120. gc();
  121. // Search again because indices may have changed.
  122. i = ~ binarySearch(fieldNumber);
  123. }
  124. if (mSize >= mFieldNumbers.length) {
  125. int n = idealIntArraySize(mSize + 1);
  126. int[] nkeys = new int[n];
  127. FieldData[] nvalues = new FieldData[n];
  128. System.arraycopy(mFieldNumbers, 0, nkeys, 0, mFieldNumbers.length);
  129. System.arraycopy(mData, 0, nvalues, 0, mData.length);
  130. mFieldNumbers = nkeys;
  131. mData = nvalues;
  132. }
  133. if (mSize - i != 0) {
  134. System.arraycopy(mFieldNumbers, i, mFieldNumbers, i + 1, mSize - i);
  135. System.arraycopy(mData, i, mData, i + 1, mSize - i);
  136. }
  137. mFieldNumbers[i] = fieldNumber;
  138. mData[i] = data;
  139. mSize++;
  140. }
  141. }
  142. /**
  143. * Returns the number of key-value mappings that this FieldArray
  144. * currently stores.
  145. */
  146. int size() {
  147. if (mGarbage) {
  148. gc();
  149. }
  150. return mSize;
  151. }
  152. public boolean isEmpty() {
  153. return size() == 0;
  154. }
  155. /**
  156. * Given an index in the range <code>0...size()-1</code>, returns
  157. * the value from the <code>index</code>th key-value mapping that this
  158. * FieldArray stores.
  159. */
  160. FieldData dataAt(int index) {
  161. if (mGarbage) {
  162. gc();
  163. }
  164. return mData[index];
  165. }
  166. @Override
  167. public boolean equals(Object o) {
  168. if (o == this) {
  169. return true;
  170. }
  171. if (!(o instanceof FieldArray)) {
  172. return false;
  173. }
  174. FieldArray other = (FieldArray) o;
  175. if (size() != other.size()) { // size() will call gc() if necessary.
  176. return false;
  177. }
  178. return arrayEquals(mFieldNumbers, other.mFieldNumbers, mSize) &&
  179. arrayEquals(mData, other.mData, mSize);
  180. }
  181. @Override
  182. public int hashCode() {
  183. if (mGarbage) {
  184. gc();
  185. }
  186. int result = 17;
  187. for (int i = 0; i < mSize; i++) {
  188. result = 31 * result + mFieldNumbers[i];
  189. result = 31 * result + mData[i].hashCode();
  190. }
  191. return result;
  192. }
  193. private int idealIntArraySize(int need) {
  194. return idealByteArraySize(need * 4) / 4;
  195. }
  196. private int idealByteArraySize(int need) {
  197. for (int i = 4; i < 32; i++)
  198. if (need <= (1 << i) - 12)
  199. return (1 << i) - 12;
  200. return need;
  201. }
  202. private int binarySearch(int value) {
  203. int lo = 0;
  204. int hi = mSize - 1;
  205. while (lo <= hi) {
  206. int mid = (lo + hi) >>> 1;
  207. int midVal = mFieldNumbers[mid];
  208. if (midVal < value) {
  209. lo = mid + 1;
  210. } else if (midVal > value) {
  211. hi = mid - 1;
  212. } else {
  213. return mid; // value found
  214. }
  215. }
  216. return ~lo; // value not present
  217. }
  218. private boolean arrayEquals(int[] a, int[] b, int size) {
  219. for (int i = 0; i < size; i++) {
  220. if (a[i] != b[i]) {
  221. return false;
  222. }
  223. }
  224. return true;
  225. }
  226. private boolean arrayEquals(FieldData[] a, FieldData[] b, int size) {
  227. for (int i = 0; i < size; i++) {
  228. if (!a[i].equals(b[i])) {
  229. return false;
  230. }
  231. }
  232. return true;
  233. }
  234. @Override
  235. public final FieldArray clone() {
  236. // Trigger GC so we compact and don't copy DELETED elements.
  237. int size = size();
  238. FieldArray clone = new FieldArray(size);
  239. System.arraycopy(mFieldNumbers, 0, clone.mFieldNumbers, 0, size);
  240. for (int i = 0; i < size; i++) {
  241. if (mData[i] != null) {
  242. clone.mData[i] = mData[i].clone();
  243. }
  244. }
  245. clone.mSize = size;
  246. return clone;
  247. }
  248. }