1 package org.robolectric.res.android; 2 3 // transliterated from https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/libs/androidfw/include/androidfw/ByteBucketArray.h 4 /** 5 * Stores a sparsely populated array. Has a fixed size of 256 6 * (number of entries that a byte can represent). 7 */ 8 public abstract class ByteBucketArray<T> { ByteBucketArray(T mDefault)9 public ByteBucketArray(T mDefault) { 10 this.mDefault = mDefault; 11 } 12 size()13 final int size() { 14 return NUM_BUCKETS * BUCKET_SIZE; 15 } 16 17 // inline const T& get(size_t index) const { 18 // return (*this)[index]; 19 // } 20 get(int index)21 final T get(int index) { 22 if (index >= size()) { 23 return mDefault; 24 } 25 26 // byte bucketIndex = static_cast<byte>(index) >> 4; 27 byte bucketIndex = (byte) (index >> 4); 28 T[] bucket = (T[]) mBuckets[bucketIndex]; 29 if (bucket == null) { 30 return mDefault; 31 } 32 T t = bucket[0x0f & ((byte) index)]; 33 return t == null ? mDefault : t; 34 } 35 editItemAt(int index)36 T editItemAt(int index) { 37 // ALOG_ASSERT(index < size(), "ByteBucketArray.getOrCreate(index=%u) with size=%u", 38 // (uint32_t) index, (uint32_t) size()); 39 40 // uint8_t bucketIndex = static_cast<uint8_t>(index) >> 4; 41 byte bucketIndex = (byte) (((byte) index) >> 4); 42 Object[] bucket = mBuckets[bucketIndex]; 43 if (bucket == null) { 44 bucket = mBuckets[bucketIndex] = new Object[BUCKET_SIZE]; 45 } 46 // return bucket[0x0f & static_cast<uint8_t>(index)]; 47 T t = (T) bucket[0x0f & ((byte) index)]; 48 if (t == null) { 49 t = newInstance(); 50 bucket[0x0f & ((byte) index)] = t; 51 } 52 return t; 53 } 54 newInstance()55 abstract T newInstance(); 56 set(int index, final T value)57 boolean set(int index, final T value) { 58 if (index >= size()) { 59 return false; 60 } 61 62 // editItemAt(index) = value; 63 byte bucketIndex = (byte) (((byte) index) >> 4); 64 Object[] bucket = mBuckets[bucketIndex]; 65 if (bucket == null) { 66 bucket = mBuckets[bucketIndex] = new Object[BUCKET_SIZE]; 67 } 68 bucket[0x0f & ((byte) index)] = value; 69 70 return true; 71 } 72 73 private static final int NUM_BUCKETS = 16, BUCKET_SIZE = 16; 74 75 Object[][] mBuckets = new Object[NUM_BUCKETS][]; 76 T mDefault; 77 78 } 79