⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iceconstants.h

📁 使用stl技术,(还没看,是听说的)
💻 H
字号:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 *	Contains code for constants.
 *	\file		IceConstants.h
 *	\author		Pierre Terdiman
 *	\date		September, 26, 2001
 */
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Include Guard
#ifndef __ICECONSTANTS_H__
#define __ICECONSTANTS_H__

	struct ICECORE_API Constant
	{
		inline_			Constant()	{}
		inline_			~Constant()	{}

		const String*	mStr;
		udword			mUserdata;
	};

	class ICECORE_API Constants : private Container
	{
		public:
		//! Constructor
											Constants();
		//! Destructor
											~Constants();

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Gets the number of stored constants. O(1)
		 *	\return		number of constants
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		inline_			udword				GetNbConstants()		const	{ return GetNbEntries()>>1;											}

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Gets the list of stored constants.
		 *	That list is lazy-sorted on-the-fly if needed, so this is O(1) most of the time, but can be O(log n).
		 *	\return		list of constants
		 *	\warning	do not modify a string (const access), as it would invalidate the sort and produce unpredictable results
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		inline_			const Constant*		GetConstants()					{ if(!mSorted)	Sort();	return (const Constant*)GetEntries();		}

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Adds a constant to the list. The list is not sorted here so this is O(1).
		 *	\param		str			[in] constant's name
		 *	\param		user_data	[in] constant's value
		 *	\return		true if success
		 *	\warning	the string's pointer is saved, so don't call it with temporary strings
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						bool				AddConstant(const char* str, udword user_data);

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Removes a constant from the list.
		 *	\param		str			[in] constant's name
		 *	\param		user_data	[out] constant's value, or null if not needed
		 *	\return		true if the constant has been found and removed
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						bool				RemoveConstant(const char* str, udword* user_data=null);

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Finds a constant given its name. Lazy-sorts the list on-the-fly if needed. So O(log n) most of the time, sometimes O(2*log n).
		 *	\param		str			[in] constant's name
		 *	\param		user_data	[out] constant's value, or null if not needed
		 *	\param		pos			[out] constant's position in the list, or null if not needed
		 *	\return		true if the constant has been found in the list
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						bool				Find(const char* str, udword* user_data=null, udword* pos=null);
		private:
						BOOL				mSorted;	//!< TRUE in sorted state
		// Lazy-sorting
						void				Sort();
	};

	class ICECORE_API ConstantManager
	{
		public:
		// Constructor / Destructor
										ConstantManager();
										~ConstantManager();

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Initializes the constant manager.
		 *	\param		nb		[in] hash size => yields the number of internal sorted lists
		 *	\return		true if success
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						bool			Init(udword nb=10);

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Releases everything.
		 *	\return		true if success
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						bool			Free();

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Adds a constant to the constant manager. This is O(1).
		 *	\param		str			[in] constant's name
		 *	\param		user_data	[in] constant's value
		 *	\return		true if success
		 *	\warning	we don't create a copy of that string, but internally keep a pointer to it - so make sure it doesn't get deleted....
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						bool			Add(const char* str, udword user_data);

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Removes a constant from the constant manager.
		 *	\param		str			[in] constant's name
		 *	\return		true if the constant has been found and removed
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						bool			RemoveConstant(const char* str);

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 *	Finds a constant given its name. Between O(1) and O(log n).
		 *	\param		str			[in] constant's name
		 *	\param		user_data	[out] constant's value, or null if not needed
		 *	\return		true if the constant has been found
		 */
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						bool			Find(const char* str, udword* user_data=null);

		private:
						udword			mNbLists;	//!< Number of internal sorted lists
						udword			mMask;		//!< Hash mask
						Constants**		mLists;		//!< List of sorted lists
	};

#endif // __ICECONSTANTS_H__

⌨️ 快捷键说明

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