Files
@ r24157:aad3518273e1
Branch filter:
Location: cpp/openttd-patchpack/source/src/core/smallmap_type.hpp - annotation
r24157:aad3518273e1
4.6 KiB
text/x-c++hdr
Update: Translations from eints
tamil: 16 changes by aswn
tamil: 16 changes by aswn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r16900:b96e0071e36a r16900:b96e0071e36a r18845:66bf168f1100 r18845:66bf168f1100 r16900:b96e0071e36a r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r18782:6453522c2154 r23516:763b689dacfc r10487:cee39532d33e r10487:cee39532d33e r15610:623a23fb6560 r15610:623a23fb6560 r10487:cee39532d33e r18845:66bf168f1100 r18845:66bf168f1100 r18845:66bf168f1100 r16900:b96e0071e36a r10487:cee39532d33e r10487:cee39532d33e r23539:bce8329ee42d r23538:8df50944b27a r10487:cee39532d33e r10487:cee39532d33e r13271:4bcd60b30aeb r10487:cee39532d33e r10487:cee39532d33e r18782:6453522c2154 r10487:cee39532d33e r18782:6453522c2154 r10487:cee39532d33e r15610:623a23fb6560 r15610:623a23fb6560 r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r23536:ce42deb0b32d r19786:2d286bcddff9 r23536:ce42deb0b32d r23536:ce42deb0b32d r23536:ce42deb0b32d r19786:2d286bcddff9 r23536:ce42deb0b32d r19786:2d286bcddff9 r19786:2d286bcddff9 r19786:2d286bcddff9 r19786:2d286bcddff9 r19786:2d286bcddff9 r19786:2d286bcddff9 r19786:2d286bcddff9 r18782:6453522c2154 r10487:cee39532d33e r23516:763b689dacfc r23516:763b689dacfc r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r23516:763b689dacfc r23516:763b689dacfc r23546:5248ab0a3163 r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23546:5248ab0a3163 r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r15610:623a23fb6560 r16240:4f0e181f881c r16240:4f0e181f881c r16240:4f0e181f881c r16240:4f0e181f881c r19786:2d286bcddff9 r16240:4f0e181f881c r23556:ef126a28738e r16240:4f0e181f881c r16240:4f0e181f881c r16240:4f0e181f881c r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r23516:763b689dacfc r15610:623a23fb6560 r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r18782:6453522c2154 r10487:cee39532d33e r23536:ce42deb0b32d r23531:603769aaf687 r23531:603769aaf687 r10487:cee39532d33e r10487:cee39532d33e r15610:623a23fb6560 r15610:623a23fb6560 r10487:cee39532d33e r11100:d2e797d6fe85 r10487:cee39532d33e r10487:cee39532d33e r18782:6453522c2154 r10487:cee39532d33e r23555:23b92d4dc3e4 r23555:23b92d4dc3e4 r23516:763b689dacfc r23555:23b92d4dc3e4 r23516:763b689dacfc r10487:cee39532d33e r10487:cee39532d33e r15610:623a23fb6560 r15610:623a23fb6560 r10487:cee39532d33e r10487:cee39532d33e r11100:d2e797d6fe85 r10487:cee39532d33e r18782:6453522c2154 r10487:cee39532d33e r16240:4f0e181f881c r23532:dc91fcd293f5 r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r15610:623a23fb6560 r15610:623a23fb6560 r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r18782:6453522c2154 r10487:cee39532d33e r23516:763b689dacfc r23516:763b689dacfc r10487:cee39532d33e r23532:dc91fcd293f5 r23532:dc91fcd293f5 r23532:dc91fcd293f5 r23532:dc91fcd293f5 r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e r10487:cee39532d33e | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file smallmap_type.hpp Simple mapping class targeted for small sets of data. Stored data shall be POD ("Plain Old Data")! */
#ifndef SMALLMAP_TYPE_HPP
#define SMALLMAP_TYPE_HPP
#include "smallvec_type.hpp"
/**
* Simple pair of data. Both types have to be POD ("Plain Old Data")!
* @tparam T Key type.
* @tparam U Value type.
*/
template <typename T, typename U>
struct SmallPair {
T first;
U second;
/** Initializes this Pair with data */
inline SmallPair(const T &first, const U &second) : first(first), second(second) { }
SmallPair() = default;
};
/**
* Implementation of simple mapping class. Both types have to be POD ("Plain Old Data")!
* It has inherited accessors from SmallVector().
* @tparam T Key type.
* @tparam U Value type.
* @tparam S Unit of allocation.
*
* @see SmallVector
*/
template <typename T, typename U>
struct SmallMap : std::vector<SmallPair<T, U> > {
typedef ::SmallPair<T, U> Pair;
typedef Pair *iterator;
typedef const Pair *const_iterator;
/** Creates new SmallMap. Data are initialized in SmallVector constructor */
inline SmallMap() { }
/** Data are freed in SmallVector destructor */
inline ~SmallMap() { }
/**
* Finds given key in this map
* @param key key to find
* @return &Pair(key, data) if found, this->End() if not
*/
inline typename std::vector<Pair>::const_iterator Find(const T &key) const
{
typename std::vector<Pair>::const_iterator it;
for (it = std::vector<Pair>::begin(); it != std::vector<Pair>::end(); it++) {
if (key == it->first) return it;
}
return it;
}
/**
* Finds given key in this map
* @param key key to find
* @return &Pair(key, data) if found, this->End() if not
*/
inline Pair *Find(const T &key)
{
for (uint i = 0; i < std::vector<Pair>::size(); i++) {
if (key == std::vector<Pair>::operator[](i).first) return &std::vector<Pair>::operator[](i);
}
return this->End();
}
inline const Pair *End() const
{
return std::vector<Pair>::data() + std::vector<Pair>::size();
}
inline Pair *End()
{
return std::vector<Pair>::data() + std::vector<Pair>::size();
}
/**
* Tests whether a key is assigned in this map.
* @param key key to test
* @return true iff the item is present
*/
inline bool Contains(const T &key) const
{
return this->Find(key) != std::vector<Pair>::end();
}
/**
* Tests whether a key is assigned in this map.
* @param key key to test
* @return true iff the item is present
*/
inline bool Contains(const T &key)
{
return this->Find(key) != this->End();
}
/**
* Removes given pair from this map
* @param pair pair to remove
* @note it has to be pointer to pair in this map. It is overwritten by the last item.
*/
inline void Erase(Pair *pair)
{
assert(pair >= std::vector<Pair>::data() && pair < this->End());
auto distance = pair - std::vector<Pair>::data();
std::vector<Pair>::erase(std::vector<Pair>::begin() + distance);
}
/**
* Removes given key from this map
* @param key key to remove
* @return true iff the key was found
* @note last item is moved to its place, so don't increase your iterator if true is returned!
*/
inline bool Erase(const T &key)
{
auto *pair = this->Find(key);
if (pair == this->End()) return false;
this->Erase(pair);
return true;
}
/**
* Adds new item to this map.
* @param key key
* @param data data
* @return true iff the key wasn't already present
*/
inline bool Insert(const T &key, const U &data)
{
if (this->Contains(key)) return false;
std::vector<Pair>::emplace_back(key, data);
return true;
}
/**
* Returns data belonging to this key
* @param key key
* @return data belonging to this key
* @note if this key wasn't present, new entry is created
*/
inline U &operator[](const T &key)
{
for (uint i = 0; i < std::vector<Pair>::size(); i++) {
if (key == std::vector<Pair>::operator[](i).first) return std::vector<Pair>::operator[](i).second;
}
/*C++17: Pair &n = */ std::vector<Pair>::emplace_back();
Pair &n = std::vector<Pair>::back();
n.first = key;
return n.second;
}
};
#endif /* SMALLMAP_TYPE_HPP */
|