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

📄 catalogue.h

📁 遗传算法做图像的模式匹配
💻 H
📖 第 1 页 / 共 2 页
字号:

/*! \file Catalogue.h
    \brief This file contains declaration and implementation of catalogue template class used to store genetic operations and other stateless objects.
*/

/*
 * 
 * website: http://www.coolsoft-sd.com/
 * contact: support@coolsoft-sd.com
 *
 */

/*
 * Genetic Algorithm Library
 * Copyright (C) 2007-2008 Coolsoft Software Development
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef __GA_CATALOGUE_H__
#define __GA_CATALOGUE_H__

#include "Platform.h"

#if defined(GAL_STL_EXT_MSVC)

#include <hash_map>
using namespace stdext;

#elif defined(GAL_STL_EXT_STLPORT)

#include <hash_map>

#elif defined(GAL_STL_EXT_GNUC)

#include <ext/hash_map>
using namespace __gnu_cxx;

#endif

#include "Threading.h"

using namespace std;

namespace Common
{
	template <typename T>
	class GaCatalogue;

	/// <summary>This template class manages key and pointer to data of a catalogue entry.
	/// Catalogue entry stores key's string value and pointer to data. Entry stores copied string value of a key. After binding data to an entry,
	/// entry object takes over responsibility for memory occupied by data. Data must be located at heap. 
	///
	/// This class has no built-in synchronizator, so <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros cannot be used with instances of this class.
	/// No public or private methods are thread-safe.</summary>
	/// <param name="T">type of stored data in the entry.</param>
	template <typename T>
	class GaCatalogueEntry
	{
	friend class GaCatalogue<T>;

	private:

		/// <summary>Pointer to string representing key of the catalogue's entry.</summary>
		char* _name;

		/// <summary>Stores length of string (key) without null-terminating character.</summary>
		int _nameLength;

		/// <summary>Pointer to the data.</summary>
		T* _data;

	public:

		/// <summary>Constructor makes copy of key and stores pointer to copied key and pointer to user specified data.</summary>
		/// <param name="name">key of the entry (null-terminating string).</param>
		/// <param name="data">data which will be stored in the entry.</param>
		GaCatalogueEntry(const char* name,
						T* data)
		{
			if( name )
			{
				_nameLength = (int)strlen( name );

				// copy name
				_name = new char[ _nameLength + 1 ];
				strcpy( _name, name );

				_data = data;

				return;
			}

			_name = NULL;
			_nameLength = 0;
			_data = NULL;
		}

		/// <summary>Frees memory used by the data and the key.</summary>
		~GaCatalogueEntry()
		{
			if( _name )
				delete[] _name;

			if( _data )
				delete _data;
		}

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns pointer to string representing key of the entry.</returns>
		inline const char* GACALL GetName() const { return _name; }

		/// <summary><c>SetName</c> method copies key's string and stores pointer to the copied string.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="name">pointer to string which represents new key.</param>
		void GACALL SetName(const char* name)
		{
			if( name )
			{
				if( _name )
					delete[] _name;

				_nameLength = strlen( name );

				// copy name
				_name = new char[ _nameLength + 1 ];
				strcpy( _name, name );
			}
		}

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns length of the key.</returns>
		inline int GACALL GetNameLength() const { return _nameLength; }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns reference to the data.</returns>
		inline T* GACALL GetData() const { return _data; }

		/// <summary><c>SetData</c> method stores pointer to data.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="data">reference to new data.</param>
		inline void GACALL SetData(T* data) { _data = data; }

	};


	/// <summary><C>GaCatalogue</c> template class stores and manages catalogue (directory) for genetic operations.
	/// Data can be accessed by its key (name). When user adds new data into catalogue, catalogue takes over responsibility for memory
	/// allocated by the data. Data must come from heap. Key/data combination is stored in <see cref="GaCatalogueEntry" /> object.
	/// Duplicates of key in catalogue are not allowed.
	///
	/// All public methods are thread-safe except <c>MakeInstance</c> and <c>FreeInstance</c>. This class has built-in synchronizator so
	/// it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros with instances of this class.</summary>
	/// <param name="T">type of stored data in catalogue.</param>
	template <typename T>
	class GaCatalogue
	{
		DEFINE_SYNC_CLASS

	private:

		// String comparator
		struct GaStringComparator
		{

		public:

			// Compare
			inline bool GACALL operator()(const char* left, const char* right) const
			{
				#if defined(GAL_STL_EXT_MSVC)

				return strcmp( left, right ) == -1;

				#elif defined(GAL_STL_EXT_GNUC) || defined (GAL_STL_EXT_STLPORT)

				return strcmp( left, right ) == 0;

				#endif
			}

		};

		// Comarator for catalogue's hash map
		class GaCataolgueHashMapComparator
		{

		public:

			// Parameters for hash table
			enum
			{
				bucket_size = 4,
				min_buckets = 8
			};	

			// Construct with default comparator
			GaCataolgueHashMapComparator() : _comparator() { }

			// Construct with _Pred comparator
			GaCataolgueHashMapComparator(GaStringComparator pred) : _comparator( pred ) { }

			// Hash function
			size_t GACALL operator()(const char* key) const
			{
				if( key == NULL )
					return 0;

				size_t t = 0;
				while( *key )
					t += *( key++ );

				ldiv_t rem = ldiv((long)t, 127773);
				rem.rem = 16807 * rem.rem - 2836 * rem.quot;
				if (rem.rem < 0)
					rem.rem += 2147483647;

				return ((size_t)rem.rem);
			}

			// Comparison
			inline bool GACALL operator()(const char* value1, const char* value2) const { return _comparator( value1, value2 ); }

⌨️ 快捷键说明

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