12 #ifndef SMALLMAP_TYPE_HPP 13 #define SMALLMAP_TYPE_HPP 22 template <
typename T,
typename U>
28 inline SmallPair(
const T &first,
const U &second) : first(first), second(second) { }
41 template <
typename T,
typename U>
42 struct SmallMap : std::vector<SmallPair<T, U> > {
43 typedef ::SmallPair<T, U>
Pair;
57 inline typename std::vector<Pair>::const_iterator
Find(
const T &key)
const 59 typename std::vector<Pair>::const_iterator it;
60 for (it = std::vector<Pair>::begin(); it != std::vector<Pair>::end(); it++) {
61 if (key == it->first)
return it;
71 inline Pair *
Find(
const T &key)
73 for (uint i = 0; i < std::vector<Pair>::size(); i++) {
74 if (key == std::vector<Pair>::operator[](i).first)
return &std::vector<Pair>::operator[](i);
79 inline const Pair *End()
const 81 return std::vector<Pair>::data() + std::vector<Pair>::size();
86 return std::vector<Pair>::data() + std::vector<Pair>::size();
97 return this->Find(key) != std::vector<Pair>::end();
107 return this->Find(key) != this->End();
117 assert(pair >= std::vector<Pair>::data() && pair < this->End());
118 auto distance = pair - std::vector<Pair>::data();
119 std::vector<Pair>::erase(std::vector<Pair>::begin() + distance);
130 auto *pair = this->Find(key);
131 if (pair == this->End())
return false;
143 inline bool Insert(
const T &key,
const U &data)
145 if (this->Contains(key))
return false;
146 std::vector<Pair>::emplace_back(key, data);
158 for (uint i = 0; i < std::vector<Pair>::size(); i++) {
159 if (key == std::vector<Pair>::operator[](i).first)
return std::vector<Pair>::operator[](i).second;
161 std::vector<Pair>::emplace_back();
162 Pair &n = std::vector<Pair>::back();
SmallPair(const T &first, const U &second)
Initializes this Pair with data.
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
Pair * Find(const T &key)
Finds given key in this map.
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
U & operator[](const T &key)
Returns data belonging to this key.
bool Erase(const T &key)
Removes given key from this map.
Implementation of simple mapping class.
bool Contains(const T &key)
Tests whether a key is assigned in this map.
SmallMap()
Creates new SmallMap.
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
~SmallMap()
Data are freed in SmallVector destructor.
bool Insert(const T &key, const U &data)
Adds new item to this map.
void Erase(Pair *pair)
Removes given pair from this map.