blob: 6f48764d6bd4847c99ed9d2b49fcec01db6d195a [file] [log] [blame]
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001/*
2 * Copyright (C) 2015 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 AAPT_UTIL_IMMUTABLEMAP_H
18#define AAPT_UTIL_IMMUTABLEMAP_H
19
20#include "util/TypeTraits.h"
21
22#include <utility>
23#include <vector>
24
25namespace aapt {
26
27template <typename TKey, typename TValue>
28class ImmutableMap {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 static_assert(is_comparable<TKey, TKey>::value, "key is not comparable");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080030
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 private:
32 std::vector<std::pair<TKey, TValue>> mData;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080033
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 explicit ImmutableMap(std::vector<std::pair<TKey, TValue>> data)
35 : mData(std::move(data)) {}
36
37 public:
38 using const_iterator = typename decltype(mData)::const_iterator;
39
40 ImmutableMap(ImmutableMap&&) = default;
41 ImmutableMap& operator=(ImmutableMap&&) = default;
42
43 ImmutableMap(const ImmutableMap&) = delete;
44 ImmutableMap& operator=(const ImmutableMap&) = delete;
45
46 static ImmutableMap<TKey, TValue> createPreSorted(
47 std::initializer_list<std::pair<TKey, TValue>> list) {
48 return ImmutableMap(
49 std::vector<std::pair<TKey, TValue>>(list.begin(), list.end()));
50 }
51
52 static ImmutableMap<TKey, TValue> createAndSort(
53 std::initializer_list<std::pair<TKey, TValue>> list) {
54 std::vector<std::pair<TKey, TValue>> data(list.begin(), list.end());
55 std::sort(data.begin(), data.end());
56 return ImmutableMap(std::move(data));
57 }
58
59 template <typename TKey2, typename = typename std::enable_if<
60 is_comparable<TKey, TKey2>::value>::type>
61 const_iterator find(const TKey2& key) const {
62 auto cmp = [](const std::pair<TKey, TValue>& candidate,
63 const TKey2& target) -> bool {
64 return candidate.first < target;
65 };
66
67 const_iterator endIter = end();
68 auto iter = std::lower_bound(mData.begin(), endIter, key, cmp);
69 if (iter == endIter || iter->first == key) {
70 return iter;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080071 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 return endIter;
73 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 const_iterator begin() const { return mData.begin(); }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 const_iterator end() const { return mData.end(); }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080078};
79
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080} // namespace aapt
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080081
82#endif /* AAPT_UTIL_IMMUTABLEMAP_H */