media/omx-plugin/include/ics/utils/KeyedVector.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (C) 2005 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef ANDROID_KEYED_VECTOR_H
michael@0 18 #define ANDROID_KEYED_VECTOR_H
michael@0 19
michael@0 20 #include <assert.h>
michael@0 21 #include <stdint.h>
michael@0 22 #include <sys/types.h>
michael@0 23
michael@0 24 #include <utils/SortedVector.h>
michael@0 25 #include <utils/TypeHelpers.h>
michael@0 26 #include <utils/Errors.h>
michael@0 27
michael@0 28 // ---------------------------------------------------------------------------
michael@0 29
michael@0 30 namespace android {
michael@0 31
michael@0 32 template <typename KEY, typename VALUE>
michael@0 33 class KeyedVector
michael@0 34 {
michael@0 35 public:
michael@0 36 typedef KEY key_type;
michael@0 37 typedef VALUE value_type;
michael@0 38
michael@0 39 inline KeyedVector();
michael@0 40
michael@0 41 /*
michael@0 42 * empty the vector
michael@0 43 */
michael@0 44
michael@0 45 inline void clear() { mVector.clear(); }
michael@0 46
michael@0 47 /*!
michael@0 48 * vector stats
michael@0 49 */
michael@0 50
michael@0 51 //! returns number of items in the vector
michael@0 52 inline size_t size() const { return mVector.size(); }
michael@0 53 //! returns wether or not the vector is empty
michael@0 54 inline bool isEmpty() const { return mVector.isEmpty(); }
michael@0 55 //! returns how many items can be stored without reallocating the backing store
michael@0 56 inline size_t capacity() const { return mVector.capacity(); }
michael@0 57 //! setst the capacity. capacity can never be reduced less than size()
michael@0 58 inline ssize_t setCapacity(size_t size) { return mVector.setCapacity(size); }
michael@0 59
michael@0 60 /*!
michael@0 61 * accessors
michael@0 62 */
michael@0 63 const VALUE& valueFor(const KEY& key) const;
michael@0 64 const VALUE& valueAt(size_t index) const;
michael@0 65 const KEY& keyAt(size_t index) const;
michael@0 66 ssize_t indexOfKey(const KEY& key) const;
michael@0 67
michael@0 68 /*!
michael@0 69 * modifing the array
michael@0 70 */
michael@0 71
michael@0 72 VALUE& editValueFor(const KEY& key);
michael@0 73 VALUE& editValueAt(size_t index);
michael@0 74
michael@0 75 /*!
michael@0 76 * add/insert/replace items
michael@0 77 */
michael@0 78
michael@0 79 ssize_t add(const KEY& key, const VALUE& item);
michael@0 80 ssize_t replaceValueFor(const KEY& key, const VALUE& item);
michael@0 81 ssize_t replaceValueAt(size_t index, const VALUE& item);
michael@0 82
michael@0 83 /*!
michael@0 84 * remove items
michael@0 85 */
michael@0 86
michael@0 87 ssize_t removeItem(const KEY& key);
michael@0 88 ssize_t removeItemsAt(size_t index, size_t count = 1);
michael@0 89
michael@0 90 private:
michael@0 91 SortedVector< key_value_pair_t<KEY, VALUE> > mVector;
michael@0 92 };
michael@0 93
michael@0 94 // ---------------------------------------------------------------------------
michael@0 95
michael@0 96 /**
michael@0 97 * Variation of KeyedVector that holds a default value to return when
michael@0 98 * valueFor() is called with a key that doesn't exist.
michael@0 99 */
michael@0 100 template <typename KEY, typename VALUE>
michael@0 101 class DefaultKeyedVector : public KeyedVector<KEY, VALUE>
michael@0 102 {
michael@0 103 public:
michael@0 104 inline DefaultKeyedVector(const VALUE& defValue = VALUE());
michael@0 105 const VALUE& valueFor(const KEY& key) const;
michael@0 106
michael@0 107 private:
michael@0 108 VALUE mDefault;
michael@0 109 };
michael@0 110
michael@0 111 // ---------------------------------------------------------------------------
michael@0 112
michael@0 113 template<typename KEY, typename VALUE> inline
michael@0 114 KeyedVector<KEY,VALUE>::KeyedVector()
michael@0 115 {
michael@0 116 }
michael@0 117
michael@0 118 template<typename KEY, typename VALUE> inline
michael@0 119 ssize_t KeyedVector<KEY,VALUE>::indexOfKey(const KEY& key) const {
michael@0 120 return mVector.indexOf( key_value_pair_t<KEY,VALUE>(key) );
michael@0 121 }
michael@0 122
michael@0 123 template<typename KEY, typename VALUE> inline
michael@0 124 const VALUE& KeyedVector<KEY,VALUE>::valueFor(const KEY& key) const {
michael@0 125 ssize_t i = indexOfKey(key);
michael@0 126 assert(i>=0);
michael@0 127 return mVector.itemAt(i).value;
michael@0 128 }
michael@0 129
michael@0 130 template<typename KEY, typename VALUE> inline
michael@0 131 const VALUE& KeyedVector<KEY,VALUE>::valueAt(size_t index) const {
michael@0 132 return mVector.itemAt(index).value;
michael@0 133 }
michael@0 134
michael@0 135 template<typename KEY, typename VALUE> inline
michael@0 136 const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const {
michael@0 137 return mVector.itemAt(index).key;
michael@0 138 }
michael@0 139
michael@0 140 template<typename KEY, typename VALUE> inline
michael@0 141 VALUE& KeyedVector<KEY,VALUE>::editValueFor(const KEY& key) {
michael@0 142 ssize_t i = indexOfKey(key);
michael@0 143 assert(i>=0);
michael@0 144 return mVector.editItemAt(i).value;
michael@0 145 }
michael@0 146
michael@0 147 template<typename KEY, typename VALUE> inline
michael@0 148 VALUE& KeyedVector<KEY,VALUE>::editValueAt(size_t index) {
michael@0 149 return mVector.editItemAt(index).value;
michael@0 150 }
michael@0 151
michael@0 152 template<typename KEY, typename VALUE> inline
michael@0 153 ssize_t KeyedVector<KEY,VALUE>::add(const KEY& key, const VALUE& value) {
michael@0 154 return mVector.add( key_value_pair_t<KEY,VALUE>(key, value) );
michael@0 155 }
michael@0 156
michael@0 157 template<typename KEY, typename VALUE> inline
michael@0 158 ssize_t KeyedVector<KEY,VALUE>::replaceValueFor(const KEY& key, const VALUE& value) {
michael@0 159 key_value_pair_t<KEY,VALUE> pair(key, value);
michael@0 160 mVector.remove(pair);
michael@0 161 return mVector.add(pair);
michael@0 162 }
michael@0 163
michael@0 164 template<typename KEY, typename VALUE> inline
michael@0 165 ssize_t KeyedVector<KEY,VALUE>::replaceValueAt(size_t index, const VALUE& item) {
michael@0 166 if (index<size()) {
michael@0 167 mVector.editItemAt(index).value = item;
michael@0 168 return index;
michael@0 169 }
michael@0 170 return BAD_INDEX;
michael@0 171 }
michael@0 172
michael@0 173 template<typename KEY, typename VALUE> inline
michael@0 174 ssize_t KeyedVector<KEY,VALUE>::removeItem(const KEY& key) {
michael@0 175 return mVector.remove(key_value_pair_t<KEY,VALUE>(key));
michael@0 176 }
michael@0 177
michael@0 178 template<typename KEY, typename VALUE> inline
michael@0 179 ssize_t KeyedVector<KEY, VALUE>::removeItemsAt(size_t index, size_t count) {
michael@0 180 return mVector.removeItemsAt(index, count);
michael@0 181 }
michael@0 182
michael@0 183 // ---------------------------------------------------------------------------
michael@0 184
michael@0 185 template<typename KEY, typename VALUE> inline
michael@0 186 DefaultKeyedVector<KEY,VALUE>::DefaultKeyedVector(const VALUE& defValue)
michael@0 187 : mDefault(defValue)
michael@0 188 {
michael@0 189 }
michael@0 190
michael@0 191 template<typename KEY, typename VALUE> inline
michael@0 192 const VALUE& DefaultKeyedVector<KEY,VALUE>::valueFor(const KEY& key) const {
michael@0 193 ssize_t i = indexOfKey(key);
michael@0 194 return i >= 0 ? KeyedVector<KEY,VALUE>::valueAt(i) : mDefault;
michael@0 195 }
michael@0 196
michael@0 197 }; // namespace android
michael@0 198
michael@0 199 // ---------------------------------------------------------------------------
michael@0 200
michael@0 201 #endif // ANDROID_KEYED_VECTOR_H

mercurial