OpenTTD Source  12.0-beta2
smallmap_type.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef SMALLMAP_TYPE_HPP
11 #define SMALLMAP_TYPE_HPP
12 
13 #include "smallvec_type.hpp"
14 #include <utility>
15 
25 template <typename T, typename U>
26 struct SmallMap : std::vector<std::pair<T, U> > {
27  typedef std::pair<T, U> Pair;
28  typedef Pair *iterator;
29  typedef const Pair *const_iterator;
30 
32  inline SmallMap() { }
34  inline ~SmallMap() { }
35 
41  inline typename std::vector<Pair>::const_iterator Find(const T &key) const
42  {
43  typename std::vector<Pair>::const_iterator it;
44  for (it = std::vector<Pair>::begin(); it != std::vector<Pair>::end(); it++) {
45  if (key == it->first) return it;
46  }
47  return it;
48  }
49 
55  inline Pair *Find(const T &key)
56  {
57  for (uint i = 0; i < std::vector<Pair>::size(); i++) {
58  if (key == std::vector<Pair>::operator[](i).first) return &std::vector<Pair>::operator[](i);
59  }
60  return this->End();
61  }
62 
63  inline const Pair *End() const
64  {
65  return std::vector<Pair>::data() + std::vector<Pair>::size();
66  }
67 
68  inline Pair *End()
69  {
70  return std::vector<Pair>::data() + std::vector<Pair>::size();
71  }
72 
73 
79  inline bool Contains(const T &key) const
80  {
81  return this->Find(key) != std::vector<Pair>::end();
82  }
83 
89  inline bool Contains(const T &key)
90  {
91  return this->Find(key) != this->End();
92  }
93 
99  inline void Erase(Pair *pair)
100  {
101  assert(pair >= std::vector<Pair>::data() && pair < this->End());
102  auto distance = pair - std::vector<Pair>::data();
103  std::vector<Pair>::erase(std::vector<Pair>::begin() + distance);
104  }
105 
112  inline bool Erase(const T &key)
113  {
114  auto *pair = this->Find(key);
115  if (pair == this->End()) return false;
116 
117  this->Erase(pair);
118  return true;
119  }
120 
127  inline bool Insert(const T &key, const U &data)
128  {
129  if (this->Contains(key)) return false;
130  std::vector<Pair>::emplace_back(key, data);
131  return true;
132  }
133 
140  inline U &operator[](const T &key)
141  {
142  for (uint i = 0; i < std::vector<Pair>::size(); i++) {
143  if (key == std::vector<Pair>::operator[](i).first) return std::vector<Pair>::operator[](i).second;
144  }
145  Pair &n = std::vector<Pair>::emplace_back();
146  n.first = key;
147  return n.second;
148  }
149 };
150 
151 #endif /* SMALLMAP_TYPE_HPP */
SmallMap::operator[]
U & operator[](const T &key)
Returns data belonging to this key.
Definition: smallmap_type.hpp:140
smallvec_type.hpp
SmallMap::Find
Pair * Find(const T &key)
Finds given key in this map.
Definition: smallmap_type.hpp:55
SmallMap::Insert
bool Insert(const T &key, const U &data)
Adds new item to this map.
Definition: smallmap_type.hpp:127
SmallMap
Implementation of simple mapping class.
Definition: smallmap_type.hpp:26
SmallMap::~SmallMap
~SmallMap()
Data are freed in std::vector destructor.
Definition: smallmap_type.hpp:34
SmallMap::SmallMap
SmallMap()
Creates new SmallMap.
Definition: smallmap_type.hpp:32
SmallMap::Erase
bool Erase(const T &key)
Removes given key from this map.
Definition: smallmap_type.hpp:112
SmallMap::Erase
void Erase(Pair *pair)
Removes given pair from this map.
Definition: smallmap_type.hpp:99
SmallMap::Contains
bool Contains(const T &key)
Tests whether a key is assigned in this map.
Definition: smallmap_type.hpp:89
SmallMap::Find
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
Definition: smallmap_type.hpp:41
SmallMap::Contains
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Definition: smallmap_type.hpp:79