1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-10-08 23:22:25 +02:00

Fixed potential thread races in Battle AI

This commit is contained in:
Ivan Savenko
2024-06-17 15:28:30 +00:00
parent f2d870e651
commit b7efa6c8cc
4 changed files with 26 additions and 37 deletions

View File

@@ -348,6 +348,15 @@ namespace vstd
return std::find(c.begin(),c.end(),i);
}
// returns existing value from map, or default value if key does not exists
template <typename Map>
const typename Map::mapped_type & find_or(const Map& m, const typename Map::key_type& key, const typename Map::mapped_type& defaultValue) {
auto it = m.find(key);
if (it == m.end())
return defaultValue;
return it->second;
}
//returns first key that maps to given value if present, returns success via found if provided
template <typename Key, typename T>
Key findKey(const std::map<Key, T> & map, const T & value, bool * found = nullptr)
@@ -684,20 +693,6 @@ namespace vstd
return false;
}
template<class M, class Key, class F>
typename M::mapped_type & getOrCompute(M & m, const Key & k, F f)
{
typedef typename M::mapped_type V;
std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
V & v = r.first->second;
if(r.second)
f(v);
return v;
}
//c++20 feature
template<typename Arithmetic, typename Floating>
Arithmetic lerp(const Arithmetic & a, const Arithmetic & b, const Floating & f)