2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __BYTE_BUCKET_ARRAY_H
18 #define __BYTE_BUCKET_ARRAY_H
20 #include <utils/Log.h>
27 * Stores a sparsely populated array. Has a fixed size of 256
28 * (number of entries that a byte can represent).
31 class ByteBucketArray {
33 ByteBucketArray() : mDefault() {
34 memset(mBuckets, 0, sizeof(mBuckets));
38 for (size_t i = 0; i < NUM_BUCKETS; i++) {
39 if (mBuckets[i] != NULL) {
40 delete [] mBuckets[i];
43 memset(mBuckets, 0, sizeof(mBuckets));
46 inline size_t size() const {
47 return NUM_BUCKETS * BUCKET_SIZE;
50 inline const T& get(size_t index) const {
51 return (*this)[index];
54 const T& operator[](size_t index) const {
55 if (index >= size()) {
59 uint8_t bucketIndex = static_cast<uint8_t>(index) >> 4;
60 T* bucket = mBuckets[bucketIndex];
64 return bucket[0x0f & static_cast<uint8_t>(index)];
67 T& editItemAt(size_t index) {
68 ALOG_ASSERT(index < size(), "ByteBucketArray.getOrCreate(index=%u) with size=%u",
69 (uint32_t) index, (uint32_t) size());
71 uint8_t bucketIndex = static_cast<uint8_t>(index) >> 4;
72 T* bucket = mBuckets[bucketIndex];
74 bucket = mBuckets[bucketIndex] = new T[BUCKET_SIZE]();
76 return bucket[0x0f & static_cast<uint8_t>(index)];
79 bool set(size_t index, const T& value) {
80 if (index >= size()) {
84 editItemAt(index) = value;
89 enum { NUM_BUCKETS = 16, BUCKET_SIZE = 16 };
91 T* mBuckets[NUM_BUCKETS];
95 } // namespace android
97 #endif // __BYTE_BUCKET_ARRAY_H