Global.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * Global.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. /* ---------------------------------------------------------------------------- */
  12. /* Compiler detection */
  13. /* ---------------------------------------------------------------------------- */
  14. // Fixed width bool data type is important for serialization
  15. static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
  16. /* ---------------------------------------------------------------------------- */
  17. /* System detection. */
  18. /* ---------------------------------------------------------------------------- */
  19. // Based on: http://sourceforge.net/p/predef/wiki/OperatingSystems/
  20. // and on: http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
  21. // TODO?: Should be moved to vstd\os_detect.h (and then included by Global.h)
  22. #ifdef _WIN16 // Defined for 16-bit environments
  23. # error "16-bit Windows isn't supported"
  24. #elif defined(_WIN64) // Defined for 64-bit environments
  25. # define VCMI_WINDOWS
  26. # define VCMI_WINDOWS_64
  27. #elif defined(_WIN32) // Defined for both 32-bit and 64-bit environments
  28. # define VCMI_WINDOWS
  29. # define VCMI_WINDOWS_32
  30. #elif defined(_WIN32_WCE)
  31. # error "Windows CE isn't supported"
  32. #elif defined(__linux__) || defined(__gnu_linux__) || defined(linux) || defined(__linux)
  33. # define VCMI_UNIX
  34. # define VCMI_XDG
  35. # if defined(__ANDROID__) || defined(ANDROID)
  36. # define VCMI_ANDROID
  37. # endif
  38. #elif defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
  39. # define VCMI_UNIX
  40. # define VCMI_XDG
  41. # define VCMI_FREEBSD
  42. #elif defined(__HAIKU__)
  43. # define VCMI_UNIX
  44. # define VCMI_XDG
  45. # define VCMI_HAIKU
  46. #elif defined(__GNU__) || defined(__gnu_hurd__) || (defined(__MACH__) && !defined(__APPLE__))
  47. # define VCMI_UNIX
  48. # define VCMI_XDG
  49. # define VCMI_HURD
  50. #elif defined(__APPLE__) && defined(__MACH__)
  51. # define VCMI_UNIX
  52. # define VCMI_APPLE
  53. # include "TargetConditionals.h"
  54. # if TARGET_OS_SIMULATOR || TARGET_IPHONE_SIMULATOR
  55. # define VCMI_IOS
  56. # define VCMI_IOS_SIM
  57. # elif TARGET_OS_IPHONE
  58. # define VCMI_IOS
  59. # elif TARGET_OS_MAC
  60. # define VCMI_MAC
  61. # else
  62. //# warning "Unknown Apple target."?
  63. # endif
  64. #else
  65. # error "This platform isn't supported"
  66. #endif
  67. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  68. #define VCMI_MOBILE
  69. #endif
  70. /* ---------------------------------------------------------------------------- */
  71. /* Commonly used C++, Boost headers */
  72. /* ---------------------------------------------------------------------------- */
  73. #ifdef VCMI_WINDOWS
  74. # ifndef WIN32_LEAN_AND_MEAN
  75. # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers - delete this line if something is missing.
  76. # endif
  77. # ifndef NOMINMAX
  78. # define NOMINMAX // Exclude min/max macros from <Windows.h>. Use std::[min/max] from <algorithm> instead.
  79. # endif
  80. # ifndef _NO_W32_PSEUDO_MODIFIERS
  81. # define _NO_W32_PSEUDO_MODIFIERS // Exclude more macros for compiling with MinGW on Linux.
  82. # endif
  83. #endif
  84. /* ---------------------------------------------------------------------------- */
  85. /* A macro to force inlining some of our functions */
  86. /* ---------------------------------------------------------------------------- */
  87. // Compiler (at least MSVC) is not so smart here-> without that displaying is MUCH slower
  88. #ifdef _MSC_VER
  89. # define STRONG_INLINE __forceinline
  90. #elif __GNUC__
  91. # define STRONG_INLINE inline __attribute__((always_inline))
  92. #else
  93. # define STRONG_INLINE inline
  94. #endif
  95. #define _USE_MATH_DEFINES
  96. #include <algorithm>
  97. #include <array>
  98. #include <atomic>
  99. #include <bitset>
  100. #include <cassert>
  101. #include <climits>
  102. #include <cmath>
  103. #include <cstdlib>
  104. #include <cstdio>
  105. #include <fstream>
  106. #include <functional>
  107. #include <iomanip>
  108. #include <iostream>
  109. #include <map>
  110. #include <memory>
  111. #include <mutex>
  112. #include <numeric>
  113. #include <queue>
  114. #include <random>
  115. #include <set>
  116. #include <sstream>
  117. #include <string>
  118. #include <unordered_map>
  119. #include <unordered_set>
  120. #include <utility>
  121. #include <vector>
  122. //The only available version is 3, as of Boost 1.50
  123. #include <boost/version.hpp>
  124. #define BOOST_FILESYSTEM_VERSION 3
  125. #if BOOST_VERSION > 105000
  126. # define BOOST_THREAD_VERSION 3
  127. #endif
  128. #define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1
  129. //need to link boost thread dynamically to avoid https://stackoverflow.com/questions/35978572/boost-thread-interupt-does-not-work-when-crossing-a-dll-boundary
  130. #define BOOST_THREAD_USE_DLL //for example VCAI::finish() may freeze on thread join after interrupt when linking this statically
  131. #define BOOST_BIND_NO_PLACEHOLDERS
  132. #if BOOST_VERSION >= 106600
  133. #define BOOST_ASIO_ENABLE_OLD_SERVICES
  134. #endif
  135. #include <boost/algorithm/string.hpp>
  136. #include <boost/any.hpp>
  137. #include <boost/current_function.hpp>
  138. #include <boost/crc.hpp>
  139. #include <boost/date_time/posix_time/posix_time.hpp>
  140. #include <boost/date_time/posix_time/posix_time_io.hpp>
  141. #include <boost/filesystem.hpp>
  142. #include <boost/filesystem/path.hpp>
  143. #include <boost/filesystem/fstream.hpp>
  144. #include <boost/format.hpp>
  145. #include <boost/functional/hash.hpp>
  146. #include <boost/lexical_cast.hpp>
  147. #ifdef VCMI_WINDOWS
  148. #include <boost/locale/generator.hpp>
  149. #endif
  150. #include <boost/logic/tribool.hpp>
  151. #include <boost/optional.hpp>
  152. #include <boost/optional/optional_io.hpp>
  153. #include <boost/range/adaptor/filtered.hpp>
  154. #include <boost/range/adaptor/reversed.hpp>
  155. #include <boost/range/algorithm.hpp>
  156. #include <boost/thread.hpp>
  157. #include <boost/variant.hpp>
  158. #include <boost/multi_array.hpp>
  159. #ifndef M_PI
  160. # define M_PI 3.14159265358979323846
  161. #endif
  162. /* ---------------------------------------------------------------------------- */
  163. /* Usings */
  164. /* ---------------------------------------------------------------------------- */
  165. using namespace std::placeholders;
  166. namespace range = boost::range;
  167. /* ---------------------------------------------------------------------------- */
  168. /* Typedefs */
  169. /* ---------------------------------------------------------------------------- */
  170. // Integral data types
  171. typedef uint64_t ui64; //unsigned int 64 bits (8 bytes)
  172. typedef uint32_t ui32; //unsigned int 32 bits (4 bytes)
  173. typedef uint16_t ui16; //unsigned int 16 bits (2 bytes)
  174. typedef uint8_t ui8; //unsigned int 8 bits (1 byte)
  175. typedef int64_t si64; //signed int 64 bits (8 bytes)
  176. typedef int32_t si32; //signed int 32 bits (4 bytes)
  177. typedef int16_t si16; //signed int 16 bits (2 bytes)
  178. typedef int8_t si8; //signed int 8 bits (1 byte)
  179. // Lock typedefs
  180. using TLockGuard = std::lock_guard<std::mutex>;
  181. using TLockGuardRec = std::lock_guard<std::recursive_mutex>;
  182. /* ---------------------------------------------------------------------------- */
  183. /* Macros */
  184. /* ---------------------------------------------------------------------------- */
  185. // Import + Export macro declarations
  186. #ifdef VCMI_WINDOWS
  187. #ifdef VCMI_DLL_STATIC
  188. # define DLL_IMPORT
  189. # define DLL_EXPORT
  190. #elif defined(__GNUC__)
  191. # define DLL_IMPORT __attribute__((dllimport))
  192. # define DLL_EXPORT __attribute__((dllexport))
  193. # else
  194. # define DLL_IMPORT __declspec(dllimport)
  195. # define DLL_EXPORT __declspec(dllexport)
  196. # endif
  197. # define ELF_VISIBILITY
  198. #else
  199. # ifdef __GNUC__
  200. # define DLL_IMPORT __attribute__ ((visibility("default")))
  201. # define DLL_EXPORT __attribute__ ((visibility("default")))
  202. # define ELF_VISIBILITY __attribute__ ((visibility("default")))
  203. # endif
  204. #endif
  205. #ifdef VCMI_DLL
  206. # define DLL_LINKAGE DLL_EXPORT
  207. #else
  208. # define DLL_LINKAGE DLL_IMPORT
  209. #endif
  210. #define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
  211. // old iOS SDKs compatibility
  212. #ifdef VCMI_IOS
  213. #include <AvailabilityVersions.h>
  214. #ifndef __IPHONE_13_0
  215. #define __IPHONE_13_0 130000
  216. #endif
  217. #endif // VCMI_IOS
  218. // single-process build makes 2 copies of the main lib by wrapping it in a namespace
  219. #ifdef VCMI_LIB_NAMESPACE
  220. #define VCMI_LIB_NAMESPACE_BEGIN namespace VCMI_LIB_NAMESPACE {
  221. #define VCMI_LIB_NAMESPACE_END }
  222. #define VCMI_LIB_USING_NAMESPACE using namespace VCMI_LIB_NAMESPACE;
  223. #define VCMI_LIB_WRAP_NAMESPACE(x) VCMI_LIB_NAMESPACE::x
  224. #else
  225. #define VCMI_LIB_NAMESPACE_BEGIN
  226. #define VCMI_LIB_NAMESPACE_END
  227. #define VCMI_LIB_USING_NAMESPACE
  228. #define VCMI_LIB_WRAP_NAMESPACE(x) ::x
  229. #endif
  230. /* ---------------------------------------------------------------------------- */
  231. /* VCMI standard library */
  232. /* ---------------------------------------------------------------------------- */
  233. #include <vstd/CLoggerBase.h>
  234. VCMI_LIB_NAMESPACE_BEGIN
  235. void inline handleException()
  236. {
  237. try
  238. {
  239. throw;
  240. }
  241. catch(const std::exception & ex)
  242. {
  243. logGlobal->error(ex.what());
  244. }
  245. catch(const std::string & ex)
  246. {
  247. logGlobal->error(ex);
  248. }
  249. catch(...)
  250. {
  251. logGlobal->error("Sorry, caught unknown exception type. No more info available.");
  252. }
  253. }
  254. namespace vstd
  255. {
  256. // combine hashes. Present in boost but not in std
  257. template <class T>
  258. inline void hash_combine(std::size_t& seed, const T& v)
  259. {
  260. std::hash<T> hasher;
  261. seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  262. }
  263. //returns true if container c contains item i
  264. template <typename Container, typename Item>
  265. bool contains(const Container & c, const Item &i)
  266. {
  267. return std::find(std::begin(c), std::end(c),i) != std::end(c);
  268. }
  269. //returns true if container c contains item i
  270. template <typename Container, typename Pred>
  271. bool contains_if(const Container & c, Pred p)
  272. {
  273. return std::find_if(std::begin(c), std::end(c), p) != std::end(c);
  274. }
  275. //returns true if map c contains item i
  276. template <typename V, typename Item, typename Item2>
  277. bool contains(const std::map<Item,V> & c, const Item2 &i)
  278. {
  279. return c.find(i)!=c.end();
  280. }
  281. //returns true if unordered set c contains item i
  282. template <typename Item>
  283. bool contains(const std::unordered_set<Item> & c, const Item &i)
  284. {
  285. return c.find(i)!=c.end();
  286. }
  287. template <typename V, typename Item, typename Item2>
  288. bool contains(const std::unordered_map<Item,V> & c, const Item2 &i)
  289. {
  290. return c.find(i)!=c.end();
  291. }
  292. //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  293. template <typename Container, typename T2>
  294. int find_pos(const Container & c, const T2 &s)
  295. {
  296. int i=0;
  297. for (auto iter = std::begin(c); iter != std::end(c); iter++, i++)
  298. if(*iter == s)
  299. return i;
  300. return -1;
  301. }
  302. //Func f tells if element matches
  303. template <typename Container, typename Func>
  304. int find_pos_if(const Container & c, const Func &f)
  305. {
  306. auto ret = boost::range::find_if(c, f);
  307. if(ret != std::end(c))
  308. return std::distance(std::begin(c), ret);
  309. return -1;
  310. }
  311. //returns iterator to the given element if present in container, end() if not
  312. template <typename Container, typename Item>
  313. typename Container::iterator find(Container & c, const Item &i)
  314. {
  315. return std::find(c.begin(),c.end(),i);
  316. }
  317. //returns const iterator to the given element if present in container, end() if not
  318. template <typename Container, typename Item>
  319. typename Container::const_iterator find(const Container & c, const Item &i)
  320. {
  321. return std::find(c.begin(),c.end(),i);
  322. }
  323. //returns first key that maps to given value if present, returns success via found if provided
  324. template <typename Key, typename T>
  325. Key findKey(const std::map<Key, T> & map, const T & value, bool * found = nullptr)
  326. {
  327. for(auto iter = map.cbegin(); iter != map.cend(); iter++)
  328. {
  329. if(iter->second == value)
  330. {
  331. if(found)
  332. *found = true;
  333. return iter->first;
  334. }
  335. }
  336. if(found)
  337. *found = false;
  338. return Key();
  339. }
  340. //removes element i from container c, returns false if c does not contain i
  341. template <typename Container, typename Item>
  342. typename Container::size_type operator-=(Container &c, const Item &i)
  343. {
  344. typename Container::iterator itr = find(c,i);
  345. if(itr == c.end())
  346. return false;
  347. c.erase(itr);
  348. return true;
  349. }
  350. //assigns greater of (a, b) to a and returns maximum of (a, b)
  351. template <typename t1, typename t2>
  352. t1 &amax(t1 &a, const t2 &b)
  353. {
  354. if(a >= (t1)b)
  355. return a;
  356. else
  357. {
  358. a = t1(b);
  359. return a;
  360. }
  361. }
  362. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  363. template <typename t1, typename t2>
  364. t1 &amin(t1 &a, const t2 &b)
  365. {
  366. if(a <= (t1)b)
  367. return a;
  368. else
  369. {
  370. a = t1(b);
  371. return a;
  372. }
  373. }
  374. //makes a to fit the range <b, c>
  375. template <typename T>
  376. void abetween(T &value, const T &min, const T &max)
  377. {
  378. value = std::clamp(value, min, max);
  379. }
  380. //checks if a is between b and c
  381. template <typename t1, typename t2, typename t3>
  382. bool isbetween(const t1 &value, const t2 &min, const t3 &max)
  383. {
  384. return value > (t1)min && value < (t1)max;
  385. }
  386. //checks if a is within b and c
  387. template <typename t1, typename t2, typename t3>
  388. bool iswithin(const t1 &value, const t2 &min, const t3 &max)
  389. {
  390. return value >= (t1)min && value <= (t1)max;
  391. }
  392. template <typename t1, typename t2>
  393. struct assigner
  394. {
  395. public:
  396. t1 &op1;
  397. t2 op2;
  398. assigner(t1 &a1, const t2 & a2)
  399. :op1(a1), op2(a2)
  400. {}
  401. void operator()()
  402. {
  403. op1 = op2;
  404. }
  405. };
  406. //deleted pointer and sets it to nullptr
  407. template <typename T>
  408. void clear_pointer(T* &ptr)
  409. {
  410. delete ptr;
  411. ptr = nullptr;
  412. }
  413. template <typename Container>
  414. typename Container::const_reference circularAt(const Container &r, size_t index)
  415. {
  416. assert(r.size());
  417. index %= r.size();
  418. auto itr = std::begin(r);
  419. std::advance(itr, index);
  420. return *itr;
  421. }
  422. template <typename Container, typename Item>
  423. void erase(Container &c, const Item &item)
  424. {
  425. c.erase(boost::remove(c, item), c.end());
  426. }
  427. template<typename Range, typename Predicate>
  428. void erase_if(Range &vec, Predicate pred)
  429. {
  430. vec.erase(boost::remove_if(vec, pred),vec.end());
  431. }
  432. template<typename Elem, typename Predicate>
  433. void erase_if(std::set<Elem> &setContainer, Predicate pred)
  434. {
  435. auto itr = setContainer.begin();
  436. auto endItr = setContainer.end();
  437. while(itr != endItr)
  438. {
  439. auto tmpItr = itr++;
  440. if(pred(*tmpItr))
  441. setContainer.erase(tmpItr);
  442. }
  443. }
  444. //works for map and std::map, maybe something else
  445. template<typename Key, typename Val, typename Predicate>
  446. void erase_if(std::map<Key, Val> &container, Predicate pred)
  447. {
  448. auto itr = container.begin();
  449. auto endItr = container.end();
  450. while(itr != endItr)
  451. {
  452. auto tmpItr = itr++;
  453. if(pred(*tmpItr))
  454. container.erase(tmpItr);
  455. }
  456. }
  457. template<typename InputRange, typename OutputIterator, typename Predicate>
  458. OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred)
  459. {
  460. return std::copy_if(std::cbegin(input), std::end(input), result, pred);
  461. }
  462. template <typename Container>
  463. std::insert_iterator<Container> set_inserter(Container &c)
  464. {
  465. return std::inserter(c, c.end());
  466. }
  467. //Returns iterator to the element for which the value of ValueFunction is minimal
  468. template<class ForwardRange, class ValueFunction>
  469. auto minElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  470. {
  471. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  472. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  473. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  474. * directly for both function parameters.
  475. */
  476. return boost::min_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  477. {
  478. return vf(lhs) < vf(rhs);
  479. });
  480. }
  481. //Returns iterator to the element for which the value of ValueFunction is maximal
  482. template<class ForwardRange, class ValueFunction>
  483. auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  484. {
  485. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  486. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  487. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  488. * directly for both function parameters.
  489. */
  490. return boost::max_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  491. {
  492. return vf(lhs) < vf(rhs);
  493. });
  494. }
  495. template<typename T>
  496. void advance(T &obj, int change)
  497. {
  498. obj = (T)(((int)obj) + change);
  499. }
  500. template <typename Container>
  501. typename Container::value_type backOrNull(const Container &c) //returns last element of container or nullptr if it is empty (to be used with containers of pointers)
  502. {
  503. if(c.size())
  504. return c.back();
  505. else
  506. return typename Container::value_type();
  507. }
  508. template <typename Container>
  509. typename Container::value_type frontOrNull(const Container &c) //returns first element of container or nullptr if it is empty (to be used with containers of pointers)
  510. {
  511. if(c.size())
  512. return c.front();
  513. else
  514. return nullptr;
  515. }
  516. template <typename Container, typename Index>
  517. bool isValidIndex(const Container &c, Index i)
  518. {
  519. return i >= 0 && i < c.size();
  520. }
  521. template <typename Container, typename Index>
  522. boost::optional<typename Container::const_reference> tryAt(const Container &c, Index i)
  523. {
  524. if(isValidIndex(c, i))
  525. {
  526. auto itr = c.begin();
  527. std::advance(itr, i);
  528. return *itr;
  529. }
  530. return boost::none;
  531. }
  532. template <typename Container, typename Pred>
  533. static boost::optional<typename Container::const_reference> tryFindIf(const Container &r, const Pred &t)
  534. {
  535. auto pos = range::find_if(r, t);
  536. if(pos == boost::end(r))
  537. return boost::none;
  538. else
  539. return *pos;
  540. }
  541. template <typename Container>
  542. typename Container::const_reference atOrDefault(const Container &r, size_t index, const typename Container::const_reference &defaultValue)
  543. {
  544. if(index < r.size())
  545. return r[index];
  546. return defaultValue;
  547. }
  548. template <typename Container, typename Item>
  549. bool erase_if_present(Container &c, const Item &item)
  550. {
  551. auto i = std::find(c.begin(), c.end(), item);
  552. if (i != c.end())
  553. {
  554. c.erase(i);
  555. return true;
  556. }
  557. return false;
  558. }
  559. template <typename V, typename Item, typename Item2>
  560. bool erase_if_present(std::map<Item,V> & c, const Item2 &item)
  561. {
  562. auto i = c.find(item);
  563. if (i != c.end())
  564. {
  565. c.erase(i);
  566. return true;
  567. }
  568. return false;
  569. }
  570. template<typename T>
  571. void removeDuplicates(std::vector<T> &vec)
  572. {
  573. std::sort(vec.begin(), vec.end());
  574. vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  575. }
  576. template <typename T>
  577. void concatenate(std::vector<T> &dest, const std::vector<T> &src)
  578. {
  579. dest.reserve(dest.size() + src.size());
  580. dest.insert(dest.end(), src.begin(), src.end());
  581. }
  582. template <typename T>
  583. std::vector<T> intersection(std::vector<T> &v1, std::vector<T> &v2)
  584. {
  585. std::vector<T> v3;
  586. std::sort(v1.begin(), v1.end());
  587. std::sort(v2.begin(), v2.end());
  588. std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
  589. return v3;
  590. }
  591. template <typename Key, typename V>
  592. bool containsMapping(const std::multimap<Key,V> & map, const std::pair<const Key,V> & mapping)
  593. {
  594. auto range = map.equal_range(mapping.first);
  595. for(auto contained = range.first; contained != range.second; contained++)
  596. {
  597. if(mapping.second == contained->second)
  598. return true;
  599. }
  600. return false;
  601. }
  602. template <class M, class Key, class F>
  603. typename M::mapped_type & getOrCompute(M & m, Key const & k, F f)
  604. {
  605. typedef typename M::mapped_type V;
  606. std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
  607. V & v = r.first->second;
  608. if(r.second)
  609. f(v);
  610. return v;
  611. }
  612. //c++20 feature
  613. template<typename Arithmetic, typename Floating>
  614. Arithmetic lerp(const Arithmetic & a, const Arithmetic & b, const Floating & f)
  615. {
  616. return a + (b - a) * f;
  617. }
  618. ///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr
  619. static constexpr int abs(int i) {
  620. if(i < 0) return -i;
  621. return i;
  622. }
  623. ///C++23
  624. template< class Enum > constexpr std::underlying_type_t<Enum> to_underlying( Enum e ) noexcept
  625. {
  626. return static_cast<std::underlying_type_t<Enum>>(e);
  627. }
  628. }
  629. using vstd::operator-=;
  630. VCMI_LIB_NAMESPACE_END