10 #ifndef SMALLMAP_TYPE_HPP 11 #define SMALLMAP_TYPE_HPP 20 template <
typename T,
typename U>
26 inline SmallPair(
const T &first,
const U &second) : first(first), second(second) { }
39 template <
typename T,
typename U>
40 struct SmallMap : std::vector<SmallPair<T, U> > {
41 typedef ::SmallPair<T, U>
Pair;
55 inline typename std::vector<Pair>::const_iterator
Find(
const T &key)
const 57 typename std::vector<Pair>::const_iterator it;
58 for (it = std::vector<Pair>::begin(); it != std::vector<Pair>::end(); it++) {
59 if (key == it->first)
return it;
69 inline Pair *
Find(
const T &key)
71 for (uint i = 0; i < std::vector<Pair>::size(); i++) {
72 if (key == std::vector<Pair>::operator[](i).first)
return &std::vector<Pair>::operator[](i);
77 inline const Pair *End()
const 79 return std::vector<Pair>::data() + std::vector<Pair>::size();
84 return std::vector<Pair>::data() + std::vector<Pair>::size();
95 return this->Find(key) != std::vector<Pair>::end();
105 return this->Find(key) != this->End();
115 assert(pair >= std::vector<Pair>::data() && pair < this->End());
116 auto distance = pair - std::vector<Pair>::data();
117 std::vector<Pair>::erase(std::vector<Pair>::begin() + distance);
128 auto *pair = this->Find(key);
129 if (pair == this->End())
return false;
141 inline bool Insert(
const T &key,
const U &data)
143 if (this->Contains(key))
return false;
144 std::vector<Pair>::emplace_back(key, data);
156 for (uint i = 0; i < std::vector<Pair>::size(); i++) {
157 if (key == std::vector<Pair>::operator[](i).first)
return std::vector<Pair>::operator[](i).second;
159 std::vector<Pair>::emplace_back();
160 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.