Global.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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_LINUX
  50. # ifdef __ANDROID__
  51. # define VCMI_ANDROID
  52. # endif
  53. #elif defined(__APPLE__) && defined(__MACH__)
  54. # define VCMI_UNIX
  55. # define VCMI_APPLE
  56. # include "TargetConditionals.h"
  57. # if TARGET_IPHONE_SIMULATOR
  58. # define VCMI_IOS
  59. # define VCMI_IOS_SIM
  60. # elif TARGET_OS_IPHONE
  61. # define VCMI_IOS
  62. # elif TARGET_OS_MAC
  63. # define VCMI_MAC
  64. # else
  65. //# warning "Unknown Apple target."?
  66. # endif
  67. #else
  68. # error "VCMI supports only Windows, OSX, Linux and Android targets"
  69. #endif
  70. #ifdef VCMI_IOS
  71. # error "iOS system isn't yet supported."
  72. #endif
  73. /* ---------------------------------------------------------------------------- */
  74. /* Commonly used C++, Boost headers */
  75. /* ---------------------------------------------------------------------------- */
  76. #ifdef VCMI_WINDOWS
  77. # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers - delete this line if something is missing.
  78. # define NOMINMAX // Exclude min/max macros from <Windows.h>. Use std::[min/max] from <algorithm> instead.
  79. #endif
  80. #define _USE_MATH_DEFINES
  81. #include <cstdio>
  82. #include <stdio.h>
  83. #include <algorithm>
  84. #include <array>
  85. #include <cassert>
  86. #include <climits>
  87. #include <cmath>
  88. #include <cstdlib>
  89. #include <functional>
  90. #include <fstream>
  91. #include <iomanip>
  92. #include <iostream>
  93. #include <map>
  94. #include <memory>
  95. #include <numeric>
  96. #include <queue>
  97. #include <random>
  98. #include <set>
  99. #include <sstream>
  100. #include <string>
  101. #include <unordered_set>
  102. #include <unordered_map>
  103. #include <utility>
  104. #include <vector>
  105. //The only available version is 3, as of Boost 1.50
  106. #include <boost/version.hpp>
  107. #define BOOST_FILESYSTEM_VERSION 3
  108. #if BOOST_VERSION > 105000
  109. # define BOOST_THREAD_VERSION 3
  110. #endif
  111. #define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1
  112. #define BOOST_BIND_NO_PLACEHOLDERS
  113. #include <boost/algorithm/string.hpp>
  114. #include <boost/cstdint.hpp>
  115. #include <boost/current_function.hpp>
  116. #include <boost/crc.hpp>
  117. #include <boost/date_time/posix_time/posix_time.hpp>
  118. #include <boost/date_time/posix_time/posix_time_io.hpp>
  119. #include <boost/filesystem.hpp>
  120. #include <boost/filesystem/path.hpp>
  121. #include <boost/filesystem/fstream.hpp>
  122. #include <boost/format.hpp>
  123. #include <boost/functional/hash.hpp>
  124. #include <boost/lexical_cast.hpp>
  125. #ifndef VCMI_ANDROID
  126. #include <boost/locale/generator.hpp>
  127. #endif
  128. #include <boost/logic/tribool.hpp>
  129. #include <boost/optional.hpp>
  130. #include <boost/program_options.hpp>
  131. #include <boost/range/adaptor/filtered.hpp>
  132. #include <boost/range/adaptor/reversed.hpp>
  133. #include <boost/range/algorithm.hpp>
  134. #include <boost/thread.hpp>
  135. #include <boost/variant.hpp>
  136. #include <boost/math/special_functions/round.hpp>
  137. #ifndef M_PI
  138. # define M_PI 3.14159265358979323846
  139. #endif
  140. /* ---------------------------------------------------------------------------- */
  141. /* Usings */
  142. /* ---------------------------------------------------------------------------- */
  143. using std::shared_ptr;
  144. using std::unique_ptr;
  145. using std::make_shared;
  146. using namespace std::placeholders;
  147. namespace range = boost::range;
  148. /* ---------------------------------------------------------------------------- */
  149. /* Typedefs */
  150. /* ---------------------------------------------------------------------------- */
  151. // Integral data types
  152. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  153. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  154. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  155. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  156. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  157. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  158. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  159. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  160. // Lock typedefs
  161. typedef boost::lock_guard<boost::mutex> TLockGuard;
  162. typedef boost::lock_guard<boost::recursive_mutex> TLockGuardRec;
  163. /* ---------------------------------------------------------------------------- */
  164. /* Macros */
  165. /* ---------------------------------------------------------------------------- */
  166. // Import + Export macro declarations
  167. #ifdef VCMI_WINDOWS
  168. # ifdef __GNUC__
  169. # define DLL_IMPORT __attribute__((dllimport))
  170. # define DLL_EXPORT __attribute__((dllexport))
  171. # else
  172. # define DLL_IMPORT __declspec(dllimport)
  173. # define DLL_EXPORT __declspec(dllexport)
  174. # endif
  175. # define ELF_VISIBILITY
  176. #else
  177. # ifdef __GNUC__
  178. # define DLL_IMPORT __attribute__ ((visibility("default")))
  179. # define DLL_EXPORT __attribute__ ((visibility("default")))
  180. # define ELF_VISIBILITY __attribute__ ((visibility("default")))
  181. # define ELF_VISIBILITY __attribute__ ((visibility("default")))
  182. # endif
  183. #endif
  184. #ifdef VCMI_DLL
  185. # define DLL_LINKAGE DLL_EXPORT
  186. #else
  187. # define DLL_LINKAGE DLL_IMPORT
  188. #endif
  189. #define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
  190. #define ASSERT_IF_CALLED_WITH_PLAYER if(!player) {logGlobal->errorStream() << BOOST_CURRENT_FUNCTION; assert(0);}
  191. //XXX pls dont - 'debug macros' are usually more trouble than it's worth
  192. #define HANDLE_EXCEPTION \
  193. catch (const std::exception& e) { \
  194. logGlobal->errorStream() << e.what(); \
  195. throw; \
  196. } \
  197. catch (const std::exception * e) \
  198. { \
  199. logGlobal->errorStream() << e->what(); \
  200. throw; \
  201. } \
  202. catch (const std::string& e) { \
  203. logGlobal->errorStream() << e; \
  204. throw; \
  205. }
  206. #define HANDLE_EXCEPTIONC(COMMAND) \
  207. catch (const std::exception& e) { \
  208. COMMAND; \
  209. logGlobal->errorStream() << e.what(); \
  210. throw; \
  211. } \
  212. catch (const std::string &e) \
  213. { \
  214. COMMAND; \
  215. logGlobal->errorStream() << e; \
  216. throw; \
  217. }
  218. // can be used for counting arrays
  219. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  220. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  221. // should be used for variables that becomes unused in release builds (e.g. only used for assert checks)
  222. #define UNUSED(VAR) ((void)VAR)
  223. /* ---------------------------------------------------------------------------- */
  224. /* VCMI standard library */
  225. /* ---------------------------------------------------------------------------- */
  226. #include "lib/logging/CLogger.h"
  227. void inline handleException()
  228. {
  229. try
  230. {
  231. throw;
  232. }
  233. catch(const std::exception & ex)
  234. {
  235. logGlobal->errorStream() << ex.what();
  236. }
  237. catch(const std::string & ex)
  238. {
  239. logGlobal->errorStream() << ex;
  240. }
  241. catch(...)
  242. {
  243. logGlobal->errorStream() << "Sorry, caught unknown exception type. No more info available.";
  244. }
  245. }
  246. template<typename T>
  247. std::ostream & operator<<(std::ostream & out, const boost::optional<T> & opt)
  248. {
  249. if(opt) return out << *opt;
  250. else return out << "empty";
  251. }
  252. template<typename T>
  253. std::ostream & operator<<(std::ostream & out, const std::vector<T> & container)
  254. {
  255. out << "[";
  256. for(auto it = container.begin(); it != container.end(); ++it)
  257. {
  258. out << *it;
  259. if(std::prev(container.end()) != it) out << ", ";
  260. }
  261. return out << "]";
  262. }
  263. namespace vstd
  264. {
  265. // combine hashes. Present in boost but not in std
  266. template <class T>
  267. inline void hash_combine(std::size_t& seed, const T& v)
  268. {
  269. std::hash<T> hasher;
  270. seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
  271. }
  272. //returns true if container c contains item i
  273. template <typename Container, typename Item>
  274. bool contains(const Container & c, const Item &i)
  275. {
  276. return std::find(std::begin(c), std::end(c),i) != std::end(c);
  277. }
  278. //returns true if container c contains item i
  279. template <typename Container, typename Pred>
  280. bool contains_if(const Container & c, Pred p)
  281. {
  282. return std::find_if(std::begin(c), std::end(c), p) != std::end(c);
  283. }
  284. //returns true if map c contains item i
  285. template <typename V, typename Item, typename Item2>
  286. bool contains(const std::map<Item,V> & c, const Item2 &i)
  287. {
  288. return c.find(i)!=c.end();
  289. }
  290. //returns true if unordered set c contains item i
  291. template <typename Item>
  292. bool contains(const std::unordered_set<Item> & c, const Item &i)
  293. {
  294. return c.find(i)!=c.end();
  295. }
  296. template <typename V, typename Item, typename Item2>
  297. bool contains(const std::unordered_map<Item,V> & c, const Item2 &i)
  298. {
  299. return c.find(i)!=c.end();
  300. }
  301. //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  302. template <typename Container, typename T2>
  303. int find_pos(const Container & c, const T2 &s)
  304. {
  305. size_t i=0;
  306. for (auto iter = std::begin(c); iter != std::end(c); iter++, i++)
  307. if(*iter == s)
  308. return i;
  309. return -1;
  310. }
  311. //Func f tells if element matches
  312. template <typename Container, typename Func>
  313. int find_pos_if(const Container & c, const Func &f)
  314. {
  315. auto ret = boost::range::find_if(c, f);
  316. if(ret != std::end(c))
  317. return std::distance(std::begin(c), ret);
  318. return -1;
  319. }
  320. //returns iterator to the given element if present in container, end() if not
  321. template <typename Container, typename Item>
  322. typename Container::iterator find(Container & c, const Item &i)
  323. {
  324. return std::find(c.begin(),c.end(),i);
  325. }
  326. //returns const iterator to the given element if present in container, end() if not
  327. template <typename Container, typename Item>
  328. typename Container::const_iterator find(const Container & c, const Item &i)
  329. {
  330. return std::find(c.begin(),c.end(),i);
  331. }
  332. //removes element i from container c, returns false if c does not contain i
  333. template <typename Container, typename Item>
  334. typename Container::size_type operator-=(Container &c, const Item &i)
  335. {
  336. typename Container::iterator itr = find(c,i);
  337. if(itr == c.end())
  338. return false;
  339. c.erase(itr);
  340. return true;
  341. }
  342. //assigns greater of (a, b) to a and returns maximum of (a, b)
  343. template <typename t1, typename t2>
  344. t1 &amax(t1 &a, const t2 &b)
  345. {
  346. if(a >= b)
  347. return a;
  348. else
  349. {
  350. a = b;
  351. return a;
  352. }
  353. }
  354. //assigns smaller of (a, b) to a and returns minimum of (a, b)
  355. template <typename t1, typename t2>
  356. t1 &amin(t1 &a, const t2 &b)
  357. {
  358. if(a <= b)
  359. return a;
  360. else
  361. {
  362. a = b;
  363. return a;
  364. }
  365. }
  366. //makes a to fit the range <b, c>
  367. template <typename t1, typename t2, typename t3>
  368. t1 &abetween(t1 &a, const t2 &b, const t3 &c)
  369. {
  370. amax(a,b);
  371. amin(a,c);
  372. return a;
  373. }
  374. //checks if a is between b and c
  375. template <typename t1, typename t2, typename t3>
  376. bool isbetween(const t1 &value, const t2 &min, const t3 &max)
  377. {
  378. return value > min && value < max;
  379. }
  380. //checks if a is within b and c
  381. template <typename t1, typename t2, typename t3>
  382. bool iswithin(const t1 &value, const t2 &min, const t3 &max)
  383. {
  384. return value >= min && value <= max;
  385. }
  386. template <typename t1, typename t2>
  387. struct assigner
  388. {
  389. public:
  390. t1 &op1;
  391. t2 op2;
  392. assigner(t1 &a1, const t2 & a2)
  393. :op1(a1), op2(a2)
  394. {}
  395. void operator()()
  396. {
  397. op1 = op2;
  398. }
  399. };
  400. // Assigns value a2 to a1. The point of time of the real operation can be controlled
  401. // with the () operator.
  402. template <typename t1, typename t2>
  403. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  404. {
  405. return assigner<t1,t2>(a1,a2);
  406. }
  407. //deleted pointer and sets it to nullptr
  408. template <typename T>
  409. void clear_pointer(T* &ptr)
  410. {
  411. delete ptr;
  412. ptr = nullptr;
  413. }
  414. #if _MSC_VER >= 1800
  415. using std::make_unique;
  416. #else
  417. template<typename T>
  418. std::unique_ptr<T> make_unique()
  419. {
  420. return std::unique_ptr<T>(new T());
  421. }
  422. template<typename T, typename Arg1>
  423. std::unique_ptr<T> make_unique(Arg1 &&arg1)
  424. {
  425. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1)));
  426. }
  427. template<typename T, typename Arg1, typename Arg2>
  428. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2)
  429. {
  430. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2)));
  431. }
  432. template<typename T, typename Arg1, typename Arg2, typename Arg3>
  433. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3)
  434. {
  435. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2), std::forward<Arg3>(arg3)));
  436. }
  437. template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  438. std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4)
  439. {
  440. return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2), std::forward<Arg3>(arg3), std::forward<Arg4>(arg4)));
  441. }
  442. #endif
  443. template <typename Container>
  444. typename Container::const_reference circularAt(const Container &r, size_t index)
  445. {
  446. assert(r.size());
  447. index %= r.size();
  448. auto itr = std::begin(r);
  449. std::advance(itr, index);
  450. return *itr;
  451. }
  452. template<typename Range, typename Predicate>
  453. void erase_if(Range &vec, Predicate pred)
  454. {
  455. vec.erase(boost::remove_if(vec, pred),vec.end());
  456. }
  457. template<typename Elem, typename Predicate>
  458. void erase_if(std::set<Elem> &setContainer, Predicate pred)
  459. {
  460. auto itr = setContainer.begin();
  461. auto endItr = setContainer.end();
  462. while(itr != endItr)
  463. {
  464. auto tmpItr = itr++;
  465. if(pred(*tmpItr))
  466. setContainer.erase(tmpItr);
  467. }
  468. }
  469. //works for map and std::map, maybe something else
  470. template<typename Key, typename Val, typename Predicate>
  471. void erase_if(std::map<Key, Val> &container, Predicate pred)
  472. {
  473. auto itr = container.begin();
  474. auto endItr = container.end();
  475. while(itr != endItr)
  476. {
  477. auto tmpItr = itr++;
  478. if(pred(*tmpItr))
  479. container.erase(tmpItr);
  480. }
  481. }
  482. template<typename InputRange, typename OutputIterator, typename Predicate>
  483. OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred)
  484. {
  485. return std::copy_if(boost::const_begin(input), std::end(input), result, pred);
  486. }
  487. template <typename Container>
  488. std::insert_iterator<Container> set_inserter(Container &c)
  489. {
  490. return std::inserter(c, c.end());
  491. }
  492. //Returns iterator to the element for which the value of ValueFunction is minimal
  493. template<class ForwardRange, class ValueFunction>
  494. auto minElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  495. {
  496. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  497. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  498. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  499. * directly for both function parameters.
  500. */
  501. return boost::min_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  502. {
  503. return vf(lhs) < vf(rhs);
  504. });
  505. }
  506. //Returns iterator to the element for which the value of ValueFunction is maximal
  507. template<class ForwardRange, class ValueFunction>
  508. auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(std::begin(rng))
  509. {
  510. /* Clang crashes when instantiating this function template and having PCH compilation enabled.
  511. * There is a bug report here: http://llvm.org/bugs/show_bug.cgi?id=18744
  512. * Current bugfix is to don't use a typedef for decltype(*std::begin(rng)) and to use decltype
  513. * directly for both function parameters.
  514. */
  515. return boost::max_element(rng, [&] (decltype(*std::begin(rng)) lhs, decltype(*std::begin(rng)) rhs) -> bool
  516. {
  517. return vf(lhs) < vf(rhs);
  518. });
  519. }
  520. template<typename T>
  521. void advance(T &obj, int change)
  522. {
  523. obj = (T)(((int)obj) + change);
  524. }
  525. template <typename Container>
  526. 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)
  527. {
  528. if(c.size())
  529. return c.back();
  530. else
  531. return typename Container::value_type();
  532. }
  533. template <typename Container>
  534. 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)
  535. {
  536. if(c.size())
  537. return c.front();
  538. else
  539. return nullptr;
  540. }
  541. template <typename Container, typename Index>
  542. bool isValidIndex(const Container &c, Index i)
  543. {
  544. return i >= 0 && i < c.size();
  545. }
  546. template <typename Container, typename Index>
  547. boost::optional<typename Container::const_reference> tryAt(const Container &c, Index i)
  548. {
  549. if(isValidIndex(c, i))
  550. {
  551. auto itr = c.begin();
  552. std::advance(itr, i);
  553. return *itr;
  554. }
  555. return boost::none;
  556. }
  557. template <typename Container, typename Pred>
  558. static boost::optional<typename Container::const_reference> tryFindIf(const Container &r, const Pred &t)
  559. {
  560. auto pos = range::find_if(r, t);
  561. if(pos == boost::end(r))
  562. return boost::none;
  563. else
  564. return *pos;
  565. }
  566. template <typename Container>
  567. typename Container::const_reference atOrDefault(const Container &r, size_t index, const typename Container::const_reference &defaultValue)
  568. {
  569. if(index < r.size())
  570. return r[index];
  571. return defaultValue;
  572. }
  573. template <typename Container, typename Item>
  574. bool erase_if_present(Container &c, const Item &item)
  575. {
  576. auto i = std::find(c.begin(), c.end(), item);
  577. if (i != c.end())
  578. {
  579. c.erase(i);
  580. return true;
  581. }
  582. return false;
  583. }
  584. template <typename V, typename Item, typename Item2>
  585. bool erase_if_present(std::map<Item,V> & c, const Item2 &item)
  586. {
  587. auto i = c.find(item);
  588. if (i != c.end())
  589. {
  590. c.erase(i);
  591. return true;
  592. }
  593. return false;
  594. }
  595. template <typename Container, typename Pred>
  596. void erase(Container &c, Pred pred)
  597. {
  598. c.erase(boost::remove_if(c, pred), c.end());
  599. }
  600. template<typename T>
  601. void removeDuplicates(std::vector<T> &vec)
  602. {
  603. boost::sort(vec);
  604. vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  605. }
  606. template <typename T>
  607. void concatenate(std::vector<T> &dest, const std::vector<T> &src)
  608. {
  609. dest.reserve(dest.size() + src.size());
  610. dest.insert(dest.end(), src.begin(), src.end());
  611. }
  612. using boost::math::round;
  613. }
  614. using vstd::operator-=;
  615. using vstd::make_unique;