20 #ifndef OPM_ORDERED_MAP_HPP
21 #define OPM_ORDERED_MAP_HPP
23 #include <unordered_map>
31 template <
typename K,
typename T>
34 using storage_type =
typename std::vector<std::pair<K,T>>;
35 using index_type =
typename std::unordered_map<K,std::size_t>;
36 using iter_type =
typename storage_type::iterator;
37 using const_iter_type =
typename storage_type::const_iterator;
41 storage_type m_vector;
47 OrderedMap(
const index_type& index,
const storage_type& storage)
53 const index_type& getIndex()
const {
return m_map; }
55 const storage_type& getStorage()
const {
return m_vector; }
57 std::size_t count(
const K& key)
const {
58 return this->m_map.count(key);
63 T& operator[](
const K& key) {
64 if (this->count(key) == 0)
65 this->insert( std::make_pair(key, T()));
71 std::size_t erase(
const K& key) {
72 if (this->count(key) == 0)
75 std::size_t index = this->m_map.at(key);
76 this->m_map.erase(key);
77 this->m_vector.erase(this->m_vector.begin() + index);
79 for (
const auto& index_pair : this->m_map) {
80 auto target_index = index_pair.second;
81 if (target_index > index)
84 this->m_map[index_pair.first] = target_index;
90 void insert(std::pair<K,T> key_value_pair) {
91 if (this->count(key_value_pair.first) > 0) {
92 auto iter = m_map.find( key_value_pair.first );
93 size_t index = iter->second;
94 m_vector[index] = key_value_pair;
96 size_t index = m_vector.size();
97 this->m_map.emplace(key_value_pair.first, index);
98 this->m_vector.push_back( std::move( key_value_pair ) );
103 T& get(
const K& key) {
104 auto iter = m_map.find( key );
105 if (iter == m_map.end())
106 throw std::invalid_argument(
"Key not found:");
108 size_t index = iter->second;
114 T& iget(
size_t index) {
115 if (index >= m_vector.size())
116 throw std::invalid_argument(
"Invalid index");
117 return m_vector[index].second;
120 const T& get(
const K& key)
const {
121 const auto& iter = this->m_map.find( key );
122 if (iter == m_map.end())
123 throw std::invalid_argument(
"Key not found: ??");
125 size_t index = iter->second;
131 const T& iget(
size_t index)
const {
132 if (index >= m_vector.size())
133 throw std::invalid_argument(
"Invalid index");
134 return m_vector[index].second;
137 const T& at(
size_t index)
const {
138 return this->iget(index);
141 const T& at(
const K& key)
const {
142 return this->get(key);
145 T& at(
size_t index) {
146 return this->iget(index);
149 T& at(
const K& key) {
150 return this->get(key);
153 size_t size()
const {
154 return m_vector.size();
158 const_iter_type begin()
const {
159 return m_vector.begin();
163 const_iter_type end()
const {
164 return m_vector.end();
168 return m_vector.begin();
172 return m_vector.end();
175 iter_type find(
const K& key) {
176 const auto map_iter = this->m_map.find(key);
177 if (map_iter == this->m_map.end())
178 return this->m_vector.end();
180 return std::next(this->m_vector.begin(), map_iter->second);
183 const_iter_type find(
const K& key)
const {
184 const auto map_iter = this->m_map.find(key);
185 if (map_iter == this->m_map.end())
186 return this->m_vector.end();
188 return std::next(this->m_vector.begin(), map_iter->second);
192 return this->getIndex() == data.getIndex() &&
193 this->getStorage() == data.getStorage();
196 template<
class Serializer>
200 serializer.vector(m_vector);
Definition: OrderedMap.hpp:32
Definition: Serializer.hpp:38
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29