blob: 59858e492c4c55d295c0bbfdbd89ce2cb5e04d89 [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
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080020#include <utility>
21#include <vector>
22
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "util/TypeTraits.h"
24
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080025namespace 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 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 using const_iterator =
33 typename std::vector<std::pair<TKey, TValue>>::const_iterator;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034
35 ImmutableMap(ImmutableMap&&) = default;
36 ImmutableMap& operator=(ImmutableMap&&) = default;
37
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 static ImmutableMap<TKey, TValue> CreatePreSorted(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 std::initializer_list<std::pair<TKey, TValue>> list) {
40 return ImmutableMap(
41 std::vector<std::pair<TKey, TValue>>(list.begin(), list.end()));
42 }
43
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 static ImmutableMap<TKey, TValue> CreateAndSort(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 std::initializer_list<std::pair<TKey, TValue>> list) {
46 std::vector<std::pair<TKey, TValue>> data(list.begin(), list.end());
47 std::sort(data.begin(), data.end());
48 return ImmutableMap(std::move(data));
49 }
50
51 template <typename TKey2, typename = typename std::enable_if<
52 is_comparable<TKey, TKey2>::value>::type>
53 const_iterator find(const TKey2& key) const {
54 auto cmp = [](const std::pair<TKey, TValue>& candidate,
55 const TKey2& target) -> bool {
56 return candidate.first < target;
57 };
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 const_iterator end_iter = end();
60 auto iter = std::lower_bound(data_.begin(), end_iter, key, cmp);
61 if (iter == end_iter || iter->first == key) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return iter;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080063 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 return end_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 const_iterator begin() const { return data_.begin(); }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 const_iterator end() const { return data_.end(); }
70
71 private:
72 DISALLOW_COPY_AND_ASSIGN(ImmutableMap);
73
74 explicit ImmutableMap(std::vector<std::pair<TKey, TValue>> data)
75 : data_(std::move(data)) {}
76
77 std::vector<std::pair<TKey, TValue>> data_;
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 */