blob: 47c2c56de3a3370d2d8ff62b9b32a6eb367ac2e1 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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
24#include <utils/SortedVector.h>
25#include <utils/TypeHelpers.h>
26#include <utils/Errors.h>
27
28// ---------------------------------------------------------------------------
29
30namespace android {
31
32template <typename KEY, typename VALUE>
33class KeyedVector
34{
35public:
36 typedef KEY key_type;
37 typedef VALUE value_type;
38
39 inline KeyedVector();
40
41 /*
42 * empty the vector
43 */
44
45 inline void clear() { mVector.clear(); }
46
47 /*!
48 * vector stats
49 */
50
51 //! returns number of items in the vector
52 inline size_t size() const { return mVector.size(); }
53 //! returns wether or not the vector is empty
54 inline bool isEmpty() const { return mVector.isEmpty(); }
55 //! returns how many items can be stored without reallocating the backing store
56 inline size_t capacity() const { return mVector.capacity(); }
57 //! setst the capacity. capacity can never be reduced less than size()
58 inline ssize_t setCapacity(size_t size) { return mVector.setCapacity(size); }
Mathias Agopian67b58512012-08-02 18:32:23 -070059
60 // returns true if the arguments is known to be identical to this vector
61 inline bool isIdenticalTo(const KeyedVector& rhs) const;
62
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080063 /*!
64 * accessors
65 */
66 const VALUE& valueFor(const KEY& key) const;
67 const VALUE& valueAt(size_t index) const;
68 const KEY& keyAt(size_t index) const;
69 ssize_t indexOfKey(const KEY& key) const;
Mathias Agopian67b58512012-08-02 18:32:23 -070070 const VALUE& operator[] (size_t index) const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080071
72 /*!
Glenn Kasten73e263e2012-01-19 08:59:58 -080073 * modifying the array
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074 */
75
76 VALUE& editValueFor(const KEY& key);
77 VALUE& editValueAt(size_t index);
78
79 /*!
80 * add/insert/replace items
81 */
82
83 ssize_t add(const KEY& key, const VALUE& item);
84 ssize_t replaceValueFor(const KEY& key, const VALUE& item);
85 ssize_t replaceValueAt(size_t index, const VALUE& item);
86
87 /*!
88 * remove items
89 */
90
91 ssize_t removeItem(const KEY& key);
92 ssize_t removeItemsAt(size_t index, size_t count = 1);
93
94private:
95 SortedVector< key_value_pair_t<KEY, VALUE> > mVector;
96};
97
Jeff Brown9a0a76d2012-03-16 14:45:49 -070098// KeyedVector<KEY, VALUE> can be trivially moved using memcpy() because its
99// underlying SortedVector can be trivially moved.
100template<typename KEY, typename VALUE> struct trait_trivial_move<KeyedVector<KEY, VALUE> > {
101 enum { value = trait_trivial_move<SortedVector< key_value_pair_t<KEY, VALUE> > >::value };
102};
103
104
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800105// ---------------------------------------------------------------------------
106
107/**
108 * Variation of KeyedVector that holds a default value to return when
109 * valueFor() is called with a key that doesn't exist.
110 */
111template <typename KEY, typename VALUE>
112class DefaultKeyedVector : public KeyedVector<KEY, VALUE>
113{
114public:
115 inline DefaultKeyedVector(const VALUE& defValue = VALUE());
116 const VALUE& valueFor(const KEY& key) const;
117
118private:
119 VALUE mDefault;
120};
121
122// ---------------------------------------------------------------------------
123
124template<typename KEY, typename VALUE> inline
125KeyedVector<KEY,VALUE>::KeyedVector()
126{
127}
128
129template<typename KEY, typename VALUE> inline
Mathias Agopian67b58512012-08-02 18:32:23 -0700130bool KeyedVector<KEY,VALUE>::isIdenticalTo(const KeyedVector<KEY,VALUE>& rhs) const {
131 return mVector.array() == rhs.mVector.array();
132}
133
134template<typename KEY, typename VALUE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800135ssize_t KeyedVector<KEY,VALUE>::indexOfKey(const KEY& key) const {
136 return mVector.indexOf( key_value_pair_t<KEY,VALUE>(key) );
137}
138
139template<typename KEY, typename VALUE> inline
140const VALUE& KeyedVector<KEY,VALUE>::valueFor(const KEY& key) const {
Al Sutton6c865b52012-02-19 08:31:19 +0000141 ssize_t i = this->indexOfKey(key);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800142 assert(i>=0);
143 return mVector.itemAt(i).value;
144}
145
146template<typename KEY, typename VALUE> inline
147const VALUE& KeyedVector<KEY,VALUE>::valueAt(size_t index) const {
148 return mVector.itemAt(index).value;
149}
150
151template<typename KEY, typename VALUE> inline
Mathias Agopian67b58512012-08-02 18:32:23 -0700152const VALUE& KeyedVector<KEY,VALUE>::operator[] (size_t index) const {
153 return valueAt(index);
154}
155
156template<typename KEY, typename VALUE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800157const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const {
158 return mVector.itemAt(index).key;
159}
160
161template<typename KEY, typename VALUE> inline
162VALUE& KeyedVector<KEY,VALUE>::editValueFor(const KEY& key) {
Al Sutton6c865b52012-02-19 08:31:19 +0000163 ssize_t i = this->indexOfKey(key);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800164 assert(i>=0);
165 return mVector.editItemAt(i).value;
166}
167
168template<typename KEY, typename VALUE> inline
169VALUE& KeyedVector<KEY,VALUE>::editValueAt(size_t index) {
170 return mVector.editItemAt(index).value;
171}
172
173template<typename KEY, typename VALUE> inline
174ssize_t KeyedVector<KEY,VALUE>::add(const KEY& key, const VALUE& value) {
175 return mVector.add( key_value_pair_t<KEY,VALUE>(key, value) );
176}
177
178template<typename KEY, typename VALUE> inline
179ssize_t KeyedVector<KEY,VALUE>::replaceValueFor(const KEY& key, const VALUE& value) {
180 key_value_pair_t<KEY,VALUE> pair(key, value);
181 mVector.remove(pair);
182 return mVector.add(pair);
183}
184
185template<typename KEY, typename VALUE> inline
186ssize_t KeyedVector<KEY,VALUE>::replaceValueAt(size_t index, const VALUE& item) {
187 if (index<size()) {
Mathias Agopiancf89aa42009-04-10 20:27:25 -0700188 mVector.editItemAt(index).value = item;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800189 return index;
190 }
191 return BAD_INDEX;
192}
193
194template<typename KEY, typename VALUE> inline
195ssize_t KeyedVector<KEY,VALUE>::removeItem(const KEY& key) {
196 return mVector.remove(key_value_pair_t<KEY,VALUE>(key));
197}
198
199template<typename KEY, typename VALUE> inline
200ssize_t KeyedVector<KEY, VALUE>::removeItemsAt(size_t index, size_t count) {
201 return mVector.removeItemsAt(index, count);
202}
203
204// ---------------------------------------------------------------------------
205
206template<typename KEY, typename VALUE> inline
207DefaultKeyedVector<KEY,VALUE>::DefaultKeyedVector(const VALUE& defValue)
208 : mDefault(defValue)
209{
210}
211
212template<typename KEY, typename VALUE> inline
213const VALUE& DefaultKeyedVector<KEY,VALUE>::valueFor(const KEY& key) const {
Al Sutton6c865b52012-02-19 08:31:19 +0000214 ssize_t i = this->indexOfKey(key);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800215 return i >= 0 ? KeyedVector<KEY,VALUE>::valueAt(i) : mDefault;
216}
217
218}; // namespace android
219
220// ---------------------------------------------------------------------------
221
222#endif // ANDROID_KEYED_VECTOR_H