tvm
Loading...
Searching...
No Matches
ordered_map.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
24#ifndef TVM_SUPPORT_ORDERED_MAP_H_
25#define TVM_SUPPORT_ORDERED_MAP_H_
26
27#include <functional>
28#include <unordered_map>
29#include <utility>
30#include <vector>
31
32namespace tvm {
33namespace support {
34
44template <typename K, typename V, typename Hash = std::hash<K>,
45 typename KeyEqual = std::equal_to<K>>
47 public:
48 OrderedMap() = default;
49
50 /* \brief Explicit copy constructor
51 *
52 * The default copy constructor would copy both `elements_` and
53 * `elem_to_iter_`. While this is the correct behavior for
54 * `elements_`, the copy of `elem_to_iter_` would contain references
55 * to the original's `element_`, rather than to its own
56 */
57 OrderedMap(const OrderedMap<K, V, Hash, KeyEqual>& other) : elements_(other.elements_) {
58 InitElementToIter();
59 }
60
61 /* \brief Explicit copy assignment
62 *
63 * Implemented in terms of the copy constructor, and the default
64 * move assignment.
65 */
67 return *this = OrderedMap(other);
68 }
69
72
73 template <typename Iter>
74 OrderedMap(Iter begin, Iter end) : elements_(begin, end) {
75 InitElementToIter();
76 }
77
78 auto find(const K& k) {
79 auto it = elem_to_index_.find(k);
80 if (it != elem_to_index_.end()) {
81 return elements_.begin() + it->second;
82 }
83 return elements_.end();
84 }
85
86 auto find(const K& k) const {
87 auto it = elem_to_index_.find(k);
88 if (it != elem_to_index_.end()) {
89 return elements_.begin() + it->second;
90 }
91 return elements_.end();
92 }
93
94 V& operator[](const K& k) {
95 auto it = elem_to_index_.find(k);
96 if (it != elem_to_index_.end()) {
97 return elements_[it->second].second;
98 }
99 elements_.emplace_back(k, V());
100 elem_to_index_[k] = elements_.size() - 1;
101 return elements_.back().second;
102 }
103
104 void insert(const K& k, V v) {
105 auto it = elem_to_index_.find(k);
106 if (it != elem_to_index_.end()) {
107 elements_[it->second].second = std::move(v);
108 } else {
109 elements_.emplace_back(k, v);
110 elem_to_index_[k] = elements_.size() - 1;
111 }
112 }
113
114 void clear() {
115 elements_.clear();
116 elem_to_index_.clear();
117 }
118
119 size_t count(const K& k) const { return elem_to_index_.count(k); }
120
121 auto begin() const { return elements_.begin(); }
122 auto end() const { return elements_.end(); }
123 auto begin() { return elements_.begin(); }
124 auto end() { return elements_.end(); }
125
126 size_t size() const { return elements_.size(); }
127 bool empty() const { return elements_.empty(); }
128
129 void reserve(size_t n) { elem_to_index_.reserve(n); }
130
131 private:
132 void InitElementToIter() {
133 for (size_t i = 0; i < elements_.size(); i++) {
134 elem_to_index_[elements_[i].first] = i;
135 }
136 }
137
138 std::vector<std::pair<K, V>> elements_;
139 std::unordered_map<K, size_t, Hash, KeyEqual> elem_to_index_;
140};
141
142} // namespace support
143} // namespace tvm
144
145#endif // TVM_SUPPORT_ORDERED_MAP_H_
An STL-like map that preserves insertion order.
Definition ordered_map.h:46
V & operator[](const K &k)
Definition ordered_map.h:94
OrderedMap & operator=(OrderedMap< K, V, Hash, KeyEqual > &&)=default
OrderedMap & operator=(const OrderedMap< K, V, Hash, KeyEqual > &other)
Definition ordered_map.h:66
auto find(const K &k) const
Definition ordered_map.h:86
bool empty() const
Definition ordered_map.h:127
auto end()
Definition ordered_map.h:124
auto begin()
Definition ordered_map.h:123
void insert(const K &k, V v)
Definition ordered_map.h:104
OrderedMap(const OrderedMap< K, V, Hash, KeyEqual > &other)
Definition ordered_map.h:57
void reserve(size_t n)
Definition ordered_map.h:129
auto find(const K &k)
Definition ordered_map.h:78
OrderedMap(Iter begin, Iter end)
Definition ordered_map.h:74
auto begin() const
Definition ordered_map.h:121
size_t count(const K &k) const
Definition ordered_map.h:119
void clear()
Definition ordered_map.h:114
auto end() const
Definition ordered_map.h:122
OrderedMap(OrderedMap< K, V, Hash, KeyEqual > &&)=default
size_t size() const
Definition ordered_map.h:126
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23