Click here to Skip to main content
11,705,123 members (54,635 online)
Click here to Skip to main content
Add your own
alternative version

How to build an in-memory state engine at runtime

, 28 Oct 2007 CPOL 20.7K 143 20
A C++ template for an efficient in-memory state engine.
tag_tree.zip
cp
Debug
TagTree.exe
sources
algorithm
container
general
mpl
tag_tree.pdf
//	Copyright � 2007, solosTec
//  All rights reserved.
//---------------------------------------------------------------------
//	License for redistribution is the GNU General Public License v2.
//	See the included readme.txt for details.
//---------------------------------------------------------------------
//	developed by solosTec
//	http://www.solostec.de
//---------------------------------------------------------------------
//
//	$Author: $
//	$Revision: $
/*	$Log: $
*/
//---------------------------------------------------------------------
/**
 */
#pragma once
#include <string>
#include <mpl/power.h>

namespace turban {
namespace algorithm {

//	+-----------------------------------------------------------------+
//	| hash [declaration]
//	+-----------------------------------------------------------------+
/**
*	Calculates a hash value for a single  sign.
*/
template < typename T >
struct hash
{
	typedef T	String_;
	typedef typename String_::value_type	value_type;

	enum { MAP_SIZE =  mpl::power< unsigned, 2, sizeof( value_type ) >::value };
	size_t operator()( value_type c ) const
	{
		return c % MAP_SIZE;
	}
};

/**
*	Calculates a hash value for a single <code>char</code>.
*/
template <>
struct hash< std::string >
{
	typedef char	value_type;

#ifdef _DEBUG
	enum { MAP_SIZE = 8 };
#else
	enum { MAP_SIZE = 64 };
#endif

	size_t operator()( value_type c ) const
	{
#ifdef _DEBUG
		//std::cout
		//	<< "hash( "
		//	<< c
		//	<< " = "
		//	<< (( c + 0x20 ) % MAP_SIZE)
		//	<< " )"
		//	<< std::endl
		//	;
#endif
		//	0x20 == SPACE
		return ( c + 0x20 ) % MAP_SIZE;
	}
};

/**
*	Calculates a hash value for a single <code>wchar_t</code>.
*/
template <>
struct hash< std::wstring >
{
	typedef wchar_t	value_type;

	enum { MAP_SIZE = 128 };
	size_t operator()( value_type c ) const
	{
		return ( c + L' ' ) % MAP_SIZE;
	}
};

}	//	namespace algorithm
}	//	namespace turban

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

osy
Software Developer (Senior)
Switzerland Switzerland
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 28 Oct 2007
Article Copyright 2007 by osy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid