Global.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. #ifndef NDEBUG
  97. // Enable additional debug checks from glibc / libstdc++ when building with enabled assertions
  98. // Since these defines must be declared BEFORE including glibc header we can not check for __GLIBCXX__ macro to detect that glibc is in use
  99. # define _GLIBCXX_ASSERTIONS
  100. #endif
  101. #include <algorithm>
  102. #include <any>
  103. #include <array>
  104. #include <atomic>
  105. #include <bitset>
  106. #include <cassert>
  107. #include <climits>
  108. #include <cmath>
  109. #include <codecvt>
  110. #include <cstdlib>
  111. #include <cstdio>
  112. #include <fstream>
  113. #include <functional>
  114. #include <iomanip>
  115. #include <iostream>
  116. #include <map>
  117. #include <memory>
  118. #include <mutex>
  119. #include <numeric>
  120. #include <optional>
  121. #include <queue>
  122. #include <random>
  123. #include <regex>
  124. #include <set>
  125. #include <sstream>
  126. #include <string>
  127. #include <unordered_map>
  128. #include <unordered_set>
  129. #include <utility>
  130. #include <variant>
  131. #include <vector>
  132. //The only available version is 3, as of Boost 1.50
  133. #include <boost/version.hpp>
  134. #define BOOST_FILESYSTEM_VERSION 3
  135. #if BOOST_VERSION > 105000
  136. # define BOOST_THREAD_VERSION 3
  137. #endif
  138. #define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1
  139. //need to link boost thread dynamically to avoid https://stackoverflow.com/questions/35978572/boost-thread-interupt-does-not-work-when-crossing-a-dll-boundary
  140. #define BOOST_THREAD_USE_DLL //for example VCAI::finish() may freeze on thread join after interrupt when linking this statically
  141. #define BOOST_BIND_NO_PLACEHOLDERS
  142. #if BOOST_VERSION >= 106600
  143. #define BOOST_ASIO_ENABLE_OLD_SERVICES
  144. #endif
  145. #include <boost/algorithm/hex.hpp>
  146. #include <boost/algorithm/string.hpp>
  147. #include <boost/crc.hpp>
  148. #include <boost/current_function.hpp>
  149. #include <boost/date_time/posix_time/posix_time_types.hpp>
  150. #include <boost/date_time/posix_time/time_formatters.hpp>
  151. #include <boost/filesystem.hpp>
  152. #include <boost/filesystem/fstream.hpp>
  153. #include <boost/filesystem/path.hpp>
  154. #include <boost/format.hpp>
  155. #include <boost/functional/hash.hpp>
  156. #include <boost/lexical_cast.hpp>
  157. #ifdef VCMI_WINDOWS
  158. #include <boost/locale/generator.hpp>
  159. #endif
  160. #include <boost/logic/tribool.hpp>
  161. #include <boost/multi_array.hpp>
  162. #include <boost/range/adaptor/filtered.hpp>
  163. #include <boost/range/adaptor/reversed.hpp>
  164. #include <boost/range/algorithm.hpp>
  165. #include <boost/thread/thread_only.hpp>
  166. #include <boost/thread/shared_mutex.hpp>
  167. #include <boost/thread/recursive_mutex.hpp>
  168. #include <boost/thread/once.hpp>
  169. #ifndef M_PI
  170. # define M_PI 3.14159265358979323846
  171. #endif
  172. /* ---------------------------------------------------------------------------- */
  173. /* Usings */
  174. /* ---------------------------------------------------------------------------- */
  175. using namespace std::placeholders;
  176. namespace range = boost::range;
  177. /* ---------------------------------------------------------------------------- */
  178. /* Typedefs */
  179. /* ---------------------------------------------------------------------------- */
  180. // Integral data types
  181. typedef uint64_t ui64; //unsigned int 64 bits (8 bytes)
  182. typedef uint32_t ui32; //unsigned int 32 bits (4 bytes)
  183. typedef uint16_t ui16; //unsigned int 16 bits (2 bytes)
  184. typedef uint8_t ui8; //unsigned int 8 bits (1 byte)
  185. typedef int64_t si64; //signed int 64 bits (8 bytes)
  186. typedef int32_t si32; //signed int 32 bits (4 bytes)
  187. typedef int16_t si16; //signed int 16 bits (2 bytes)
  188. typedef int8_t si8; //signed int 8 bits (1 byte)
  189. // Lock typedefs
  190. using TLockGuard = std::lock_guard<std::mutex>;
  191. using TLockGuardRec = std::lock_guard<std::recursive_mutex>;
  192. /* ---------------------------------------------------------------------------- */
  193. /* Macros */
  194. /* ---------------------------------------------------------------------------- */
  195. // Import + Export macro declarations
  196. #ifdef VCMI_WINDOWS
  197. #ifdef VCMI_DLL_STATIC
  198. # define DLL_IMPORT
  199. # define DLL_EXPORT
  200. #elif defined(__GNUC__)
  201. # define DLL_IMPORT __attribute__((dllimport))
  202. # define DLL_EXPORT __attribute__((dllexport))
  203. # else
  204. # define DLL_IMPORT __declspec(dllimport)
  205. # define DLL_EXPORT __declspec(dllexport)
  206. # endif
  207. # define ELF_VISIBILITY
  208. #else
  209. # ifdef __GNUC__
  210. # define DLL_IMPORT __attribute__ ((visibility("default")))
  211. # define DLL_EXPORT __attribute__ ((visibility("default")))
  212. # define ELF_VISIBILITY __attribute__ ((visibility("default")))
  213. # endif
  214. #endif
  215. #ifdef VCMI_DLL
  216. # define DLL_LINKAGE DLL_EXPORT
  217. #else
  218. # define DLL_LINKAGE DLL_IMPORT
  219. #endif
  220. #define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
  221. // old iOS SDKs compatibility
  222. #ifdef VCMI_IOS
  223. #include <AvailabilityVersions.h>
  224. #ifndef __IPHONE_13_0
  225. #define __IPHONE_13_0 130000
  226. #endif
  227. #endif // VCMI_IOS
  228. // single-process build makes 2 copies of the main lib by wrapping it in a namespace
  229. #ifdef VCMI_LIB_NAMESPACE
  230. #define VCMI_LIB_NAMESPACE_BEGIN namespace VCMI_LIB_NAMESPACE {
  231. #define VCMI_LIB_NAMESPACE_END }
  232. #define VCMI_LIB_USING_NAMESPACE using namespace VCMI_LIB_NAMESPACE;
  233. #define VCMI_LIB_WRAP_NAMESPACE(x) VCMI_LIB_NAMESPACE::x
  234. #else
  235. #define VCMI_LIB_NAMESPACE_BEGIN
  236. #define VCMI_LIB_NAMESPACE_END
  237. #define VCMI_LIB_USING_NAMESPACE
  238. #define VCMI_LIB_WRAP_NAMESPACE(x) ::x
  239. #endif
  240. /* ---------------------------------------------------------------------------- */
  241. /* VCMI standard library */
  242. /* ---------------------------------------------------------------------------- */
  243. #include <vstd/CLoggerBase.h>
  244. VCMI_LIB_NAMESPACE_BEGIN
  245. namespace vstd
  246. {
  247. // combine hashes. Present in boost but not in std
  248. template <class T>
  249. inline void hash_combine(std::size_t& seed, const T& v)
  250. {
  251. std::hash<T> hasher;
  252. seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  253. }
  254. //returns true if container c contains item i
  255. template <typename Container, typename Item>
  256. bool contains(const Container & c, const Item &i)
  257. {
  258. return std::find(std::begin(c), std::end(c),i) != std::end(c);
  259. }
  260. //returns true if container c contains item i
  261. template <typename Container, typename Pred>
  262. bool contains_if(const Container & c, Pred p)
  263. {
  264. return std::find_if(std::begin(c), std::end(c), p) != std::end(c);
  265. }
  266. //returns true if map c contains item i
  267. template <typename V, typename Item, typename Item2>
  268. bool contains(const std::map<Item,V> & c, const Item2 &i)
  269. {
  270. return c.find(i)!=c.end();
  271. }
  272. //returns true if unordered set c contains item i
  273. template <typename Item>
  274. bool contains(const std::unordered_set<Item> & c, const Item &i)
  275. {
  276. return c.find(i)!=c.end();
  277. }
  278. template <typename V, typename Item, typename Item2>
  279. bool contains(const std::unordered_map<Item,V> & c, const Item2 &i)
  280. {
  281. return c.find(i)!=c.end();
  282. }
  283. //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  284. template <typename Container, typename T2>
  285. int find_pos(const Container & c, const T2 &s)
  286. {
  287. int i=0;
  288. for (auto iter = std::begin(c); iter != std::end(c); iter++, i++)
  289. if(*iter == s)
  290. return i;
  291. return -1;
  292. }
  293. //Func f tells if element matches
  294. template <typename Container, typename Func>
  295. int find_pos_if(const Container & c, const Func &f)
  296. {
  297. auto ret = boost::range::find_if(c, f);
  298. if(ret != std::end(c))
  299. return std::distance(std::begin(c), ret);
  300. return -1;
  301. }
  302. //returns iterator to the given element if present in container, end() if not
  303. template <typename Container, typename Item>
  304. typename Container::iterator find(Container & c, const Item &i)
  305. {
  306. return std::find(c.begin(),c.end(),i);
  307. }
  308. //returns const iterator to the given element if present in container, end() if not
  309. template <typename Container, typename Item>
  310. typename Container::const_iterator find(const Container & c, const Item &i)
  311. {
  312. return std::find(c.begin(),c.end(),i);
  313. }
  314. //returns first key that maps to given value if present, returns success via found if provided
  315. template <typename Key, typename T>
  316. Key findKey(const std::map<Key, T> & map, const T & value, bool * found = nullptr)
  317. {
  318. for(auto iter = map.cbegin(); iter != map.cend(); iter++)
  319. {
  320. if(iter->second == value)
  321. {
  322. if(found)
  323. *found = true;
  324. return iter->first;
  325. }
  326. }
  327. if(found)
  328. *found = false;
  329. return Key();
  330. }
  331. //removes element i from container c, returns false if c does not contain i
  332. template <typename Container, typename Item>
  333. typename Container::size_type operator-=(Container &c, const Item &i)
  334. {
  335. typename Container::iterator itr = find(c,i);
  336. if(itr == c.end())
  337. return false;
  338. c.erase(itr);
  339. return true;
  340. }
  341. //assigns greater of (a, b) to a and returns maximum of (a, b)
  342. template <typename t1, typename t2>
  343. t1 &amax(t1 &a, const t2 &b)
  344. {
  345. if(a >= (t1)b)
  346. return a;
  347. else
  348. {
  349. a = t1(b);
  350. return a;
  351. }
  352. }
  353. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  354. template <typename t1, typename t2>
  355. t1 &amin(t1 &a, const t2 &b)
  356. {
  357. if(a <= (t1)b)
  358. return a;
  359. else
  360. {
  361. a = t1(b);
  362. return a;
  363. }
  364. }
  365. //makes a to fit the range <b, c>
  366. template <typename T>
  367. void abetween(T &value, const T &min, const T &max)
  368. {
  369. value = std::clamp(value, min, max);
  370. }
  371. //checks if a is between b and c
  372. template <typename t1, typename t2, typename t3>
  373. bool isbetween(const t1 &value, const t2 &min, const t3 &max)
  374. {
  375. return value > (t1)min && value < (t1)max;
  376. }
  377. //checks if a is within b and c
  378. template <typename t1, typename t2, typename t3>
  379. bool iswithin(const t1 &value, const t2 &min, const t3 &max)
  380. {
  381. return value >= (t1)min && value <= (t1)max;
  382. }
  383. template <typename t1, typename t2>
  384. struct assigner
  385. {
  386. public:
  387. t1 &op1;
  388. t2 op2;
  389. assigner(t1 &a1, const t2 & a2)
  390. :op1(a1), op2(a2)
  391. {}
  392. void operator()()
  393. {
  394. op1 = op2;
  395. }
  396. };
  397. //deleted pointer and sets it to nullptr
  398. template <typename T>
  399. void clear_pointer(T* &ptr)
  400. {
  401. delete ptr;
  402. ptr = nullptr;
  403. }
  404. template <typename Container>
  405. typename Container::const_reference circularAt(const Container &r, size_t index)
  406. {
  407. assert(r.size());
  408. index %= r.size();
  409. auto itr = std::begin(r);
  410. std::advance(itr, index);
  411. return *itr;
  412. }
  413. template <typename Container, typename Item>
  414. void erase(Container &c, const Item &item)
  415. {
  416. c.erase(boost::remove(c, item), c.end());
  417. }
  418. template<typename Range, typename Predicate>
  419. void erase_if(Range &vec, Predicate pred)
  420. {
  421. vec.erase(boost::remove_if(vec, pred),vec.end());
  422. }
  423. template<typename Elem, typename Predicate>
  424. void erase_if(std::set<Elem> &setContainer, Predicate pred)
  425. {
  426. auto itr = setContainer.begin();
  427. auto endItr = setContainer.end();
  428. while(itr != endItr)
  429. {
  430. auto tmpItr = itr++;
  431. if(pred(*tmpItr))
  432. setContainer.erase(tmpItr);
  433. }
  434. }
  435. template<typename Elem, typename Predicate>
  436. void erase_if(std::unordered_set<Elem> &setContainer, Predicate pred)
  437. {
  438. auto itr = setContainer.begin();
  439. auto endItr = setContainer.end();
  440. while(itr != endItr)
  441. {
  442. auto tmpItr = itr++;
  443. if(pred(*tmpItr))
  444. setContainer.erase(tmpItr);
  445. }
  446. }
  447. //works for map and std::map, maybe something else
  448. template<typename Key, typename Val, typename Predicate>
  449. void erase_if(std::map<Key, Val> &container, Predicate pred)
  450. {
  451. auto itr = container.begin();
  452. auto endItr = container.end();
  453. while(itr != endItr)
  454. {
  455. auto tmpItr = itr++;
  456. if(pred(*tmpItr))
  457. container.erase(tmpItr);
  458. }
  459. }
  460. template<typename InputRange, typename OutputIterator, typename Predicate>
  461. OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred)
  462. {
  463. return std::copy_if(std::cbegin(input), std::end(input), result, pred);
  464. }
  465. template <typename Container>
  466. std::insert_iterator<Container> set_inserter(Container &c)
  467. {
  468. return std::inserter(c, c.end());
  469. }
  470. //Returns iterator to the element for which the value of ValueFunction is minimal
  471. template<class ForwardRange, class ValueFunction>
  472. auto minElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  473. {
  474. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  475. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  476. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  477. * directly for both function parameters.
  478. */
  479. return boost::min_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  480. {
  481. return vf(lhs) < vf(rhs);
  482. });
  483. }
  484. //Returns iterator to the element for which the value of ValueFunction is maximal
  485. template<class ForwardRange, class ValueFunction>
  486. auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  487. {
  488. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  489. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  490. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  491. * directly for both function parameters.
  492. */
  493. return boost::max_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  494. {
  495. return vf(lhs) < vf(rhs);
  496. });
  497. }
  498. /// Increments value by specific delta
  499. /// similar to std::next but works with other types, e.g. enum class
  500. template<typename T>
  501. T next(const T &obj, int change)
  502. {
  503. return static_cast<T>(static_cast<ptrdiff_t>(obj) + change);
  504. }
  505. template <typename Container>
  506. 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)
  507. {
  508. if(c.size())
  509. return c.back();
  510. else
  511. return typename Container::value_type();
  512. }
  513. template <typename Container>
  514. 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)
  515. {
  516. if(c.size())
  517. return c.front();
  518. else
  519. return nullptr;
  520. }
  521. template <typename Container, typename Index>
  522. bool isValidIndex(const Container &c, Index i)
  523. {
  524. return i >= 0 && i < c.size();
  525. }
  526. template <typename Container>
  527. typename Container::const_reference atOrDefault(const Container &r, size_t index, const typename Container::const_reference &defaultValue)
  528. {
  529. if(index < r.size())
  530. return r[index];
  531. return defaultValue;
  532. }
  533. template <typename Container, typename Item>
  534. bool erase_if_present(Container &c, const Item &item)
  535. {
  536. auto i = std::find(c.begin(), c.end(), item);
  537. if (i != c.end())
  538. {
  539. c.erase(i);
  540. return true;
  541. }
  542. return false;
  543. }
  544. template <typename V, typename Item, typename Item2>
  545. bool erase_if_present(std::map<Item,V> & c, const Item2 &item)
  546. {
  547. auto i = c.find(item);
  548. if (i != c.end())
  549. {
  550. c.erase(i);
  551. return true;
  552. }
  553. return false;
  554. }
  555. template<typename T>
  556. void removeDuplicates(std::vector<T> &vec)
  557. {
  558. std::sort(vec.begin(), vec.end());
  559. vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  560. }
  561. template <typename T>
  562. void concatenate(std::vector<T> &dest, const std::vector<T> &src)
  563. {
  564. dest.reserve(dest.size() + src.size());
  565. dest.insert(dest.end(), src.begin(), src.end());
  566. }
  567. template <typename T>
  568. std::vector<T> intersection(std::vector<T> &v1, std::vector<T> &v2)
  569. {
  570. std::vector<T> v3;
  571. std::sort(v1.begin(), v1.end());
  572. std::sort(v2.begin(), v2.end());
  573. std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
  574. return v3;
  575. }
  576. template <typename T>
  577. std::set<T> difference(const std::set<T> &s1, const std::set<T> s2)
  578. {
  579. std::set<T> s3;
  580. std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(s3, s3.end()));
  581. return s3;
  582. }
  583. template <typename Key, typename V>
  584. bool containsMapping(const std::multimap<Key,V> & map, const std::pair<const Key,V> & mapping)
  585. {
  586. auto range = map.equal_range(mapping.first);
  587. for(auto contained = range.first; contained != range.second; contained++)
  588. {
  589. if(mapping.second == contained->second)
  590. return true;
  591. }
  592. return false;
  593. }
  594. template<class M, class Key, class F>
  595. typename M::mapped_type & getOrCompute(M & m, const Key & k, F f)
  596. {
  597. typedef typename M::mapped_type V;
  598. std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
  599. V & v = r.first->second;
  600. if(r.second)
  601. f(v);
  602. return v;
  603. }
  604. //c++20 feature
  605. template<typename Arithmetic, typename Floating>
  606. Arithmetic lerp(const Arithmetic & a, const Arithmetic & b, const Floating & f)
  607. {
  608. return a + (b - a) * f;
  609. }
  610. ///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr
  611. static constexpr int abs(int i) {
  612. if(i < 0) return -i;
  613. return i;
  614. }
  615. ///C++23
  616. template< class Enum > constexpr std::underlying_type_t<Enum> to_underlying( Enum e ) noexcept
  617. {
  618. return static_cast<std::underlying_type_t<Enum>>(e);
  619. }
  620. }
  621. using vstd::operator-=;
  622. VCMI_LIB_NAMESPACE_END