|
| 1 | +#ifndef _G3_CONTAINERS_H |
| 2 | +#define _G3_CONTAINERS_H |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <unordered_map> |
| 6 | +#include <iterator> |
| 7 | + |
| 8 | +template <typename K, typename T> |
| 9 | +class OrderedMap { |
| 10 | +public: |
| 11 | + using map_type = std::unordered_map<K, T>; |
| 12 | + using key_map_type = std::vector<K>; |
| 13 | + using key_type = K; |
| 14 | + using mapped_type = T; |
| 15 | + using value_type = typename map_type::value_type; |
| 16 | + using difference_type = typename map_type::difference_type; |
| 17 | + using size_type = typename map_type::size_type; |
| 18 | + |
| 19 | + OrderedMap() {}; |
| 20 | + OrderedMap(const OrderedMap& other) : |
| 21 | + map_(other.map_), keys_(other.keys_) {} |
| 22 | + OrderedMap(OrderedMap&& other) : |
| 23 | + map_(std::move(other.map_)), keys_(std::move(other.keys_)) {} |
| 24 | + virtual ~OrderedMap() {}; |
| 25 | + |
| 26 | + OrderedMap& operator=(const OrderedMap&) = default; |
| 27 | + OrderedMap& operator=(OrderedMap&&) = default; |
| 28 | + |
| 29 | + mapped_type& operator[](const key_type& key) { |
| 30 | + auto it = std::find(keys_.begin(), keys_.end(), key); |
| 31 | + if (it == keys_.end()) |
| 32 | + keys_.push_back(key); |
| 33 | + return map_[key]; |
| 34 | + } |
| 35 | + const mapped_type& operator[](const key_type& key) const { return map_.at(key); } |
| 36 | + mapped_type& at(const key_type& key) { return map_.at(key); } |
| 37 | + const mapped_type& at(const key_type& key) const { return map_.at(key); } |
| 38 | + bool contains(const key_type& key) const { return map_.count(key) > 0; } |
| 39 | + |
| 40 | + size_type size() const { return map_.size(); } |
| 41 | + size_type max_size() const { return map_.max_size(); } |
| 42 | + bool empty() const { return map_.empty(); } |
| 43 | + |
| 44 | + void clear() { |
| 45 | + map_.clear(); |
| 46 | + keys_.clear(); |
| 47 | + } |
| 48 | + |
| 49 | + size_type erase(const key_type& key) { |
| 50 | + auto it = std::find(keys_.begin(), keys_.end(), key); |
| 51 | + if (it == keys_.end()) |
| 52 | + return 0; |
| 53 | + |
| 54 | + keys_.erase(it); |
| 55 | + return map_.erase(key); |
| 56 | + } |
| 57 | + |
| 58 | + class iterator { |
| 59 | + public: |
| 60 | + using iterator_category = std::forward_iterator_tag; |
| 61 | + using value_type = typename map_type::value_type; |
| 62 | + using difference_type = typename map_type::difference_type; |
| 63 | + using pointer = value_type*; |
| 64 | + using reference = value_type&; |
| 65 | + |
| 66 | + typename key_map_type::iterator cur; |
| 67 | + map_type& map; |
| 68 | + |
| 69 | + iterator(typename key_map_type::iterator it, map_type& map) : |
| 70 | + cur(it), map(map) {} |
| 71 | + |
| 72 | + iterator& operator++() { |
| 73 | + ++cur; |
| 74 | + return *this; |
| 75 | + } |
| 76 | + |
| 77 | + iterator operator++(int) { |
| 78 | + iterator tmp = *this; |
| 79 | + ++(*this); |
| 80 | + return tmp; |
| 81 | + } |
| 82 | + |
| 83 | + bool operator==(const iterator& other) const { return cur == other.cur; } |
| 84 | + bool operator!=(const iterator& other) const { return cur != other.cur; } |
| 85 | + |
| 86 | + pointer operator->() const { return &(*(map.find(*cur))); } |
| 87 | + reference operator*() const { return *(map.find(*cur)); } |
| 88 | + }; |
| 89 | + |
| 90 | + class const_iterator { |
| 91 | + public: |
| 92 | + using iterator_category = std::forward_iterator_tag; |
| 93 | + using value_type = typename map_type::value_type; |
| 94 | + using difference_type = typename map_type::difference_type; |
| 95 | + using pointer = const value_type*; |
| 96 | + using reference = const value_type&; |
| 97 | + |
| 98 | + typename key_map_type::const_iterator cur; |
| 99 | + const map_type& map; |
| 100 | + |
| 101 | + const_iterator(typename key_map_type::const_iterator it, |
| 102 | + const map_type& map) : cur(it), map(map) {} |
| 103 | + |
| 104 | + const_iterator(const iterator& other) : cur(other.cur), map(other.map) {} |
| 105 | + |
| 106 | + const_iterator& operator++() { |
| 107 | + ++cur; |
| 108 | + return *this; |
| 109 | + } |
| 110 | + |
| 111 | + const_iterator operator++(int) { |
| 112 | + const_iterator tmp = *this; |
| 113 | + ++(*this); |
| 114 | + return tmp; |
| 115 | + } |
| 116 | + |
| 117 | + bool operator==(const const_iterator& other) const { return cur == other.cur; } |
| 118 | + bool operator!=(const const_iterator& other) const { return cur != other.cur; } |
| 119 | + bool operator==(const iterator& other) const { return cur == other.cur; } |
| 120 | + bool operator!=(const iterator& other) const { return cur != other.cur; } |
| 121 | + |
| 122 | + pointer operator->() const { return &(*(map.find(*cur))); } |
| 123 | + reference operator*() const { return *(map.find(*cur)); } |
| 124 | + }; |
| 125 | + |
| 126 | + iterator begin() { return iterator(keys_.begin(), map_); } |
| 127 | + iterator end() { return iterator(keys_.end(), map_); } |
| 128 | + const_iterator begin() const { return const_iterator(keys_.begin(), map_); } |
| 129 | + const_iterator end() const { return const_iterator(keys_.end(), map_); } |
| 130 | + const_iterator cbegin() const { return const_iterator(keys_.cbegin(), map_); } |
| 131 | + const_iterator cend() const { return const_iterator(keys_.cend(), map_); } |
| 132 | + |
| 133 | + iterator find(const key_type& key) { |
| 134 | + auto it = std::find(keys_.begin(), keys_.end(), key); |
| 135 | + if (it == keys_.end()) |
| 136 | + return end(); |
| 137 | + return iterator(it, map_); |
| 138 | + } |
| 139 | + |
| 140 | + const_iterator find(const key_type& key) const { |
| 141 | + auto it = std::find(keys_.cbegin(), keys_.cend(), key); |
| 142 | + if (it == keys_.cend()) |
| 143 | + return cend(); |
| 144 | + return const_iterator(it, map_); |
| 145 | + } |
| 146 | + |
| 147 | + std::pair<iterator, bool> insert(const value_type& pair) { |
| 148 | + const key_type& key = pair.first; |
| 149 | + |
| 150 | + bool check = false; |
| 151 | + auto it = std::find(keys_.begin(), keys_.end(), key); |
| 152 | + if (it == keys_.end()) { |
| 153 | + keys_.push_back(key); |
| 154 | + it = std::prev(keys_.end()); |
| 155 | + check = true; |
| 156 | + } |
| 157 | + map_.insert(pair); |
| 158 | + return {iterator(it, map_), check}; |
| 159 | + } |
| 160 | + |
| 161 | + std::pair<iterator, bool> insert(value_type&& pair) { |
| 162 | + const key_type& key = pair.first; |
| 163 | + |
| 164 | + bool check = false; |
| 165 | + auto it = std::find(keys_.begin(), keys_.end(), key); |
| 166 | + if (it == keys_.end()) { |
| 167 | + keys_.push_back(key); |
| 168 | + it = std::prev(keys_.end()); |
| 169 | + check = true; |
| 170 | + } |
| 171 | + map_.insert(pair); |
| 172 | + return {iterator(it, map_), check}; |
| 173 | + } |
| 174 | + |
| 175 | +protected: |
| 176 | + map_type map_; |
| 177 | + key_map_type keys_; |
| 178 | +}; |
| 179 | + |
| 180 | +#endif |
0 commit comments