blob: 9d8668b9cfbf8db9a165c0593d2d92daea1cb4bd [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
Mathias Agopian9857d992013-04-01 15:17:55 -07002 * Copyright 2005 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08003 *
Mathias Agopian9857d992013-04-01 15:17:55 -07004 * 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08007 *
Mathias Agopian9857d992013-04-01 15:17:55 -07008 * 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.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015 */
16
Mathias Agopian9857d992013-04-01 15:17:55 -070017#ifndef ANDROID_PIXELFLINGER_KEYED_VECTOR_H
18#define ANDROID_PIXELFLINGER_KEYED_VECTOR_H
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019
20#include <assert.h>
21#include <stdint.h>
22#include <sys/types.h>
23
Mathias Agopian9857d992013-04-01 15:17:55 -070024#include "Errors.h"
25#include "SortedVector.h"
26#include "TypeHelpers.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
28// ---------------------------------------------------------------------------
29
30namespace android {
Mathias Agopian9857d992013-04-01 15:17:55 -070031namespace tinyutils {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
33template <typename KEY, typename VALUE>
34class KeyedVector
35{
36public:
37 typedef KEY key_type;
38 typedef VALUE value_type;
39
40 inline KeyedVector();
41
42 /*
43 * empty the vector
44 */
45
46 inline void clear() { mVector.clear(); }
47
48 /*!
49 * vector stats
50 */
51
52 //! returns number of items in the vector
53 inline size_t size() const { return mVector.size(); }
54 //! returns wether or not the vector is empty
55 inline bool isEmpty() const { return mVector.isEmpty(); }
56 //! returns how many items can be stored without reallocating the backing store
57 inline size_t capacity() const { return mVector.capacity(); }
58 //! setst the capacity. capacity can never be reduced less than size()
59 inline ssize_t setCapacity(size_t size) { return mVector.setCapacity(size); }
60
61 /*!
62 * accessors
63 */
64 const VALUE& valueFor(const KEY& key) const;
65 const VALUE& valueAt(size_t index) const;
66 const KEY& keyAt(size_t index) const;
67 ssize_t indexOfKey(const KEY& key) const;
68
69 /*!
70 * modifing the array
71 */
72
73 VALUE& editValueFor(const KEY& key);
74 VALUE& editValueAt(size_t index);
75
76 /*!
77 * add/insert/replace items
78 */
79
80 ssize_t add(const KEY& key, const VALUE& item);
81 ssize_t replaceValueFor(const KEY& key, const VALUE& item);
82 ssize_t replaceValueAt(size_t index, const VALUE& item);
83
84 /*!
85 * remove items
86 */
87
88 ssize_t removeItem(const KEY& key);
89 ssize_t removeItemsAt(size_t index, size_t count = 1);
90
91private:
92 SortedVector< key_value_pair_t<KEY, VALUE> > mVector;
93};
94
95// ---------------------------------------------------------------------------
96
97/**
98 * Variation of KeyedVector that holds a default value to return when
99 * valueFor() is called with a key that doesn't exist.
100 */
101template <typename KEY, typename VALUE>
102class DefaultKeyedVector : public KeyedVector<KEY, VALUE>
103{
104public:
105 inline DefaultKeyedVector(const VALUE& defValue = VALUE());
106 const VALUE& valueFor(const KEY& key) const;
107
108private:
109 VALUE mDefault;
110};
111
112// ---------------------------------------------------------------------------
113
114template<typename KEY, typename VALUE> inline
115KeyedVector<KEY,VALUE>::KeyedVector()
116{
117}
118
119template<typename KEY, typename VALUE> inline
120ssize_t KeyedVector<KEY,VALUE>::indexOfKey(const KEY& key) const {
121 return mVector.indexOf( key_value_pair_t<KEY,VALUE>(key) );
122}
123
124template<typename KEY, typename VALUE> inline
125const VALUE& KeyedVector<KEY,VALUE>::valueFor(const KEY& key) const {
126 ssize_t i = indexOfKey(key);
127 assert(i>=0);
128 return mVector.itemAt(i).value;
129}
130
131template<typename KEY, typename VALUE> inline
132const VALUE& KeyedVector<KEY,VALUE>::valueAt(size_t index) const {
133 return mVector.itemAt(index).value;
134}
135
136template<typename KEY, typename VALUE> inline
137const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const {
138 return mVector.itemAt(index).key;
139}
140
141template<typename KEY, typename VALUE> inline
142VALUE& KeyedVector<KEY,VALUE>::editValueFor(const KEY& key) {
143 ssize_t i = indexOfKey(key);
144 assert(i>=0);
145 return mVector.editItemAt(i).value;
146}
147
148template<typename KEY, typename VALUE> inline
149VALUE& KeyedVector<KEY,VALUE>::editValueAt(size_t index) {
150 return mVector.editItemAt(index).value;
151}
152
153template<typename KEY, typename VALUE> inline
154ssize_t KeyedVector<KEY,VALUE>::add(const KEY& key, const VALUE& value) {
155 return mVector.add( key_value_pair_t<KEY,VALUE>(key, value) );
156}
157
158template<typename KEY, typename VALUE> inline
159ssize_t KeyedVector<KEY,VALUE>::replaceValueFor(const KEY& key, const VALUE& value) {
160 key_value_pair_t<KEY,VALUE> pair(key, value);
161 mVector.remove(pair);
162 return mVector.add(pair);
163}
164
165template<typename KEY, typename VALUE> inline
166ssize_t KeyedVector<KEY,VALUE>::replaceValueAt(size_t index, const VALUE& item) {
167 if (index<size()) {
168 mVector.editValueAt(index).value = item;
169 return index;
170 }
171 return BAD_INDEX;
172}
173
174template<typename KEY, typename VALUE> inline
175ssize_t KeyedVector<KEY,VALUE>::removeItem(const KEY& key) {
176 return mVector.remove(key_value_pair_t<KEY,VALUE>(key));
177}
178
179template<typename KEY, typename VALUE> inline
180ssize_t KeyedVector<KEY, VALUE>::removeItemsAt(size_t index, size_t count) {
181 return mVector.removeItemsAt(index, count);
182}
183
184// ---------------------------------------------------------------------------
185
186template<typename KEY, typename VALUE> inline
187DefaultKeyedVector<KEY,VALUE>::DefaultKeyedVector(const VALUE& defValue)
188 : mDefault(defValue)
189{
190}
191
192template<typename KEY, typename VALUE> inline
193const VALUE& DefaultKeyedVector<KEY,VALUE>::valueFor(const KEY& key) const {
194 ssize_t i = indexOfKey(key);
195 return i >= 0 ? KeyedVector<KEY,VALUE>::valueAt(i) : mDefault;
196}
197
Mathias Agopian9857d992013-04-01 15:17:55 -0700198} // namespace tinyutils
199} // namespace android
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
201// ---------------------------------------------------------------------------
202
Mathias Agopian9857d992013-04-01 15:17:55 -0700203#endif // ANDROID_PIXELFLINGER_KEYED_VECTOR_H