2012-03-13 15:47:47 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Standard include file
|
|
|
|
// Contents:
|
|
|
|
// Includes C/C++ libraries, STL libraries, IOStream and String libraries
|
|
|
|
// Includes the most important boost headers
|
|
|
|
// Defines the import + export, override and exception handling macros
|
|
|
|
// Defines the vstd library
|
|
|
|
// Includes the logger
|
|
|
|
|
|
|
|
// This file shouldn't be changed, except if there is a important header file missing which is shared among several projects.
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Global.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-02-04 00:05:44 +03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning (disable : 4800 ) /* disable conversion to bool warning -- I think it's intended in all places */
|
|
|
|
#endif //_MSC_VER
|
|
|
|
|
|
|
|
|
2012-03-13 15:47:47 +03:00
|
|
|
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
|
|
|
#include <cstdio>
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <tchar.h>
|
|
|
|
#else
|
|
|
|
#include "tchar_amigaos4.h"
|
|
|
|
#endif
|
|
|
|
|
2012-08-08 11:25:27 +03:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2012-03-13 15:47:47 +03:00
|
|
|
#include <cassert>
|
2012-08-08 11:25:27 +03:00
|
|
|
#include <climits>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
2012-03-13 15:47:47 +03:00
|
|
|
#include <map>
|
2012-08-08 11:25:27 +03:00
|
|
|
#include <memory>
|
|
|
|
#include <numeric>
|
2012-03-13 15:47:47 +03:00
|
|
|
#include <queue>
|
|
|
|
#include <set>
|
2012-08-08 11:25:27 +03:00
|
|
|
#include <sstream>
|
2012-08-26 12:07:48 +03:00
|
|
|
#include <string>
|
|
|
|
//#include <unordered_map>
|
2012-03-13 15:47:47 +03:00
|
|
|
#include <utility>
|
2012-08-26 12:07:48 +03:00
|
|
|
#include <vector>
|
2012-03-13 15:47:47 +03:00
|
|
|
|
2012-07-02 12:04:05 +03:00
|
|
|
//The only available version is 3, as of Boost 1.50
|
2012-09-16 18:49:22 +03:00
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
2012-07-02 12:04:05 +03:00
|
|
|
#define BOOST_FILESYSTEM_VERSION 3
|
2012-09-16 18:49:22 +03:00
|
|
|
#if ( BOOST_VERSION>105000 )
|
2012-09-15 22:16:16 +03:00
|
|
|
#define BOOST_THREAD_VERSION 3
|
2012-09-16 18:49:22 +03:00
|
|
|
#endif
|
2012-09-15 22:16:16 +03:00
|
|
|
#define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1
|
|
|
|
//#define BOOST_SYSTEM_NO_DEPRECATED 1
|
2012-03-13 15:47:47 +03:00
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <boost/assign.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/logic/tribool.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
2012-08-26 12:07:48 +03:00
|
|
|
#include <boost/optional.hpp>
|
2012-04-08 04:15:18 +03:00
|
|
|
#include <boost/range/algorithm.hpp>
|
2012-03-13 15:47:47 +03:00
|
|
|
#include <boost/thread.hpp>
|
2012-08-26 12:07:48 +03:00
|
|
|
#include <boost/unordered_map.hpp>
|
2012-03-13 15:47:47 +03:00
|
|
|
#include <boost/unordered_set.hpp>
|
2012-08-08 13:17:46 +03:00
|
|
|
#include <boost/unordered_map.hpp>
|
2012-04-14 05:20:22 +03:00
|
|
|
#include <boost/variant.hpp>
|
2012-03-13 15:47:47 +03:00
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Integral data types
|
|
|
|
typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
|
|
|
|
typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
|
|
|
|
typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
|
|
|
|
typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
|
|
|
|
typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
|
|
|
|
typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
|
|
|
|
typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
|
|
|
|
typedef boost::int8_t si8; //signed int 8 bits (1 byte)
|
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
// Fixed width bool data type is important for serialization
|
|
|
|
static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
|
|
|
|
|
2012-08-18 13:29:54 +03:00
|
|
|
#if defined _M_X64 && defined _WIN32 //Win64 -> cannot load 32-bit DLLs for video handling
|
|
|
|
#define DISABLE_VIDEO
|
|
|
|
#endif
|
|
|
|
|
2012-03-13 15:47:47 +03:00
|
|
|
#ifdef __GNUC__
|
2012-10-24 18:29:54 +03:00
|
|
|
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__GNUC__) && (GCC_VERSION == 470 || GCC_VERSION == 471)
|
|
|
|
#error This GCC version has buggy std::array::at version and should not be used. Please update to 4.7.2 or use 4.6.x.
|
2012-03-13 15:47:47 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Import + Export macro declarations
|
|
|
|
#ifdef _WIN32
|
2012-09-15 22:16:16 +03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define DLL_EXPORT __attribute__((dllexport))
|
|
|
|
#else
|
2012-03-13 15:47:47 +03:00
|
|
|
#define DLL_EXPORT __declspec(dllexport)
|
2012-09-15 22:16:16 +03:00
|
|
|
#endif
|
2012-03-13 15:47:47 +03:00
|
|
|
#else
|
|
|
|
#if defined(__GNUC__) && GCC_VERSION >= 400
|
|
|
|
#define DLL_EXPORT __attribute__ ((visibility("default")))
|
|
|
|
#else
|
|
|
|
#define DLL_EXPORT
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-09-15 22:16:16 +03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define DLL_IMPORT __attribute__((dllimport))
|
|
|
|
#else
|
2012-03-13 15:47:47 +03:00
|
|
|
#define DLL_IMPORT __declspec(dllimport)
|
2012-09-15 22:16:16 +03:00
|
|
|
#endif
|
2012-03-13 15:47:47 +03:00
|
|
|
#else
|
|
|
|
#if defined(__GNUC__) && GCC_VERSION >= 400
|
|
|
|
#define DLL_IMPORT __attribute__ ((visibility("default")))
|
|
|
|
#else
|
|
|
|
#define DLL_IMPORT
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef VCMI_DLL
|
|
|
|
#define DLL_LINKAGE DLL_EXPORT
|
|
|
|
#else
|
|
|
|
#define DLL_LINKAGE DLL_IMPORT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//defining available c++11 features
|
|
|
|
|
|
|
|
//initialization lists - only gcc-4.4 or later
|
2012-10-24 18:29:54 +03:00
|
|
|
#if defined(__clang__) || (defined(__GNUC__) && (GCC_VERSION >= 440))
|
2012-03-13 15:47:47 +03:00
|
|
|
#define CPP11_USE_INITIALIZERS_LIST
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//nullptr - only msvc and gcc-4.6 or later, othervice define it as NULL
|
2012-10-24 18:29:54 +03:00
|
|
|
#if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 460))
|
2012-03-13 15:47:47 +03:00
|
|
|
#define nullptr NULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//override keyword - only msvc and gcc-4.7 or later.
|
2012-10-24 18:29:54 +03:00
|
|
|
#if !defined(_MSC_VER) && !(defined(__GNUC__) && (GCC_VERSION >= 470))
|
2012-03-13 15:47:47 +03:00
|
|
|
#define override
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//workaround to support existing code
|
|
|
|
#define OVERRIDE override
|
|
|
|
|
|
|
|
//a normal std::map with a const operator[] for sanity
|
|
|
|
template<typename KeyT, typename ValT>
|
|
|
|
class bmap : public std::map<KeyT, ValT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const ValT & operator[](KeyT key) const
|
|
|
|
{
|
2012-04-08 13:34:23 +03:00
|
|
|
return this->find(key)->second;
|
2012-03-13 15:47:47 +03:00
|
|
|
}
|
|
|
|
ValT & operator[](KeyT key)
|
|
|
|
{
|
|
|
|
return static_cast<std::map<KeyT, ValT> &>(*this)[key];
|
|
|
|
}
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & static_cast<std::map<KeyT, ValT> &>(*this);
|
|
|
|
}
|
2012-10-03 21:11:19 +03:00
|
|
|
|
|
|
|
bmap()
|
|
|
|
{}
|
2012-10-05 15:38:17 +03:00
|
|
|
#if 0 // What is _Myt? gcc\clang does not have that
|
2012-10-03 21:11:19 +03:00
|
|
|
bmap(const typename std::map<KeyT, ValT>::_Myt& _Right)
|
2012-10-05 15:38:17 +03:00
|
|
|
: std::map<KeyT, ValT>(_Right)
|
2012-10-03 21:11:19 +03:00
|
|
|
{}
|
2012-10-05 15:38:17 +03:00
|
|
|
#endif
|
2012-10-03 21:11:19 +03:00
|
|
|
explicit bmap(const typename std::map<KeyT, ValT>::key_compare& _Pred)
|
2012-10-05 15:38:17 +03:00
|
|
|
: std::map<KeyT, ValT>(_Pred)
|
2012-10-03 21:11:19 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
bmap(const typename std::map<KeyT, ValT>::key_compare& _Pred, const typename std::map<KeyT, ValT>::allocator_type& _Al)
|
2012-10-05 15:38:17 +03:00
|
|
|
: std::map<KeyT, ValT>(_Pred, _Al)
|
2012-10-03 21:11:19 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
template<class _Iter>
|
|
|
|
bmap(_Iter _First, _Iter _Last)
|
|
|
|
: std::map<KeyT, ValT>(_First, _Last)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class _Iter>
|
|
|
|
bmap(_Iter _First, _Iter _Last,
|
|
|
|
const typename std::map<KeyT, ValT>::key_compare& _Pred)
|
|
|
|
: std::map<KeyT, ValT>(_First, _Last, _Pred)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class _Iter>
|
|
|
|
bmap(_Iter _First, _Iter _Last,
|
|
|
|
const typename std::map<KeyT, ValT>::key_compare& _Pred, const typename std::map<KeyT, ValT>::allocator_type& _Al)
|
|
|
|
: std::map<KeyT, ValT>(_First, _Last, _Pred, _Al)
|
|
|
|
{}
|
|
|
|
|
2012-03-13 15:47:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace vstd
|
|
|
|
{
|
|
|
|
//returns true if container c contains item i
|
|
|
|
template <typename Container, typename Item>
|
|
|
|
bool contains(const Container & c, const Item &i)
|
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
return std::find(boost::begin(c), boost::end(c),i) != boost::end(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
//returns true if container c contains item i
|
|
|
|
template <typename Container, typename Pred>
|
|
|
|
bool contains_if(const Container & c, Pred p)
|
|
|
|
{
|
|
|
|
return std::find_if(boost::begin(c), boost::end(c), p) != boost::end(c);
|
2012-03-13 15:47:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//returns true if map c contains item i
|
|
|
|
template <typename V, typename Item, typename Item2>
|
|
|
|
bool contains(const std::map<Item,V> & c, const Item2 &i)
|
|
|
|
{
|
|
|
|
return c.find(i)!=c.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
//returns true if bmap c contains item i
|
|
|
|
template <typename V, typename Item, typename Item2>
|
|
|
|
bool contains(const bmap<Item,V> & c, const Item2 &i)
|
|
|
|
{
|
|
|
|
return c.find(i)!=c.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
//returns true if unordered set c contains item i
|
|
|
|
template <typename Item>
|
|
|
|
bool contains(const boost::unordered_set<Item> & c, const Item &i)
|
|
|
|
{
|
|
|
|
return c.find(i)!=c.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
//returns position of first element in vector c equal to s, if there is no such element, -1 is returned
|
2012-06-13 16:04:06 +03:00
|
|
|
template <typename Container, typename T2>
|
|
|
|
int find_pos(const Container & c, const T2 &s)
|
2012-03-13 15:47:47 +03:00
|
|
|
{
|
2012-06-13 16:04:06 +03:00
|
|
|
size_t i=0;
|
2012-08-26 12:07:48 +03:00
|
|
|
for (auto iter = boost::begin(c); iter != boost::end(c); iter++, i++)
|
2012-06-13 16:04:06 +03:00
|
|
|
if(*iter == s)
|
2012-03-13 15:47:47 +03:00
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Func(T1,T2) must say if these elements matches
|
|
|
|
template <typename T1, typename T2, typename Func>
|
|
|
|
int find_pos(const std::vector<T1> & c, const T2 &s, const Func &f)
|
|
|
|
{
|
|
|
|
for(size_t i=0; i < c.size(); ++i)
|
|
|
|
if(f(c[i],s))
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//returns iterator to the given element if present in container, end() if not
|
|
|
|
template <typename Container, typename Item>
|
|
|
|
typename Container::iterator find(Container & c, const Item &i)
|
|
|
|
{
|
|
|
|
return std::find(c.begin(),c.end(),i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//returns const iterator to the given element if present in container, end() if not
|
|
|
|
template <typename Container, typename Item>
|
|
|
|
typename Container::const_iterator find(const Container & c, const Item &i)
|
|
|
|
{
|
|
|
|
return std::find(c.begin(),c.end(),i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//removes element i from container c, returns false if c does not contain i
|
|
|
|
template <typename Container, typename Item>
|
|
|
|
typename Container::size_type operator-=(Container &c, const Item &i)
|
|
|
|
{
|
|
|
|
typename Container::iterator itr = find(c,i);
|
|
|
|
if(itr == c.end())
|
|
|
|
return false;
|
|
|
|
c.erase(itr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//assigns greater of (a, b) to a and returns maximum of (a, b)
|
|
|
|
template <typename t1, typename t2>
|
|
|
|
t1 &amax(t1 &a, const t2 &b)
|
|
|
|
{
|
|
|
|
if(a >= b)
|
|
|
|
return a;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a = b;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//assigns smaller of (a, b) to a and returns minimum of (a, b)
|
|
|
|
template <typename t1, typename t2>
|
|
|
|
t1 &amin(t1 &a, const t2 &b)
|
|
|
|
{
|
|
|
|
if(a <= b)
|
|
|
|
return a;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a = b;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//makes a to fit the range <b, c>
|
|
|
|
template <typename t1, typename t2, typename t3>
|
|
|
|
t1 &abetween(t1 &a, const t2 &b, const t3 &c)
|
|
|
|
{
|
|
|
|
amax(a,b);
|
|
|
|
amin(a,c);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
//checks if a is between b and c
|
|
|
|
template <typename t1, typename t2, typename t3>
|
2012-08-06 10:34:37 +03:00
|
|
|
bool isbetween(const t1 &value, const t2 &min, const t3 &max)
|
2012-03-13 15:47:47 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
return value > min && value < max;
|
2012-03-13 15:47:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//checks if a is within b and c
|
|
|
|
template <typename t1, typename t2, typename t3>
|
2012-08-06 10:34:37 +03:00
|
|
|
bool iswithin(const t1 &value, const t2 &min, const t3 &max)
|
2012-03-13 15:47:47 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
return value >= min && value <= max;
|
2012-03-13 15:47:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename t1, typename t2>
|
|
|
|
struct assigner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
t1 &op1;
|
|
|
|
t2 op2;
|
|
|
|
assigner(t1 &a1, const t2 & a2)
|
|
|
|
:op1(a1), op2(a2)
|
|
|
|
{}
|
|
|
|
void operator()()
|
|
|
|
{
|
|
|
|
op1 = op2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Assigns value a2 to a1. The point of time of the real operation can be controlled
|
|
|
|
// with the () operator.
|
|
|
|
template <typename t1, typename t2>
|
|
|
|
assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
|
|
|
|
{
|
|
|
|
return assigner<t1,t2>(a1,a2);
|
|
|
|
}
|
|
|
|
|
|
|
|
//deleted pointer and sets it to NULL
|
|
|
|
template <typename T>
|
|
|
|
void clear_pointer(T* &ptr)
|
|
|
|
{
|
|
|
|
delete ptr;
|
|
|
|
ptr = NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-20 00:03:43 +03:00
|
|
|
template<typename T>
|
|
|
|
std::unique_ptr<T> make_unique()
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T());
|
2012-03-13 15:47:47 +03:00
|
|
|
}
|
2012-02-20 00:03:43 +03:00
|
|
|
template<typename T, typename Arg1>
|
2012-04-08 04:15:18 +03:00
|
|
|
std::unique_ptr<T> make_unique(Arg1 &&arg1)
|
2012-02-20 00:03:43 +03:00
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1)));
|
2012-03-13 15:47:47 +03:00
|
|
|
}
|
2012-04-08 04:15:18 +03:00
|
|
|
template<typename T, typename Arg1, typename Arg2>
|
|
|
|
std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2)));
|
|
|
|
}
|
2013-01-20 17:43:58 +03:00
|
|
|
template<typename T, typename Arg1, typename Arg2, typename Arg3>
|
|
|
|
std::unique_ptr<T> make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2), std::forward<Arg3>(arg3)));
|
|
|
|
}
|
2012-05-05 00:16:39 +03:00
|
|
|
|
|
|
|
template <typename Container>
|
|
|
|
typename Container::const_reference circularAt(const Container &r, size_t index)
|
|
|
|
{
|
|
|
|
assert(r.size());
|
|
|
|
index %= r.size();
|
2012-08-26 12:07:48 +03:00
|
|
|
auto itr = boost::begin(r);
|
2012-05-05 00:16:39 +03:00
|
|
|
std::advance(itr, index);
|
|
|
|
return *itr;
|
|
|
|
}
|
2012-08-26 12:59:07 +03:00
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
template<typename Range, typename Predicate>
|
|
|
|
void erase_if(Range &vec, Predicate pred)
|
|
|
|
{
|
|
|
|
vec.erase(boost::remove_if(vec, pred),vec.end());
|
|
|
|
}
|
|
|
|
|
2012-09-24 02:10:56 +03:00
|
|
|
template<typename Elem, typename Predicate>
|
|
|
|
void erase_if(std::set<Elem> &setContainer, Predicate pred)
|
|
|
|
{
|
|
|
|
auto itr = setContainer.begin();
|
|
|
|
auto endItr = setContainer.end();
|
|
|
|
while(itr != endItr)
|
|
|
|
{
|
|
|
|
auto tmpItr = itr++;
|
|
|
|
if(pred(*tmpItr))
|
|
|
|
setContainer.erase(tmpItr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 20:36:48 +03:00
|
|
|
//works for map and bmap, maybe something else
|
|
|
|
template<typename Key, typename Val, typename Predicate>
|
|
|
|
void erase_if(std::map<Key, Val> &container, Predicate pred)
|
|
|
|
{
|
|
|
|
auto itr = container.begin();
|
|
|
|
auto endItr = container.end();
|
|
|
|
while(itr != endItr)
|
|
|
|
{
|
|
|
|
auto tmpItr = itr++;
|
|
|
|
if(pred(*tmpItr))
|
|
|
|
container.erase(tmpItr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
template<typename InputRange, typename OutputIterator, typename Predicate>
|
|
|
|
OutputIterator copy_if(const InputRange &input, OutputIterator result, Predicate pred)
|
|
|
|
{
|
|
|
|
return std::copy_if(boost::const_begin(input), boost::end(input), result, pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Container>
|
|
|
|
std::insert_iterator<Container> set_inserter(Container &c)
|
|
|
|
{
|
|
|
|
return std::inserter(c, c.end());
|
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
//Returns iterator to the element for which the value of ValueFunction is minimal
|
2012-08-26 12:07:48 +03:00
|
|
|
template<class ForwardRange, class ValueFunction>
|
|
|
|
auto minElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(boost::begin(rng))
|
|
|
|
{
|
|
|
|
typedef decltype(*boost::begin(rng)) ElemType;
|
2012-08-26 12:59:07 +03:00
|
|
|
return boost::min_element(rng, [&] (ElemType lhs, ElemType rhs) -> bool
|
2012-08-26 12:07:48 +03:00
|
|
|
{
|
|
|
|
return vf(lhs) < vf(rhs);
|
|
|
|
});
|
|
|
|
}
|
2012-09-20 19:55:21 +03:00
|
|
|
|
|
|
|
//Returns iterator to the element for which the value of ValueFunction is maximal
|
|
|
|
template<class ForwardRange, class ValueFunction>
|
|
|
|
auto maxElementByFun(const ForwardRange& rng, ValueFunction vf) -> decltype(boost::begin(rng))
|
|
|
|
{
|
|
|
|
typedef decltype(*boost::begin(rng)) ElemType;
|
|
|
|
return boost::max_element(rng, [&] (ElemType lhs, ElemType rhs) -> bool
|
|
|
|
{
|
|
|
|
return vf(lhs) < vf(rhs);
|
|
|
|
});
|
|
|
|
}
|
2013-02-04 15:32:53 +03:00
|
|
|
|
|
|
|
static int retreiveRandNum(const boost::function<int()> &randGen)
|
|
|
|
{
|
|
|
|
if (randGen)
|
|
|
|
return randGen();
|
|
|
|
else
|
|
|
|
return rand();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> const T & pickRandomElementOf(const std::vector<T> &v, const boost::function<int()> &randGen)
|
|
|
|
{
|
|
|
|
return v.at(retreiveRandNum(randGen) % v.size());
|
|
|
|
}
|
2012-03-13 15:47:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
using std::shared_ptr;
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::make_shared;
|
|
|
|
using vstd::make_unique;
|
|
|
|
|
|
|
|
using vstd::operator-=;
|
|
|
|
|
2012-04-08 04:15:18 +03:00
|
|
|
namespace range = boost::range;
|
|
|
|
|
2012-03-13 15:47:47 +03:00
|
|
|
// can be used for counting arrays
|
|
|
|
template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
|
|
|
|
#define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
|
|
|
|
|
2012-06-08 03:12:31 +03:00
|
|
|
|
|
|
|
#define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
|
|
|
|
|
2012-03-13 15:47:47 +03:00
|
|
|
//XXX pls dont - 'debug macros' are usually more trouble than it's worth
|
|
|
|
#define HANDLE_EXCEPTION \
|
|
|
|
catch (const std::exception& e) { \
|
|
|
|
tlog1 << e.what() << std::endl; \
|
|
|
|
throw; \
|
|
|
|
} \
|
|
|
|
catch (const std::exception * e) \
|
|
|
|
{ \
|
|
|
|
tlog1 << e->what()<< std::endl; \
|
|
|
|
throw; \
|
|
|
|
} \
|
|
|
|
catch (const std::string& e) { \
|
|
|
|
tlog1 << e << std::endl; \
|
|
|
|
throw; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HANDLE_EXCEPTIONC(COMMAND) \
|
|
|
|
catch (const std::exception& e) { \
|
|
|
|
COMMAND; \
|
|
|
|
tlog1 << e.what() << std::endl; \
|
|
|
|
throw; \
|
|
|
|
} \
|
|
|
|
catch (const std::string &e) \
|
|
|
|
{ \
|
|
|
|
COMMAND; \
|
|
|
|
tlog1 << e << std::endl; \
|
|
|
|
throw; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/CLogger.h"
|