blob: 4ff9a42f6d5d26fd781dcb145aa5595392268eae [file] [log] [blame]
Chris Craik527a3aa2013-03-04 10:19:31 -08001/*
2 * Copyright (C) 2013 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_HWUI_TINYHASHMAP_H
18#define ANDROID_HWUI_TINYHASHMAP_H
19
20#include <utils/BasicHashtable.h>
21
22namespace android {
23namespace uirenderer {
24
25/**
26 * A very simple hash map that doesn't allow duplicate keys, overwriting the older entry.
Chris Craik527a3aa2013-03-04 10:19:31 -080027 */
28template <typename TKey, typename TValue>
29class TinyHashMap {
30public:
31 typedef key_value_pair_t<TKey, TValue> TEntry;
32
33 /**
34 * Puts an entry in the hash, removing any existing entry with the same key
35 */
36 void put(TKey key, TValue value) {
Ashok Bhate9bea2a2014-02-07 16:31:41 +000037 hash_t hash = android::hash_type(key);
Chris Craik527a3aa2013-03-04 10:19:31 -080038
39 ssize_t index = mTable.find(-1, hash, key);
40 if (index != -1) {
41 mTable.removeAt(index);
42 }
43
44 TEntry initEntry(key, value);
45 mTable.add(hash, initEntry);
46 }
47
48 /**
49 * Return true if key is in the map, in which case stores the value in the output ref
50 */
51 bool get(TKey key, TValue& outValue) {
Ashok Bhate9bea2a2014-02-07 16:31:41 +000052 hash_t hash = android::hash_type(key);
Chris Craik527a3aa2013-03-04 10:19:31 -080053 ssize_t index = mTable.find(-1, hash, key);
54 if (index == -1) {
55 return false;
56 }
57 outValue = mTable.entryAt(index).value;
58 return true;
59 }
60
61 void clear() { mTable.clear(); }
62
63private:
64 BasicHashtable<TKey, TEntry> mTable;
65};
66
67}; // namespace uirenderer
68}; // namespace android
69
70#endif // ANDROID_HWUI_TINYHASHMAP_H