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

📄 bsearch.hpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 HPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Base/BSearch.hpp,v $
 * $Date: 2003/05/13 18:41:55 $
 *
 Description:
\*____________________________________________________________________________*/

#ifndef BSEARCH_HPP_
#define BSEARCH_HPP_

#include "BSearch.h"

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
template<class T>
BSearch<T>::BSearch()
:	iCompiled( 0 ),
	iNumberOfEntries( 0 ),
	ppLinear( 0 )
{
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
template<class T>
BSearch<T>::~BSearch()
{
	for( DblListIterator i( lRawEntries ); !i.BadIndex(); i++ )
		{
		PI_DELETE( (Entry *)i.Current() );
		};
	for( int j=0; j<iNumberOfEntries; j++ )
		{
		PI_DELETE( ppLinear[j] );
		};
	delete [] ppLinear;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Add a before element into the table. This must happen before the list
	is generated. Returns non-zero on success, 0 on error. An attempt to
	add an entry which would cause a duplicate key is considered an error 
	condition.
\*____________________________________________________________________________*/
template<class T>
int BSearch<T>::Add( const char *pKey, T tElement )
{
	/* --- sanity, etc. --- */
	assert( pKey );
	if ( !pKey || iCompiled || ppLinear )
		{ return 0; };

	/* --- check if entry already exists --- */
	for( DblListIterator i( lRawEntries ); !i.BadIndex(); i++ )
		{
		Entry *pRE = (Entry *)i.Current();
		assert( pRE );
		if ( pRE->sKey==pKey )	/* --- invokes PIString::operator==() --- */
			{ /* --- key already exists --- */ return 0; };
		};

	/* --- key does not already exists, insert new raw entry --- */
	lRawEntries.Append( (DblList::type) PI_NEW( Entry( pKey, tElement ) ) );

	return 1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Little comparision function for callback by qsort().
\*____________________________________________________________________________*/
template<class T>
int BSearch<T>::fnSortEntryComparision( const void *pV1, const void *pV2 )
{
	const PIString &sKey1 = (*((Entry **)pV1))->sKey;
	const PIString &sKey2 = (*((Entry **)pV2))->sKey;
	int iCmp = sKey1.Len() - sKey2.Len();
	if ( !iCmp )
		{ return strcmp( sKey1, sKey2 ); }
	else
		{ return iCmp; };
}
 
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Little comparision function for callback by bsearch().
\*____________________________________________________________________________*/
template<class T>
int BSearch<T>::fnSearchEntryComparision( const void *pV1, const void *pV2 )
{
	const PIString &sKey1 = *((PIString *)pV1);
	const PIString &sKey2 = (*((Entry **)pV2))->sKey;
	int iCmp = sKey1.Len() - sKey2.Len();
	if ( !iCmp )
		{ return strcmp( sKey1, sKey2 ); }
	else
		{ return iCmp; };
}
 
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Compile raw entries into a sorted linear array for bsearch().
	This can only be done once.
\*____________________________________________________________________________*/
template<class T>
int BSearch<T>::Compile()
{
	if ( iCompiled )
		{ /* --- already compiled --- */; return 0; };

	/* --- 
	There are 2 stages to generating linear array
	--- */

	/* ---
	Stage 1: convert the raw entries linked list into an linear array of
	objects of type Entry
	--- */
	iNumberOfEntries = lRawEntries.Size();
	ppLinear = PI_NEW( Entry *[iNumberOfEntries] );
	int j=0;
	for( DblListIterator i( lRawEntries ); !i.BadIndex(); i++ )
		{
		ppLinear[j++] = (Entry *)i.Current();
		};
	lRawEntries.Clear();
	assert( j==iNumberOfEntries );

	/* ---
	Stage 2: Sort the linear array by the keys
	--- */
	qsort( ppLinear, iNumberOfEntries, sizeof( Entry * ),
		fnSortEntryComparision );

	iCompiled = 1;
	return 1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Binary tree lookup of an entry given a key.

	Try to do everything possible so this becomes an inline function.
\*____________________________________________________________________________*/
template<class T>
inline T BSearch<T>::Lookup( const PIString &sKey ) const
{
	/* --- sanity --- */
	assert( iCompiled && ppLinear && iNumberOfEntries );
	
	/* --- CAUTION: no run time checking that Compile() has been invoked for
	production builds. This will most certainly crash if Compile() has not
	been called 
	--- */
	Entry **ppEntry = (Entry **)bsearch( &sKey, ppLinear, iNumberOfEntries, 
		sizeof( Entry * ), fnSearchEntryComparision );

	return ppEntry ? ( (*ppEntry)->tElement ) : 0;
}

#endif /* BSEARCH_HPP_ */

⌨️ 快捷键说明

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