gmacros.h

来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C头文件 代码 · 共 442 行

H
442
字号
/*	Copyright (C) 2006, Mike Gashler	This library is free software; you can redistribute it and/or	modify it under the terms of the GNU Lesser General Public	License as published by the Free Software Foundation; either	version 2.1 of the License, or (at your option) any later version.	see http://www.gnu.org/copyleft/lesser.html*/#ifndef __GMACROS_H__#define __GMACROS_H__#include <string.h>#include <stdio.h>#include <wchar.h>#ifdef WIN32#	include <malloc.h>#	pragma warning(disable: 4996)#else // WIN32#	include <alloca.h>#	include <sys/stat.h>#	include <unistd.h>#	include <signal.h>#endif // !WIN32// ********************************************// *************  Useful Macros  **************// ********************************************inline int MIN(int a, int b){	return a < b ? a : b;}inline unsigned int MIN(unsigned int a, unsigned int b){	return a < b ? a : b;}inline float MIN(float a, float b){	return a < b ? a : b;}inline double MIN(double a, double b){	return a < b ? a : b;}inline int MAX(int a, int b){	return a > b ? a : b;}inline unsigned int MAX(unsigned int a, unsigned int b){	return a > b ? a : b;}inline float MAX(float a, float b){	return a > b ? a : b;}inline double MAX(double a, double b){	return a > b ? a : b;}#ifdef WIN32#define BAD_HANDLE (void*)0xffffffff#else // WIN32#define HANDLE unsigned long#define SOCKET int#define BAD_HANDLE 0xffffffff#define INVALID_SOCKET -1#endif // else WIN32#ifndef UCHAR#define UCHAR(c) ((c) & (~32))#endif // UCHARinline float ABS(float f){	return (f >= 0 ? f : -f);}inline double ABS(double d){	return (d >= 0 ? d : -d);}inline int ABS(int n){	return (int)((unsigned int)n & 0x7fffffff);}#ifndef ABS#define ABS(n) ((n) >= 0 ? (n) : (-(n)))#endif // ABS#ifndef PI#define PI (3.14159265358979323846)#endif // PI#define MEMBEROFFSET(type, member) ((int)&(((type*)0)->member))#ifndef GAssert#ifdef _DEBUG#ifdef WIN32#define GAssert(x,y)			\	{				\		if(!(x))		\		{			\			printf("Debug Assert Failed: %s\n", y);\			__asm int 3	\		}			\	}#else // WIN32#define GAssert(x,y)			\	{				\		if(!(x))                \		{                       \			fprintf(stderr, "Debug Assert Failed: %s\n", y); \			fprintf(stdout, "Debug Assert Failed: %s\n", y); \			kill(getpid(), SIGINT);	\		}                       \	}#endif // !WIN32#else // _DEBUG#define GAssert(x,y)	((void)0)#endif // else _DEBUG#endif // GAssertvoid ThrowError(const char* szFormat, ...);// Macro for allocating a temporary buffer#define MAX_STACK_TEMP_BUFFER 1024class GTempBufHelper{public:	char* m_pBuf;	GTempBufHelper(int nSize)	{		m_pBuf = ((nSize > MAX_STACK_TEMP_BUFFER) ? new char[nSize] : NULL);	}	~GTempBufHelper()	{		if(m_pBuf) delete(m_pBuf);	}};#define GTEMPBUF(typ, var, cnt)\	GTempBufHelper var##__(sizeof(typ) * (cnt));\	typ* var = (((sizeof(typ) * (cnt)) <= MAX_STACK_TEMP_BUFFER) ? (typ*)alloca(sizeof(typ) * (cnt)) : (typ*)var##__.m_pBuf);// Macro for converting Unicode to Ansi--todo: use "wcstombs" in stdlib.h instead#define ConvertUnicodeToAnsi(wszUnicode, szAnsi)            \    char* szAnsi = (char*)alloca(wcslen(wszUnicode) + 1);   \    do                                                      \    {                                                       \        int n;                                              \        for(n = 0; wszUnicode[n] != L'\0'; n++)             \            szAnsi[n] = (char)wszUnicode[n];                \        szAnsi[n] = '\0';                                   \    } while(false);// Macro for converting Ansi to Unicode--todo: use "mbstowcs" in stdlib.h instead #define ConvertAnsiToUnicode(szAnsi, wszUnicode)                                \    wchar_t* wszUnicode = (wchar_t*)alloca((strlen(szAnsi) + 1) * sizeof(wchar_t));  \    do                                                                          \    {                                                                           \        int n;                                                                  \        for(n = 0; szAnsi[n] != '\0'; n++)                                      \            wszUnicode[n] = (wchar_t)szAnsi[n];                                 \        wszUnicode[n] = L'\0';                                                  \    } while(false);// A Holder will hold a pointer to allocated memory and will guarantee to// delete it when the holder is deleted.  (Usually you will put a holder on// the stack so it will free up the memory it holds a pointer to even if// an exception is thrown.template <class T>class Holder{private:	T m_p;public:	Holder(T p)	{		m_p = p;	}	~Holder()	{		delete(m_p);	}	Holder<T>(const Holder<T>& other)	{		GAssert(false, "todo: implement me");	}	const Holder<T>& operator=(const Holder<T>&)	{		GAssert(false, "todo: implement me");	}	void Set(T p)	{		delete(m_p);		m_p = p;	}	T Get()	{		return m_p;	}	T Drop()	{		T pTmp = m_p;		m_p = NULL;		return pTmp;	}};template <class T>class ArrayHolder{private:	T m_p;public:	ArrayHolder(T p)	{		m_p = p;	}	~ArrayHolder()	{		delete [] m_p;	}	void Set(T p)	{		delete [] m_p;		m_p = p;	}	T Get()	{		return m_p;	}	T Drop()	{		T pTmp = m_p;		m_p = NULL;		return pTmp;	}};class FileHolder{private:	FILE* m_pFile;public:	FileHolder()	{		m_pFile = NULL;	}	FileHolder(FILE* pFile)	{		m_pFile = pFile;	}	~FileHolder()	{		if(m_pFile)			fclose(m_pFile);	}	void Set(FILE* pFile)	{		if(m_pFile)		{			if(fclose(m_pFile) != 0)				GAssert(false, "failed to close file");		}		m_pFile = pFile;	}	// Returns true if successfull	bool Close()	{		bool answer = true;		if(m_pFile)		{			if(fclose(m_pFile) != 0)				answer = false; // wasn't able to close it properly!			}		m_pFile = NULL;		return answer;	}	FILE* Get()	{		return m_pFile;	}	FILE* Drop()	{		FILE* pFile = m_pFile;		m_pFile = NULL;		return pFile;	}};class StringHolderArray{public:	int m_nSize;	char** m_pData;	StringHolderArray(int nSize)	{		m_nSize = nSize;		if(nSize > 0)		{			m_pData = new char*[nSize];			memset(m_pData, '\0', nSize * sizeof(char*));		}		else			m_pData = NULL;	}	~StringHolderArray()	{		int n;		for(n = 0; n < m_nSize; n++)			delete(m_pData[n]);		delete(m_pData);	}};// ----------------------------// Platform Compatability Stuff// ----------------------------#ifdef WIN32// The Windows version of swprintf doesn't conform to the prototype required by the ISO C Standard,// but _snwprintf is provided to replace it with a safe version that does conform to the standard.#define swprintf _snwprintf#else // WIN32inline char* itoa(int n, char* szBuf, int b){	sprintf(szBuf, "%d", n);	return szBuf;}inline int stricmp(const char* szA, const char* szB){	while(*szA)	{		if((*szA | 32) < (*szB | 32))			return -1;		if((*szA | 32) > (*szB | 32))			return 1;		szA++;		szB++;	}	if(*szB)		return -1;	return 0;}inline int wcsicmp(const wchar_t* wszA, const wchar_t* wszB){	while(*wszA)	{		if((*wszA | 32) < (*wszB | 32))			return -1;		if((*wszA | 32) > (*wszB | 32))			return 1;		wszA++;		wszB++;	}	if(*wszB)		return -1;	return 0;}inline int strnicmp(const char* szA, const char* szB, int len){	int n;	for(n = 0; n < len; n++)	{		if((*szA | 32) < (*szB | 32))			return -1;		if((*szA | 32) > (*szB | 32))			return 1;		szA++;		szB++;	}	return 0;}inline long filelength(int filedes){	struct stat s;	if(fstat(filedes, &s) == -1)		return 0;	return s.st_size;}#endif // !WIN32#endif // __GMACROS_H__

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?