The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_KEYED_VECTOR_H |
| 18 | #define ANDROID_KEYED_VECTOR_H |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | |
Mark Salyzyn | 51c33b7 | 2017-01-12 15:44:06 -0800 | [diff] [blame] | 24 | #include <log/log.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/SortedVector.h> |
| 27 | #include <utils/TypeHelpers.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | namespace android { |
| 32 | |
Steven Moreland | b8f152d | 2017-12-18 16:14:13 -0800 | [diff] [blame] | 33 | // DO NOT USE: please use std::map |
| 34 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | template <typename KEY, typename VALUE> |
| 36 | class KeyedVector |
| 37 | { |
| 38 | public: |
| 39 | typedef KEY key_type; |
| 40 | typedef VALUE value_type; |
| 41 | |
| 42 | inline KeyedVector(); |
| 43 | |
| 44 | /* |
| 45 | * empty the vector |
| 46 | */ |
| 47 | |
| 48 | inline void clear() { mVector.clear(); } |
| 49 | |
| 50 | /*! |
| 51 | * vector stats |
| 52 | */ |
| 53 | |
| 54 | //! returns number of items in the vector |
| 55 | inline size_t size() const { return mVector.size(); } |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 56 | //! returns whether or not the vector is empty |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | inline bool isEmpty() const { return mVector.isEmpty(); } |
| 58 | //! returns how many items can be stored without reallocating the backing store |
| 59 | inline size_t capacity() const { return mVector.capacity(); } |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 60 | //! sets the capacity. capacity can never be reduced less than size() |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | inline ssize_t setCapacity(size_t size) { return mVector.setCapacity(size); } |
Mathias Agopian | 67b5851 | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 62 | |
| 63 | // returns true if the arguments is known to be identical to this vector |
| 64 | inline bool isIdenticalTo(const KeyedVector& rhs) const; |
| 65 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | /*! |
| 67 | * accessors |
| 68 | */ |
| 69 | const VALUE& valueFor(const KEY& key) const; |
| 70 | const VALUE& valueAt(size_t index) const; |
| 71 | const KEY& keyAt(size_t index) const; |
| 72 | ssize_t indexOfKey(const KEY& key) const; |
Mathias Agopian | 67b5851 | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 73 | const VALUE& operator[] (size_t index) const; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
| 75 | /*! |
Glenn Kasten | 73e263e | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 76 | * modifying the array |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | */ |
| 78 | |
| 79 | VALUE& editValueFor(const KEY& key); |
| 80 | VALUE& editValueAt(size_t index); |
| 81 | |
| 82 | /*! |
| 83 | * add/insert/replace items |
| 84 | */ |
| 85 | |
| 86 | ssize_t add(const KEY& key, const VALUE& item); |
| 87 | ssize_t replaceValueFor(const KEY& key, const VALUE& item); |
| 88 | ssize_t replaceValueAt(size_t index, const VALUE& item); |
| 89 | |
| 90 | /*! |
| 91 | * remove items |
| 92 | */ |
| 93 | |
| 94 | ssize_t removeItem(const KEY& key); |
| 95 | ssize_t removeItemsAt(size_t index, size_t count = 1); |
| 96 | |
| 97 | private: |
| 98 | SortedVector< key_value_pair_t<KEY, VALUE> > mVector; |
| 99 | }; |
| 100 | |
| 101 | // --------------------------------------------------------------------------- |
| 102 | |
| 103 | /** |
| 104 | * Variation of KeyedVector that holds a default value to return when |
| 105 | * valueFor() is called with a key that doesn't exist. |
| 106 | */ |
| 107 | template <typename KEY, typename VALUE> |
| 108 | class DefaultKeyedVector : public KeyedVector<KEY, VALUE> |
| 109 | { |
| 110 | public: |
| 111 | inline DefaultKeyedVector(const VALUE& defValue = VALUE()); |
| 112 | const VALUE& valueFor(const KEY& key) const; |
| 113 | |
| 114 | private: |
| 115 | VALUE mDefault; |
| 116 | }; |
| 117 | |
| 118 | // --------------------------------------------------------------------------- |
| 119 | |
| 120 | template<typename KEY, typename VALUE> inline |
| 121 | KeyedVector<KEY,VALUE>::KeyedVector() |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | template<typename KEY, typename VALUE> inline |
Mathias Agopian | 67b5851 | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 126 | bool KeyedVector<KEY,VALUE>::isIdenticalTo(const KeyedVector<KEY,VALUE>& rhs) const { |
| 127 | return mVector.array() == rhs.mVector.array(); |
| 128 | } |
| 129 | |
| 130 | template<typename KEY, typename VALUE> inline |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | ssize_t KeyedVector<KEY,VALUE>::indexOfKey(const KEY& key) const { |
| 132 | return mVector.indexOf( key_value_pair_t<KEY,VALUE>(key) ); |
| 133 | } |
| 134 | |
| 135 | template<typename KEY, typename VALUE> inline |
| 136 | const VALUE& KeyedVector<KEY,VALUE>::valueFor(const KEY& key) const { |
Al Sutton | 6c865b5 | 2012-02-19 08:31:19 +0000 | [diff] [blame] | 137 | ssize_t i = this->indexOfKey(key); |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 138 | LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | return mVector.itemAt(i).value; |
| 140 | } |
| 141 | |
| 142 | template<typename KEY, typename VALUE> inline |
| 143 | const VALUE& KeyedVector<KEY,VALUE>::valueAt(size_t index) const { |
| 144 | return mVector.itemAt(index).value; |
| 145 | } |
| 146 | |
| 147 | template<typename KEY, typename VALUE> inline |
Mathias Agopian | 67b5851 | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 148 | const VALUE& KeyedVector<KEY,VALUE>::operator[] (size_t index) const { |
| 149 | return valueAt(index); |
| 150 | } |
| 151 | |
| 152 | template<typename KEY, typename VALUE> inline |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const { |
| 154 | return mVector.itemAt(index).key; |
| 155 | } |
| 156 | |
| 157 | template<typename KEY, typename VALUE> inline |
| 158 | VALUE& KeyedVector<KEY,VALUE>::editValueFor(const KEY& key) { |
Al Sutton | 6c865b5 | 2012-02-19 08:31:19 +0000 | [diff] [blame] | 159 | ssize_t i = this->indexOfKey(key); |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 160 | LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__); |
Colin Cross | 1811d15 | 2016-09-26 14:24:48 -0700 | [diff] [blame] | 161 | return mVector.editItemAt(static_cast<size_t>(i)).value; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | template<typename KEY, typename VALUE> inline |
| 165 | VALUE& KeyedVector<KEY,VALUE>::editValueAt(size_t index) { |
| 166 | return mVector.editItemAt(index).value; |
| 167 | } |
| 168 | |
| 169 | template<typename KEY, typename VALUE> inline |
| 170 | ssize_t KeyedVector<KEY,VALUE>::add(const KEY& key, const VALUE& value) { |
| 171 | return mVector.add( key_value_pair_t<KEY,VALUE>(key, value) ); |
| 172 | } |
| 173 | |
| 174 | template<typename KEY, typename VALUE> inline |
| 175 | ssize_t KeyedVector<KEY,VALUE>::replaceValueFor(const KEY& key, const VALUE& value) { |
| 176 | key_value_pair_t<KEY,VALUE> pair(key, value); |
| 177 | mVector.remove(pair); |
| 178 | return mVector.add(pair); |
| 179 | } |
| 180 | |
| 181 | template<typename KEY, typename VALUE> inline |
| 182 | ssize_t KeyedVector<KEY,VALUE>::replaceValueAt(size_t index, const VALUE& item) { |
| 183 | if (index<size()) { |
Mathias Agopian | cf89aa4 | 2009-04-10 20:27:25 -0700 | [diff] [blame] | 184 | mVector.editItemAt(index).value = item; |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 185 | return static_cast<ssize_t>(index); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |
| 187 | return BAD_INDEX; |
| 188 | } |
| 189 | |
| 190 | template<typename KEY, typename VALUE> inline |
| 191 | ssize_t KeyedVector<KEY,VALUE>::removeItem(const KEY& key) { |
| 192 | return mVector.remove(key_value_pair_t<KEY,VALUE>(key)); |
| 193 | } |
| 194 | |
| 195 | template<typename KEY, typename VALUE> inline |
| 196 | ssize_t KeyedVector<KEY, VALUE>::removeItemsAt(size_t index, size_t count) { |
| 197 | return mVector.removeItemsAt(index, count); |
| 198 | } |
| 199 | |
| 200 | // --------------------------------------------------------------------------- |
| 201 | |
| 202 | template<typename KEY, typename VALUE> inline |
| 203 | DefaultKeyedVector<KEY,VALUE>::DefaultKeyedVector(const VALUE& defValue) |
| 204 | : mDefault(defValue) |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | template<typename KEY, typename VALUE> inline |
| 209 | const VALUE& DefaultKeyedVector<KEY,VALUE>::valueFor(const KEY& key) const { |
Al Sutton | 6c865b5 | 2012-02-19 08:31:19 +0000 | [diff] [blame] | 210 | ssize_t i = this->indexOfKey(key); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | return i >= 0 ? KeyedVector<KEY,VALUE>::valueAt(i) : mDefault; |
| 212 | } |
| 213 | |
Pirama Arumuga Nainar | 90affce | 2018-04-11 23:10:28 -0700 | [diff] [blame] | 214 | } // namespace android |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | |
| 216 | // --------------------------------------------------------------------------- |
| 217 | |
| 218 | #endif // ANDROID_KEYED_VECTOR_H |