michael@0: /* michael@0: * Copyright (C) 2005 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ANDROID_KEYED_VECTOR_H michael@0: #define ANDROID_KEYED_VECTOR_H michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: namespace android { michael@0: michael@0: template michael@0: class KeyedVector michael@0: { michael@0: public: michael@0: typedef KEY key_type; michael@0: typedef VALUE value_type; michael@0: michael@0: inline KeyedVector(); michael@0: michael@0: /* michael@0: * empty the vector michael@0: */ michael@0: michael@0: inline void clear() { mVector.clear(); } michael@0: michael@0: /*! michael@0: * vector stats michael@0: */ michael@0: michael@0: //! returns number of items in the vector michael@0: inline size_t size() const { return mVector.size(); } michael@0: //! returns wether or not the vector is empty michael@0: inline bool isEmpty() const { return mVector.isEmpty(); } michael@0: //! returns how many items can be stored without reallocating the backing store michael@0: inline size_t capacity() const { return mVector.capacity(); } michael@0: //! setst the capacity. capacity can never be reduced less than size() michael@0: inline ssize_t setCapacity(size_t size) { return mVector.setCapacity(size); } michael@0: michael@0: /*! michael@0: * accessors michael@0: */ michael@0: const VALUE& valueFor(const KEY& key) const; michael@0: const VALUE& valueAt(size_t index) const; michael@0: const KEY& keyAt(size_t index) const; michael@0: ssize_t indexOfKey(const KEY& key) const; michael@0: michael@0: /*! michael@0: * modifing the array michael@0: */ michael@0: michael@0: VALUE& editValueFor(const KEY& key); michael@0: VALUE& editValueAt(size_t index); michael@0: michael@0: /*! michael@0: * add/insert/replace items michael@0: */ michael@0: michael@0: ssize_t add(const KEY& key, const VALUE& item); michael@0: ssize_t replaceValueFor(const KEY& key, const VALUE& item); michael@0: ssize_t replaceValueAt(size_t index, const VALUE& item); michael@0: michael@0: /*! michael@0: * remove items michael@0: */ michael@0: michael@0: ssize_t removeItem(const KEY& key); michael@0: ssize_t removeItemsAt(size_t index, size_t count = 1); michael@0: michael@0: private: michael@0: SortedVector< key_value_pair_t > mVector; michael@0: }; michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Variation of KeyedVector that holds a default value to return when michael@0: * valueFor() is called with a key that doesn't exist. michael@0: */ michael@0: template michael@0: class DefaultKeyedVector : public KeyedVector michael@0: { michael@0: public: michael@0: inline DefaultKeyedVector(const VALUE& defValue = VALUE()); michael@0: const VALUE& valueFor(const KEY& key) const; michael@0: michael@0: private: michael@0: VALUE mDefault; michael@0: }; michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: template inline michael@0: KeyedVector::KeyedVector() michael@0: { michael@0: } michael@0: michael@0: template inline michael@0: ssize_t KeyedVector::indexOfKey(const KEY& key) const { michael@0: return mVector.indexOf( key_value_pair_t(key) ); michael@0: } michael@0: michael@0: template inline michael@0: const VALUE& KeyedVector::valueFor(const KEY& key) const { michael@0: ssize_t i = indexOfKey(key); michael@0: assert(i>=0); michael@0: return mVector.itemAt(i).value; michael@0: } michael@0: michael@0: template inline michael@0: const VALUE& KeyedVector::valueAt(size_t index) const { michael@0: return mVector.itemAt(index).value; michael@0: } michael@0: michael@0: template inline michael@0: const KEY& KeyedVector::keyAt(size_t index) const { michael@0: return mVector.itemAt(index).key; michael@0: } michael@0: michael@0: template inline michael@0: VALUE& KeyedVector::editValueFor(const KEY& key) { michael@0: ssize_t i = indexOfKey(key); michael@0: assert(i>=0); michael@0: return mVector.editItemAt(i).value; michael@0: } michael@0: michael@0: template inline michael@0: VALUE& KeyedVector::editValueAt(size_t index) { michael@0: return mVector.editItemAt(index).value; michael@0: } michael@0: michael@0: template inline michael@0: ssize_t KeyedVector::add(const KEY& key, const VALUE& value) { michael@0: return mVector.add( key_value_pair_t(key, value) ); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t KeyedVector::replaceValueFor(const KEY& key, const VALUE& value) { michael@0: key_value_pair_t pair(key, value); michael@0: mVector.remove(pair); michael@0: return mVector.add(pair); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t KeyedVector::replaceValueAt(size_t index, const VALUE& item) { michael@0: if (index inline michael@0: ssize_t KeyedVector::removeItem(const KEY& key) { michael@0: return mVector.remove(key_value_pair_t(key)); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t KeyedVector::removeItemsAt(size_t index, size_t count) { michael@0: return mVector.removeItemsAt(index, count); michael@0: } michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: template inline michael@0: DefaultKeyedVector::DefaultKeyedVector(const VALUE& defValue) michael@0: : mDefault(defValue) michael@0: { michael@0: } michael@0: michael@0: template inline michael@0: const VALUE& DefaultKeyedVector::valueFor(const KEY& key) const { michael@0: ssize_t i = indexOfKey(key); michael@0: return i >= 0 ? KeyedVector::valueAt(i) : mDefault; michael@0: } michael@0: michael@0: }; // namespace android michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: #endif // ANDROID_KEYED_VECTOR_H