Global.h 20 KB

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