2012-03-13 15:47:47 +03:00
/*
* 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
*
*/
2017-07-13 10:26:03 +02:00
# pragma once
2012-03-13 15:47:47 +03:00
2013-04-05 13:29:46 +03:00
/* ---------------------------------------------------------------------------- */
/* Compiler detection */
/* ---------------------------------------------------------------------------- */
// Fixed width bool data type is important for serialization
static_assert ( sizeof ( bool ) = = 1 , " Bool needs to be 1 byte in size. " ) ;
2014-08-11 00:42:39 +03:00
/* ---------------------------------------------------------------------------- */
/* System detection. */
/* ---------------------------------------------------------------------------- */
// Based on: http://sourceforge.net/p/predef/wiki/OperatingSystems/
// and on: http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
// TODO?: Should be moved to vstd\os_detect.h (and then included by Global.h)
# ifdef _WIN16 // Defined for 16-bit environments
2014-08-30 16:04:00 +03:00
# error "16-bit Windows isn't supported"
2014-08-11 00:42:39 +03:00
# elif defined(_WIN64) // Defined for 64-bit environments
2014-08-30 16:04:00 +03:00
# define VCMI_WINDOWS
# define VCMI_WINDOWS_64
2014-08-11 00:42:39 +03:00
# elif defined(_WIN32) // Defined for both 32-bit and 64-bit environments
2014-08-30 16:04:00 +03:00
# define VCMI_WINDOWS
# define VCMI_WINDOWS_32
2014-08-11 00:42:39 +03:00
# elif defined(_WIN32_WCE)
2014-08-30 16:04:00 +03:00
# error "Windows CE isn't supported"
2014-08-11 00:42:39 +03:00
# elif defined(__linux__) || defined(__gnu_linux__) || defined(linux) || defined(__linux)
2014-08-30 16:04:00 +03:00
# define VCMI_UNIX
2014-11-17 13:19:29 +02:00
# define VCMI_XDG
2023-02-25 09:15:09 +02:00
# if defined(__ANDROID__) || defined(ANDROID)
2016-02-15 12:34:37 +02:00
# define VCMI_ANDROID
2014-08-30 16:04:00 +03:00
# endif
2014-11-17 13:25:10 +02:00
# elif defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
# define VCMI_UNIX
# define VCMI_XDG
# define VCMI_FREEBSD
2023-03-26 18:42:08 +02:00
# elif defined(__HAIKU__)
# define VCMI_UNIX
# define VCMI_XDG
# define VCMI_HAIKU
2015-10-12 07:33:52 +02:00
# elif defined(__GNU__) || defined(__gnu_hurd__) || (defined(__MACH__) && !defined(__APPLE__))
2014-11-17 14:15:50 +02:00
# define VCMI_UNIX
# define VCMI_XDG
# define VCMI_HURD
2014-08-11 00:42:39 +03:00
# elif defined(__APPLE__) && defined(__MACH__)
2014-08-30 16:04:00 +03:00
# define VCMI_UNIX
# define VCMI_APPLE
# include "TargetConditionals.h"
2021-03-07 16:21:32 +02:00
# if TARGET_OS_SIMULATOR || TARGET_IPHONE_SIMULATOR
2014-08-30 16:04:00 +03:00
# define VCMI_IOS
# define VCMI_IOS_SIM
# elif TARGET_OS_IPHONE
# define VCMI_IOS
# elif TARGET_OS_MAC
# define VCMI_MAC
# else
//# warning "Unknown Apple target."?
# endif
2014-08-11 00:42:39 +03:00
# else
2023-02-27 12:00:13 +02:00
# error "This platform isn't supported"
# endif
# if defined(VCMI_ANDROID) || defined(VCMI_IOS)
# define VCMI_MOBILE
2014-08-11 00:42:39 +03:00
# endif
2013-04-05 13:29:46 +03:00
/* ---------------------------------------------------------------------------- */
/* Commonly used C++, Boost headers */
/* ---------------------------------------------------------------------------- */
2014-08-27 13:31:58 +03:00
# ifdef VCMI_WINDOWS
2022-12-08 23:20:42 +02:00
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers - delete this line if something is missing.
# endif
# ifndef NOMINMAX
# define NOMINMAX // Exclude min/max macros from <Windows.h>. Use std::[min/max] from <algorithm> instead.
# endif
# ifndef _NO_W32_PSEUDO_MODIFIERS
# define _NO_W32_PSEUDO_MODIFIERS // Exclude more macros for compiling with MinGW on Linux.
# endif
2014-08-21 23:26:28 +03:00
# endif
2015-11-22 06:14:52 +02:00
/* ---------------------------------------------------------------------------- */
/* A macro to force inlining some of our functions */
/* ---------------------------------------------------------------------------- */
// Compiler (at least MSVC) is not so smart here-> without that displaying is MUCH slower
# ifdef _MSC_VER
# define STRONG_INLINE __forceinline
# elif __GNUC__
# define STRONG_INLINE inline __attribute__((always_inline))
# else
# define STRONG_INLINE inline
# endif
2013-08-02 11:28:52 +03:00
# define _USE_MATH_DEFINES
2012-08-08 11:25:27 +03:00
# include <algorithm>
2023-04-15 01:42:48 +02:00
# include <any>
2012-08-08 11:25:27 +03:00
# include <array>
2023-02-20 18:37:33 +02:00
# include <atomic>
# include <bitset>
2012-03-13 15:47:47 +03:00
# include <cassert>
2012-08-08 11:25:27 +03:00
# include <climits>
# include <cmath>
# include <cstdlib>
2023-02-20 18:37:33 +02:00
# include <cstdio>
2012-08-08 11:25:27 +03:00
# include <fstream>
2023-02-20 18:37:33 +02:00
# include <functional>
2012-08-08 11:25:27 +03:00
# include <iomanip>
# include <iostream>
2012-03-13 15:47:47 +03:00
# include <map>
2012-08-08 11:25:27 +03:00
# include <memory>
2023-02-20 18:37:33 +02:00
# include <mutex>
2012-08-08 11:25:27 +03:00
# include <numeric>
2023-08-17 09:20:46 +02:00
# include <optional>
2012-03-13 15:47:47 +03:00
# include <queue>
2013-06-26 14:18:27 +03:00
# include <random>
2023-08-22 20:43:44 +02:00
# include <regex>
2012-03-13 15:47:47 +03:00
# include <set>
2012-08-08 11:25:27 +03:00
# include <sstream>
2012-08-26 12:07:48 +03:00
# include <string>
2013-06-29 16:05:48 +03:00
# include <unordered_map>
2023-02-20 18:37:33 +02:00
# include <unordered_set>
2012-03-13 15:47:47 +03:00
# include <utility>
2023-08-17 09:20:46 +02:00
# include <variant>
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
2014-02-05 23:25:36 +03:00
# if BOOST_VERSION > 105000
2014-08-30 16:04:00 +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
2019-05-26 17:55:22 +02:00
//need to link boost thread dynamically to avoid https://stackoverflow.com/questions/35978572/boost-thread-interupt-does-not-work-when-crossing-a-dll-boundary
# define BOOST_THREAD_USE_DLL //for example VCAI::finish() may freeze on thread join after interrupt when linking this statically
2014-08-04 21:33:59 +03:00
# define BOOST_BIND_NO_PLACEHOLDERS
2012-03-13 15:47:47 +03:00
2018-04-05 14:35:08 +02:00
# if BOOST_VERSION >= 106600
# define BOOST_ASIO_ENABLE_OLD_SERVICES
# endif
2016-09-16 21:44:22 +02:00
2012-03-13 15:47:47 +03:00
# include <boost/algorithm/string.hpp>
2013-11-08 23:36:26 +03:00
# include <boost/crc.hpp>
2023-04-16 23:24:11 +02:00
# include <boost/current_function.hpp>
2012-03-13 15:47:47 +03:00
# include <boost/date_time/posix_time/posix_time.hpp>
2013-04-05 13:29:46 +03:00
# include <boost/date_time/posix_time/posix_time_io.hpp>
2012-03-13 15:47:47 +03:00
# include <boost/filesystem.hpp>
2014-08-11 00:42:39 +03:00
# include <boost/filesystem/fstream.hpp>
2023-04-16 23:24:11 +02:00
# include <boost/filesystem/path.hpp>
2012-03-13 15:47:47 +03:00
# include <boost/format.hpp>
2014-07-07 11:02:55 +03:00
# include <boost/functional/hash.hpp>
2012-03-13 15:47:47 +03:00
# include <boost/lexical_cast.hpp>
2021-03-02 02:10:00 +02:00
# ifdef VCMI_WINDOWS
2014-08-21 23:26:28 +03:00
# include <boost/locale/generator.hpp>
2014-11-02 15:19:14 +02:00
# endif
2012-03-13 15:47:47 +03:00
# include <boost/logic/tribool.hpp>
2023-04-16 23:24:11 +02:00
# include <boost/multi_array.hpp>
2013-02-09 22:19:14 +03:00
# include <boost/range/adaptor/filtered.hpp>
2013-06-29 16:05:48 +03:00
# include <boost/range/adaptor/reversed.hpp>
2013-06-26 14:18:27 +03:00
# include <boost/range/algorithm.hpp>
2012-03-13 15:47:47 +03:00
# include <boost/thread.hpp>
2023-08-22 20:43:44 +02:00
# include <boost/uuid/uuid.hpp>
# include <boost/uuid/uuid_generators.hpp>
# include <boost/uuid/uuid_io.hpp>
2013-07-07 11:27:27 +03:00
2014-07-09 17:42:22 +03:00
# ifndef M_PI
2014-08-27 13:31:58 +03:00
# define M_PI 3.14159265358979323846
2014-07-09 17:42:22 +03:00
# endif
2013-04-05 13:29:46 +03:00
/* ---------------------------------------------------------------------------- */
/* Usings */
/* ---------------------------------------------------------------------------- */
2014-08-04 21:33:59 +03:00
using namespace std : : placeholders ;
2013-04-05 13:29:46 +03:00
namespace range = boost : : range ;
/* ---------------------------------------------------------------------------- */
/* Typedefs */
/* ---------------------------------------------------------------------------- */
2012-03-13 15:47:47 +03:00
// Integral data types
2017-01-29 17:44:54 +02:00
typedef uint64_t ui64 ; //unsigned int 64 bits (8 bytes)
typedef uint32_t ui32 ; //unsigned int 32 bits (4 bytes)
typedef uint16_t ui16 ; //unsigned int 16 bits (2 bytes)
typedef uint8_t ui8 ; //unsigned int 8 bits (1 byte)
typedef int64_t si64 ; //signed int 64 bits (8 bytes)
typedef int32_t si32 ; //signed int 32 bits (4 bytes)
typedef int16_t si16 ; //signed int 16 bits (2 bytes)
typedef int8_t si8 ; //signed int 8 bits (1 byte)
2012-03-13 15:47:47 +03:00
2013-04-05 13:29:46 +03:00
// Lock typedefs
2023-04-11 16:11:14 +02:00
using TLockGuard = std : : lock_guard < std : : mutex > ;
using TLockGuardRec = std : : lock_guard < std : : recursive_mutex > ;
2012-03-13 15:47:47 +03:00
2013-04-05 13:29:46 +03:00
/* ---------------------------------------------------------------------------- */
/* Macros */
/* ---------------------------------------------------------------------------- */
2012-03-13 15:47:47 +03:00
// Import + Export macro declarations
2014-08-21 23:26:28 +03:00
# ifdef VCMI_WINDOWS
2022-12-06 18:35:24 +02:00
# ifdef VCMI_DLL_STATIC
# define DLL_IMPORT
# define DLL_EXPORT
# elif defined(__GNUC__)
2014-08-30 16:04:00 +03:00
# define DLL_IMPORT __attribute__((dllimport))
2013-06-26 14:18:27 +03:00
# define DLL_EXPORT __attribute__((dllexport))
# else
2014-08-30 16:04:00 +03:00
# define DLL_IMPORT __declspec(dllimport)
2013-06-26 14:18:27 +03:00
# define DLL_EXPORT __declspec(dllexport)
# endif
2014-06-26 18:34:54 +03:00
# define ELF_VISIBILITY
2012-03-13 15:47:47 +03:00
# else
2013-06-26 14:18:27 +03:00
# ifdef __GNUC__
2014-08-30 16:04:00 +03:00
# define DLL_IMPORT __attribute__ ((visibility("default")))
2014-06-26 18:34:54 +03:00
# define DLL_EXPORT __attribute__ ((visibility("default")))
# define ELF_VISIBILITY __attribute__ ((visibility("default")))
2013-06-26 14:18:27 +03:00
# endif
2012-03-13 15:47:47 +03:00
# endif
# ifdef VCMI_DLL
2013-06-26 14:18:27 +03:00
# define DLL_LINKAGE DLL_EXPORT
2012-03-13 15:47:47 +03:00
# else
2013-06-26 14:18:27 +03:00
# define DLL_LINKAGE DLL_IMPORT
2012-03-13 15:47:47 +03:00
# endif
2023-04-11 00:30:08 +02:00
# define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
// old iOS SDKs compatibility
# ifdef VCMI_IOS
# include <AvailabilityVersions.h>
2022-09-22 13:31:57 +02:00
# ifndef __IPHONE_13_0
# define __IPHONE_13_0 130000
# endif
# endif // VCMI_IOS
2022-07-26 15:07:42 +02:00
// single-process build makes 2 copies of the main lib by wrapping it in a namespace
# ifdef VCMI_LIB_NAMESPACE
# define VCMI_LIB_NAMESPACE_BEGIN namespace VCMI_LIB_NAMESPACE {
# define VCMI_LIB_NAMESPACE_END }
# define VCMI_LIB_USING_NAMESPACE using namespace VCMI_LIB_NAMESPACE;
# define VCMI_LIB_WRAP_NAMESPACE(x) VCMI_LIB_NAMESPACE::x
# else
# define VCMI_LIB_NAMESPACE_BEGIN
# define VCMI_LIB_NAMESPACE_END
# define VCMI_LIB_USING_NAMESPACE
2023-04-05 02:26:29 +02:00
# define VCMI_LIB_WRAP_NAMESPACE(x) ::x
2022-07-26 15:07:42 +02:00
# endif
2013-12-03 18:24:13 +03:00
/* ---------------------------------------------------------------------------- */
/* VCMI standard library */
/* ---------------------------------------------------------------------------- */
2016-08-12 14:51:14 +02:00
# include <vstd/CLoggerBase.h>
2013-12-28 15:47:55 +03:00
2022-07-26 15:07:42 +02:00
VCMI_LIB_NAMESPACE_BEGIN
2012-03-13 15:47:47 +03:00
namespace vstd
{
2013-06-29 16:05:48 +03:00
// combine hashes. Present in boost but not in std
template < class T >
inline void hash_combine ( std : : size_t & seed , const T & v )
{
std : : hash < T > hasher ;
seed ^ = hasher ( v ) + 0x9e3779b9 + ( seed < < 6 ) + ( seed > > 2 ) ;
}
2016-02-15 12:34:37 +02:00
2012-03-13 15:47:47 +03:00
//returns true if container c contains item i
template < typename Container , typename Item >
bool contains ( const Container & c , const Item & i )
{
2013-06-29 16:05:48 +03:00
return std : : find ( std : : begin ( c ) , std : : end ( c ) , i ) ! = std : : end ( c ) ;
2012-08-26 12:07:48 +03:00
}
//returns true if container c contains item i
template < typename Container , typename Pred >
bool contains_if ( const Container & c , Pred p )
{
2013-06-29 16:05:48 +03:00
return std : : find_if ( std : : begin ( c ) , std : : end ( c ) , p ) ! = std : : 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 )
{
2013-12-03 18:24:13 +03:00
return c . find ( i ) ! = c . end ( ) ;
}
//returns true if unordered set c contains item i
template < typename Item >
bool contains ( const std : : unordered_set < Item > & c , const Item & i )
2012-03-13 15:47:47 +03:00
{
return c . find ( i ) ! = c . end ( ) ;
}
2013-03-03 21:00:37 +03:00
template < typename V , typename Item , typename Item2 >
2013-06-29 16:05:48 +03:00
bool contains ( const std : : unordered_map < Item , V > & c , const Item2 & i )
2013-03-03 21:00:37 +03:00
{
return c . find ( i ) ! = c . end ( ) ;
}
2012-03-13 15:47:47 +03:00
//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
{
2020-10-01 10:38:06 +02:00
int i = 0 ;
2013-06-29 16:05:48 +03:00
for ( auto iter = std : : begin ( c ) ; iter ! = std : : 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 ;
}
2013-07-22 01:01:29 +03:00
//Func f tells if element matches
template < typename Container , typename Func >
int find_pos_if ( const Container & c , const Func & f )
2012-03-13 15:47:47 +03:00
{
2013-07-22 01:01:29 +03:00
auto ret = boost : : range : : find_if ( c , f ) ;
if ( ret ! = std : : end ( c ) )
return std : : distance ( std : : begin ( c ) , ret ) ;
2012-03-13 15:47:47 +03:00
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 ) ;
}
2017-09-12 08:15:13 +02:00
//returns first key that maps to given value if present, returns success via found if provided
template < typename Key , typename T >
Key findKey ( const std : : map < Key , T > & map , const T & value , bool * found = nullptr )
{
for ( auto iter = map . cbegin ( ) ; iter ! = map . cend ( ) ; iter + + )
{
if ( iter - > second = = value )
2018-02-18 21:50:43 +02:00
{
if ( found )
* found = true ;
2017-09-12 08:15:13 +02:00
return iter - > first ;
2018-02-18 21:50:43 +02:00
}
2017-09-12 08:15:13 +02:00
}
2018-02-18 21:50:43 +02:00
if ( found )
* found = false ;
2017-09-12 08:15:13 +02:00
return Key ( ) ;
}
2012-03-13 15:47:47 +03:00
//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 )
{
2020-10-01 10:38:06 +02:00
if ( a > = ( t1 ) b )
2012-03-13 15:47:47 +03:00
return a ;
else
{
2020-10-01 10:38:06 +02:00
a = t1 ( b ) ;
2012-03-13 15:47:47 +03:00
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 )
{
2020-10-01 10:38:06 +02:00
if ( a < = ( t1 ) b )
2012-03-13 15:47:47 +03:00
return a ;
else
{
2020-10-01 10:38:06 +02:00
a = t1 ( b ) ;
2012-03-13 15:47:47 +03:00
return a ;
}
}
//makes a to fit the range <b, c>
2023-04-11 00:00:46 +02:00
template < typename T >
void abetween ( T & value , const T & min , const T & max )
2012-03-13 15:47:47 +03:00
{
2023-04-11 00:00:46 +02:00
value = std : : clamp ( value , min , max ) ;
2012-03-13 15:47:47 +03:00
}
//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
{
2020-10-01 10:38:06 +02:00
return value > ( t1 ) min & & value < ( t1 ) 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
{
2020-10-01 10:38:06 +02:00
return value > = ( t1 ) min & & value < = ( t1 ) 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 ;
}
} ;
2013-06-26 14:18:27 +03:00
//deleted pointer and sets it to nullptr
2012-03-13 15:47:47 +03:00
template < typename T >
void clear_pointer ( T * & ptr )
{
delete ptr ;
2013-06-26 14:18:27 +03:00
ptr = nullptr ;
2012-03-13 15:47:47 +03:00
}
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 ( ) ;
2013-06-29 16:05:48 +03:00
auto itr = std : : begin ( r ) ;
2012-05-05 00:16:39 +03:00
std : : advance ( itr , index ) ;
return * itr ;
}
2012-08-26 12:59:07 +03:00
2022-12-13 15:10:31 +02:00
template < typename Container , typename Item >
2022-12-13 16:54:49 +02:00
void erase ( Container & c , const Item & item )
2022-12-13 15:10:31 +02:00
{
c . erase ( boost : : remove ( c , item ) , c . end ( ) ) ;
}
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 ( ) ;
2016-02-15 12:34:37 +02:00
auto endItr = setContainer . end ( ) ;
2012-09-24 02:10:56 +03:00
while ( itr ! = endItr )
{
auto tmpItr = itr + + ;
if ( pred ( * tmpItr ) )
setContainer . erase ( tmpItr ) ;
2013-12-03 18:24:13 +03:00
}
}
//works for map and std::map, maybe something else
template < typename Key , typename Val , typename Predicate >
void erase_if ( std : : map < Key , Val > & container , Predicate pred )
{
2012-09-29 20:36:48 +03:00
auto itr = container . begin ( ) ;
2016-02-15 12:34:37 +02:00
auto endItr = container . end ( ) ;
2012-09-29 20:36:48 +03:00
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 )
{
2023-04-11 16:11:14 +02:00
return std : : copy_if ( std : : cbegin ( input ) , std : : end ( input ) , result , pred ) ;
2012-08-26 12:07:48 +03:00
}
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 >
2013-06-29 16:05:48 +03:00
auto minElementByFun ( const ForwardRange & rng , ValueFunction vf ) - > decltype ( std : : begin ( rng ) )
2012-08-26 12:07:48 +03:00
{
2014-02-05 23:25:36 +03:00
/* Clang crashes when instantiating this function template and having PCH compilation enabled.
* There is a bug report here : http : //llvm.org/bugs/show_bug.cgi?id=18744
* Current bugfix is to don ' t use a typedef for decltype ( * std : : begin ( rng ) ) and to use decltype
* directly for both function parameters .
*/
return boost : : min_element ( rng , [ & ] ( decltype ( * std : : begin ( rng ) ) lhs , decltype ( * std : : begin ( rng ) ) rhs ) - > bool
2012-08-26 12:07:48 +03:00
{
return vf ( lhs ) < vf ( rhs ) ;
} ) ;
}
2016-02-15 12:34:37 +02:00
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 >
2013-06-29 16:05:48 +03:00
auto maxElementByFun ( const ForwardRange & rng , ValueFunction vf ) - > decltype ( std : : begin ( rng ) )
2012-09-20 19:55:21 +03:00
{
2014-02-05 23:25:36 +03:00
/* Clang crashes when instantiating this function template and having PCH compilation enabled.
* There is a bug report here : http : //llvm.org/bugs/show_bug.cgi?id=18744
* Current bugfix is to don ' t use a typedef for decltype ( * std : : begin ( rng ) ) and to use decltype
* directly for both function parameters .
*/
return boost : : max_element ( rng , [ & ] ( decltype ( * std : : begin ( rng ) ) lhs , decltype ( * std : : begin ( rng ) ) rhs ) - > bool
2012-09-20 19:55:21 +03:00
{
return vf ( lhs ) < vf ( rhs ) ;
} ) ;
}
2013-02-04 15:32:53 +03:00
2023-04-27 23:29:16 +02:00
/// Increments value by specific delta
/// similar to std::next but works with other types, e.g. enum class
2023-04-27 19:21:06 +02:00
template < typename T >
2023-04-27 23:29:16 +02:00
T next ( const T & obj , int change )
2023-04-27 19:21:06 +02:00
{
2023-04-27 23:29:16 +02:00
return static_cast < T > ( static_cast < ptrdiff_t > ( obj ) + change ) ;
2013-02-07 02:24:43 +03:00
}
2013-04-20 14:34:01 +03:00
template < typename Container >
2013-06-26 14:18:27 +03:00
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)
2013-04-20 14:34:01 +03:00
{
if ( c . size ( ) )
return c . back ( ) ;
else
2013-06-20 00:26:27 +03:00
return typename Container : : value_type ( ) ;
2013-04-20 14:34:01 +03:00
}
template < typename Container >
2013-06-26 14:18:27 +03:00
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)
2013-04-20 14:34:01 +03:00
{
if ( c . size ( ) )
return c . front ( ) ;
else
2013-04-21 15:49:26 +03:00
return nullptr ;
2013-04-20 14:34:01 +03:00
}
2013-07-07 11:27:27 +03:00
2013-09-06 01:11:13 +03:00
template < typename Container , typename Index >
bool isValidIndex ( const Container & c , Index i )
{
return i > = 0 & & i < c . size ( ) ;
}
2013-09-12 00:57:08 +03:00
template < typename Container >
typename Container : : const_reference atOrDefault ( const Container & r , size_t index , const typename Container : : const_reference & defaultValue )
{
2013-09-21 21:29:26 +03:00
if ( index < r . size ( ) )
2013-09-12 00:57:08 +03:00
return r [ index ] ;
2016-02-15 12:34:37 +02:00
2013-09-12 00:57:08 +03:00
return defaultValue ;
}
2014-06-05 18:19:11 +03:00
template < typename Container , typename Item >
bool erase_if_present ( Container & c , const Item & item )
{
auto i = std : : find ( c . begin ( ) , c . end ( ) , item ) ;
if ( i ! = c . end ( ) )
{
c . erase ( i ) ;
return true ;
}
return false ;
}
template < typename V , typename Item , typename Item2 >
bool erase_if_present ( std : : map < Item , V > & c , const Item2 & item )
{
auto i = c . find ( item ) ;
if ( i ! = c . end ( ) )
{
c . erase ( i ) ;
return true ;
}
return false ;
}
template < typename T >
void removeDuplicates ( std : : vector < T > & vec )
{
2023-04-11 16:11:14 +02:00
std : : sort ( vec . begin ( ) , vec . end ( ) ) ;
2014-06-05 18:19:11 +03:00
vec . erase ( std : : unique ( vec . begin ( ) , vec . end ( ) ) , vec . end ( ) ) ;
}
2016-02-15 12:34:37 +02:00
2014-10-03 23:34:13 +03:00
template < typename T >
void concatenate ( std : : vector < T > & dest , const std : : vector < T > & src )
{
dest . reserve ( dest . size ( ) + src . size ( ) ) ;
2016-02-15 12:34:37 +02:00
dest . insert ( dest . end ( ) , src . begin ( ) , src . end ( ) ) ;
2014-10-03 23:34:13 +03:00
}
2014-06-05 18:19:11 +03:00
2015-03-08 13:41:17 +02:00
template < typename T >
std : : vector < T > intersection ( std : : vector < T > & v1 , std : : vector < T > & v2 )
{
std : : vector < T > v3 ;
std : : sort ( v1 . begin ( ) , v1 . end ( ) ) ;
std : : sort ( v2 . begin ( ) , v2 . end ( ) ) ;
2015-03-08 19:19:00 +02:00
std : : set_intersection ( v1 . begin ( ) , v1 . end ( ) , v2 . begin ( ) , v2 . end ( ) , std : : back_inserter ( v3 ) ) ;
2015-03-08 13:41:17 +02:00
return v3 ;
}
2023-07-02 12:32:56 +02:00
template < typename T >
std : : set < T > difference ( const std : : set < T > & s1 , const std : : set < T > s2 )
{
std : : set < T > s3 ;
std : : set_difference ( s1 . begin ( ) , s1 . end ( ) , s2 . begin ( ) , s2 . end ( ) , std : : inserter ( s3 , s3 . end ( ) ) ) ;
return s3 ;
}
2017-08-25 08:29:34 +02:00
template < typename Key , typename V >
bool containsMapping ( const std : : multimap < Key , V > & map , const std : : pair < const Key , V > & mapping )
{
auto range = map . equal_range ( mapping . first ) ;
for ( auto contained = range . first ; contained ! = range . second ; contained + + )
{
if ( mapping . second = = contained - > second )
return true ;
}
return false ;
}
2023-04-16 19:42:56 +02:00
template < class M , class Key , class F >
typename M : : mapped_type & getOrCompute ( M & m , const Key & k , F f )
2022-09-26 20:01:07 +02:00
{
typedef typename M : : mapped_type V ;
std : : pair < typename M : : iterator , bool > r = m . insert ( typename M : : value_type ( k , V ( ) ) ) ;
V & v = r . first - > second ;
if ( r . second )
f ( v ) ;
return v ;
}
2022-12-15 23:24:03 +02:00
//c++20 feature
template < typename Arithmetic , typename Floating >
Arithmetic lerp ( const Arithmetic & a , const Arithmetic & b , const Floating & f )
{
return a + ( b - a ) * f ;
}
2023-02-12 03:03:04 +02:00
///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr
static constexpr int abs ( int i ) {
if ( i < 0 ) return - i ;
return i ;
}
2023-04-04 12:36:42 +02:00
///C++23
template < class Enum > constexpr std : : underlying_type_t < Enum > to_underlying ( Enum e ) noexcept
{
return static_cast < std : : underlying_type_t < Enum > > ( e ) ;
}
2012-03-13 15:47:47 +03:00
}
using vstd : : operator - = ;
2017-05-25 19:57:20 +02:00
2023-02-26 11:18:24 +02:00
VCMI_LIB_NAMESPACE_END