You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ordered_map.h 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  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. #ifndef MINDSPORE_CCSRC_UTILS_ORDERED_MAP_H_
  17. #define MINDSPORE_CCSRC_UTILS_ORDERED_MAP_H_
  18. #include <algorithm>
  19. #include <unordered_map>
  20. #include <utility>
  21. #include <list>
  22. #include <string>
  23. #include <functional>
  24. #include <memory>
  25. #include "utils/log_adapter.h"
  26. namespace mindspore {
  27. // Implementation of OrderedMap that keeps insertion order
  28. // using unordered_map to improve the performance of find/erase, and use list to keep insertion order
  29. template <typename KeyT, typename ValueT, class Hash = std::hash<KeyT>, class Equal = std::equal_to<KeyT>>
  30. class OrderedMap {
  31. public:
  32. using key_t = KeyT;
  33. using value_t = ValueT;
  34. using hasher = Hash;
  35. using equal = Equal;
  36. using pair_type = std::pair<key_t, value_t>;
  37. using sequential_type = std::list<pair_type>;
  38. using iterator = typename sequential_type::iterator;
  39. using const_iterator = typename sequential_type::const_iterator;
  40. using reverse_iterator = typename sequential_type::reverse_iterator;
  41. using const_reverse_iterator = typename sequential_type::const_reverse_iterator;
  42. using map_type = std::unordered_map<key_t, iterator, hasher, equal>;
  43. using value_type = typename sequential_type::value_type;
  44. using size_type = typename sequential_type::size_type;
  45. iterator begin() { return sequential_data_.begin(); }
  46. iterator end() { return sequential_data_.end(); }
  47. const_iterator begin() const { return sequential_data_.cbegin(); }
  48. const_iterator end() const { return sequential_data_.cend(); }
  49. const_iterator cbegin() const { return sequential_data_.cbegin(); }
  50. const_iterator cend() const { return sequential_data_.cend(); }
  51. reverse_iterator rbegin() { return sequential_data_.rbegin(); }
  52. reverse_iterator rend() { return sequential_data_.rend(); }
  53. const_reverse_iterator rbegin() const { return sequential_data_.rbegin(); }
  54. const_reverse_iterator rend() const { return sequential_data_.rend(); }
  55. pair_type &front() { return sequential_data_.front(); }
  56. const pair_type &front() const { return sequential_data_.front(); }
  57. pair_type &back() { return sequential_data_.back(); }
  58. const pair_type &back() const { return sequential_data_.back(); }
  59. OrderedMap() = default;
  60. ~OrderedMap() = default;
  61. OrderedMap(const OrderedMap &os) {
  62. for (auto &item : os.sequential_data_) {
  63. (void)insert(pair_type(item.first, item.second));
  64. }
  65. }
  66. // Explicitly construct OrderedMap use sequential_type
  67. explicit OrderedMap(const sequential_type &other) {
  68. for (auto &item : other) {
  69. (void)insert(pair_type(item.first, item.second));
  70. }
  71. }
  72. OrderedMap &operator=(const OrderedMap &os) {
  73. if (this != &os) {
  74. for (auto &item : os.sequential_data_) {
  75. (void)insert(pair_type(item.first, item.second));
  76. }
  77. }
  78. return *this;
  79. }
  80. void clear() {
  81. map_data_.clear();
  82. sequential_data_.clear();
  83. }
  84. void swap(OrderedMap &rhs) {
  85. std::swap(map_data_, rhs.map_data_);
  86. std::swap(sequential_data_, rhs.sequential_data_);
  87. }
  88. void reserve(size_type num_entries) {
  89. map_data_.reserve(num_entries);
  90. sequential_data_.reserve(num_entries);
  91. }
  92. std::pair<iterator, bool> add(const key_t &key) {
  93. iterator empty_itr;
  94. std::pair<key_t, typename map_type::mapped_type> map_pair = std::make_pair(key, empty_itr);
  95. std::pair<typename map_type::iterator, bool> result = map_data_.insert(map_pair);
  96. auto &seq_itr = result.first->second;
  97. if (result.second) {
  98. auto it = sequential_data_.insert(sequential_data_.end(), std::make_pair(key, ValueT()));
  99. seq_itr = it;
  100. }
  101. return std::pair<iterator, bool>(seq_itr, result.second);
  102. }
  103. ValueT &operator[](const key_t &key) {
  104. auto result = add(key);
  105. return (*result.first).second;
  106. }
  107. std::pair<iterator, bool> insert(const pair_type &kv) {
  108. auto result = add(kv.first);
  109. if (result.second) {
  110. *(result.first) = kv.second;
  111. return std::make_pair(std::prev(end()), true);
  112. }
  113. return std::make_pair(result.first, false);
  114. }
  115. std::pair<iterator, bool> insert(pair_type &&kv) {
  116. iterator empty_itr;
  117. std::pair<key_t, typename map_type::mapped_type> map_pair = std::make_pair(kv.first, empty_itr);
  118. std::pair<typename map_type::iterator, bool> result = map_data_.insert(map_pair);
  119. auto &seq_itr = result.first->second;
  120. if (result.second) {
  121. auto it = sequential_data_.insert(sequential_data_.end(), std::move(kv));
  122. seq_itr = it;
  123. return std::make_pair(std::prev(end()), true);
  124. }
  125. return std::make_pair(seq_itr, false);
  126. }
  127. bool empty() const { return sequential_data_.empty(); }
  128. size_type size() const { return sequential_data_.size(); }
  129. size_type count(const key_t &key) const {
  130. auto pos = map_data_.find(key);
  131. return pos == map_data_.end() ? 0 : 1;
  132. }
  133. iterator find(const key_t &key) {
  134. typename map_type::const_iterator pos = map_data_.find(key);
  135. return pos == map_data_.end() ? sequential_data_.end() : (pos->second);
  136. }
  137. const_iterator find(const key_t &key) const {
  138. auto pos = map_data_.find(key);
  139. return pos == map_data_.end() ? sequential_data_.end() : (pos->second);
  140. }
  141. // Remove the last element from the sequential_data_.
  142. void pop_back() {
  143. typename map_type::iterator pos = map_data_.find(sequential_data_.back().first);
  144. map_data_.erase(pos);
  145. sequential_data_.pop_back();
  146. }
  147. // Remove the first element from the sequential_data_.
  148. void pop_front() {
  149. typename map_type::iterator pos = map_data_.find(sequential_data_.first().first);
  150. map_data_.erase(pos);
  151. sequential_data_.pop_front();
  152. }
  153. // Remove the element given by Iterator.
  154. typename sequential_type::iterator erase(const typename sequential_type::iterator &itr) {
  155. (void)map_data_.erase(itr->first);
  156. auto next = sequential_data_.erase(itr);
  157. if (next == sequential_data_.end()) return next;
  158. return next;
  159. }
  160. // Remove the element with the given key
  161. size_type erase(const key_t &key) {
  162. auto itr = find(key);
  163. if (itr == end()) return 0;
  164. (void)erase(itr);
  165. return 1;
  166. }
  167. private:
  168. map_type map_data_;
  169. sequential_type sequential_data_;
  170. };
  171. } // namespace mindspore
  172. #endif // MINDSPORE_CCSRC_UTILS_ORDERED_MAP_H_