blob: 7bda99beb5a2dc57bd7a19c01bb968a519672e4e [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
Mark Salyzyn51c33b72017-01-12 15:44:06 -080024#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070025#include <utils/Errors.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026#include <utils/SortedVector.h>
27#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028
29// ---------------------------------------------------------------------------
30
31namespace android {
32
Steven Morelandb8f152d2017-12-18 16:14:13 -080033// DO NOT USE: please use std::map
34
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080035template <typename KEY, typename VALUE>
36class KeyedVector
37{
38public:
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 Agopianbdf73c72012-08-09 19:39:15 -070056 //! returns whether or not the vector is empty
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080057 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 Agopianbdf73c72012-08-09 19:39:15 -070060 //! sets the capacity. capacity can never be reduced less than size()
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080061 inline ssize_t setCapacity(size_t size) { return mVector.setCapacity(size); }
Mathias Agopian67b58512012-08-02 18:32:23 -070062
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 Projectcbb10112009-03-03 19:31:44 -080066 /*!
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 Agopian67b58512012-08-02 18:32:23 -070073 const VALUE& operator[] (size_t index) const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074
75 /*!
Glenn Kasten73e263e2012-01-19 08:59:58 -080076 * modifying the array
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080077 */
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
97private:
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 */
107template <typename KEY, typename VALUE>
108class DefaultKeyedVector : public KeyedVector<KEY, VALUE>
109{
110public:
111 inline DefaultKeyedVector(const VALUE& defValue = VALUE());
112 const VALUE& valueFor(const KEY& key) const;
113
114private:
115 VALUE mDefault;
116};
117
118// ---------------------------------------------------------------------------
119
120template<typename KEY, typename VALUE> inline
121KeyedVector<KEY,VALUE>::KeyedVector()
122{
123}
124
125template<typename KEY, typename VALUE> inline
Mathias Agopian67b58512012-08-02 18:32:23 -0700126bool KeyedVector<KEY,VALUE>::isIdenticalTo(const KeyedVector<KEY,VALUE>& rhs) const {
127 return mVector.array() == rhs.mVector.array();
128}
129
130template<typename KEY, typename VALUE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131ssize_t KeyedVector<KEY,VALUE>::indexOfKey(const KEY& key) const {
132 return mVector.indexOf( key_value_pair_t<KEY,VALUE>(key) );
133}
134
135template<typename KEY, typename VALUE> inline
136const VALUE& KeyedVector<KEY,VALUE>::valueFor(const KEY& key) const {
Al Sutton6c865b52012-02-19 08:31:19 +0000137 ssize_t i = this->indexOfKey(key);
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700138 LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800139 return mVector.itemAt(i).value;
140}
141
142template<typename KEY, typename VALUE> inline
143const VALUE& KeyedVector<KEY,VALUE>::valueAt(size_t index) const {
144 return mVector.itemAt(index).value;
145}
146
147template<typename KEY, typename VALUE> inline
Mathias Agopian67b58512012-08-02 18:32:23 -0700148const VALUE& KeyedVector<KEY,VALUE>::operator[] (size_t index) const {
149 return valueAt(index);
150}
151
152template<typename KEY, typename VALUE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800153const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const {
154 return mVector.itemAt(index).key;
155}
156
157template<typename KEY, typename VALUE> inline
158VALUE& KeyedVector<KEY,VALUE>::editValueFor(const KEY& key) {
Al Sutton6c865b52012-02-19 08:31:19 +0000159 ssize_t i = this->indexOfKey(key);
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700160 LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__);
Colin Cross1811d152016-09-26 14:24:48 -0700161 return mVector.editItemAt(static_cast<size_t>(i)).value;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800162}
163
164template<typename KEY, typename VALUE> inline
165VALUE& KeyedVector<KEY,VALUE>::editValueAt(size_t index) {
166 return mVector.editItemAt(index).value;
167}
168
169template<typename KEY, typename VALUE> inline
170ssize_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
174template<typename KEY, typename VALUE> inline
175ssize_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
181template<typename KEY, typename VALUE> inline
182ssize_t KeyedVector<KEY,VALUE>::replaceValueAt(size_t index, const VALUE& item) {
183 if (index<size()) {
Mathias Agopiancf89aa42009-04-10 20:27:25 -0700184 mVector.editItemAt(index).value = item;
Colin Cross17b5b822016-09-15 18:15:37 -0700185 return static_cast<ssize_t>(index);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800186 }
187 return BAD_INDEX;
188}
189
190template<typename KEY, typename VALUE> inline
191ssize_t KeyedVector<KEY,VALUE>::removeItem(const KEY& key) {
192 return mVector.remove(key_value_pair_t<KEY,VALUE>(key));
193}
194
195template<typename KEY, typename VALUE> inline
196ssize_t KeyedVector<KEY, VALUE>::removeItemsAt(size_t index, size_t count) {
197 return mVector.removeItemsAt(index, count);
198}
199
200// ---------------------------------------------------------------------------
201
202template<typename KEY, typename VALUE> inline
203DefaultKeyedVector<KEY,VALUE>::DefaultKeyedVector(const VALUE& defValue)
204 : mDefault(defValue)
205{
206}
207
208template<typename KEY, typename VALUE> inline
209const VALUE& DefaultKeyedVector<KEY,VALUE>::valueFor(const KEY& key) const {
Al Sutton6c865b52012-02-19 08:31:19 +0000210 ssize_t i = this->indexOfKey(key);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800211 return i >= 0 ? KeyedVector<KEY,VALUE>::valueAt(i) : mDefault;
212}
213
Pirama Arumuga Nainar90affce2018-04-11 23:10:28 -0700214} // namespace android
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800215
216// ---------------------------------------------------------------------------
217
218#endif // ANDROID_KEYED_VECTOR_H