Global.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #pragma once
  2. /*
  3. * Global.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  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. #ifdef __GNUC__
  17. # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__)
  18. #endif
  19. #if !defined(__clang__) && defined(__GNUC__) && (GCC_VERSION < 470)
  20. # error VCMI requires at least gcc-4.7.2 for successful compilation or clang-3.1. Please update your compiler
  21. #endif
  22. #if defined(__GNUC__) && (GCC_VERSION == 470 || GCC_VERSION == 471)
  23. # error This GCC version has buggy std::array::at version and should not be used. Please update to 4.7.2 or later
  24. #endif
  25. /* ---------------------------------------------------------------------------- */
  26. /* Suppress some compiler warnings */
  27. /* ---------------------------------------------------------------------------- */
  28. #ifdef _MSC_VER
  29. # pragma warning (disable : 4800 ) /* disable conversion to bool warning -- I think it's intended in all places */
  30. #endif
  31. /* ---------------------------------------------------------------------------- */
  32. /* System detection. */
  33. /* ---------------------------------------------------------------------------- */
  34. // Based on: http://sourceforge.net/p/predef/wiki/OperatingSystems/
  35. // and on: http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
  36. // TODO?: Should be moved to vstd\os_detect.h (and then included by Global.h)
  37. #ifdef _WIN16 // Defined for 16-bit environments
  38. # error "16-bit Windows isn't supported"
  39. #elif defined(_WIN64) // Defined for 64-bit environments
  40. # define VCMI_WINDOWS
  41. # define VCMI_WINDOWS_64
  42. #elif defined(_WIN32) // Defined for both 32-bit and 64-bit environments
  43. # define VCMI_WINDOWS
  44. # define VCMI_WINDOWS_32
  45. #elif defined(_WIN32_WCE)
  46. # error "Windows CE isn't supported"
  47. #elif defined(__linux__) || defined(__gnu_linux__) || defined(linux) || defined(__linux)
  48. # define VCMI_UNIX
  49. # define VCMI_XDG
  50. # ifdef __ANDROID__
  51. # define VCMI_ANDROID
  52. # endif
  53. #elif defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
  54. # define VCMI_UNIX
  55. # define VCMI_XDG
  56. # define VCMI_FREEBSD
  57. #elif defined(__GNU__) || defined(__gnu_hurd__) || (defined(__MACH__) && !defined(__APPLE))
  58. # define VCMI_UNIX
  59. # define VCMI_XDG
  60. # define VCMI_HURD
  61. #elif defined(__APPLE__) && defined(__MACH__)
  62. # define VCMI_UNIX
  63. # define VCMI_APPLE
  64. # include "TargetConditionals.h"
  65. # if TARGET_IPHONE_SIMULATOR
  66. # define VCMI_IOS
  67. # define VCMI_IOS_SIM
  68. # elif TARGET_OS_IPHONE
  69. # define VCMI_IOS
  70. # elif TARGET_OS_MAC
  71. # define VCMI_MAC
  72. # else
  73. //# warning "Unknown Apple target."?
  74. # endif
  75. #else
  76. # error "VCMI supports only Windows, OSX, Linux and Android targets"
  77. #endif
  78. #ifdef VCMI_IOS
  79. # error "iOS system isn't yet supported."
  80. #endif
  81. /* ---------------------------------------------------------------------------- */
  82. /* Commonly used C++, Boost headers */
  83. /* ---------------------------------------------------------------------------- */
  84. #ifdef VCMI_WINDOWS
  85. # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers - delete this line if something is missing.
  86. # define NOMINMAX // Exclude min/max macros from <Windows.h>. Use std::[min/max] from <algorithm> instead.
  87. #endif
  88. #define _USE_MATH_DEFINES
  89. #include <cstdio>
  90. #include <stdio.h>
  91. #include <algorithm>
  92. #include <array>
  93. #include <cassert>
  94. #include <climits>
  95. #include <cmath>
  96. #include <cstdlib>
  97. #include <functional>
  98. #include <fstream>
  99. #include <iomanip>
  100. #include <iostream>
  101. #include <map>
  102. #include <memory>
  103. #include <numeric>
  104. #include <queue>
  105. #include <random>
  106. #include <set>
  107. #include <sstream>
  108. #include <string>
  109. #include <unordered_set>
  110. #include <unordered_map>
  111. #include <utility>
  112. #include <vector>
  113. //The only available version is 3, as of Boost 1.50
  114. #include <boost/version.hpp>
  115. #define BOOST_FILESYSTEM_VERSION 3
  116. #if BOOST_VERSION > 105000
  117. # define BOOST_THREAD_VERSION 3
  118. #endif
  119. #define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1
  120. #define BOOST_BIND_NO_PLACEHOLDERS
  121. #include <boost/algorithm/string.hpp>
  122. #include <boost/cstdint.hpp>
  123. #include <boost/current_function.hpp>
  124. #include <boost/crc.hpp>
  125. #include <boost/date_time/posix_time/posix_time.hpp>
  126. #include <boost/date_time/posix_time/posix_time_io.hpp>
  127. #include <boost/filesystem.hpp>
  128. #include <boost/filesystem/path.hpp>
  129. #include <boost/filesystem/fstream.hpp>
  130. #include <boost/format.hpp>
  131. #include <boost/functional/hash.hpp>
  132. #include <boost/lexical_cast.hpp>
  133. #ifndef VCMI_ANDROID
  134. #include <boost/locale/generator.hpp>
  135. #endif
  136. #include <boost/logic/tribool.hpp>
  137. #include <boost/optional.hpp>
  138. #include <boost/program_options.hpp>
  139. #include <boost/range/adaptor/filtered.hpp>
  140. #include <boost/range/adaptor/reversed.hpp>
  141. #include <boost/range/algorithm.hpp>
  142. #include <boost/thread.hpp>
  143. #include <boost/variant.hpp>
  144. #include <boost/math/special_functions/round.hpp>
  145. #ifndef M_PI
  146. # define M_PI 3.14159265358979323846
  147. #endif
  148. /* ---------------------------------------------------------------------------- */
  149. /* Usings */
  150. /* ---------------------------------------------------------------------------- */
  151. using std::shared_ptr;
  152. using std::unique_ptr;
  153. using std::make_shared;
  154. using namespace std::placeholders;
  155. namespace range = boost::range;
  156. /* ---------------------------------------------------------------------------- */
  157. /* Typedefs */
  158. /* ---------------------------------------------------------------------------- */
  159. // Integral data types
  160. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  161. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  162. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  163. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  164. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  165. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  166. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  167. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  168. // Lock typedefs
  169. typedef boost::lock_guard<boost::mutex> TLockGuard;
  170. typedef boost::lock_guard<boost::recursive_mutex> TLockGuardRec;
  171. /* ---------------------------------------------------------------------------- */
  172. /* Macros */
  173. /* ---------------------------------------------------------------------------- */
  174. // Import + Export macro declarations
  175. #ifdef VCMI_WINDOWS
  176. # ifdef __GNUC__
  177. # define DLL_IMPORT __attribute__((dllimport))
  178. # define DLL_EXPORT __attribute__((dllexport))
  179. # else
  180. # define DLL_IMPORT __declspec(dllimport)
  181. # define DLL_EXPORT __declspec(dllexport)
  182. # endif
  183. # define ELF_VISIBILITY
  184. #else
  185. # ifdef __GNUC__
  186. # define DLL_IMPORT __attribute__ ((visibility("default")))
  187. # define DLL_EXPORT __attribute__ ((visibility("default")))
  188. # define ELF_VISIBILITY __attribute__ ((visibility("default")))
  189. # define ELF_VISIBILITY __attribute__ ((visibility("default")))
  190. # endif
  191. #endif
  192. #ifdef VCMI_DLL
  193. # define DLL_LINKAGE DLL_EXPORT
  194. #else
  195. # define DLL_LINKAGE DLL_IMPORT
  196. #endif
  197. #define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
  198. #define ASSERT_IF_CALLED_WITH_PLAYER if(!player) {logGlobal->errorStream() << BOOST_CURRENT_FUNCTION; assert(0);}
  199. // can be used for counting arrays
  200. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  201. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  202. // should be used for variables that becomes unused in release builds (e.g. only used for assert checks)
  203. #define UNUSED(VAR) ((void)VAR)
  204. /* ---------------------------------------------------------------------------- */
  205. /* VCMI standard library */
  206. /* ---------------------------------------------------------------------------- */
  207. #include "lib/logging/CLogger.h"
  208. void inline handleException()
  209. {
  210. try
  211. {
  212. throw;
  213. }
  214. catch(const std::exception & ex)
  215. {
  216. logGlobal->errorStream() << ex.what();
  217. }
  218. catch(const std::string & ex)
  219. {
  220. logGlobal->errorStream() << ex;
  221. }
  222. catch(...)
  223. {
  224. logGlobal->errorStream() << "Sorry, caught unknown exception type. No more info available.";
  225. }
  226. }
  227. template<typename T>
  228. std::ostream & operator<<(std::ostream & out, const boost::optional<T> & opt)
  229. {
  230. if(opt) return out << *opt;
  231. else return out << "empty";
  232. }
  233. template<typename T>
  234. std::ostream & operator<<(std::ostream & out, const std::vector<T> & container)
  235. {
  236. out << "[";
  237. for(auto it = container.begin(); it != container.end(); ++it)
  238. {
  239. out << *it;
  240. if(std::prev(container.end()) != it) out << ", ";
  241. }
  242. return out << "]";
  243. }
  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. size_t 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. //removes element i from container c, returns false if c does not contain i
  314. template <typename Container, typename Item>
  315. typename Container::size_type operator-=(Container &c, const Item &i)
  316. {
  317. typename Container::iterator itr = find(c,i);
  318. if(itr == c.end())
  319. return false;
  320. c.erase(itr);
  321. return true;
  322. }
  323. //assigns greater of (a, b) to a and returns maximum of (a, b)
  324. template <typename t1, typename t2>
  325. t1 &amax(t1 &a, const t2 &b)
  326. {
  327. if(a >= b)
  328. return a;
  329. else
  330. {
  331. a = b;
  332. return a;
  333. }
  334. }
  335. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  336. template <typename t1, typename t2>
  337. t1 &amin(t1 &a, const t2 &b)
  338. {
  339. if(a <= b)
  340. return a;
  341. else
  342. {
  343. a = b;
  344. return a;
  345. }
  346. }
  347. //makes a to fit the range <b, c>
  348. template <typename t1, typename t2, typename t3>
  349. t1 &abetween(t1 &a, const t2 &b, const t3 &c)
  350. {
  351. amax(a,b);
  352. amin(a,c);
  353. return a;
  354. }
  355. //checks if a is between b and c
  356. template <typename t1, typename t2, typename t3>
  357. bool isbetween(const t1 &value, const t2 &min, const t3 &max)
  358. {
  359. return value > min && value < max;
  360. }
  361. //checks if a is within b and c
  362. template <typename t1, typename t2, typename t3>
  363. bool iswithin(const t1 &value, const t2 &min, const t3 &max)
  364. {
  365. return value >= min && value <= max;
  366. }
  367. template <typename t1, typename t2>
  368. struct assigner
  369. {
  370. public:
  371. t1 &op1;
  372. t2 op2;
  373. assigner(t1 &a1, const t2 & a2)
  374. :op1(a1), op2(a2)
  375. {}
  376. void operator()()
  377. {
  378. op1 = op2;
  379. }
  380. };
  381. // Assigns value a2 to a1. The point of time of the real operation can be controlled
  382. // with the () operator.
  383. template <typename t1, typename t2>
  384. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  385. {
  386. return assigner<t1,t2>(a1,a2);
  387. }
  388. //deleted pointer and sets it to nullptr
  389. template <typename T>
  390. void clear_pointer(T* &ptr)
  391. {
  392. delete ptr;
  393. ptr = nullptr;
  394. }
  395. #if _MSC_VER >= 1800
  396. using std::make_unique;
  397. #else
  398. template<typename T>
  399. std::unique_ptr<T> make_unique()
  400. {
  401. return std::unique_ptr<T>(new T());
  402. }
  403. template<typename T, typename Arg1>
  404. std::unique_ptr<T> make_unique(Arg1 &&arg1)
  405. {
  406. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1)));
  407. }
  408. template<typename T, typename Arg1, typename Arg2>
  409. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2)
  410. {
  411. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2)));
  412. }
  413. template<typename T, typename Arg1, typename Arg2, typename Arg3>
  414. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3)
  415. {
  416. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2), std::forward<Arg3>(arg3)));
  417. }
  418. template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  419. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4)
  420. {
  421. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2), std::forward<Arg3>(arg3), std::forward<Arg4>(arg4)));
  422. }
  423. #endif
  424. template <typename Container>
  425. typename Container::const_reference circularAt(const Container &r, size_t index)
  426. {
  427. assert(r.size());
  428. index %= r.size();
  429. auto itr = std::begin(r);
  430. std::advance(itr, index);
  431. return *itr;
  432. }
  433. template<typename Range, typename Predicate>
  434. void erase_if(Range &vec, Predicate pred)
  435. {
  436. vec.erase(boost::remove_if(vec, pred),vec.end());
  437. }
  438. template<typename Elem, typename Predicate>
  439. void erase_if(std::set<Elem> &setContainer, Predicate pred)
  440. {
  441. auto itr = setContainer.begin();
  442. auto endItr = setContainer.end();
  443. while(itr != endItr)
  444. {
  445. auto tmpItr = itr++;
  446. if(pred(*tmpItr))
  447. setContainer.erase(tmpItr);
  448. }
  449. }
  450. //works for map and std::map, maybe something else
  451. template<typename Key, typename Val, typename Predicate>
  452. void erase_if(std::map<Key, Val> &container, Predicate pred)
  453. {
  454. auto itr = container.begin();
  455. auto endItr = container.end();
  456. while(itr != endItr)
  457. {
  458. auto tmpItr = itr++;
  459. if(pred(*tmpItr))
  460. container.erase(tmpItr);
  461. }
  462. }
  463. template<typename InputRange, typename OutputIterator, typename Predicate>
  464. OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred)
  465. {
  466. return std::copy_if(boost::const_begin(input), std::end(input), result, pred);
  467. }
  468. template <typename Container>
  469. std::insert_iterator<Container> set_inserter(Container &c)
  470. {
  471. return std::inserter(c, c.end());
  472. }
  473. //Returns iterator to the element for which the value of ValueFunction is minimal
  474. template<class ForwardRange, class ValueFunction>
  475. auto minElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  476. {
  477. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  478. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  479. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  480. * directly for both function parameters.
  481. */
  482. return boost::min_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  483. {
  484. return vf(lhs) < vf(rhs);
  485. });
  486. }
  487. //Returns iterator to the element for which the value of ValueFunction is maximal
  488. template<class ForwardRange, class ValueFunction>
  489. auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  490. {
  491. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  492. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  493. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  494. * directly for both function parameters.
  495. */
  496. return boost::max_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  497. {
  498. return vf(lhs) < vf(rhs);
  499. });
  500. }
  501. template<typename T>
  502. void advance(T &obj, int change)
  503. {
  504. obj = (T)(((int)obj) + change);
  505. }
  506. template <typename Container>
  507. 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)
  508. {
  509. if(c.size())
  510. return c.back();
  511. else
  512. return typename Container::value_type();
  513. }
  514. template <typename Container>
  515. 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)
  516. {
  517. if(c.size())
  518. return c.front();
  519. else
  520. return nullptr;
  521. }
  522. template <typename Container, typename Index>
  523. bool isValidIndex(const Container &c, Index i)
  524. {
  525. return i >= 0 && i < c.size();
  526. }
  527. template <typename Container, typename Index>
  528. boost::optional<typename Container::const_reference> tryAt(const Container &c, Index i)
  529. {
  530. if(isValidIndex(c, i))
  531. {
  532. auto itr = c.begin();
  533. std::advance(itr, i);
  534. return *itr;
  535. }
  536. return boost::none;
  537. }
  538. template <typename Container, typename Pred>
  539. static boost::optional<typename Container::const_reference> tryFindIf(const Container &r, const Pred &t)
  540. {
  541. auto pos = range::find_if(r, t);
  542. if(pos == boost::end(r))
  543. return boost::none;
  544. else
  545. return *pos;
  546. }
  547. template <typename Container>
  548. typename Container::const_reference atOrDefault(const Container &r, size_t index, const typename Container::const_reference &defaultValue)
  549. {
  550. if(index < r.size())
  551. return r[index];
  552. return defaultValue;
  553. }
  554. template <typename Container, typename Item>
  555. bool erase_if_present(Container &c, const Item &item)
  556. {
  557. auto i = std::find(c.begin(), c.end(), item);
  558. if (i != c.end())
  559. {
  560. c.erase(i);
  561. return true;
  562. }
  563. return false;
  564. }
  565. template <typename V, typename Item, typename Item2>
  566. bool erase_if_present(std::map<Item,V> & c, const Item2 &item)
  567. {
  568. auto i = c.find(item);
  569. if (i != c.end())
  570. {
  571. c.erase(i);
  572. return true;
  573. }
  574. return false;
  575. }
  576. template <typename Container, typename Pred>
  577. void erase(Container &c, Pred pred)
  578. {
  579. c.erase(boost::remove_if(c, pred), c.end());
  580. }
  581. template<typename T>
  582. void removeDuplicates(std::vector<T> &vec)
  583. {
  584. boost::sort(vec);
  585. vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  586. }
  587. template <typename T>
  588. void concatenate(std::vector<T> &dest, const std::vector<T> &src)
  589. {
  590. dest.reserve(dest.size() + src.size());
  591. dest.insert(dest.end(), src.begin(), src.end());
  592. }
  593. template <typename T>
  594. std::vector<T> intersection(std::vector<T> &v1, std::vector<T> &v2)
  595. {
  596. std::vector<T> v3;
  597. std::sort(v1.begin(), v1.end());
  598. std::sort(v2.begin(), v2.end());
  599. std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v3));
  600. return v3;
  601. }
  602. using boost::math::round;
  603. }
  604. using vstd::operator-=;
  605. using vstd::make_unique;